Rewrite uicb_exec
[awesome.git] / uicb.c
blobb08a0cc044bd18cdce0a262ed922059a97906dc2
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 /* strndup() */
27 #define _GNU_SOURCE
29 #include <sys/wait.h>
30 #include <string.h>
32 #include "awesome.h"
33 #include "tag.h"
34 #include "mouse.h"
35 #include "statusbar.h"
36 #include "widget.h"
37 #include "focus.h"
38 #include "client.h"
39 #include "screen.h"
40 #include "layouts/tile.h"
42 extern AwesomeConf globalconf;
44 #include "uicbgen.h"
46 /** Execute another process, replacing the current instance of Awesome
47 * \param screen Screen ID
48 * \param arg Command
49 * \ingroup ui_callback
51 void
52 uicb_exec(int screen __attribute__ ((unused)), char *cmd)
54 Client *c;
55 char *args, *path;
57 /* remap all clients since some WM won't handle them otherwise */
58 for(c = globalconf.clients; c; c = c->next)
59 client_unban(c);
61 XSync(globalconf.display, False);
63 /* Ignore the leading spaces if any */
64 while(cmd[0] && cmd[0] == ' ')
65 cmd++;
67 /* Get the beginning of the arguments */
68 args = strchr(cmd, ' ');
70 if(args)
71 path = strndup(cmd, args - cmd);
72 else
73 path = strndup(cmd, a_strlen(cmd));
75 execlp(path, cmd, NULL);
77 p_delete(&path);
80 /** Spawn another process
81 * \param screen Screen ID
82 * \param arg Command
83 * \ingroup ui_callback
85 void
86 uicb_spawn(int screen, char *arg)
88 static char *shell = NULL;
89 char *display = NULL;
90 char *tmp, newdisplay[128];
92 if(!arg)
93 return;
95 if(!shell && !(shell = getenv("SHELL")))
96 shell = a_strdup("/bin/sh");
98 if(!globalconf.screens_info->xinerama_is_active && (tmp = getenv("DISPLAY")))
100 display = a_strdup(tmp);
101 if((tmp = strrchr(display, '.')))
102 *tmp = '\0';
103 snprintf(newdisplay, sizeof(newdisplay), "%s.%d", display, screen);
104 setenv("DISPLAY", newdisplay, 1);
108 /* The double-fork construct avoids zombie processes and keeps the code
109 * clean from stupid signal handlers. */
110 if(fork() == 0)
112 if(fork() == 0)
114 if(globalconf.display)
115 close(ConnectionNumber(globalconf.display));
116 setsid();
117 execl(shell, shell, "-c", arg, NULL);
118 warn("execl '%s -c %s'", shell, arg);
119 perror(" failed");
121 exit(EXIT_SUCCESS);
123 wait(0);
126 static int
127 run_uicb(char *cmd)
129 char *p, *argcpy;
130 const char *arg;
131 int screen;
132 ssize_t len;
133 Uicb *uicb;
135 len = a_strlen(cmd);
136 p = strtok(cmd, " ");
137 if (!p)
139 warn("ignoring malformed command\n");
140 return -1;
142 screen = atoi(cmd);
143 if(screen >= globalconf.screens_info->nscreen || screen < 0)
145 warn("invalid screen specified: %i\n", screen);
146 return -1;
149 p = strtok(NULL, " ");
150 if (!p)
152 warn("ignoring malformed command.\n");
153 return -1;
156 uicb = name_func_lookup(p, UicbList);
157 if (!uicb)
159 warn("unknown uicb function: %s.\n", p);
160 return -1;
163 if (p + a_strlen(p) < cmd + len)
165 arg = p + a_strlen(p) + 1;
166 len = a_strlen(arg);
167 /* Allow our callees to modify this string. */
168 argcpy = p_new(char, len + 1);
169 a_strncpy(argcpy, len + 1, arg, len);
170 uicb(screen, argcpy);
171 p_delete(&argcpy);
173 else
174 uicb(screen, NULL);
176 return 0;
180 parse_control(char *cmd)
182 char *p, *curcmd = cmd;
184 if(!a_strlen(cmd))
185 return -1;
187 while((p = strchr(curcmd, '\n')))
189 *p = '\0';
190 run_uicb(curcmd);
191 curcmd = p + 1;
194 return 0;
197 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80