1 | #include <gtk/gtk.h> |
2 | |
3 | static GtkWidget *win; |
4 | static GtkWidget *inhibit_entry; |
5 | static GtkWidget *inhibit_logout; |
6 | static GtkWidget *inhibit_switch; |
7 | static GtkWidget *inhibit_suspend; |
8 | static GtkWidget *inhibit_idle; |
9 | static GtkWidget *inhibit_label; |
10 | |
11 | static void |
12 | inhibitor_toggled (GtkToggleButton *button, GtkApplication *app) |
13 | { |
14 | gboolean active; |
15 | const char *reason; |
16 | GtkApplicationInhibitFlags flags; |
17 | GtkWidget *toplevel; |
18 | guint cookie; |
19 | |
20 | active = gtk_toggle_button_get_active (toggle_button: button); |
21 | reason = gtk_editable_get_text (GTK_EDITABLE (inhibit_entry)); |
22 | |
23 | flags = 0; |
24 | if (gtk_check_button_get_active (GTK_CHECK_BUTTON (inhibit_logout))) |
25 | flags |= GTK_APPLICATION_INHIBIT_LOGOUT; |
26 | if (gtk_check_button_get_active (GTK_CHECK_BUTTON (inhibit_switch))) |
27 | flags |= GTK_APPLICATION_INHIBIT_SWITCH; |
28 | if (gtk_check_button_get_active (GTK_CHECK_BUTTON (inhibit_suspend))) |
29 | flags |= GTK_APPLICATION_INHIBIT_SUSPEND; |
30 | if (gtk_check_button_get_active (GTK_CHECK_BUTTON (inhibit_idle))) |
31 | flags |= GTK_APPLICATION_INHIBIT_IDLE; |
32 | |
33 | toplevel = GTK_WIDGET (gtk_widget_get_root (GTK_WIDGET (button))); |
34 | |
35 | if (active) |
36 | { |
37 | char *text; |
38 | |
39 | g_print (format: "Calling gtk_application_inhibit: %d, '%s'\n" , flags, reason); |
40 | |
41 | cookie = gtk_application_inhibit (application: app, GTK_WINDOW (toplevel), flags, reason); |
42 | g_object_set_data (G_OBJECT (button), key: "cookie" , GUINT_TO_POINTER (cookie)); |
43 | if (cookie == 0) |
44 | { |
45 | g_signal_handlers_block_by_func (button, inhibitor_toggled, app); |
46 | gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), FALSE); |
47 | g_signal_handlers_unblock_by_func (button, inhibitor_toggled, app); |
48 | active = FALSE; |
49 | } |
50 | else |
51 | { |
52 | text = g_strdup_printf (format: "%#x" , cookie); |
53 | gtk_label_set_label (GTK_LABEL (inhibit_label), str: text); |
54 | g_free (mem: text); |
55 | } |
56 | } |
57 | else |
58 | { |
59 | cookie = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (button), "cookie" )); |
60 | g_print (format: "Calling gtk_application_uninhibit: %#x\n" , cookie); |
61 | gtk_application_uninhibit (application: app, cookie); |
62 | gtk_label_set_label (GTK_LABEL (inhibit_label), str: "" ); |
63 | } |
64 | |
65 | gtk_widget_set_sensitive (widget: inhibit_entry, sensitive: !active); |
66 | gtk_widget_set_sensitive (widget: inhibit_logout, sensitive: !active); |
67 | gtk_widget_set_sensitive (widget: inhibit_switch, sensitive: !active); |
68 | gtk_widget_set_sensitive (widget: inhibit_suspend, sensitive: !active); |
69 | gtk_widget_set_sensitive (widget: inhibit_idle, sensitive: !active); |
70 | } |
71 | |
72 | static void |
73 | activate (GtkApplication *app, |
74 | gpointer data) |
75 | { |
76 | GtkWidget *box; |
77 | GtkWidget *separator; |
78 | GtkWidget *grid; |
79 | GtkWidget *button; |
80 | GtkWidget *label; |
81 | |
82 | win = gtk_window_new (); |
83 | |
84 | box = gtk_box_new (orientation: GTK_ORIENTATION_VERTICAL, spacing: 12); |
85 | gtk_widget_set_margin_start (widget: box, margin: 12); |
86 | gtk_widget_set_margin_end (widget: box, margin: 12); |
87 | gtk_widget_set_margin_top (widget: box, margin: 12); |
88 | gtk_widget_set_margin_bottom (widget: box, margin: 12); |
89 | gtk_window_set_child (GTK_WINDOW (win), child: box); |
90 | |
91 | grid = gtk_grid_new (); |
92 | gtk_grid_set_row_spacing (GTK_GRID (grid), spacing: 6); |
93 | gtk_grid_set_column_spacing (GTK_GRID (grid), spacing: 6); |
94 | |
95 | gtk_box_append (GTK_BOX (box), child: grid); |
96 | |
97 | label = gtk_label_new (str: "Inhibitor" ); |
98 | gtk_grid_attach (GTK_GRID (grid), child: label, column: 0, row: 0, width: 1, height: 1); |
99 | |
100 | inhibit_label = gtk_label_new (str: "" ); |
101 | gtk_grid_attach (GTK_GRID (grid), child: inhibit_label, column: 1, row: 0, width: 1, height: 1); |
102 | |
103 | inhibit_logout = gtk_check_button_new_with_label (label: "Logout" ); |
104 | gtk_grid_attach (GTK_GRID (grid), child: inhibit_logout, column: 1, row: 1, width: 1, height: 1); |
105 | |
106 | inhibit_switch = gtk_check_button_new_with_label (label: "User switching" ); |
107 | gtk_grid_attach (GTK_GRID (grid), child: inhibit_switch, column: 1, row: 2, width: 1, height: 1); |
108 | |
109 | inhibit_suspend = gtk_check_button_new_with_label (label: "Suspend" ); |
110 | gtk_grid_attach (GTK_GRID (grid), child: inhibit_suspend, column: 1, row: 4, width: 1, height: 1); |
111 | |
112 | inhibit_idle = gtk_check_button_new_with_label (label: "Idle" ); |
113 | gtk_grid_attach (GTK_GRID (grid), child: inhibit_idle, column: 1, row: 5, width: 1, height: 1); |
114 | |
115 | inhibit_entry = gtk_entry_new (); |
116 | gtk_grid_attach (GTK_GRID (grid), child: inhibit_entry, column: 1, row: 6, width: 1, height: 1); |
117 | |
118 | button = gtk_toggle_button_new_with_label (label: "Inhibit" ); |
119 | g_signal_connect (button, "toggled" , |
120 | G_CALLBACK (inhibitor_toggled), app); |
121 | gtk_grid_attach (GTK_GRID (grid), child: button, column: 2, row: 6, width: 1, height: 1); |
122 | |
123 | separator = gtk_separator_new (orientation: GTK_ORIENTATION_HORIZONTAL); |
124 | gtk_box_append (GTK_BOX (box), child: separator); |
125 | |
126 | grid = gtk_grid_new (); |
127 | gtk_grid_set_row_spacing (GTK_GRID (grid), spacing: 6); |
128 | gtk_grid_set_column_spacing (GTK_GRID (grid), spacing: 6); |
129 | |
130 | gtk_widget_show (widget: win); |
131 | |
132 | gtk_application_add_window (application: app, GTK_WINDOW (win)); |
133 | } |
134 | |
135 | int |
136 | main (int argc, char *argv[]) |
137 | { |
138 | GtkApplication *app; |
139 | |
140 | app = gtk_application_new (application_id: "org.gtk.Test.session" , flags: 0); |
141 | g_object_set (object: app, first_property_name: "register-session" , TRUE, NULL); |
142 | |
143 | g_signal_connect (app, "activate" , |
144 | G_CALLBACK (activate), NULL); |
145 | |
146 | g_application_run (G_APPLICATION (app), argc, argv); |
147 | |
148 | return 0; |
149 | } |
150 | |