Permalink
Browse files

Added some more helpers

  • Loading branch information...
1 parent 2550eab commit 8e8488e77076c7206415c31a41c87e32c65de556 @khorben khorben committed Dec 24, 2012
Showing with 87 additions and 0 deletions.
  1. +79 −0 tools/helper.c
  2. +5 −0 tools/notify.c
  3. +3 −0 tools/test.c
View
@@ -19,6 +19,12 @@ static char const _license[] =
+#ifdef __NetBSD__
+# include <sys/param.h>
+# include <sys/sysctl.h>
+#else
+# include <fcntl.h>
+#endif
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
@@ -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;
+}
View
@@ -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;
}
View
@@ -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;
}

0 comments on commit 8e8488e

Please sign in to comment.