wmcalc: Add freedesktop.org desktop entry file.
[dockapps.git] / wmshutdown / wmshutdown.c
blob429a500e6e7966991dd42babd86885d4dffa231a
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 #ifdef CONSOLEKIT
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"
36 #else
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"
42 #endif
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 },
50 { 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)
59 GdkDisplay *display;
60 GtkWidget *foobox;
61 Display *d;
62 Window mainwin, iw, w, p, dummy1, *dummy2;
63 unsigned int dummy3;
64 XWMHints *wmHints;
66 (void) s;
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);
80 if (dummy2)
81 XFree(dummy2);
82 w = XCreateSimpleWindow(d, p, 0, 0, 1, 1, 0, 0, 0);
83 XReparentWindow(d, mainwin, w, 0, 0);
84 gtk_widget_show(mw);
85 gtk_widget_show(foobox);
86 wmHints = XGetWMHints(d, mainwin);
87 if (!wmHints)
88 wmHints = XAllocWMHints();
89 wmHints->flags |= IconWindowHint;
90 wmHints->icon_window = iw;
91 XSetWMHints(d, mainwin, wmHints);
92 XFree(wmHints);
93 XReparentWindow(d, mainwin, p, 0, 0);
94 XDestroyWindow(d, w);
96 return foobox;
99 void fecha(void)
101 gtk_widget_destroy(dialog);
102 dialog = NULL;
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(
115 NULL,
116 DBUS_PATH,
117 DBUS_INTERFACE,
118 method);
119 #ifndef CONSOLEKIT
120 g_dbus_message_set_body(message, g_variant_new("(b)", TRUE));
121 #endif
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) {
128 GtkWidget *dialog;
130 dialog = gtk_message_dialog_new(
131 GTK_WINDOW(gtk_widget_get_toplevel(widget)),
132 GTK_DIALOG_DESTROY_WITH_PARENT,
133 GTK_MESSAGE_ERROR,
134 GTK_BUTTONS_CLOSE,
135 "%s",
136 g_strcompress(g_variant_print(
137 g_dbus_message_get_body(reply),
138 TRUE)));
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)
150 GtkWidget *label;
151 GtkWidget *halt_button;
152 GtkWidget *reboot_button;
153 GtkWidget *cancel_button;
154 GdkEventButton *bevent;
156 (void) widget;
158 bevent = (GdkEventButton *)event;
159 switch (bevent->button) {
160 case 1:
161 if (dialog != NULL)
162 return;
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))),
167 label);
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(
174 GTK_DIALOG(dialog),
175 GTK_RESPONSE_HALT);
176 reboot_button = gtk_dialog_get_widget_for_response(
177 GTK_DIALOG(dialog),
178 GTK_RESPONSE_REBOOT);
179 cancel_button = gtk_dialog_get_widget_for_response(
180 GTK_DIALOG(dialog),
181 GTK_RESPONSE_CANCEL);
183 g_signal_connect(dialog, "destroy", G_CALLBACK(fecha), NULL);
184 g_signal_connect(cancel_button,
185 "clicked",
186 G_CALLBACK(fecha),
187 (gpointer) dialog);
188 g_signal_connect(halt_button,
189 "clicked",
190 G_CALLBACK(handle_click),
191 HALT_METHOD);
192 g_signal_connect(reboot_button,
193 "clicked",
194 G_CALLBACK(handle_click),
195 REBOOT_METHOD);
196 gtk_widget_show_all(dialog);
197 break;
198 default:
199 break;
203 int main(int argc, char *argv[])
205 GError *error = NULL;
206 GOptionContext *context;
207 GtkWidget *gtkiw;
208 GtkWidget *dockArea;
209 GtkWidget *pixmap;
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);
219 if (showVersion) {
220 printf(PACKAGE_STRING"\n");
221 return 0;
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);
235 gtk_main();
237 return 0;