#ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #include #include #include "eggtrayicon.h" //intl stuff #include #include #define _(String) gettext (String) #define gettext_noop(String) String #define N_(String) gettext_noop (String) Atom wmAtom; GMutex * mutx; EggTrayIcon * mainIcon; GtkWidget * menu; GtkTooltips * tips; GtkWidget * useStrictBindingItem=NULL; gboolean useStrictBind=FALSE; gboolean hasMouse=FALSE; gboolean berylLaunched=FALSE; gboolean decoratorLaunched=FALSE; gboolean reloadingBeryl=FALSE; gboolean reloadingDecorator=FALSE; gboolean XGL=FALSE; gboolean NV9XXX=FALSE; gboolean useFB=TRUE; gboolean firstStart=TRUE; gint WM=0; gint fallBackWM=0; gint DM=0; typedef struct _DMInfo { gchar * Name; gchar * Prog; GtkWidget * Item; } DMInfo; typedef struct _WMInfo { gchar * Name; gchar * Prog; gchar * Opts; gchar * Grep; gint xXGL; // if nonzero, can't use on XGL gint Kill; // if nonzero, means has to be killall'd by its grep-name, and should NOT be xkill'd GtkWidget * Item; GtkWidget * FBItem; } WMInfo; typedef struct _XLSItem { gchar * Command; Window w; } XLSItem; DMInfo DMs[] = { {N_("Standard Beryl Decorator (Emerald)"), "emerald" ,0}, {N_("Light Themable Decorator (yawd)"), "yawd" ,0}, {N_("GTK Window Decorator"), "gtk-window-decorator" ,0}, }; #define numDM (sizeof(DMs)/sizeof(DMInfo)) WMInfo WMs[]={ {N_("Beryl"), 0, 0, 0 ,0,0,0,0}, {N_("Compiz"), "compiz", "--replace gconf" ,0,0,0,0}, {N_("Compiz with COW"), "compiz", "--use-cow --replace gconf" ,0,0,0,0}, {N_("Metacity (Gnome Window Manager)"), "metacity", "--replace", "metacity" ,0,0,0,0}, {N_("KWin (KDE Window Manager)"), "kwin", "--replace", "kwin" ,0,0,0,0}, {N_("xfwm4 (XFCE Window Manager)"), "xfwm4", "", "xfwm4" ,0,1,0,0}, {N_("WindowMaker"), "wmaker", "", "WindowMaker" ,0,0,0,0}, {N_("FluxBox"), "fluxbox", "", "fluxbox" ,1,1,0,0}, {N_("BlackBox"), "blackbox", "", "blackbox" ,0,1,0,0}, {N_("OpenBox"), "openbox", "", "openbox" ,0,0,0,0}, {N_("IceWM"), "icewm", "", "icewm" ,0,0,0,0}, {N_("Enlightenment"), "enlightenment", "", "enlightenment" ,0,1,0,0}, }; #define numWM (sizeof(WMs)/sizeof(WMInfo)) GtkWidget * useFBItem; GtkWidget * FBSubItem; GtkWidget * DMSubItem; GtkWidget * reloadDecoratorItem; void launchWM(); GSList * parse_xlsclients(gchar * output) { GSList * ret = NULL; gchar * curpos; for(curpos=output;curpos && curpos[0];curpos=strstr(curpos,"\nWindow")) { XLSItem * i; gchar * p; gchar * q; curpos+=8; i=malloc(sizeof(XLSItem)); i->w=strtol(curpos,NULL,16); p=strstr(curpos,"Command: "); if (!p) { g_warning(_("Malformed xlsclients, no command?")); free(i); continue; } q=strchr(p,'\n'); if (!q) { g_warning(_("Malformed xlsclients, no command?")); free(i); continue; } q[0]='\0'; p=g_strdup(p+10); q[0]='\n'; //trim p down to its basename q=strchr(p,' '); if (q) q[0]='\0'; q=strrchr(p,'/'); if (!q) q=p; else q++; i->Command=g_strdup(q); g_free(p); ret=g_slist_prepend(ret,i); } return ret; } void load_settings() { gchar * path = g_strconcat(g_get_home_dir(),"/.beryl-managerrc",NULL); GKeyFile * f = g_key_file_new(); if (g_key_file_load_from_file(f,path,0,NULL)) { GError * e = NULL; gint i; gboolean b; i=g_key_file_get_integer(f,"wm-settings","active_wm",&e); if (!e) WM=i; e=NULL; i=g_key_file_get_integer(f,"wm-settings","fallback_wm",&e); if (!e) fallBackWM=i; e=NULL; i=g_key_file_get_integer(f,"wm-settings","active_dm",&e); if (!e) DM=i; e=NULL; b=g_key_file_get_boolean(f,"wm-settings","use_fallback_wm",&e); if (!e) useFB=b; e=NULL; if (!XGL && NV9XXX) { b=g_key_file_get_boolean(f,"beryl-settings", "use_strict_binding",&e); if (!e) useStrictBind=b; e=NULL; } else useStrictBind=FALSE; } g_key_file_free(f); g_free(path); } void save_settings() { gchar * path = g_strconcat(g_get_home_dir(),"/.beryl-managerrc",NULL); GKeyFile * f = g_key_file_new(); g_key_file_set_integer(f,"wm-settings","active_wm",WM); g_key_file_set_integer(f,"wm-settings","fallback_wm",fallBackWM); g_key_file_set_integer(f,"wm-settings","active_dm",DM); g_key_file_set_boolean(f,"wm-settings","use_fallback_wm",useFB); if (!XGL && NV9XXX) g_key_file_set_boolean(f,"beryl-settings","use_strict_binding", useStrictBind); g_file_set_contents(path,g_key_file_to_data(f,NULL,NULL),-1,NULL); g_key_file_free(f); g_free(path); } gboolean is_running(const gchar * command) { gint ex; if (!g_spawn_command_line_sync( g_strdup_printf("sh -c 'pidof %s > /dev/null'",command), NULL,NULL,&ex,NULL)) g_error(_("can't use this app, no ps or grep")); if (WIFEXITED(ex)) { if (WEXITSTATUS(ex)==0) return TRUE; } else g_error(_("something went wrong with ps or grep")); return FALSE; } gboolean is_decor_running() { if (DM>numDM || DM<0) DM=0; return (is_running(DMs[DM].Prog)); } void showMenu(guint button,guint32 time) { gtk_menu_popup(GTK_MENU(menu),NULL,NULL,NULL,NULL,0,time); } gboolean buttonUp(GtkWidget * w, GdkEventButton * e, gpointer d) { if (hasMouse) showMenu(e->button,e->time); return TRUE; } gboolean enterNotify(GtkWidget * w, GdkEventCrossing * e, gpointer d) { hasMouse=TRUE; return TRUE; } gboolean leaveNotify(GtkWidget * w, GdkEventCrossing * e, gpointer d) { hasMouse=FALSE; return TRUE; } void startApp(GtkWidget * w, gchar * command) { g_spawn_command_line_async(command,NULL); } gboolean decoratorSignalled(gint signal) { g_warning(_("Decorator caught deadly signal %d"),signal); return FALSE; // terminate for now } gboolean berylSignalled(gint signal) { g_warning(_("Beryl caught deadly signal %d"),signal); return FALSE; // terminate for now } gpointer decorThread(gpointer d) { gint ex=0; decoratorLaunched=TRUE; while(decoratorLaunched) { gchar * s = g_strconcat(DMs[DM].Prog," --replace",NULL); if (!g_spawn_command_line_sync(s,NULL,NULL,&ex,NULL)) { g_warning(_("Couldn't launch selected decorator:%s"),DMs[DM].Name); decoratorLaunched=FALSE; } else if (reloadingDecorator) { g_free(s); reloadingDecorator=FALSE; return NULL; } else if (WIFEXITED(ex)) // returned something { if (WEXITSTATUS(ex)) // returned something other than 0 { decoratorLaunched=FALSE; } } else if (WIFSIGNALED(ex)) // killed by a signal { if (!decoratorSignalled(WTERMSIG(ex))) { decoratorLaunched=FALSE; } } g_free(s); } return NULL; } void killIfWM(XLSItem * i, Display * d) { gint j; for (j=1;jCommand,WMs[j].Grep)==0) { g_message(_("Killing window %x for client %s."),(unsigned int)i->w,WMs[j].Name); XKillClient(d,i->w); } } } } void freeXLS(XLSItem * i, gpointer p) { g_free(i->Command); free(i); } void killWM() { if (!g_mutex_trylock(mutx)) return; { Display * d = GDK_DISPLAY_XDISPLAY(gdk_display_get_default()); XLockDisplay(d); { gint i; for (i=1;i0) { nam=WMs[wm].Grep; return is_running(nam); } else { return is_running("beryl") || is_running("beryl-xgl"); } } gboolean detect_app(const gchar * app) { gint ex; if (!g_spawn_command_line_sync( g_strdup_printf("sh -c 'which %s > /dev/null'",app), NULL,NULL,&ex,NULL)) g_error(_("can't use this app, no which")); if (WIFEXITED(ex)) { if (WEXITSTATUS(ex)==0) return TRUE; } else g_error(_("something went wrong with which")); return FALSE; } gboolean detect_wm(gint wm) { if (!XGL || !WMs[wm].xXGL) { if (detect_app(WMs[wm].Prog)) return TRUE; } if (wm == WM) WM=-1; if (wm == fallBackWM+1) fallBackWM=-1; return FALSE; } gboolean detect_dm(gint dm) { if (detect_app(DMs[dm].Prog)) return TRUE; if (dm == DM) DM=-1; return FALSE; } void setUseStrict(GtkWidget * w, gpointer p) { useStrictBind=gtk_check_menu_item_get_active( GTK_CHECK_MENU_ITEM(w)); save_settings(); if (WM==0) reloadBeryl(); } void init_menu() { GtkWidget * mitem; GtkWidget * smshell; GSList * wms = NULL; GSList * fbk = NULL; GSList * dms = NULL; gint i; menu = gtk_menu_new(); g_object_ref(menu); mitem = gtk_image_menu_item_new_with_label(_("Beryl Settings Manager")); gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(mitem), make_image(PIXMAPS_DIR "/beryl-settings.svg")); gtk_menu_shell_append(GTK_MENU_SHELL(menu),mitem); gtk_widget_show(mitem); g_signal_connect(mitem,"activate",G_CALLBACK(startApp),"beryl-settings"); mitem = gtk_image_menu_item_new_with_label(_("Emerald Theme Manager")); gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(mitem), make_image(PIXMAPS_DIR "/emerald-theme-manager-icon.png")); gtk_menu_shell_append(GTK_MENU_SHELL(menu),mitem); gtk_widget_show(mitem); g_signal_connect(mitem,"activate",G_CALLBACK(startApp),"emerald-theme-manager"); mitem = gtk_separator_menu_item_new(); gtk_menu_shell_append(GTK_MENU_SHELL(menu),mitem); gtk_widget_show(mitem); mitem = gtk_image_menu_item_new_with_label(_("Reload Window Manager")); gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(mitem), gtk_image_new_from_stock(GTK_STOCK_REFRESH,GTK_ICON_SIZE_MENU)); g_signal_connect(mitem,"activate",G_CALLBACK(reloadWM),NULL); gtk_menu_shell_append(GTK_MENU_SHELL(menu),mitem); gtk_widget_show(mitem); mitem = gtk_image_menu_item_new_with_label(_("Select Window Manager")); gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(mitem), make_image(DATADIR "/beryl/icon.png")); gtk_menu_shell_append(GTK_MENU_SHELL(menu),mitem); gtk_widget_show(mitem); smshell=gtk_menu_new(); gtk_menu_item_set_submenu(GTK_MENU_ITEM(mitem),smshell); for (i=0;i /dev/null'", NULL,NULL,&ex,NULL)) g_error(_("can't use this app, no glxinfo or no grep")); if (WIFEXITED(ex)) { if (WEXITSTATUS(ex)==0) NV9XXX=TRUE; else NV9XXX=FALSE; } else g_error(_("something went wrong with glxinfo or grep")); } void detect_xgl() { //find out if we have XGL gint ex=0; if (!g_spawn_command_line_sync("sh -c 'xvinfo | grep -i Xgl > /dev/null'", NULL,NULL,&ex,NULL)) g_error(_("can't use this app, no xvinfo or no grep")); if (WIFEXITED(ex)) { if (WEXITSTATUS(ex)==0) XGL=TRUE; else XGL=FALSE; } else g_error(_("something went wrong with xvinfo or grep")); } int main(int argc, char ** argv) { gint fd; gint e; Display * d; //set the locale stuff setlocale(LC_ALL,""); bindtextdomain("beryl-manager","/usr/share/locale"); textdomain("beryl-manager"); for (fd=0;fd(numWM)) WM=-1; if (WM==-1) { //set WM to beryl for now gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(WMs[0].Item),TRUE); } if (fallBackWM<0 || fallBackWM > (numWM-1)) fallBackWM=-1; if (fallBackWM==-1) { //we have to find a fall back WM that exists gint i; for (i=1;i numDM) DM=-1; if (DM==-1) { //we have to find a DM that exists gint i; for (i=0;i