Add window indicating size and position on move and resize (FS#107)
[awesome.git] / uicb.c
blob65664d278b39782c997f3219b6ce4b024ea870bf
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 (unused)
51 * \param arg arg (unused)
52 * \ingroup ui_callback
54 void
55 uicb_restart(int screen __attribute__ ((unused)),
56 char *arg __attribute__ ((unused)))
58 uicb_exec(0, globalconf.argv);
61 /** Execute another process, replacing the current instance of Awesome
62 * \param screen Screen ID
63 * \param cmd Command
64 * \ingroup ui_callback
66 void
67 uicb_exec(int screen __attribute__ ((unused)), char *cmd)
69 Client *c;
70 char *args, *path;
72 /* remap all clients since some WM won't handle them otherwise */
73 for(c = globalconf.clients; c; c = c->next)
74 client_unban(c);
76 XSync(globalconf.display, False);
77 XCloseDisplay(globalconf.display);
79 /* Ignore the leading spaces if any */
80 while(cmd[0] && cmd[0] == ' ')
81 cmd++;
83 /* Get the beginning of the arguments */
84 args = strchr(cmd, ' ');
86 if(args)
87 path = strndup(cmd, args - cmd);
88 else
89 path = strndup(cmd, a_strlen(cmd));
91 execlp(path, cmd, NULL);
93 p_delete(&path);
96 /** Spawn another process
97 * \param screen Screen ID
98 * \param arg Command
99 * \ingroup ui_callback
101 void
102 uicb_spawn(int screen, char *arg)
104 static char *shell = NULL;
105 char *display = NULL;
106 char *tmp, newdisplay[128];
108 if(!arg)
109 return;
111 if(!shell && !(shell = getenv("SHELL")))
112 shell = a_strdup("/bin/sh");
114 if(!globalconf.screens_info->xinerama_is_active && (tmp = getenv("DISPLAY")))
116 display = a_strdup(tmp);
117 if((tmp = strrchr(display, '.')))
118 *tmp = '\0';
119 snprintf(newdisplay, sizeof(newdisplay), "%s.%d", display, screen);
120 setenv("DISPLAY", newdisplay, 1);
124 /* The double-fork construct avoids zombie processes and keeps the code
125 * clean from stupid signal handlers. */
126 if(fork() == 0)
128 if(fork() == 0)
130 if(globalconf.display)
131 close(ConnectionNumber(globalconf.display));
132 setsid();
133 execl(shell, shell, "-c", arg, NULL);
134 warn("execl '%s -c %s' failed: %s\n", shell, arg, strerror(errno));
136 exit(EXIT_SUCCESS);
138 wait(0);
141 static int
142 run_uicb(char *cmd)
144 char *p, *argcpy;
145 const char *arg;
146 int screen;
147 ssize_t len;
148 Uicb *uicb;
150 len = a_strlen(cmd);
151 p = strtok(cmd, " ");
152 if (!p)
154 warn("ignoring malformed command\n");
155 return -1;
157 screen = atoi(cmd);
158 if(screen >= globalconf.screens_info->nscreen || screen < 0)
160 warn("invalid screen specified: %i\n", screen);
161 return -1;
164 p = strtok(NULL, " ");
165 if (!p)
167 warn("ignoring malformed command.\n");
168 return -1;
171 uicb = name_func_lookup(p, UicbList);
172 if (!uicb)
174 warn("unknown uicb function: %s.\n", p);
175 return -1;
178 if (p + a_strlen(p) < cmd + len)
180 arg = p + a_strlen(p) + 1;
181 len = a_strlen(arg);
182 /* Allow our callees to modify this string. */
183 argcpy = p_new(char, len + 1);
184 a_strncpy(argcpy, len + 1, arg, len);
185 uicb(screen, argcpy);
186 p_delete(&argcpy);
188 else
189 uicb(screen, NULL);
191 return 0;
195 parse_control(char *cmd)
197 char *p, *curcmd = cmd;
199 if(!a_strlen(cmd))
200 return -1;
202 while((p = strchr(curcmd, '\n')))
204 *p = '\0';
205 run_uicb(curcmd);
206 curcmd = p + 1;
209 return 0;
212 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80