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 display
= gdk_display_get_default();
63 foobox
= gtk_window_new(GTK_WINDOW_POPUP
);
65 gtk_window_set_wmclass(GTK_WINDOW(mw
), g_get_prgname(), "DockApp");
66 gtk_widget_set_size_request(foobox
, 47, 47);
68 gtk_widget_realize(mw
);
69 gtk_widget_realize(foobox
);
71 Display
*d
= GDK_DISPLAY_XDISPLAY(display
);
72 Window mainwin
= GDK_WINDOW_XID(gtk_widget_get_window(mw
));
73 Window iw
= GDK_WINDOW_XID(gtk_widget_get_window(foobox
));
74 Window p
, dummy1
, *dummy2
;
76 XQueryTree(d
, mainwin
, &dummy1
, &p
, &dummy2
, &dummy3
);
79 Window w
= XCreateSimpleWindow(d
, p
, 0, 0, 1, 1, 0, 0, 0);
80 XReparentWindow(d
, mainwin
, w
, 0, 0);
82 gtk_widget_show(foobox
);
83 XWMHints
*wmHints
= XGetWMHints(d
, mainwin
);
85 wmHints
= XAllocWMHints();
86 wmHints
->flags
|= IconWindowHint
;
87 wmHints
->icon_window
= iw
;
88 XSetWMHints(d
, mainwin
, wmHints
);
90 XReparentWindow(d
, mainwin
, p
, 0, 0);
98 gtk_widget_destroy(dialog
);
102 void handle_click(GtkWidget
*widget
, gpointer data
)
104 GDBusConnection
*connection
;
105 GDBusMessage
*message
, *reply
;
106 GError
*error
= NULL
;
108 gchar
*method
= (gchar
*)data
;
110 connection
= g_bus_get_sync(G_BUS_TYPE_SYSTEM
, NULL
, &error
);
111 message
= g_dbus_message_new_method_call(
117 g_dbus_message_set_body(message
, g_variant_new("(b)", TRUE
));
119 g_dbus_message_set_destination(message
, DBUS_DESTINATION
);
120 reply
= g_dbus_connection_send_message_with_reply_sync(
121 connection
, message
, 0, -1, NULL
, NULL
, &error
);
123 if (g_dbus_message_get_message_type(reply
) ==
124 G_DBUS_MESSAGE_TYPE_ERROR
) {
127 dialog
= gtk_message_dialog_new(
128 GTK_WINDOW(gtk_widget_get_toplevel(widget
)),
129 GTK_DIALOG_DESTROY_WITH_PARENT
,
133 g_strcompress(g_variant_print(
134 g_dbus_message_get_body(reply
),
136 gtk_dialog_run(GTK_DIALOG(dialog
));
137 gtk_widget_destroy(dialog
);
140 g_object_unref(message
);
141 g_object_unref(reply
);
142 g_object_unref(connection
);
145 void button_press(GtkWidget
*widget
, GdkEvent
*event
)
148 GtkWidget
*halt_button
;
149 GtkWidget
*reboot_button
;
150 GtkWidget
*cancel_button
;
152 GdkEventButton
*bevent
= (GdkEventButton
*)event
;
153 switch (bevent
->button
) {
157 dialog
= gtk_dialog_new();
158 label
= gtk_label_new("Shutdown confirmation");
159 gtk_container_add(GTK_CONTAINER(gtk_dialog_get_content_area(
160 GTK_DIALOG(dialog
))),
163 gtk_dialog_add_buttons(GTK_DIALOG(dialog
),
164 "Halt", GTK_RESPONSE_HALT
,
165 "Reboot", GTK_RESPONSE_REBOOT
,
166 "Cancel", GTK_RESPONSE_CANCEL
, NULL
);
167 halt_button
= gtk_dialog_get_widget_for_response(
170 reboot_button
= gtk_dialog_get_widget_for_response(
172 GTK_RESPONSE_REBOOT
);
173 cancel_button
= gtk_dialog_get_widget_for_response(
175 GTK_RESPONSE_CANCEL
);
177 g_signal_connect(dialog
, "destroy", G_CALLBACK(fecha
), NULL
);
178 g_signal_connect(cancel_button
,
182 g_signal_connect(halt_button
,
184 G_CALLBACK(handle_click
),
186 g_signal_connect(reboot_button
,
188 G_CALLBACK(handle_click
),
190 gtk_widget_show_all(dialog
);
197 int main(int argc
, char *argv
[])
199 GError
*error
= NULL
;
200 GOptionContext
*context
;
205 gtk_init(&argc
, &argv
);
207 context
= g_option_context_new(
208 "- dockapp to shutdown or reboot your machine");
209 g_option_context_add_main_entries(context
, entries
, NULL
);
210 g_option_context_add_group(context
, gtk_get_option_group(TRUE
));
211 g_option_context_parse(context
, &argc
, &argv
, &error
);
214 printf(PACKAGE_STRING
"\n");
218 gtkiw
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
219 dockArea
= cria_dock(gtkiw
, 47);
221 pixmap
= gtk_image_new_from_file(DATADIR
"/wmshutdown.xpm");
222 gtk_widget_show(pixmap
);
223 gtk_container_add(GTK_CONTAINER(dockArea
), pixmap
);
225 gtk_widget_add_events(dockArea
, GDK_BUTTON_PRESS_MASK
);
226 g_signal_connect(dockArea
, "button-press-event",
227 G_CALLBACK(button_press
), NULL
);