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
30 #define HALT_METHOD "PowerOff"
31 #define REBOOT_METHOD "Reboot"
32 #define DBUS_PATH "/org/freedesktop/login1"
33 #define DBUS_INTERFACE "org.freedesktop.login1.Manager"
34 #define DBUS_DESTINATION "org.freedesktop.login1"
36 static int showVersion
;
37 GtkWidget
*dialog
= NULL
;
39 static GOptionEntry entries
[] = {
40 { "version", 'v', 0, G_OPTION_ARG_NONE
, &showVersion
,
41 "Display version information", NULL
},
45 /* gtk3 dockapp code based on wmpasman by Brad Jorsch
46 * <anomie@users.sourceforge.net>
47 * http://sourceforge.net/projects/wmpasman */
49 GtkWidget
*cria_dock(GtkWidget
*mw
, unsigned int s
)
54 Window mainwin
, iw
, w
, p
, dummy1
, *dummy2
;
59 display
= gdk_display_get_default();
60 foobox
= gtk_window_new(GTK_WINDOW_POPUP
);
62 gtk_window_set_wmclass(GTK_WINDOW(mw
), g_get_prgname(), "DockApp");
63 gtk_widget_set_size_request(foobox
, 47, 47);
65 gtk_widget_realize(mw
);
66 gtk_widget_realize(foobox
);
68 d
= GDK_DISPLAY_XDISPLAY(display
);
69 mainwin
= GDK_WINDOW_XID(gtk_widget_get_window(mw
));
70 iw
= GDK_WINDOW_XID(gtk_widget_get_window(foobox
));
71 XQueryTree(d
, mainwin
, &dummy1
, &p
, &dummy2
, &dummy3
);
74 w
= XCreateSimpleWindow(d
, p
, 0, 0, 1, 1, 0, 0, 0);
75 XReparentWindow(d
, mainwin
, w
, 0, 0);
77 gtk_widget_show(foobox
);
78 wmHints
= XGetWMHints(d
, mainwin
);
80 wmHints
= XAllocWMHints();
81 wmHints
->flags
|= IconWindowHint
;
82 wmHints
->icon_window
= iw
;
83 XSetWMHints(d
, mainwin
, wmHints
);
85 XReparentWindow(d
, mainwin
, p
, 0, 0);
93 gtk_widget_destroy(dialog
);
97 void handle_click(GtkWidget
*widget
, gpointer data
)
99 GDBusConnection
*connection
;
100 GDBusMessage
*message
, *reply
;
101 GError
*error
= NULL
;
103 gchar
*method
= (gchar
*)data
;
105 connection
= g_bus_get_sync(G_BUS_TYPE_SYSTEM
, NULL
, &error
);
106 message
= g_dbus_message_new_method_call(
111 g_dbus_message_set_body(message
, g_variant_new("(b)", TRUE
));
112 g_dbus_message_set_destination(message
, DBUS_DESTINATION
);
113 reply
= g_dbus_connection_send_message_with_reply_sync(
114 connection
, message
, 0, -1, NULL
, NULL
, &error
);
116 if (g_dbus_message_get_message_type(reply
) ==
117 G_DBUS_MESSAGE_TYPE_ERROR
) {
120 dialog
= gtk_message_dialog_new(
121 GTK_WINDOW(gtk_widget_get_toplevel(widget
)),
122 GTK_DIALOG_DESTROY_WITH_PARENT
,
126 g_strcompress(g_variant_print(
127 g_dbus_message_get_body(reply
),
129 gtk_dialog_run(GTK_DIALOG(dialog
));
130 gtk_widget_destroy(dialog
);
133 g_object_unref(message
);
134 g_object_unref(reply
);
135 g_object_unref(connection
);
138 void button_press(GtkWidget
*widget
, GdkEvent
*event
)
141 GtkWidget
*halt_button
;
142 GtkWidget
*reboot_button
;
143 GtkWidget
*cancel_button
;
144 GdkEventButton
*bevent
;
148 bevent
= (GdkEventButton
*)event
;
149 switch (bevent
->button
) {
153 dialog
= gtk_dialog_new();
154 label
= gtk_label_new("Shutdown confirmation");
155 gtk_container_add(GTK_CONTAINER(gtk_dialog_get_content_area(
156 GTK_DIALOG(dialog
))),
159 gtk_dialog_add_buttons(GTK_DIALOG(dialog
),
160 "Halt", GTK_RESPONSE_HALT
,
161 "Reboot", GTK_RESPONSE_REBOOT
,
162 "Cancel", GTK_RESPONSE_CANCEL
, NULL
);
163 halt_button
= gtk_dialog_get_widget_for_response(
166 reboot_button
= gtk_dialog_get_widget_for_response(
168 GTK_RESPONSE_REBOOT
);
169 cancel_button
= gtk_dialog_get_widget_for_response(
171 GTK_RESPONSE_CANCEL
);
173 g_signal_connect(dialog
, "destroy", G_CALLBACK(fecha
), NULL
);
174 g_signal_connect(cancel_button
,
178 g_signal_connect(halt_button
,
180 G_CALLBACK(handle_click
),
182 g_signal_connect(reboot_button
,
184 G_CALLBACK(handle_click
),
186 gtk_widget_show_all(dialog
);
193 int main(int argc
, char *argv
[])
195 GError
*error
= NULL
;
196 GOptionContext
*context
;
201 gtk_init(&argc
, &argv
);
203 context
= g_option_context_new(
204 "- dockapp to shutdown or reboot your machine");
205 g_option_context_add_main_entries(context
, entries
, NULL
);
206 g_option_context_add_group(context
, gtk_get_option_group(TRUE
));
207 g_option_context_parse(context
, &argc
, &argv
, &error
);
210 printf(PACKAGE_STRING
"\n");
214 gtkiw
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
215 dockArea
= cria_dock(gtkiw
, 47);
217 pixmap
= gtk_image_new_from_file(DATADIR
"/wmshutdown.xpm");
218 gtk_widget_show(pixmap
);
219 gtk_container_add(GTK_CONTAINER(dockArea
), pixmap
);
221 gtk_widget_add_events(dockArea
, GDK_BUTTON_PRESS_MASK
);
222 g_signal_connect(dockArea
, "button-press-event",
223 G_CALLBACK(button_press
), NULL
);