BUG's page
[awesome.git] / uicb.c
blob9704f49d9352077b08044c8c4424df1c8d2f6967
1 /*
2 * uicb.c - user interface callbacks management
4 * Copyright © 2007-2008 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>
31 #include <unistd.h>
32 #include <errno.h>
34 #include "awesome.h"
35 #include "tag.h"
36 #include "mouse.h"
37 #include "statusbar.h"
38 #include "widget.h"
39 #include "focus.h"
40 #include "client.h"
41 #include "screen.h"
42 #include "titlebar.h"
43 #include "layouts/tile.h"
45 extern AwesomeConf globalconf;
47 #include "uicbgen.h"
49 /** Restart awesome with the current command line.
50 * \param screen ID
51 * \param arg arg (unused)
52 * \ingroup ui_callback
54 void
55 uicb_restart(int screen, char *arg __attribute__ ((unused)))
57 uicb_exec(screen, globalconf.argv);
60 /** Execute another process, replacing the current instance of awesome.
61 * \param screen Screen ID
62 * \param cmd Command
63 * \ingroup ui_callback
65 void
66 uicb_exec(int screen __attribute__ ((unused)), char *cmd)
68 Client *c;
69 char *args, *path;
71 /* remap all clients since some WM won't handle them otherwise */
72 for(c = globalconf.clients; c; c = c->next)
73 client_unban(c);
75 XSync(globalconf.display, False);
76 XCloseDisplay(globalconf.display);
78 /* Ignore the leading spaces if any */
79 while(cmd[0] && cmd[0] == ' ')
80 cmd++;
82 /* Get the beginning of the arguments */
83 args = strchr(cmd, ' ');
85 if(args)
86 path = strndup(cmd, args - cmd);
87 else
88 path = strndup(cmd, a_strlen(cmd));
90 execlp(path, cmd, NULL);
92 p_delete(&path);
95 /** Spawn another process.
96 * \param screen Screen ID
97 * \param arg Command
98 * \ingroup ui_callback
100 void
101 uicb_spawn(int screen, char *arg)
103 static char *shell = NULL;
104 char *display = NULL;
105 char *tmp, newdisplay[128];
107 if(!arg)
108 return;
110 if(!shell && !(shell = getenv("SHELL")))
111 shell = a_strdup("/bin/sh");
113 if(!globalconf.screens_info->xinerama_is_active && (tmp = getenv("DISPLAY")))
115 display = a_strdup(tmp);
116 if((tmp = strrchr(display, '.')))
117 *tmp = '\0';
118 snprintf(newdisplay, sizeof(newdisplay), "%s.%d", display, screen);
119 setenv("DISPLAY", newdisplay, 1);
123 /* The double-fork construct avoids zombie processes and keeps the code
124 * clean from stupid signal handlers. */
125 if(fork() == 0)
127 if(fork() == 0)
129 if(globalconf.display)
130 close(ConnectionNumber(globalconf.display));
131 setsid();
132 execl(shell, shell, "-c", arg, NULL);
133 warn("execl '%s -c %s' failed: %s\n", shell, arg, strerror(errno));
135 exit(EXIT_SUCCESS);
137 wait(0);
140 /** Run the uicb
141 * \param cmd the uicb command to parse
142 * \return 0 on succes, -1 on failure
144 static int
145 __uicb_run(char *cmd)
147 char *p, *argcpy;
148 const char *arg;
149 int screen;
150 ssize_t len;
151 Uicb *uicb;
153 len = a_strlen(cmd);
154 p = strtok(cmd, " ");
155 if (!p)
157 warn("ignoring malformed command\n");
158 return -1;
160 screen = atoi(cmd);
161 if(screen >= globalconf.screens_info->nscreen || screen < 0)
163 warn("invalid screen specified: %i\n", screen);
164 return -1;
167 p = strtok(NULL, " ");
168 if (!p)
170 warn("ignoring malformed command.\n");
171 return -1;
174 uicb = name_func_lookup(p, UicbList);
175 if (!uicb)
177 warn("unknown uicb function: %s.\n", p);
178 return -1;
181 if (p + a_strlen(p) < cmd + len)
183 arg = p + a_strlen(p) + 1;
184 len = a_strlen(arg);
185 /* Allow our callees to modify this string. */
186 argcpy = p_new(char, len + 1);
187 a_strncpy(argcpy, len + 1, arg, len);
188 uicb(screen, argcpy);
189 p_delete(&argcpy);
191 else
192 uicb(screen, NULL);
194 return 0;
197 /** Parse the control buffer.
198 * \param cmd the control buffer
199 * \return 0 on succes, -1 on failure
202 parse_control(char *cmd)
204 char *p, *curcmd = cmd;
206 if(!a_strlen(cmd))
207 return -1;
209 while((p = strchr(curcmd, '\n')))
211 *p = '\0';
212 __uicb_run(curcmd);
213 curcmd = p + 1;
216 return 0;
219 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80