From 8e8488e77076c7206415c31a41c87e32c65de556 Mon Sep 17 00:00:00 2001 From: Pierre Pronchery Date: Mon, 24 Dec 2012 03:27:12 +0100 Subject: [PATCH] Added some more helpers --- tools/helper.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++ tools/notify.c | 5 ++++ tools/test.c | 3 ++ 3 files changed, 87 insertions(+) diff --git a/tools/helper.c b/tools/helper.c index 021605c..ae5552b 100644 --- a/tools/helper.c +++ b/tools/helper.c @@ -19,6 +19,12 @@ static char const _license[] = +#ifdef __NetBSD__ +# include +# include +#else +# include +#endif #include #include #include @@ -35,6 +41,7 @@ struct _Panel Config * config; GtkWidget * window; gint timeout; + guint source; }; @@ -72,11 +79,13 @@ static char const * _helper_config_get(Panel * panel, char const * section, char const * variable); static int _helper_error(Panel * panel, char const * message, int ret); static void _helper_about_dialog(Panel * panel); +static int _helper_lock(Panel * panel); static void _helper_logout_dialog(Panel * panel); static void _helper_position_menu(Panel * panel, GtkMenu * menu, gint * x, gint * y, gboolean * push_in); static void _helper_rotate_screen(Panel * panel); static void _helper_shutdown_dialog(Panel * panel); +static int _helper_suspend(Panel * panel); /* functions */ @@ -154,6 +163,7 @@ static void _helper_init(PanelAppletHelper * helper, Panel * panel, helper->position_menu = _helper_position_menu; helper->rotate_screen = _helper_rotate_screen; helper->shutdown_dialog = _helper_shutdown_dialog; + helper->suspend = _helper_suspend; } @@ -204,6 +214,37 @@ static void _helper_about_dialog(Panel * panel) } +/* helper_lock */ +static gboolean _lock_on_idle(gpointer data); + +static int _helper_lock(Panel * panel) +{ + panel->source = g_idle_add(_lock_on_idle, panel); + return 0; +} + +static gboolean _lock_on_idle(gpointer data) +{ + /* XXX code duplicated from panel.c */ + /* FIXME default to calling XActivateScreenSaver() */ + Panel * panel = data; + char const * command = "xset s activate"; + char const * p; + GError * error = NULL; + + panel->source = 0; + if((p = config_get(panel->config, "lock", "command")) != NULL) + command = p; + if(g_spawn_command_line_async(command, &error) != TRUE) + { + /* XXX will also call perror() */ + _helper_error(panel, error->message, 1); + g_error_free(error); + } + return FALSE; +} + + /* helper_logout_dialog */ static void _helper_logout_dialog(Panel * panel) { @@ -306,3 +347,41 @@ static void _helper_shutdown_dialog(Panel * panel) gtk_dialog_run(GTK_DIALOG(dialog)); gtk_widget_destroy(dialog); } + + +/* helper_suspend */ +static int _helper_suspend(Panel * panel) +{ + /* XXX code duplicated from panel.c */ +#ifdef __NetBSD__ + int sleep_state = 3; +#else + int fd; + char * suspend[] = { "/usr/bin/sudo", "sudo", "/usr/bin/apm", "-s", + NULL }; + int flags = G_SPAWN_FILE_AND_ARGV_ZERO; + GError * error = NULL; +#endif + +#ifdef __NetBSD__ + if(sysctlbyname("machdep.sleep_state", NULL, NULL, &sleep_state, + sizeof(sleep_state)) != 0) + return -_helper_error(panel, "sysctl", 1); +#else + if((fd = open("/sys/power/state", O_WRONLY)) >= 0) + { + write(fd, "mem\n", 4); + close(fd); + } + else if(g_spawn_async(NULL, suspend, NULL, flags, NULL, NULL, NULL, + &error) != TRUE) + { + /* XXX will also call perror() */ + _helper_error(panel, error->message, 1); + g_error_free(error); + return -1; + } +#endif + _helper_lock(panel); /* XXX may already be suspended */ + return 0; +} diff --git a/tools/notify.c b/tools/notify.c index a799216..6ca0234 100644 --- a/tools/notify.c +++ b/tools/notify.c @@ -110,7 +110,12 @@ static int _notify(GtkIconSize iconsize, int timeout, char * applets[]) if(timeout > 0) panel.timeout = g_timeout_add(timeout * 1000, _notify_on_timeout, &panel); + panel.source = 0; gtk_main(); + if(panel.timeout != 0) + g_source_remove(panel.timeout); + if(panel.source != 0) + g_source_remove(panel.source); return 0; } diff --git a/tools/test.c b/tools/test.c index d44f836..3a18c7b 100644 --- a/tools/test.c +++ b/tools/test.c @@ -95,7 +95,10 @@ static int _test(GtkIconSize iconsize, char * applets[]) gtk_container_add(GTK_CONTAINER(panel.window), box); gtk_widget_show_all(panel.window); panel.timeout = 0; + panel.source = 0; gtk_main(); + if(panel.source != 0) + g_source_remove(panel.source); return 0; }