wmshutdown: Add version 1.3 to repository.
[dockapps.git] / wmshutdown / wmshutdown.c
blob8602d9cafb3293b2f227513b47c5978e5691d8e6
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;
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;
75 unsigned int dummy3;
76 XQueryTree(d, mainwin, &dummy1, &p, &dummy2, &dummy3);
77 if (dummy2)
78 XFree(dummy2);
79 Window w = XCreateSimpleWindow(d, p, 0, 0, 1, 1, 0, 0, 0);
80 XReparentWindow(d, mainwin, w, 0, 0);
81 gtk_widget_show(mw);
82 gtk_widget_show(foobox);
83 XWMHints *wmHints = XGetWMHints(d, mainwin);
84 if (!wmHints)
85 wmHints = XAllocWMHints();
86 wmHints->flags |= IconWindowHint;
87 wmHints->icon_window = iw;
88 XSetWMHints(d, mainwin, wmHints);
89 XFree(wmHints);
90 XReparentWindow(d, mainwin, p, 0, 0);
91 XDestroyWindow(d, w);
93 return foobox;
96 void fecha(void)
98 gtk_widget_destroy(dialog);
99 dialog = NULL;
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(
112 NULL,
113 DBUS_PATH,
114 DBUS_INTERFACE,
115 method);
116 #ifndef CONSOLEKIT
117 g_dbus_message_set_body(message, g_variant_new("(b)", TRUE));
118 #endif
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) {
125 GtkWidget *dialog;
127 dialog = gtk_message_dialog_new(
128 GTK_WINDOW(gtk_widget_get_toplevel(widget)),
129 GTK_DIALOG_DESTROY_WITH_PARENT,
130 GTK_MESSAGE_ERROR,
131 GTK_BUTTONS_CLOSE,
132 "%s",
133 g_strcompress(g_variant_print(
134 g_dbus_message_get_body(reply),
135 TRUE)));
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)
147 GtkWidget *label;
148 GtkWidget *halt_button;
149 GtkWidget *reboot_button;
150 GtkWidget *cancel_button;
152 GdkEventButton *bevent = (GdkEventButton *)event;
153 switch (bevent->button) {
154 case 1:
155 if (dialog != NULL)
156 return;
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))),
161 label);
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(
168 GTK_DIALOG(dialog),
169 GTK_RESPONSE_HALT);
170 reboot_button = gtk_dialog_get_widget_for_response(
171 GTK_DIALOG(dialog),
172 GTK_RESPONSE_REBOOT);
173 cancel_button = gtk_dialog_get_widget_for_response(
174 GTK_DIALOG(dialog),
175 GTK_RESPONSE_CANCEL);
177 g_signal_connect(dialog, "destroy", G_CALLBACK(fecha), NULL);
178 g_signal_connect(cancel_button,
179 "clicked",
180 G_CALLBACK(fecha),
181 (gpointer) dialog);
182 g_signal_connect(halt_button,
183 "clicked",
184 G_CALLBACK(handle_click),
185 HALT_METHOD);
186 g_signal_connect(reboot_button,
187 "clicked",
188 G_CALLBACK(handle_click),
189 REBOOT_METHOD);
190 gtk_widget_show_all(dialog);
191 break;
192 default:
193 break;
197 int main(int argc, char *argv[])
199 GError *error = NULL;
200 GOptionContext *context;
201 GtkWidget *gtkiw;
202 GtkWidget *dockArea;
203 GtkWidget *pixmap;
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);
213 if (showVersion) {
214 printf(PACKAGE_STRING"\n");
215 return 0;
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);
229 gtk_main();
231 return 0;