first version of awesome-menu
[awesome.git] / uicb.c
blob33e29f68bf1ece3405a6bd5fb5e97db33a69dde9
1 /*
2 * uicb.c - user interface callbacks management
4 * Copyright © 2007 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 /**
23 * @defgroup ui_callback User Interface Callbacks
26 #include <X11/extensions/Xinerama.h>
27 #include <sys/wait.h>
29 #include "awesome.h"
30 #include "tag.h"
31 #include "mouse.h"
32 #include "statusbar.h"
33 #include "widget.h"
34 #include "focus.h"
35 #include "client.h"
36 #include "screen.h"
37 #include "layouts/tile.h"
39 extern AwesomeConf globalconf;
41 #include "uicbgen.h"
43 /** Execute another process, replacing the current instance of Awesome
44 * \param screen Screen ID
45 * \param arg Command
46 * \ingroup ui_callback
48 void
49 uicb_exec(int screen __attribute__ ((unused)), char *arg)
51 Client *c;
52 char path[PATH_MAX];
54 /* remap all clients since some WM won't handle them otherwise */
55 for(c = globalconf.clients; c; c = c->next)
56 client_unban(c);
58 XSync(globalconf.display, False);
60 if(globalconf.display)
61 close(ConnectionNumber(globalconf.display));
63 sscanf(arg, "%s", path);
64 execlp(path, arg, NULL);
67 /** Spawn another process
68 * \param screen Screen ID
69 * \param arg Command
70 * \ingroup ui_callback
72 void
73 uicb_spawn(int screen, char *arg)
75 static char *shell = NULL;
76 char *display = NULL;
77 char *tmp, newdisplay[128];
79 if(!arg)
80 return;
82 if(!shell && !(shell = getenv("SHELL")))
83 shell = a_strdup("/bin/sh");
85 if(!XineramaIsActive(globalconf.display) && (tmp = getenv("DISPLAY")))
87 display = a_strdup(tmp);
88 if((tmp = strrchr(display, '.')))
89 *tmp = '\0';
90 snprintf(newdisplay, sizeof(newdisplay), "%s.%d", display, screen);
91 setenv("DISPLAY", newdisplay, 1);
95 /* The double-fork construct avoids zombie processes and keeps the code
96 * clean from stupid signal handlers. */
97 if(fork() == 0)
99 if(fork() == 0)
101 if(globalconf.display)
102 close(ConnectionNumber(globalconf.display));
103 setsid();
104 execl(shell, shell, "-c", arg, NULL);
105 warn("execl '%s -c %s'", shell, arg);
106 perror(" failed");
108 exit(EXIT_SUCCESS);
110 wait(0);
113 static int
114 run_uicb(char *cmd)
116 char *p, *argcpy;
117 const char *arg;
118 int screen;
119 ssize_t len;
120 Uicb *uicb;
122 len = a_strlen(cmd);
123 p = strtok(cmd, " ");
124 if (!p)
126 warn("Ignoring malformed command\n");
127 return -1;
129 screen = atoi(cmd);
130 if(screen >= globalconf.nscreen || screen < 0)
132 warn("Invalid screen specified: %i\n", screen);
133 return -1;
136 p = strtok(NULL, " ");
137 if (!p)
139 warn("Ignoring malformed command.\n");
140 return -1;
143 uicb = name_func_lookup(p, UicbList);
144 if (!uicb)
146 warn("Unknown UICB function: %s.\n", p);
147 return -1;
150 if (p + a_strlen(p) < cmd + len)
152 arg = p + a_strlen(p) + 1;
153 len = a_strlen(arg);
154 /* Allow our callees to modify this string. */
155 argcpy = p_new(char, len + 1);
156 a_strncpy(argcpy, len + 1, arg, len);
157 uicb(screen, argcpy);
158 p_delete(&argcpy);
160 else
161 uicb(screen, NULL);
163 return 0;
167 parse_control(char *cmd)
169 char *p, *curcmd = cmd;
171 if(!a_strlen(cmd))
172 return -1;
174 while((p = strchr(curcmd, '\n')))
176 *p = '\0';
177 run_uicb(curcmd);
178 curcmd = p + 1;
181 return 0;
184 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80