diff options
author | test-tools <test-tools> | 2006-10-13 14:49:56 +0000 |
---|---|---|
committer | test-tools <test-tools> | 2006-10-13 14:49:56 +0000 |
commit | 0ddc923145b07913d92456a5c2cf00781c47fb33 (patch) | |
tree | 0f86f8523ac4d4f2a6eb7dcaed716c3a94a9d1fe /src | |
parent | fc2af7ab7d367f9b9851cf65aa2487e798da62c1 (diff) | |
download | beryl-manager-0ddc923145b07913d92456a5c2cf00781c47fb33.tar.gz beryl-manager-0ddc923145b07913d92456a5c2cf00781c47fb33.tar.bz2 |
beryl-manager: Should now work with multiple DISPLAYs on same machine.
.beryl-manager.lock became .beryl-manager.DISPLAYNAME.pid, no flock() here, flock() doesn't work on NFS-mounted homedirs.
Function is_running() rewritten.
Don't create a Atom "WM_S0" anymore, if we don't get one,.
Minor code cleans, doc updated.
This is_running() stuff only work on the same machine, when having stuff running remote and parts from different machines, we are still in bigger trouble.
Diffstat (limited to 'src')
-rw-r--r-- | src/main.c | 171 |
1 files changed, 115 insertions, 56 deletions
@@ -43,6 +43,8 @@ gboolean useFB=TRUE; gint WM=0; gint fallBackWM=0; gint DM=0; +gchar* pidfile; +gchar* displayname; typedef struct _DMInfo { gchar * Name; @@ -70,7 +72,7 @@ DMInfo DMs[] = { {N_("Light Themable Decorator (yawd)"), "yawd" ,0}, {N_("GTK Window Decorator"), "gtk-window-decorator" ,0}, }; -#define numDM (sizeof(DMs)/sizeof(DMInfo)) +#define numDM (gint)(sizeof(DMs)/sizeof(DMInfo)) WMInfo WMs[]={ {N_("Beryl"), 0, 0, 0 ,0,0,0,0}, {N_("Compiz"), "compiz", "--replace gconf", "compiz" ,0,0,0,0}, @@ -85,7 +87,7 @@ WMInfo WMs[]={ {N_("IceWM"), "icewm", "", "icewm" ,0,0,0,0}, {N_("Enlightenment"), "enlightenment", "", "enlightenment" ,0,1,0,0}, }; -#define numWM (sizeof(WMs)/sizeof(WMInfo)) +#define numWM (gint)(sizeof(WMs)/sizeof(WMInfo)) GtkWidget * useFBItem; GtkWidget * FBSubItem; GtkWidget * DMSubItem; @@ -232,18 +234,48 @@ void save_settings() } gboolean is_running(const gchar * command) { - gint ex; - if (!g_spawn_command_line_sync( - g_strdup_printf("sh -c \"ps xo comm | egrep '^%s$' > /dev/null\"",command), - NULL,NULL,&ex,NULL)) - manager_error(_("can't use this app, no ps or grep")); - if (WIFEXITED(ex)) - { - if (WEXITSTATUS(ex)==0) - return TRUE; + gchar* cmd = g_strconcat("pidof ", command,NULL); + gchar* pret; + gint i; + + if (g_spawn_command_line_sync(cmd, + &pret, + NULL,NULL,NULL)) { + g_strchomp(pret); + if (pret && *pret) { + gchar **pidlist = g_strsplit(pret," ",0); + i = 0; + while (pidlist[i]) { + if (atoi(pidlist[i])) { + gchar *buffer; + gsize len; + gchar *file = g_strconcat("/proc/",pidlist[i],"/environ",NULL); + if (g_file_get_contents(file,&buffer,&len,NULL)) { + gchar * cp; + for(cp=buffer;cp<(buffer+len);cp+=strlen(cp)+1) + { + if (strncmp(cp,"DISPLAY=",8)==0) + { + if (strcmp(cp+8,displayname)==0) + { + g_strfreev(pidlist); + g_free(pret); + g_free(file); + g_free(buffer); + return TRUE; + } + } + } + } + g_free(buffer); + g_free(file); + } + i++; + } + g_strfreev(pidlist); + } + g_free(pret); } - else - manager_error(_("something went wrong with ps or grep")); return FALSE; } gboolean is_decor_running() @@ -365,7 +397,8 @@ void killWM() g_warning(_("No pidof, this may not work right.")); else { - if (pret && strcmp(pret,"\n")) + g_strchomp(pret); + if (pret) { gchar * cret; if (strlen(pret)) @@ -395,7 +428,7 @@ void killWM() if (strncmp(cp,"DISPLAY=",8)==0) { //might be killable - gchar * md = getenv("DISPLAY"); + gchar * md = displayname; if (strcmp(cp+8,md)==0) { //on same display, go ahead kill @@ -419,7 +452,7 @@ void killWM() g_free(cm); } } - Window w=XGetSelectionOwner(d,wmAtom); + Window w=wmAtom?XGetSelectionOwner(d,wmAtom):None; if (w!=None) XKillClient(d,w); else if (killed) @@ -903,13 +936,9 @@ void detect_xgl() } int main(int argc, char ** argv) { - gint fd; - gint e; + gint i; 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 @@ -927,14 +956,14 @@ int main(int argc, char ** argv) textdomain (GETTEXT_PACKAGE); //parse command line arguments - for (fd=1;fd<argc;fd++) { - if (strcmp(argv[fd],"--help")==0) { + for (i=1;i<argc;i++) { + if (strcmp(argv[i],"--help")==0) { usage(argv[0]); return 0; - } else if (strcmp(argv[fd],"--version")==0) { + } else if (strcmp(argv[i],"--version")==0) { printf (PACKAGE_STRING "\n"); return 0; - } else if (strcmp(argv[fd],"-d")==0) { + } else if (strcmp(argv[i],"-d")==0) { daemon_mode = FALSE; } else { usage(argv[0]); @@ -948,44 +977,75 @@ int main(int argc, char ** argv) close(0); } - //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) + if (!XInitThreads()) { - close (fd); - if (pid) - if (kill(pid,SIGUSR1)) - return 0; - - return 1; + g_warning(_("Can't init XLib thread support")); + return 3; } - buffer = g_strdup_printf("%d\n",getpid()); + gtk_init(&argc,&argv); + d = GDK_DISPLAY_XDISPLAY(gdk_display_get_default()); + + buffer = g_strdup(DisplayString(gdk_x11_display_get_xdisplay(gdk_display_get_default()))); if (buffer) { - write(fd,buffer,strlen(buffer)); + gint num; + gchar *p=buffer; + displayname = g_strdup(buffer); + + if (g_strrstr(p,":")) { + p=g_strrstr(p,":"); + p++; + } + num = atoi(p); g_free(buffer); + buffer = g_strdup_printf("%d",num); + } else { + buffer=strdup("-"); } + + pidfile = g_strconcat(g_get_home_dir(),"/.beryl-manager.",buffer,".pid", NULL); + g_free(buffer); + - if (!XInitThreads()) - { - g_warning(_("Can't init XLib thread support")); - return 3; + //Already running? Get the pid out of the file + if (g_file_get_contents(pidfile,&buffer,NULL,NULL)) { + GPid pid = atoi(buffer); + gchar * pret; + + g_free(buffer); + + // Look for already live instances + if (pid && + g_spawn_command_line_sync("pidof beryl-manager", + &pret, + NULL,NULL,NULL)) { + g_strchomp(pret); + if (pret && *pret) { + gchar **pidlist = g_strsplit(pret," ",0); + i = 0; + while (pidlist[i]) { + if (pid == atoi(pidlist[i])) { + //correct process exists + if (kill(pid,SIGUSR1)==0) { + //signal delivered + return 0; + } + } + i++; + } + g_strfreev(pidlist); + } + if (pret) + g_free(pret); + } } - gtk_init(&argc,&argv); + buffer = g_strdup_printf("%d\n",getpid()); + g_file_set_contents(pidfile, buffer, -1, NULL); + g_free(buffer); + + // Check for the windows manager + wmAtom = XInternAtom(d,"WM_S0",1); - d = GDK_DISPLAY_XDISPLAY(gdk_display_get_default()); - wmAtom = XInternAtom(d,"WM_S0",0); if (!g_thread_supported()) g_thread_init(NULL); mutx=g_mutex_new(); @@ -1047,7 +1107,6 @@ int main(int argc, char ** argv) gtk_main(); - close(fd); - g_unlink(lockfile); + g_unlink(pidfile); return 0; } |