1 /* wmshutdown - dockapp to shutdown or reboot your machine
3 * Copyright 2001, 2002 Rafael V. Aroca <rafael@linuxqos.cjb.net>
4 * Copyright 2014 Doug Torrance <dtorrance@monmouthcollege.edu>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #define GTK_RESPONSE_HALT 1
28 #define GTK_RESPONSE_REBOOT 2
31 #define HALT_METHOD "Stop"
32 #define REBOOT_METHOD "Restart"
33 #define DBUS_PATH "/org/freedesktop/ConsoleKit/Manager"
34 #define DBUS_INTERFACE "org.freedesktop.ConsoleKit.Manager"
35 #define DBUS_DESTINATION "org.freedesktop.ConsoleKit"
37 #define HALT_METHOD "PowerOff"
38 #define REBOOT_METHOD "Reboot"
39 #define DBUS_PATH "/org/freedesktop/login1"
40 #define DBUS_INTERFACE "org.freedesktop.login1.Manager"
41 #define DBUS_DESTINATION "org.freedesktop.login1"
44 static int showVersion
;
45 GtkWidget
*dialog
= NULL
;
47 static GOptionEntry entries
[] = {
48 { "version", 'v', 0, G_OPTION_ARG_NONE
, &showVersion
,
49 "Display version information", NULL
},
53 /* gtk3 dockapp code based on wmpasman by Brad Jorsch
54 * <anomie@users.sourceforge.net>
55 * http://sourceforge.net/projects/wmpasman */
57 GtkWidget
*cria_dock(GtkWidget
*mw
, unsigned int s
)
62 Window mainwin
, iw
, w
, p
, dummy1
, *dummy2
;
67 display
= gdk_display_get_default();
68 foobox
= gtk_window_new(GTK_WINDOW_POPUP
);
70 gtk_window_set_wmclass(GTK_WINDOW(mw
), g_get_prgname(), "DockApp");
71 gtk_widget_set_size_request(foobox
, 47, 47);
73 gtk_widget_realize(mw
);
74 gtk_widget_realize(foobox
);
76 d
= GDK_DISPLAY_XDISPLAY(display
);
77 mainwin
= GDK_WINDOW_XID(gtk_widget_get_window(mw
));
78 iw
= GDK_WINDOW_XID(gtk_widget_get_window(foobox
));
79 XQueryTree(d
, mainwin
, &dummy1
, &p
, &dummy2
, &dummy3
);
82 w
= XCreateSimpleWindow(d
, p
, 0, 0, 1, 1, 0, 0, 0);
83 XReparentWindow(d
, mainwin
, w
, 0, 0);
85 gtk_widget_show(foobox
);
86 wmHints
= XGetWMHints(d
, mainwin
);
88 wmHints
= XAllocWMHints();
89 wmHints
->flags
|= IconWindowHint
;
90 wmHints
->icon_window
= iw
;
91 XSetWMHints(d
, mainwin
, wmHints
);
93 XReparentWindow(d
, mainwin
, p
, 0, 0);
101 gtk_widget_destroy(dialog
);
105 void handle_click(GtkWidget
*widget
, gpointer data
)
107 GDBusConnection
*connection
;
108 GDBusMessage
*message
, *reply
;
109 GError
*error
= NULL
;
111 gchar
*method
= (gchar
*)data
;
113 connection
= g_bus_get_sync(G_BUS_TYPE_SYSTEM
, NULL
, &error
);
114 message
= g_dbus_message_new_method_call(
120 g_dbus_message_set_body(message
, g_variant_new("(b)", TRUE
));
122 g_dbus_message_set_destination(message
, DBUS_DESTINATION
);
123 reply
= g_dbus_connection_send_message_with_reply_sync(
124 connection
, message
, 0, -1, NULL
, NULL
, &error
);
126 if (g_dbus_message_get_message_type(reply
) ==
127 G_DBUS_MESSAGE_TYPE_ERROR
) {
130 dialog
= gtk_message_dialog_new(
131 GTK_WINDOW(gtk_widget_get_toplevel(widget
)),
132 GTK_DIALOG_DESTROY_WITH_PARENT
,
136 g_strcompress(g_variant_print(
137 g_dbus_message_get_body(reply
),
139 gtk_dialog_run(GTK_DIALOG(dialog
));
140 gtk_widget_destroy(dialog
);
143 g_object_unref(message
);
144 g_object_unref(reply
);
145 g_object_unref(connection
);
148 void button_press(GtkWidget
*widget
, GdkEvent
*event
)
151 GtkWidget
*halt_button
;
152 GtkWidget
*reboot_button
;
153 GtkWidget
*cancel_button
;
154 GdkEventButton
*bevent
;
158 bevent
= (GdkEventButton
*)event
;
159 switch (bevent
->button
) {
163 dialog
= gtk_dialog_new();
164 label
= gtk_label_new("Shutdown confirmation");
165 gtk_container_add(GTK_CONTAINER(gtk_dialog_get_content_area(
166 GTK_DIALOG(dialog
))),
169 gtk_dialog_add_buttons(GTK_DIALOG(dialog
),
170 "Halt", GTK_RESPONSE_HALT
,
171 "Reboot", GTK_RESPONSE_REBOOT
,
172 "Cancel", GTK_RESPONSE_CANCEL
, NULL
);
173 halt_button
= gtk_dialog_get_widget_for_response(
176 reboot_button
= gtk_dialog_get_widget_for_response(
178 GTK_RESPONSE_REBOOT
);
179 cancel_button
= gtk_dialog_get_widget_for_response(
181 GTK_RESPONSE_CANCEL
);
183 g_signal_connect(dialog
, "destroy", G_CALLBACK(fecha
), NULL
);
184 g_signal_connect(cancel_button
,
188 g_signal_connect(halt_button
,
190 G_CALLBACK(handle_click
),
192 g_signal_connect(reboot_button
,
194 G_CALLBACK(handle_click
),
196 gtk_widget_show_all(dialog
);
203 int main(int argc
, char *argv
[])
205 GError
*error
= NULL
;
206 GOptionContext
*context
;
211 gtk_init(&argc
, &argv
);
213 context
= g_option_context_new(
214 "- dockapp to shutdown or reboot your machine");
215 g_option_context_add_main_entries(context
, entries
, NULL
);
216 g_option_context_add_group(context
, gtk_get_option_group(TRUE
));
217 g_option_context_parse(context
, &argc
, &argv
, &error
);
220 printf(PACKAGE_STRING
"\n");
224 gtkiw
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
225 dockArea
= cria_dock(gtkiw
, 47);
227 pixmap
= gtk_image_new_from_file(DATADIR
"/wmshutdown.xpm");
228 gtk_widget_show(pixmap
);
229 gtk_container_add(GTK_CONTAINER(dockArea
), pixmap
);
231 gtk_widget_add_events(dockArea
, GDK_BUTTON_PRESS_MASK
);
232 g_signal_connect(dockArea
, "button-press-event",
233 G_CALLBACK(button_press
), NULL
);