forked from awesomeWM/awesome
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuicb.c
More file actions
221 lines (187 loc) · 5.14 KB
/
Copy pathuicb.c
File metadata and controls
221 lines (187 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* uicb.c - user interface callbacks management
*
* Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
/**
* @defgroup ui_callback User Interface Callbacks
*/
#include <sys/wait.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "awesome.h"
#include "tag.h"
#include "mouse.h"
#include "statusbar.h"
#include "widget.h"
#include "focus.h"
#include "client.h"
#include "screen.h"
#include "titlebar.h"
#include "layouts/tile.h"
extern AwesomeConf globalconf;
#include "uicbgen.h"
/** Restart awesome with the current command line.
* \param screen ID
* \param arg arg (unused)
* \ingroup ui_callback
*/
void
uicb_restart(int screen, char *arg __attribute__ ((unused)))
{
uicb_exec(screen, globalconf.argv);
}
/** Execute another process, replacing the current instance of awesome.
* \param screen Screen ID
* \param cmd Command
* \ingroup ui_callback
*/
void
uicb_exec(int screen __attribute__ ((unused)), char *cmd)
{
Client *c;
char *args, *path;
/* remap all clients since some WM won't handle them otherwise */
for(c = globalconf.clients; c; c = c->next)
client_unban(c);
XSync(globalconf.display, False);
XCloseDisplay(globalconf.display);
/* Ignore the leading spaces if any */
while(cmd[0] && cmd[0] == ' ')
cmd++;
/* Get the beginning of the arguments */
args = strchr(cmd, ' ');
if(args)
path = a_strndup(cmd, args - cmd);
else
path = a_strndup(cmd, a_strlen(cmd));
execlp(path, cmd, NULL);
p_delete(&path);
}
/** Spawn another process.
* \param screen Screen ID
* \param arg Command
* \ingroup ui_callback
*/
void
uicb_spawn(int screen, char *arg)
{
static char *shell = NULL;
char *display = NULL;
char *tmp, newdisplay[128];
if(!arg)
return;
if(!shell && !(shell = getenv("SHELL")))
shell = a_strdup("/bin/sh");
if(!globalconf.screens_info->xinerama_is_active && (tmp = getenv("DISPLAY")))
{
display = a_strdup(tmp);
if ((tmp = strrchr(display, '.')) && !strchr(tmp, ':'))
*tmp = '\0';
snprintf(newdisplay, sizeof(newdisplay), "%s.%d", display, screen);
setenv("DISPLAY", newdisplay, 1);
p_delete(&display);
}
/* The double-fork construct avoids zombie processes and keeps the code
* clean from stupid signal handlers. */
if(fork() == 0)
{
if(fork() == 0)
{
if(globalconf.display)
close(ConnectionNumber(globalconf.display));
setsid();
execl(shell, shell, "-c", arg, NULL);
warn("execl '%s -c %s' failed: %s\n", shell, arg, strerror(errno));
}
exit(EXIT_SUCCESS);
}
wait(0);
}
static void
__uicb_run_args(Uicb* fun, int screen, char *cmd) {
char *argcpy = NULL;
const char *arg;
ssize_t len;
if (cmd && (a_strlen(cmd) > 0)) {
ssize_t len = a_strlen(cmd);
argcpy = p_new(char, len);
a_strncpy(argcpy, len + 1, cmd, len);
}
fun(screen, argcpy);
if (argcpy)
p_delete(&argcpy);
}
/** Run the uicb
* \param cmd the uicb command to parse
* \return 0 on succes, -1 on failure
*/
static int
__uicb_run(char *cmd)
{
char *p, *argcpy;
const char *arg;
char *tmp;
int screen;
ssize_t len;
Uicb *uicb;
len = a_strlen(cmd);
p = strsep(&cmd, " ");
screen = (int) strtol(p, &tmp, 10);
if ((*tmp != '\0') || (screen > globalconf.screens_info->nscreen) || (screen < -1)) {
warn("invalid screen: %s\n", p);
return -1;
}
p = strsep(&cmd, " ");
if ((!p) || (*p == '\0')) {
warn("ignoring malformed command\n");
return -1;
}
uicb = name_func_lookup(p, UicbList);
if (!uicb)
{
warn("unknown uicb function: %s.\n", p);
return -1;
}
if (screen == -1) {
for (int x = 0; x < globalconf.screens_info->nscreen; x++)
__uicb_run_args(uicb, x, cmd);
} else
__uicb_run_args(uicb, screen, cmd);
return 0;
}
/** Parse the control buffer.
* \param cmd the control buffer
* \return 0 on succes, -1 on failure
*/
int
parse_control(char *cmd)
{
char *p, *curcmd = cmd;
if(!a_strlen(cmd))
return -1;
while((p = strchr(curcmd, '\n')))
{
*p = '\0';
__uicb_run(curcmd);
curcmd = p + 1;
}
return 0;
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80