diff options
author | test-tools <test-tools> | 2006-10-11 10:08:39 +0000 |
---|---|---|
committer | test-tools <test-tools> | 2006-10-11 10:08:39 +0000 |
commit | 40bf9a94ba6cde2397e71dde5ea68bb993e277ba (patch) | |
tree | ff3928126c72f3c46c7fe0bf40fd8f347127e4a8 /src | |
parent | d5f7aebc13c296bf2763dc13d3b4a248550cd61d (diff) | |
download | beryl-manager-40bf9a94ba6cde2397e71dde5ea68bb993e277ba.tar.gz beryl-manager-40bf9a94ba6cde2397e71dde5ea68bb993e277ba.tar.bz2 |
beryl-manager: Added signal USR1 handling.
Trying to invoke multiple times will result in popping up the menu from already running instance.
Diffstat (limited to 'src')
-rw-r--r-- | src/main.c | 73 |
1 files changed, 58 insertions, 15 deletions
@@ -1,7 +1,11 @@ #ifdef HAVE_CONFIG_H #include <config.h> #endif + +#include <glib.h> +#include <glib/gstdio.h> #include <gtk/gtk.h> + #include <sys/types.h> #include <sys/wait.h> #include <sys/file.h> @@ -9,6 +13,7 @@ #include <string.h> #include <unistd.h> #include <stdlib.h> +#include <signal.h> #include "eggtrayicon.h" //intl stuff #include<libintl.h> @@ -87,6 +92,7 @@ GtkWidget * DMSubItem; GtkWidget * reloadDecoratorItem; void launchWM(); +void showMenu(guint,guint32); static void usage (const char *programName) @@ -95,6 +101,20 @@ usage (const char *programName) "[-n] [--help] [--version]" "\n"), programName); } +static void +signalHandler (int sig) +{ + switch (sig) + { + case SIGUSR1: + //FIXME, here we should temporarily disable/hide the last "Quit" entry, + //Could be missleading to the average user... + showMenu(0,gtk_get_current_event_time()); + break; + default: + break; + } +} void beryl_manager_log_handler (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, @@ -333,8 +353,9 @@ void killWM() { if (WMs[i].Kill) { - gchar * cm = g_strconcat("pidof ",WMs[i].Grep,NULL); gchar * pret; + gchar * cm = g_strconcat("pidof ",WMs[i].Grep,NULL); + if (!g_spawn_command_line_sync(cm,&pret,NULL,NULL,NULL)) g_warning(_("No pidof, this may not work right.")); else @@ -878,7 +899,11 @@ int main(int argc, char ** argv) { gint fd; gint e; + gchar *buffer; + gsize len; + GPid pid=0; gboolean daemon_mode = TRUE; + gchar* lockfile = g_strconcat(g_get_home_dir(),"/.beryl-manager.lock",NULL); Display * d; //Intialise error handler @@ -895,6 +920,7 @@ int main(int argc, char ** argv) bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); textdomain (GETTEXT_PACKAGE); + //parse command line arguments for (fd=1;fd<argc;fd++) { if (strcmp(argv[fd],"--help")==0) { usage(argv[0]); @@ -909,30 +935,42 @@ int main(int argc, char ** argv) return 1; } } + + //detach from console or not if (daemon_mode) { daemon(1,1); close(0); } - fd = creat(g_strconcat(g_get_home_dir(),"/.beryl-manager.lock",NULL) - ,00755); - e=errno; - if (fd==-1) - { - - g_warning(_("Couldn't open lockfile.\nError:%s"),strerror(e)); - //example to get localization - //g_warning(gettext("Couldn't open lockfile.\nError:%s"),strerror(e)); + //Already running? + if (g_file_get_contents(lockfile,&buffer,&len,NULL)) { + pid = atoi(buffer); + } + //Open or create the lockfile + fd = g_open(lockfile, O_WRONLY|O_CREAT ,00755); + e=errno; + if (fd==-1) { + g_warning(_("Couldn't open lockfile.\nError:%s"),strerror(e)); return 1; } + if (flock(fd,LOCK_EX | LOCK_NB)!=0) - { - // Commenting out, too much respawned beryl-managers - //execlp("beryl-manager","beryl-manager",NULL); - manager_error(_("Couldn't get lock on lockfile")); + { + close (fd); + if (pid) + if (kill(pid,SIGUSR1)) + return 0; + + return 1; + } + + buffer = g_strdup_printf("%d\n",getpid()); + if (buffer) { + write(fd,buffer,strlen(buffer)); + g_free(buffer); } - dprintf(fd,"%d\n",getpid()); + if (!XInitThreads()) { g_warning(_("Can't init XLib thread support")); @@ -999,6 +1037,11 @@ int main(int argc, char ** argv) if (!is_wm_running(WM)) launchWM(); + signal (SIGUSR1, signalHandler); + gtk_main(); + + close(fd); + g_unlink(lockfile); return 0; } |