1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
|
/**
* Copyright (c) 2011-2013 Fabrice THIROUX <fabrice.thiroux@free.fr> (GPL-3+).
* Copyright (c) 2010-2011 Hong Jen Yee (aka PCMan) <pcman.tw@gmail.com>
* (GPL-2+).
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or any
* later version. See http://www.gnu.org/copyleft/lgpl.html the full text
* of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <locale.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include "config.h"
#include "dbus-interface.h"
#include "obsession.h"
/* Command parameters. */
static char * prompt = NULL;
static char * banner_side = NULL;
static char * banner_path = NULL;
static GOptionEntry opt_entries[] =
{
{ "prompt", 'p', 0, G_OPTION_ARG_STRING, &prompt, N_("Custom message to show on the dialog"), N_("message") },
{ "banner", 'b', 0, G_OPTION_ARG_STRING, &banner_path, N_("Banner to show on the dialog"), N_("image file") },
{ "side", 's', 0, G_OPTION_ARG_STRING, &banner_side, N_("Position of the banner"), "top|left|right|bottom" },
{ NULL }
};
static void logout_clicked(GtkButton * button, HandlerContext * handler_context);
static void shutdown_clicked(GtkButton * button, HandlerContext * handler_context);
static void reboot_clicked(GtkButton * button, HandlerContext * handler_context);
static void suspend_clicked(GtkButton * button, HandlerContext * handler_context);
static void hibernate_clicked(GtkButton * button, HandlerContext * handler_context);
static void switch_user_clicked(GtkButton * button, HandlerContext * handler_context);
static void cancel_clicked(GtkButton * button, gpointer user_data);
static GtkPositionType get_banner_position(void);
static GdkPixbuf * get_background_pixbuf(void);
gboolean expose_event(GtkWidget * widget, GdkEventExpose * event, GdkPixbuf * pixbuf);
/* Handler for "clicked" signal on Logout button. */
static void logout_clicked(GtkButton * button, HandlerContext * handler_context)
{
/* kill(handler_context->lxsession_pid, SIGTERM); */
g_spawn_command_line_async(handler_context->logout_cmd, NULL);
gtk_main_quit();
}
/* Handler for "clicked" signal on Shutdown button. */
static void shutdown_clicked(GtkButton * button, HandlerContext * handler_context)
{
GError *err = NULL;
gtk_label_set_text(GTK_LABEL(handler_context->error_label), NULL);
system_poweroff (handler_context, err);
if (err)
{
gtk_label_set_text(GTK_LABEL(handler_context->error_label), err->message);
g_error_free (err);
}
else gtk_main_quit();
}
/* Handler for "clicked" signal on Reboot button. */
static void reboot_clicked(GtkButton * button, HandlerContext * handler_context)
{
GError *err = NULL;
gtk_label_set_text(GTK_LABEL(handler_context->error_label), NULL);
system_reboot (handler_context, err);
if (err)
{
gtk_label_set_text(GTK_LABEL(handler_context->error_label), err->message);
g_error_free (err);
}
else gtk_main_quit();
}
/* Handler for "clicked" signal on Suspend button. */
static void suspend_clicked(GtkButton * button, HandlerContext * handler_context)
{
GError *err = NULL;
gtk_label_set_text(GTK_LABEL(handler_context->error_label), NULL);
system_suspend (handler_context, err);
if (err)
{
gtk_label_set_text(GTK_LABEL(handler_context->error_label), err->message);
g_error_free (err);
}
else gtk_main_quit();
}
/* Handler for "clicked" signal on Hibernate button. */
static void hibernate_clicked(GtkButton * button, HandlerContext * handler_context)
{
GError *err = NULL;
gtk_label_set_text(GTK_LABEL(handler_context->error_label), NULL);
system_hibernate (handler_context, err);
if (err)
{
gtk_label_set_text(GTK_LABEL(handler_context->error_label), err->message);
g_error_free (err);
}
else gtk_main_quit();
}
/* Handler for "clicked" signal on Switch User button. */
static void switch_user_clicked(GtkButton * button, HandlerContext * handler_context)
{
gtk_label_set_text(GTK_LABEL(handler_context->error_label), NULL);
system_user_switch (handler_context);
gtk_main_quit();
}
/* Handler for "clicked" signal on Cancel button. */
static void cancel_clicked(GtkButton * button, gpointer user_data)
{
gtk_main_quit();
}
/* Convert the --side parameter to a GtkPositionType. */
static GtkPositionType get_banner_position(void)
{
if (banner_side != NULL)
{
if (strcmp(banner_side, "right") == 0)
return GTK_POS_RIGHT;
if (strcmp(banner_side, "top") == 0)
return GTK_POS_TOP;
if (strcmp(banner_side, "bottom") == 0)
return GTK_POS_BOTTOM;
}
return GTK_POS_LEFT;
}
/* Get the background pixbuf. */
static GdkPixbuf * get_background_pixbuf(void)
{
/* Get the root window pixmap. */
GdkScreen * screen = gdk_screen_get_default();
#ifdef ENABLE_GTK3
GdkPixbuf * pixbuf = gdk_pixbuf_get_from_window(
gdk_get_default_root_window(),
0,
0,
gdk_screen_get_width(screen), /* Width */
gdk_screen_get_height(screen)); /* Height */
#else
GdkPixbuf * pixbuf = gdk_pixbuf_get_from_drawable(
NULL, /* Allocate a new pixbuf */
gdk_get_default_root_window(), /* The drawable */
NULL, /* Its colormap */
0, 0, 0, 0, /* Coordinates */
gdk_screen_get_width(screen), /* Width */
gdk_screen_get_height(screen)); /* Height */
#endif
/* Make the background darker. */
if (pixbuf != NULL)
{
unsigned char * pixels = gdk_pixbuf_get_pixels(pixbuf);
int width = gdk_pixbuf_get_width(pixbuf);
int height = gdk_pixbuf_get_height(pixbuf);
int pixel_stride = ((gdk_pixbuf_get_has_alpha(pixbuf)) ? 4 : 3);
int row_stride = gdk_pixbuf_get_rowstride(pixbuf);
int y;
for (y = 0; y < height; y += 1)
{
unsigned char * p = pixels;
int x;
for (x = 0; x < width; x += 1)
{
p[0] = p[0] / 2;
p[1] = p[1] / 2;
p[2] = p[2] / 2;
p += pixel_stride;
}
pixels += row_stride;
}
}
return pixbuf;
}
/* Handler for "expose_event" on background. */
gboolean expose_event(GtkWidget * widget, GdkEventExpose * event, GdkPixbuf * pixbuf)
{
if (pixbuf != NULL)
{
/* Copy the appropriate rectangle of the root window pixmap to the drawing area.
* All drawing areas are immediate children of the toplevel window, so the allocation yields the source coordinates directly. */
#if GTK_CHECK_VERSION(2,14,0)
cairo_t * cr = gdk_cairo_create (gtk_widget_get_window(widget));
#else
cairo_t * cr = gdk_cairo_create (widget->window);
#endif
gdk_cairo_set_source_pixbuf (
cr,
pixbuf,
0,
0);
cairo_paint (cr);
cairo_destroy(cr);
}
return FALSE;
}
/* Main program. */
int main(int argc, char * argv[])
{
#ifdef ENABLE_NLS
setlocale(LC_ALL, "");
bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
textdomain (GETTEXT_PACKAGE);
#endif
/* Initialize GTK (via g_option_context_parse) and parse command line arguments. */
GOptionContext * context = g_option_context_new("");
g_option_context_add_main_entries(context, opt_entries, GETTEXT_PACKAGE);
g_option_context_add_group(context, gtk_get_option_group(TRUE));
GError * err = NULL;
if ( ! g_option_context_parse(context, &argc, &argv, &err))
{
g_print(_("Error: %s\n"), err->message);
g_error_free(err);
return 1;
}
g_option_context_free(context);
HandlerContext handler_context;
initialize_context (&handler_context);
/* Make the button images accessible. */
gtk_icon_theme_append_search_path(gtk_icon_theme_get_default(), PACKAGE_DATA_DIR "/obsession/images");
/* Get the background pixbuf. */
GdkPixbuf * pixbuf = get_background_pixbuf();
/* Create the toplevel window. */
GtkWidget * window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
gtk_window_fullscreen(GTK_WINDOW(window));
GdkScreen* screen = gtk_widget_get_screen(window);
gtk_window_set_default_size(GTK_WINDOW(window), gdk_screen_get_width(screen), gdk_screen_get_height(screen));
gtk_widget_set_app_paintable(window, TRUE);
g_signal_connect(G_OBJECT(window), "expose_event", G_CALLBACK(expose_event), pixbuf);
/* Toplevel container */
GtkWidget* alignment = gtk_alignment_new(0.5, 0.5, 0.0, 0.0);
gtk_container_add(GTK_CONTAINER(window), alignment);
GtkWidget* center_area = gtk_event_box_new();
gtk_container_add(GTK_CONTAINER(alignment), center_area);
GtkWidget* center_vbox = gtk_vbox_new(FALSE, 6);
gtk_container_set_border_width(GTK_CONTAINER(center_vbox), 12);
gtk_container_add(GTK_CONTAINER(center_area), center_vbox);
GtkWidget* controls = gtk_vbox_new(FALSE, 6);
/* If specified, apply a user-specified banner image. */
if (banner_path != NULL)
{
GtkWidget * banner_image = gtk_image_new_from_file(banner_path);
GtkPositionType banner_position = get_banner_position();
switch (banner_position)
{
case GTK_POS_LEFT:
case GTK_POS_RIGHT:
{
/* Create a horizontal box to contain the image and the controls. */
GtkWidget * box = gtk_hbox_new(FALSE, 2);
gtk_box_pack_start(GTK_BOX(center_vbox), box, FALSE, FALSE, 0);
/* Pack the image and a separator. */
gtk_misc_set_alignment(GTK_MISC(banner_image), 0.5, 0.0);
if (banner_position == GTK_POS_LEFT)
{
gtk_box_pack_start(GTK_BOX(box), banner_image, FALSE, FALSE, 2);
gtk_box_pack_start(GTK_BOX(box), gtk_vseparator_new(), FALSE, FALSE, 2);
gtk_box_pack_start(GTK_BOX(box), controls, FALSE, FALSE, 2);
}
else
{
gtk_box_pack_start(GTK_BOX(box), controls, FALSE, FALSE, 2);
gtk_box_pack_end(GTK_BOX(box), gtk_vseparator_new(), FALSE, FALSE, 2);
gtk_box_pack_end(GTK_BOX(box), banner_image, FALSE, FALSE, 2);
}
}
break;
case GTK_POS_TOP:
gtk_box_pack_start(GTK_BOX(controls), banner_image, FALSE, FALSE, 2);
gtk_box_pack_start(GTK_BOX(controls), gtk_hseparator_new(), FALSE, FALSE, 2);
gtk_box_pack_start(GTK_BOX(center_vbox), controls, FALSE, FALSE, 0);
break;
case GTK_POS_BOTTOM:
gtk_box_pack_end(GTK_BOX(controls), banner_image, FALSE, FALSE, 2);
gtk_box_pack_end(GTK_BOX(controls), gtk_hseparator_new(), FALSE, FALSE, 2);
gtk_box_pack_start(GTK_BOX(center_vbox), controls, FALSE, FALSE, 0);
break;
}
}
else
gtk_box_pack_start(GTK_BOX(center_vbox), controls, FALSE, FALSE, 0);
/* Create the label. */
GtkWidget * label = gtk_label_new("");
if (prompt == NULL)
{
const char * session_name = "OpenBox"; // was LXDE
prompt = g_strdup_printf(_("<b><big>Logout %s session?</big></b>"), session_name);
}
gtk_label_set_markup(GTK_LABEL(label), prompt);
gtk_box_pack_start(GTK_BOX(controls), label, FALSE, FALSE, 4);
/* Create the Shutdown button. */
if (handler_context.poweroff)
{
GtkWidget * shutdown_button = gtk_button_new_with_mnemonic(_("Sh_utdown"));
GtkWidget * image = gtk_image_new_from_icon_name("system-shutdown", GTK_ICON_SIZE_BUTTON);
gtk_button_set_image(GTK_BUTTON(shutdown_button), image);
gtk_button_set_alignment(GTK_BUTTON(shutdown_button), 0.0, 0.5);
g_signal_connect(G_OBJECT(shutdown_button), "clicked", G_CALLBACK(shutdown_clicked), &handler_context);
gtk_box_pack_start(GTK_BOX(controls), shutdown_button, FALSE, FALSE, 4);
}
/* Create the Reboot button. */
if (handler_context.reboot)
{
GtkWidget * reboot_button = gtk_button_new_with_mnemonic(_("_Reboot"));
GtkWidget * image = gtk_image_new_from_icon_name("system-restart", GTK_ICON_SIZE_BUTTON);
gtk_button_set_image(GTK_BUTTON(reboot_button), image);
gtk_button_set_alignment(GTK_BUTTON(reboot_button), 0.0, 0.5);
g_signal_connect(G_OBJECT(reboot_button), "clicked", G_CALLBACK(reboot_clicked), &handler_context);
gtk_box_pack_start(GTK_BOX(controls), reboot_button, FALSE, FALSE, 4);
}
/* Create the Suspend button. */
if (handler_context.suspend)
{
GtkWidget * suspend_button = gtk_button_new_with_mnemonic(_("_Suspend"));
GtkWidget * image = gtk_image_new_from_icon_name("system-suspend", GTK_ICON_SIZE_BUTTON);
gtk_button_set_image(GTK_BUTTON(suspend_button), image);
gtk_button_set_alignment(GTK_BUTTON(suspend_button), 0.0, 0.5);
g_signal_connect(G_OBJECT(suspend_button), "clicked", G_CALLBACK(suspend_clicked), &handler_context);
gtk_box_pack_start(GTK_BOX(controls), suspend_button, FALSE, FALSE, 4);
}
/* Create the Hibernate button. */
if (handler_context.hibernate)
{
GtkWidget * hibernate_button = gtk_button_new_with_mnemonic(_("_Hibernate"));
GtkWidget * image = gtk_image_new_from_icon_name("system-hibernate", GTK_ICON_SIZE_BUTTON);
gtk_button_set_image(GTK_BUTTON(hibernate_button), image);
gtk_button_set_alignment(GTK_BUTTON(hibernate_button), 0.0, 0.5);
g_signal_connect(G_OBJECT(hibernate_button), "clicked", G_CALLBACK(hibernate_clicked), &handler_context);
gtk_box_pack_start(GTK_BOX(controls), hibernate_button, FALSE, FALSE, 4);
}
/* Create the Switch User button. */
if (handler_context.switch_user)
{
GtkWidget * switch_user_button = gtk_button_new_with_mnemonic(_("S_witch User"));
GtkWidget * image = gtk_image_new_from_icon_name("system-switch-user", GTK_ICON_SIZE_BUTTON);
gtk_button_set_image(GTK_BUTTON(switch_user_button), image);
gtk_button_set_alignment(GTK_BUTTON(switch_user_button), 0.0, 0.5);
g_signal_connect(G_OBJECT(switch_user_button), "clicked", G_CALLBACK(switch_user_clicked), &handler_context);
gtk_box_pack_start(GTK_BOX(controls), switch_user_button, FALSE, FALSE, 4);
}
/* Create the Logout button. */
GtkWidget * logout_button = gtk_button_new_with_mnemonic(_("_Logout"));
GtkWidget * image = gtk_image_new_from_icon_name("system-log-out", GTK_ICON_SIZE_BUTTON);
gtk_button_set_image(GTK_BUTTON(logout_button), image);
gtk_button_set_alignment(GTK_BUTTON(logout_button), 0.0, 0.5);
g_signal_connect(G_OBJECT(logout_button), "clicked", G_CALLBACK(logout_clicked), &handler_context);
gtk_box_pack_start(GTK_BOX(controls), logout_button, FALSE, FALSE, 4);
/* Create the Cancel button. */
GtkWidget * cancel_button = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
gtk_button_set_alignment(GTK_BUTTON(cancel_button), 0.0, 0.5);
g_signal_connect(G_OBJECT(cancel_button), "clicked", G_CALLBACK(cancel_clicked), NULL);
gtk_box_pack_start(GTK_BOX(controls), cancel_button, FALSE, FALSE, 4);
/* Create the error text. */
handler_context.error_label = gtk_label_new("");
gtk_label_set_justify(GTK_LABEL(handler_context.error_label), GTK_JUSTIFY_CENTER);
gtk_box_pack_start(GTK_BOX(controls), handler_context.error_label, FALSE, FALSE, 4);
/* Show everything. */
gtk_widget_show_all(window);
/* Run the main event loop. */
gtk_main();
/* Return. */
return 0;
}
|