wmshutdown: Remove support for deprecated Consolekit.
[dockapps.git] / wmshutdown / wmshutdown.c
bloba55662cb094ab37c8ce4d5f6973780633f36a261
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/>. */
19 #include <gtk/gtk.h>
20 #include <gdk/gdkx.h>
21 #include <gio/gio.h>
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
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 },
42 { 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)
51 GdkDisplay *display;
52 GtkWidget *foobox;
53 Display *d;
54 Window mainwin, iw, w, p, dummy1, *dummy2;
55 unsigned int dummy3;
56 XWMHints *wmHints;
58 (void) s;
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);
72 if (dummy2)
73 XFree(dummy2);
74 w = XCreateSimpleWindow(d, p, 0, 0, 1, 1, 0, 0, 0);
75 XReparentWindow(d, mainwin, w, 0, 0);
76 gtk_widget_show(mw);
77 gtk_widget_show(foobox);
78 wmHints = XGetWMHints(d, mainwin);
79 if (!wmHints)
80 wmHints = XAllocWMHints();
81 wmHints->flags |= IconWindowHint;
82 wmHints->icon_window = iw;
83 XSetWMHints(d, mainwin, wmHints);
84 XFree(wmHints);
85 XReparentWindow(d, mainwin, p, 0, 0);
86 XDestroyWindow(d, w);
88 return foobox;
91 void fecha(void)
93 gtk_widget_destroy(dialog);
94 dialog = NULL;
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(
107 NULL,
108 DBUS_PATH,
109 DBUS_INTERFACE,
110 method);
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) {
118 GtkWidget *dialog;
120 dialog = gtk_message_dialog_new(
121 GTK_WINDOW(gtk_widget_get_toplevel(widget)),
122 GTK_DIALOG_DESTROY_WITH_PARENT,
123 GTK_MESSAGE_ERROR,
124 GTK_BUTTONS_CLOSE,
125 "%s",
126 g_strcompress(g_variant_print(
127 g_dbus_message_get_body(reply),
128 TRUE)));
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)
140 GtkWidget *label;
141 GtkWidget *halt_button;
142 GtkWidget *reboot_button;
143 GtkWidget *cancel_button;
144 GdkEventButton *bevent;
146 (void) widget;
148 bevent = (GdkEventButton *)event;
149 switch (bevent->button) {
150 case 1:
151 if (dialog != NULL)
152 return;
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))),
157 label);
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(
164 GTK_DIALOG(dialog),
165 GTK_RESPONSE_HALT);
166 reboot_button = gtk_dialog_get_widget_for_response(
167 GTK_DIALOG(dialog),
168 GTK_RESPONSE_REBOOT);
169 cancel_button = gtk_dialog_get_widget_for_response(
170 GTK_DIALOG(dialog),
171 GTK_RESPONSE_CANCEL);
173 g_signal_connect(dialog, "destroy", G_CALLBACK(fecha), NULL);
174 g_signal_connect(cancel_button,
175 "clicked",
176 G_CALLBACK(fecha),
177 (gpointer) dialog);
178 g_signal_connect(halt_button,
179 "clicked",
180 G_CALLBACK(handle_click),
181 HALT_METHOD);
182 g_signal_connect(reboot_button,
183 "clicked",
184 G_CALLBACK(handle_click),
185 REBOOT_METHOD);
186 gtk_widget_show_all(dialog);
187 break;
188 default:
189 break;
193 int main(int argc, char *argv[])
195 GError *error = NULL;
196 GOptionContext *context;
197 GtkWidget *gtkiw;
198 GtkWidget *dockArea;
199 GtkWidget *pixmap;
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);
209 if (showVersion) {
210 printf(PACKAGE_STRING"\n");
211 return 0;
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);
225 gtk_main();
227 return 0;