-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMapping.cpp
More file actions
191 lines (150 loc) · 4.35 KB
/
Mapping.cpp
File metadata and controls
191 lines (150 loc) · 4.35 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
//
// Telescope - graphical task switcher
//
// (c) Ilya Skriblovsky, 2010
// <Ilya.Skriblovsky@gmail.com>
//
// $Id: Mapping.cpp 158 2011-02-22 20:05:07Z mitrandir $
#include "Mapping.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "TeleWindow.h"
Mapping::Mapping(Event event, KeyCode keyCode, Type type, const char *action)
: _event(event), _keyCode(keyCode), _type(type)
{
_action = strdup(action);
}
Mapping::~Mapping()
{
free(_action);
}
// Line syntax is:
// event(keysym) : actiontype(action)
//
// Parts are:
// event: press | globalpress
// keysym: X11 keysym
// actiontype: shell | internal
// action: shell command | internal command
//
// Spaces are permitted only around colon (and inside
// shell commands, of course)
Mapping* Mapping::parseMappingLine(Display *dpy, const char *line, const char **error)
{
*error = 0;
const char *eventstart = &line[strspn(line, " \t")];
if (*eventstart == '\n') return 0;
if (*eventstart == '\0') return 0;
if (*eventstart == '#') return 0;
// event
const char *eventend = eventstart + strcspn(eventstart, "(");
char *event = strndup(eventstart, eventend - eventstart);
// keysym
const char *keysymstart = eventend + 1;
const char *keysymend = keysymstart + strcspn(keysymstart, ")");
char *keysym = strndup(keysymstart, keysymend - keysymstart);
// colon
const char *colonstart = keysymend + 1;
const char *colonend = colonstart + strspn(colonstart, " :");
// actiontype
const char *actiontypestart = colonend;
const char *actiontypeend = actiontypestart + strcspn(actiontypestart, "(");
char *actiontype = strndup(actiontypestart, actiontypeend - actiontypestart);
// action
const char *actionstart = actiontypeend + 1;
const char *actionend = actionstart + strcspn(actionstart, ")");
char *action = strndup(actionstart, actionend - actionstart);
// printf("event:\t%s\n", event);
// printf("keysym:\t%s\n", keysym);
// printf("actiontype:\t%s\n", actiontype);
// printf("action:\t%s\n", action);
// printf("\n");
KeySym keySym = XStringToKeysym(keysym);
KeyCode keyCode = XKeysymToKeycode(dpy, keySym);
Mapping *result = 0;
Event ev;
if (strcmp(event, "press") == 0)
ev = Press;
else if (strcmp(event, "globalpress") == 0)
ev = GlobalPress;
else
{
*error = "Invalid event";
goto finish;
}
if (keyCode == 0)
{
*error = "Invalid keysym";
goto finish;
}
Type type;
if (strcmp(actiontype, "shell") == 0)
type = Shell;
else if (strcmp(actiontype, "internal") == 0)
type = Internal;
else
{
*error = "Invalid action type";
goto finish;
}
result = new Mapping(ev, keyCode, type, action);
if (ev == GlobalPress)
{
Window rootWindow = RootWindow(dpy, DefaultScreen(dpy));
XGrabKey(dpy, keyCode, 0, rootWindow, False, GrabModeAsync, GrabModeAsync);
}
finish:
free(event);
free(keysym);
free(actiontype);
free(action);
return result;
}
void Mapping::handleEvent(TeleWindow *teleWindow, Mapping::Event event, KeyCode keyCode)
{
if (_event == event && _keyCode == keyCode)
execute(teleWindow);
}
void Mapping::execute(TeleWindow *teleWindow)
{
switch (_type)
{
case Shell:
{
static const int MAX_ARGS = 10;
char *args[MAX_ARGS + 2];
memset(args, 0, sizeof(args));
int cmdlen = strcspn(_action, " ");
args[0] = strndup(_action, cmdlen);
char *arg = _action + cmdlen;
arg += strspn(arg, " ");
for (int i = 1; i <= MAX_ARGS; ++i)
{
if (*arg == 0)
break;
int arglen = strcspn(arg, " ");
args[i] = strndup(arg, arglen);
arg += arglen;
arg += strspn(arg, " ");
}
if (vfork() == 0)
{
execvp(args[0], args);
}
else
{
for (int i = 0; i <= MAX_ARGS; ++i)
free(args[i]);
teleWindow->hide();
}
break;
}
case Internal:
{
teleWindow->internalCommand(_action);
break;
}
}
}