Blame


1 f5e234d6 2018-05-18 omar.polo #include <stdio.h>
2 f5e234d6 2018-05-18 omar.polo #include <stdlib.h>
3 f5e234d6 2018-05-18 omar.polo #include <string.h> // strdup, strnlen, ...
4 f5e234d6 2018-05-18 omar.polo #include <ctype.h> // isalnum
5 f5e234d6 2018-05-18 omar.polo #include <locale.h> // setlocale
6 f5e234d6 2018-05-18 omar.polo #include <unistd.h>
7 f5e234d6 2018-05-18 omar.polo #include <sysexits.h>
8 f5e234d6 2018-05-18 omar.polo #include <stdbool.h>
9 f5e234d6 2018-05-18 omar.polo #include <limits.h>
10 f5e234d6 2018-05-18 omar.polo #include <errno.h>
11 f5e234d6 2018-05-18 omar.polo
12 f5e234d6 2018-05-18 omar.polo #include <X11/Xlib.h>
13 f5e234d6 2018-05-18 omar.polo #include <X11/Xutil.h> // XLookupString
14 f5e234d6 2018-05-18 omar.polo #include <X11/Xresource.h>
15 f5e234d6 2018-05-18 omar.polo #include <X11/Xcms.h> // colors
16 347d23da 2018-05-19 omar.polo #include <X11/keysym.h>
17 f5e234d6 2018-05-18 omar.polo
18 f5e234d6 2018-05-18 omar.polo #ifdef USE_XINERAMA
19 f5e234d6 2018-05-18 omar.polo # include <X11/extensions/Xinerama.h>
20 f5e234d6 2018-05-18 omar.polo #endif
21 f5e234d6 2018-05-18 omar.polo
22 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
23 c9a3bfaa 2018-05-21 omar.polo # include <X11/Xft/Xft.h>
24 c9a3bfaa 2018-05-21 omar.polo #endif
25 c9a3bfaa 2018-05-21 omar.polo
26 f5e234d6 2018-05-18 omar.polo #define nil NULL
27 347d23da 2018-05-19 omar.polo #define resname "MyMenu"
28 347d23da 2018-05-19 omar.polo #define resclass "mymenu"
29 f5e234d6 2018-05-18 omar.polo
30 9e94fcbe 2018-05-22 omar.polo #ifdef USE_XFT
31 9e94fcbe 2018-05-22 omar.polo # define default_fontname "monospace"
32 9e94fcbe 2018-05-22 omar.polo #else
33 9e94fcbe 2018-05-22 omar.polo # define default_fontname "fixed"
34 9e94fcbe 2018-05-22 omar.polo #endif
35 9e94fcbe 2018-05-22 omar.polo
36 9e94fcbe 2018-05-22 omar.polo #define MIN(a, b) ((a) < (b) ? (a) : (b))
37 f5e234d6 2018-05-18 omar.polo #define MAX(a, b) ((a) > (b) ? (a) : (b))
38 9e94fcbe 2018-05-22 omar.polo
39 9e94fcbe 2018-05-22 omar.polo // If we don't have it or we don't want an "ignore case" completion
40 9e94fcbe 2018-05-22 omar.polo // style, fall back to `strstr(3)`
41 9e94fcbe 2018-05-22 omar.polo #ifndef USE_STRCASESTR
42 9e94fcbe 2018-05-22 omar.polo #define strcasestr strstr
43 9e94fcbe 2018-05-22 omar.polo #endif
44 f5e234d6 2018-05-18 omar.polo
45 f5e234d6 2018-05-18 omar.polo #define update_completions(cs, text, lines) { \
46 f5e234d6 2018-05-18 omar.polo compl_delete(cs); \
47 f5e234d6 2018-05-18 omar.polo cs = filter(text, lines); \
48 f5e234d6 2018-05-18 omar.polo }
49 f5e234d6 2018-05-18 omar.polo
50 95b27a5e 2018-05-19 omar.polo #define INITIAL_ITEMS 64
51 f5e234d6 2018-05-18 omar.polo
52 f5e234d6 2018-05-18 omar.polo #define cannot_allocate_memory { \
53 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Could not allocate memory\n"); \
54 9e94fcbe 2018-05-22 omar.polo abort(); \
55 f5e234d6 2018-05-18 omar.polo }
56 f5e234d6 2018-05-18 omar.polo
57 f5e234d6 2018-05-18 omar.polo #define check_allocation(a) { \
58 f5e234d6 2018-05-18 omar.polo if (a == nil) \
59 f5e234d6 2018-05-18 omar.polo cannot_allocate_memory; \
60 f5e234d6 2018-05-18 omar.polo }
61 f5e234d6 2018-05-18 omar.polo
62 f5e234d6 2018-05-18 omar.polo enum state {LOOPING, OK, ERR};
63 f5e234d6 2018-05-18 omar.polo
64 c9a3bfaa 2018-05-21 omar.polo enum text_type {PROMPT, COMPL, COMPL_HIGH};
65 c9a3bfaa 2018-05-21 omar.polo
66 f5e234d6 2018-05-18 omar.polo struct rendering {
67 f5e234d6 2018-05-18 omar.polo Display *d;
68 f5e234d6 2018-05-18 omar.polo Window w;
69 f5e234d6 2018-05-18 omar.polo GC prompt;
70 f5e234d6 2018-05-18 omar.polo GC prompt_bg;
71 f5e234d6 2018-05-18 omar.polo GC completion;
72 f5e234d6 2018-05-18 omar.polo GC completion_bg;
73 f5e234d6 2018-05-18 omar.polo GC completion_highlighted;
74 f5e234d6 2018-05-18 omar.polo GC completion_highlighted_bg;
75 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
76 c9a3bfaa 2018-05-21 omar.polo XftDraw *xftdraw;
77 c9a3bfaa 2018-05-21 omar.polo XftColor xft_prompt;
78 c9a3bfaa 2018-05-21 omar.polo XftColor xft_completion;
79 c9a3bfaa 2018-05-21 omar.polo XftColor xft_completion_highlighted;
80 c9a3bfaa 2018-05-21 omar.polo XftFont *font;
81 c9a3bfaa 2018-05-21 omar.polo #else
82 c9a3bfaa 2018-05-21 omar.polo XFontSet *font;
83 c9a3bfaa 2018-05-21 omar.polo #endif
84 f5e234d6 2018-05-18 omar.polo int width;
85 f5e234d6 2018-05-18 omar.polo int height;
86 36a15a9f 2018-05-19 omar.polo bool horizontal_layout;
87 8758854a 2018-05-20 omar.polo char *ps1;
88 8758854a 2018-05-20 omar.polo int ps1len;
89 f5e234d6 2018-05-18 omar.polo };
90 f5e234d6 2018-05-18 omar.polo
91 f5e234d6 2018-05-18 omar.polo struct completions {
92 f5e234d6 2018-05-18 omar.polo char *completion;
93 f5e234d6 2018-05-18 omar.polo bool selected;
94 f5e234d6 2018-05-18 omar.polo struct completions *next;
95 f5e234d6 2018-05-18 omar.polo };
96 f5e234d6 2018-05-18 omar.polo
97 f5e234d6 2018-05-18 omar.polo struct completions *compl_new() {
98 f5e234d6 2018-05-18 omar.polo struct completions *c = malloc(sizeof(struct completions));
99 f5e234d6 2018-05-18 omar.polo
100 f5e234d6 2018-05-18 omar.polo if (c == nil)
101 f5e234d6 2018-05-18 omar.polo return c;
102 f5e234d6 2018-05-18 omar.polo
103 f5e234d6 2018-05-18 omar.polo c->completion = nil;
104 f5e234d6 2018-05-18 omar.polo c->selected = false;
105 f5e234d6 2018-05-18 omar.polo c->next = nil;
106 f5e234d6 2018-05-18 omar.polo return c;
107 f5e234d6 2018-05-18 omar.polo }
108 f5e234d6 2018-05-18 omar.polo
109 f5e234d6 2018-05-18 omar.polo void compl_delete(struct completions *c) {
110 f5e234d6 2018-05-18 omar.polo free(c);
111 f5e234d6 2018-05-18 omar.polo }
112 f5e234d6 2018-05-18 omar.polo
113 347d23da 2018-05-19 omar.polo struct completions *compl_select_next(struct completions *c, bool n) {
114 347d23da 2018-05-19 omar.polo if (c == nil)
115 347d23da 2018-05-19 omar.polo return nil;
116 347d23da 2018-05-19 omar.polo if (n) {
117 347d23da 2018-05-19 omar.polo c->selected = true;
118 347d23da 2018-05-19 omar.polo return c;
119 347d23da 2018-05-19 omar.polo }
120 347d23da 2018-05-19 omar.polo
121 347d23da 2018-05-19 omar.polo struct completions *orig = c;
122 347d23da 2018-05-19 omar.polo while (c != nil) {
123 347d23da 2018-05-19 omar.polo if (c->selected) {
124 347d23da 2018-05-19 omar.polo c->selected = false;
125 347d23da 2018-05-19 omar.polo if (c->next != nil) {
126 347d23da 2018-05-19 omar.polo // the current one is selected and the next one exists
127 347d23da 2018-05-19 omar.polo c->next->selected = true;
128 347d23da 2018-05-19 omar.polo return c->next;
129 347d23da 2018-05-19 omar.polo } else {
130 347d23da 2018-05-19 omar.polo // the current one is selected and the next one is nill,
131 347d23da 2018-05-19 omar.polo // select the first one
132 347d23da 2018-05-19 omar.polo orig->selected = true;
133 347d23da 2018-05-19 omar.polo return orig;
134 347d23da 2018-05-19 omar.polo }
135 347d23da 2018-05-19 omar.polo }
136 347d23da 2018-05-19 omar.polo c = c->next;
137 347d23da 2018-05-19 omar.polo }
138 347d23da 2018-05-19 omar.polo return nil;
139 347d23da 2018-05-19 omar.polo }
140 347d23da 2018-05-19 omar.polo
141 347d23da 2018-05-19 omar.polo struct completions *compl_select_prev(struct completions *c, bool n) {
142 347d23da 2018-05-19 omar.polo if (c == nil)
143 347d23da 2018-05-19 omar.polo return nil;
144 347d23da 2018-05-19 omar.polo
145 347d23da 2018-05-19 omar.polo struct completions *cc = c;
146 347d23da 2018-05-19 omar.polo
147 347d23da 2018-05-19 omar.polo if (n) // select the last one
148 347d23da 2018-05-19 omar.polo while (cc != nil) {
149 347d23da 2018-05-19 omar.polo if (cc->next == nil) {
150 347d23da 2018-05-19 omar.polo cc->selected = true;
151 347d23da 2018-05-19 omar.polo return cc;
152 347d23da 2018-05-19 omar.polo }
153 347d23da 2018-05-19 omar.polo cc = cc->next;
154 347d23da 2018-05-19 omar.polo }
155 347d23da 2018-05-19 omar.polo else // select the previous one
156 347d23da 2018-05-19 omar.polo while (cc != nil) {
157 347d23da 2018-05-19 omar.polo if (cc->next != nil && cc->next->selected) {
158 347d23da 2018-05-19 omar.polo cc->next->selected = false;
159 347d23da 2018-05-19 omar.polo cc->selected = true;
160 347d23da 2018-05-19 omar.polo return cc;
161 347d23da 2018-05-19 omar.polo }
162 347d23da 2018-05-19 omar.polo cc = cc->next;
163 347d23da 2018-05-19 omar.polo }
164 7ca8829b 2018-05-21 omar.polo
165 347d23da 2018-05-19 omar.polo return nil;
166 347d23da 2018-05-19 omar.polo }
167 347d23da 2018-05-19 omar.polo
168 f5e234d6 2018-05-18 omar.polo struct completions *filter(char *text, char **lines) {
169 f5e234d6 2018-05-18 omar.polo int i = 0;
170 f5e234d6 2018-05-18 omar.polo struct completions *root = compl_new();
171 f5e234d6 2018-05-18 omar.polo struct completions *c = root;
172 f5e234d6 2018-05-18 omar.polo
173 f5e234d6 2018-05-18 omar.polo for (;;) {
174 f5e234d6 2018-05-18 omar.polo char *l = lines[i];
175 f5e234d6 2018-05-18 omar.polo if (l == nil)
176 f5e234d6 2018-05-18 omar.polo break;
177 f5e234d6 2018-05-18 omar.polo
178 9e94fcbe 2018-05-22 omar.polo if (strcasestr(l, text) != nil) {
179 f5e234d6 2018-05-18 omar.polo c->next = compl_new();
180 f5e234d6 2018-05-18 omar.polo c = c->next;
181 f5e234d6 2018-05-18 omar.polo c->completion = l;
182 f5e234d6 2018-05-18 omar.polo }
183 f5e234d6 2018-05-18 omar.polo
184 f5e234d6 2018-05-18 omar.polo ++i;
185 f5e234d6 2018-05-18 omar.polo }
186 f5e234d6 2018-05-18 omar.polo
187 f5e234d6 2018-05-18 omar.polo struct completions *r = root->next;
188 f5e234d6 2018-05-18 omar.polo compl_delete(root);
189 f5e234d6 2018-05-18 omar.polo return r;
190 f5e234d6 2018-05-18 omar.polo }
191 f5e234d6 2018-05-18 omar.polo
192 f5e234d6 2018-05-18 omar.polo // push the character c at the end of the string pointed by p
193 f5e234d6 2018-05-18 omar.polo int pushc(char **p, int maxlen, char c) {
194 f5e234d6 2018-05-18 omar.polo int len = strnlen(*p, maxlen);
195 f5e234d6 2018-05-18 omar.polo
196 f5e234d6 2018-05-18 omar.polo if (!(len < maxlen -2)) {
197 f5e234d6 2018-05-18 omar.polo maxlen += maxlen >> 1;
198 f5e234d6 2018-05-18 omar.polo char *newptr = realloc(*p, maxlen);
199 f5e234d6 2018-05-18 omar.polo if (newptr == nil) { // bad!
200 f5e234d6 2018-05-18 omar.polo return -1;
201 f5e234d6 2018-05-18 omar.polo }
202 f5e234d6 2018-05-18 omar.polo *p = newptr;
203 f5e234d6 2018-05-18 omar.polo }
204 f5e234d6 2018-05-18 omar.polo
205 f5e234d6 2018-05-18 omar.polo (*p)[len] = c;
206 f5e234d6 2018-05-18 omar.polo (*p)[len+1] = '\0';
207 f5e234d6 2018-05-18 omar.polo return maxlen;
208 f5e234d6 2018-05-18 omar.polo }
209 f5e234d6 2018-05-18 omar.polo
210 347d23da 2018-05-19 omar.polo int utf8strnlen(char *s, int maxlen) {
211 347d23da 2018-05-19 omar.polo int len = 0;
212 347d23da 2018-05-19 omar.polo while (*s && maxlen > 0) {
213 347d23da 2018-05-19 omar.polo len += (*s++ & 0xc0) != 0x80;
214 347d23da 2018-05-19 omar.polo maxlen--;
215 347d23da 2018-05-19 omar.polo }
216 347d23da 2018-05-19 omar.polo return len;
217 347d23da 2018-05-19 omar.polo }
218 347d23da 2018-05-19 omar.polo
219 347d23da 2018-05-19 omar.polo // remove the last *glyph* from the *utf8* string!
220 347d23da 2018-05-19 omar.polo // this is different from just setting the last byte to 0 (in some
221 347d23da 2018-05-19 omar.polo // cases ofc). The actual implementation is quite inefficient because
222 347d23da 2018-05-19 omar.polo // it remove the last char until the number of glyphs doesn't change
223 f5e234d6 2018-05-18 omar.polo void popc(char *p, int maxlen) {
224 f5e234d6 2018-05-18 omar.polo int len = strnlen(p, maxlen);
225 347d23da 2018-05-19 omar.polo
226 347d23da 2018-05-19 omar.polo if (len == 0)
227 347d23da 2018-05-19 omar.polo return;
228 347d23da 2018-05-19 omar.polo
229 347d23da 2018-05-19 omar.polo int ulen = utf8strnlen(p, maxlen);
230 347d23da 2018-05-19 omar.polo while (len > 0 && utf8strnlen(p, maxlen) == ulen) {
231 347d23da 2018-05-19 omar.polo len--;
232 347d23da 2018-05-19 omar.polo p[len] = 0;
233 347d23da 2018-05-19 omar.polo }
234 f5e234d6 2018-05-18 omar.polo }
235 f5e234d6 2018-05-18 omar.polo
236 8758854a 2018-05-20 omar.polo // If the string is surrounded by quotes (`"`) remove them and replace
237 8758854a 2018-05-20 omar.polo // every `\"` in the string with `"`
238 8758854a 2018-05-20 omar.polo char *normalize_str(const char *str) {
239 8758854a 2018-05-20 omar.polo int len = strlen(str);
240 8758854a 2018-05-20 omar.polo if (len == 0)
241 8758854a 2018-05-20 omar.polo return nil;
242 8758854a 2018-05-20 omar.polo
243 8758854a 2018-05-20 omar.polo char *s = calloc(len, sizeof(char));
244 8758854a 2018-05-20 omar.polo check_allocation(s);
245 8758854a 2018-05-20 omar.polo int p = 0;
246 8758854a 2018-05-20 omar.polo while (*str) {
247 8758854a 2018-05-20 omar.polo char c = *str;
248 8758854a 2018-05-20 omar.polo if (*str == '\\') {
249 8758854a 2018-05-20 omar.polo if (*(str + 1)) {
250 8758854a 2018-05-20 omar.polo s[p] = *(str + 1);
251 8758854a 2018-05-20 omar.polo p++;
252 8758854a 2018-05-20 omar.polo str += 2; // skip this and the next char
253 8758854a 2018-05-20 omar.polo continue;
254 8758854a 2018-05-20 omar.polo } else {
255 8758854a 2018-05-20 omar.polo break;
256 8758854a 2018-05-20 omar.polo }
257 8758854a 2018-05-20 omar.polo }
258 8758854a 2018-05-20 omar.polo if (c == '"') {
259 8758854a 2018-05-20 omar.polo str++; // skip only this char
260 8758854a 2018-05-20 omar.polo continue;
261 8758854a 2018-05-20 omar.polo }
262 8758854a 2018-05-20 omar.polo s[p] = c;
263 8758854a 2018-05-20 omar.polo p++;
264 8758854a 2018-05-20 omar.polo str++;
265 8758854a 2018-05-20 omar.polo }
266 8758854a 2018-05-20 omar.polo return s;
267 8758854a 2018-05-20 omar.polo }
268 8758854a 2018-05-20 omar.polo
269 f5e234d6 2018-05-18 omar.polo // read an arbitrary long line from stdin and return a pointer to it
270 f5e234d6 2018-05-18 omar.polo // TODO: resize the allocated memory to exactly fit the string once
271 f5e234d6 2018-05-18 omar.polo // read?
272 f5e234d6 2018-05-18 omar.polo char *readline(bool *eof) {
273 f5e234d6 2018-05-18 omar.polo int maxlen = 8;
274 f5e234d6 2018-05-18 omar.polo char *str = calloc(maxlen, sizeof(char));
275 f5e234d6 2018-05-18 omar.polo if (str == nil) {
276 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Cannot allocate memory!\n");
277 f5e234d6 2018-05-18 omar.polo exit(EX_UNAVAILABLE);
278 f5e234d6 2018-05-18 omar.polo }
279 f5e234d6 2018-05-18 omar.polo
280 f5e234d6 2018-05-18 omar.polo int c;
281 f5e234d6 2018-05-18 omar.polo while((c = getchar()) != EOF) {
282 f5e234d6 2018-05-18 omar.polo if (c == '\n')
283 f5e234d6 2018-05-18 omar.polo return str;
284 f5e234d6 2018-05-18 omar.polo else
285 f5e234d6 2018-05-18 omar.polo maxlen = pushc(&str, maxlen, c);
286 f5e234d6 2018-05-18 omar.polo
287 f5e234d6 2018-05-18 omar.polo if (maxlen == -1) {
288 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Cannot allocate memory!\n");
289 f5e234d6 2018-05-18 omar.polo exit(EX_UNAVAILABLE);
290 f5e234d6 2018-05-18 omar.polo }
291 f5e234d6 2018-05-18 omar.polo }
292 f5e234d6 2018-05-18 omar.polo *eof = true;
293 f5e234d6 2018-05-18 omar.polo return str;
294 f5e234d6 2018-05-18 omar.polo }
295 f5e234d6 2018-05-18 omar.polo
296 95b27a5e 2018-05-19 omar.polo int readlines (char ***lns, int items) {
297 f5e234d6 2018-05-18 omar.polo bool finished = false;
298 f5e234d6 2018-05-18 omar.polo int n = 0;
299 95b27a5e 2018-05-19 omar.polo char **lines = *lns;
300 95b27a5e 2018-05-19 omar.polo while (true) {
301 f5e234d6 2018-05-18 omar.polo lines[n] = readline(&finished);
302 f5e234d6 2018-05-18 omar.polo
303 6da98882 2018-05-20 omar.polo if (strlen(lines[n]) == 0 || lines[n][0] == '\n') {
304 6da98882 2018-05-20 omar.polo free(lines[n]);
305 f5e234d6 2018-05-18 omar.polo --n; // forget about this line
306 6da98882 2018-05-20 omar.polo }
307 f5e234d6 2018-05-18 omar.polo
308 f5e234d6 2018-05-18 omar.polo if (finished)
309 f5e234d6 2018-05-18 omar.polo break;
310 f5e234d6 2018-05-18 omar.polo
311 f5e234d6 2018-05-18 omar.polo ++n;
312 95b27a5e 2018-05-19 omar.polo
313 95b27a5e 2018-05-19 omar.polo if (n == items - 1) {
314 95b27a5e 2018-05-19 omar.polo items += items >>1;
315 95b27a5e 2018-05-19 omar.polo char **l = realloc(lines, sizeof(char*) * items);
316 95b27a5e 2018-05-19 omar.polo check_allocation(l);
317 95b27a5e 2018-05-19 omar.polo *lns = l;
318 95b27a5e 2018-05-19 omar.polo lines = l;
319 95b27a5e 2018-05-19 omar.polo }
320 f5e234d6 2018-05-18 omar.polo }
321 95b27a5e 2018-05-19 omar.polo
322 f5e234d6 2018-05-18 omar.polo n++;
323 f5e234d6 2018-05-18 omar.polo lines[n] = nil;
324 95b27a5e 2018-05-19 omar.polo return items;
325 c9a3bfaa 2018-05-21 omar.polo }
326 c9a3bfaa 2018-05-21 omar.polo
327 25bf99d2 2018-05-22 omar.polo // Compute the dimension of the string str once rendered, return the
328 25bf99d2 2018-05-22 omar.polo // width and save the width and the height in ret_width and ret_height
329 c9a3bfaa 2018-05-21 omar.polo int text_extents(char *str, int len, struct rendering *r, int *ret_width, int *ret_height) {
330 c9a3bfaa 2018-05-21 omar.polo int height;
331 c9a3bfaa 2018-05-21 omar.polo int width;
332 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
333 c9a3bfaa 2018-05-21 omar.polo XGlyphInfo gi;
334 c9a3bfaa 2018-05-21 omar.polo XftTextExtentsUtf8(r->d, r->font, str, len, &gi);
335 25bf99d2 2018-05-22 omar.polo // Honestly I don't know why this won't work, but I found that this
336 25bf99d2 2018-05-22 omar.polo // formula seems to work with various ttf font
337 25bf99d2 2018-05-22 omar.polo /* height = gi.height; */
338 25bf99d2 2018-05-22 omar.polo height = (gi.height + (r->font->ascent - r->font->descent)/2) / 2;
339 c9a3bfaa 2018-05-21 omar.polo width = gi.width - gi.x;
340 c9a3bfaa 2018-05-21 omar.polo #else
341 c9a3bfaa 2018-05-21 omar.polo XRectangle rect;
342 c9a3bfaa 2018-05-21 omar.polo XmbTextExtents(*r->font, str, len, nil, &rect);
343 c9a3bfaa 2018-05-21 omar.polo height = rect.height;
344 c9a3bfaa 2018-05-21 omar.polo width = rect.width;
345 c9a3bfaa 2018-05-21 omar.polo #endif
346 c9a3bfaa 2018-05-21 omar.polo if (ret_width != nil) *ret_width = width;
347 c9a3bfaa 2018-05-21 omar.polo if (ret_height != nil) *ret_height = height;
348 c9a3bfaa 2018-05-21 omar.polo return width;
349 c9a3bfaa 2018-05-21 omar.polo }
350 c9a3bfaa 2018-05-21 omar.polo
351 25bf99d2 2018-05-22 omar.polo // Draw the string str
352 c9a3bfaa 2018-05-21 omar.polo void draw_string(char *str, int len, int x, int y, struct rendering *r, enum text_type tt) {
353 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
354 c9a3bfaa 2018-05-21 omar.polo XftColor xftcolor;
355 c9a3bfaa 2018-05-21 omar.polo if (tt == PROMPT) xftcolor = r->xft_prompt;
356 c9a3bfaa 2018-05-21 omar.polo if (tt == COMPL) xftcolor = r->xft_completion;
357 c9a3bfaa 2018-05-21 omar.polo if (tt == COMPL_HIGH) xftcolor = r->xft_completion_highlighted;
358 c9a3bfaa 2018-05-21 omar.polo
359 c9a3bfaa 2018-05-21 omar.polo XftDrawStringUtf8(r->xftdraw, &xftcolor, r->font, x, y, str, len);
360 c9a3bfaa 2018-05-21 omar.polo #else
361 c9a3bfaa 2018-05-21 omar.polo GC gc;
362 c9a3bfaa 2018-05-21 omar.polo if (tt == PROMPT) gc = r->prompt;
363 c9a3bfaa 2018-05-21 omar.polo if (tt == COMPL) gc = r->completion;
364 c9a3bfaa 2018-05-21 omar.polo if (tt == COMPL_HIGH) gc = r->completion_highlighted;
365 c9a3bfaa 2018-05-21 omar.polo Xutf8DrawString(r->d, r->w, *r->font, gc, x, y, str, len);
366 c9a3bfaa 2018-05-21 omar.polo #endif
367 f5e234d6 2018-05-18 omar.polo }
368 f5e234d6 2018-05-18 omar.polo
369 25bf99d2 2018-05-22 omar.polo // Duplicate the string str and substitute every space with a 'n'
370 c9a3bfaa 2018-05-21 omar.polo char *strdupn(char *str) {
371 c9a3bfaa 2018-05-21 omar.polo int len = strlen(str);
372 c9a3bfaa 2018-05-21 omar.polo
373 c9a3bfaa 2018-05-21 omar.polo if (str == nil || len == 0)
374 c9a3bfaa 2018-05-21 omar.polo return nil;
375 c9a3bfaa 2018-05-21 omar.polo
376 c9a3bfaa 2018-05-21 omar.polo char *dup = strdup(str);
377 c9a3bfaa 2018-05-21 omar.polo if (dup == nil)
378 c9a3bfaa 2018-05-21 omar.polo return nil;
379 c9a3bfaa 2018-05-21 omar.polo
380 c9a3bfaa 2018-05-21 omar.polo for (int i = 0; i < len; ++i)
381 c9a3bfaa 2018-05-21 omar.polo if (dup[i] == ' ')
382 c9a3bfaa 2018-05-21 omar.polo dup[i] = 'n';
383 c9a3bfaa 2018-05-21 omar.polo
384 c9a3bfaa 2018-05-21 omar.polo return dup;
385 c9a3bfaa 2018-05-21 omar.polo }
386 c9a3bfaa 2018-05-21 omar.polo
387 f5e234d6 2018-05-18 omar.polo // |------------------|----------------------------------------------|
388 f5e234d6 2018-05-18 omar.polo // | 20 char text | completion | completion | completion | compl |
389 f5e234d6 2018-05-18 omar.polo // |------------------|----------------------------------------------|
390 36a15a9f 2018-05-19 omar.polo void draw_horizontally(struct rendering *r, char *text, struct completions *cs) {
391 f5e234d6 2018-05-18 omar.polo // TODO: make these dynamic?
392 f5e234d6 2018-05-18 omar.polo int prompt_width = 20; // char
393 f5e234d6 2018-05-18 omar.polo int padding = 10;
394 f5e234d6 2018-05-18 omar.polo
395 c9a3bfaa 2018-05-21 omar.polo int width, height;
396 c9a3bfaa 2018-05-21 omar.polo char *ps1_dup = strdupn(r->ps1);
397 c9a3bfaa 2018-05-21 omar.polo ps1_dup = ps1_dup == nil ? r->ps1 : ps1_dup;
398 c9a3bfaa 2018-05-21 omar.polo int ps1xlen = text_extents(ps1_dup, r->ps1len, r, &width, &height);
399 c9a3bfaa 2018-05-21 omar.polo free(ps1_dup);
400 8758854a 2018-05-20 omar.polo int start_at = ps1xlen;
401 8758854a 2018-05-20 omar.polo
402 c9a3bfaa 2018-05-21 omar.polo start_at = text_extents("n", 1, r, nil, nil);
403 347d23da 2018-05-19 omar.polo start_at = start_at * prompt_width + padding;
404 347d23da 2018-05-19 omar.polo
405 c9a3bfaa 2018-05-21 omar.polo int texty = (height + r->height) >>1;
406 347d23da 2018-05-19 omar.polo
407 f5e234d6 2018-05-18 omar.polo XFillRectangle(r->d, r->w, r->prompt_bg, 0, 0, start_at, r->height);
408 f5e234d6 2018-05-18 omar.polo
409 f5e234d6 2018-05-18 omar.polo int text_len = strlen(text);
410 f5e234d6 2018-05-18 omar.polo if (text_len > prompt_width)
411 f5e234d6 2018-05-18 omar.polo text = text + (text_len - prompt_width);
412 c9a3bfaa 2018-05-21 omar.polo draw_string(r->ps1, r->ps1len, padding, texty, r, PROMPT);
413 c9a3bfaa 2018-05-21 omar.polo draw_string(text, MIN(text_len, prompt_width), padding + ps1xlen, texty, r, PROMPT);
414 f5e234d6 2018-05-18 omar.polo
415 f5e234d6 2018-05-18 omar.polo XFillRectangle(r->d, r->w, r->completion_bg, start_at, 0, r->width, r->height);
416 f5e234d6 2018-05-18 omar.polo
417 f5e234d6 2018-05-18 omar.polo while (cs != nil) {
418 c9a3bfaa 2018-05-21 omar.polo enum text_type tt = cs->selected ? COMPL_HIGH : COMPL;
419 f5e234d6 2018-05-18 omar.polo GC h = cs->selected ? r->completion_highlighted_bg : r->completion_bg;
420 f5e234d6 2018-05-18 omar.polo
421 f5e234d6 2018-05-18 omar.polo int len = strlen(cs->completion);
422 c9a3bfaa 2018-05-21 omar.polo int text_width = text_extents(cs->completion, len, r, nil, nil);
423 f5e234d6 2018-05-18 omar.polo
424 f5e234d6 2018-05-18 omar.polo XFillRectangle(r->d, r->w, h, start_at, 0, text_width + padding*2, r->height);
425 f5e234d6 2018-05-18 omar.polo
426 c9a3bfaa 2018-05-21 omar.polo draw_string(cs->completion, len, start_at + padding, texty, r, tt);
427 347d23da 2018-05-19 omar.polo
428 f5e234d6 2018-05-18 omar.polo start_at += text_width + padding * 2;
429 f5e234d6 2018-05-18 omar.polo
430 0ee198aa 2018-05-19 omar.polo if (start_at > r->width)
431 0ee198aa 2018-05-19 omar.polo break; // don't draw completion if the space isn't enough
432 0ee198aa 2018-05-19 omar.polo
433 f5e234d6 2018-05-18 omar.polo cs = cs->next;
434 f5e234d6 2018-05-18 omar.polo }
435 f5e234d6 2018-05-18 omar.polo
436 f5e234d6 2018-05-18 omar.polo XFlush(r->d);
437 f5e234d6 2018-05-18 omar.polo }
438 36a15a9f 2018-05-19 omar.polo
439 36a15a9f 2018-05-19 omar.polo // |-----------------------------------------------------------------|
440 36a15a9f 2018-05-19 omar.polo // | prompt |
441 36a15a9f 2018-05-19 omar.polo // |-----------------------------------------------------------------|
442 36a15a9f 2018-05-19 omar.polo // | completion |
443 36a15a9f 2018-05-19 omar.polo // |-----------------------------------------------------------------|
444 36a15a9f 2018-05-19 omar.polo // | completion |
445 36a15a9f 2018-05-19 omar.polo // |-----------------------------------------------------------------|
446 36a15a9f 2018-05-19 omar.polo // TODO: dunno why but in the call to Xutf8DrawString, by logic,
447 36a15a9f 2018-05-19 omar.polo // should be padding and not padding*2, but the text doesn't seem to
448 36a15a9f 2018-05-19 omar.polo // be vertically centered otherwise....
449 36a15a9f 2018-05-19 omar.polo void draw_vertically(struct rendering *r, char *text, struct completions *cs) {
450 36a15a9f 2018-05-19 omar.polo int padding = 10; // TODO make this dynamic
451 f5e234d6 2018-05-18 omar.polo
452 c9a3bfaa 2018-05-21 omar.polo int height, width;
453 c9a3bfaa 2018-05-21 omar.polo text_extents("fjpgl", 5, r, &width, &height);
454 c9a3bfaa 2018-05-21 omar.polo int start_at = height + padding*2;
455 36a15a9f 2018-05-19 omar.polo
456 36a15a9f 2018-05-19 omar.polo XFillRectangle(r->d, r->w, r->completion_bg, 0, 0, r->width, r->height);
457 36a15a9f 2018-05-19 omar.polo XFillRectangle(r->d, r->w, r->prompt_bg, 0, 0, r->width, start_at);
458 36a15a9f 2018-05-19 omar.polo
459 c9a3bfaa 2018-05-21 omar.polo char *ps1_dup = strdupn(r->ps1);
460 c9a3bfaa 2018-05-21 omar.polo ps1_dup = ps1_dup == nil ? r->ps1 : ps1_dup;
461 c9a3bfaa 2018-05-21 omar.polo int ps1xlen = text_extents(ps1_dup, r->ps1len, r, nil, nil);
462 c9a3bfaa 2018-05-21 omar.polo free(ps1_dup);
463 c9a3bfaa 2018-05-21 omar.polo
464 c9a3bfaa 2018-05-21 omar.polo draw_string(r->ps1, r->ps1len, padding, padding*2, r, PROMPT);
465 c9a3bfaa 2018-05-21 omar.polo draw_string(text, strlen(text), padding + ps1xlen, padding*2, r, PROMPT);
466 c9a3bfaa 2018-05-21 omar.polo
467 36a15a9f 2018-05-19 omar.polo while (cs != nil) {
468 c9a3bfaa 2018-05-21 omar.polo enum text_type tt = cs->selected ? COMPL_HIGH : COMPL;
469 36a15a9f 2018-05-19 omar.polo GC h = cs->selected ? r->completion_highlighted_bg : r->completion_bg;
470 36a15a9f 2018-05-19 omar.polo
471 36a15a9f 2018-05-19 omar.polo int len = strlen(cs->completion);
472 c9a3bfaa 2018-05-21 omar.polo text_extents(cs->completion, len, r, &width, &height);
473 c9a3bfaa 2018-05-21 omar.polo XFillRectangle(r->d, r->w, h, 0, start_at, r->width, height + padding*2);
474 c9a3bfaa 2018-05-21 omar.polo draw_string(cs->completion, len, padding, start_at + padding*2, r, tt);
475 36a15a9f 2018-05-19 omar.polo
476 c9a3bfaa 2018-05-21 omar.polo start_at += height + padding *2;
477 0ee198aa 2018-05-19 omar.polo
478 0ee198aa 2018-05-19 omar.polo if (start_at > r->height)
479 0ee198aa 2018-05-19 omar.polo break; // don't draw completion if the space isn't enough
480 0ee198aa 2018-05-19 omar.polo
481 36a15a9f 2018-05-19 omar.polo cs = cs->next;
482 36a15a9f 2018-05-19 omar.polo }
483 36a15a9f 2018-05-19 omar.polo
484 36a15a9f 2018-05-19 omar.polo XFlush(r->d);
485 36a15a9f 2018-05-19 omar.polo }
486 36a15a9f 2018-05-19 omar.polo
487 36a15a9f 2018-05-19 omar.polo void draw(struct rendering *r, char *text, struct completions *cs) {
488 36a15a9f 2018-05-19 omar.polo if (r->horizontal_layout)
489 36a15a9f 2018-05-19 omar.polo draw_horizontally(r, text, cs);
490 36a15a9f 2018-05-19 omar.polo else
491 36a15a9f 2018-05-19 omar.polo draw_vertically(r, text, cs);
492 36a15a9f 2018-05-19 omar.polo }
493 36a15a9f 2018-05-19 omar.polo
494 f5e234d6 2018-05-18 omar.polo /* Set some WM stuff */
495 f5e234d6 2018-05-18 omar.polo void set_win_atoms_hints(Display *d, Window w, int width, int height) {
496 f5e234d6 2018-05-18 omar.polo Atom type;
497 f5e234d6 2018-05-18 omar.polo type = XInternAtom(d, "_NET_WM_WINDOW_TYPE_DOCK", false);
498 f5e234d6 2018-05-18 omar.polo XChangeProperty(
499 f5e234d6 2018-05-18 omar.polo d,
500 f5e234d6 2018-05-18 omar.polo w,
501 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "_NET_WM_WINDOW_TYPE", false),
502 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "ATOM", false),
503 f5e234d6 2018-05-18 omar.polo 32,
504 f5e234d6 2018-05-18 omar.polo PropModeReplace,
505 f5e234d6 2018-05-18 omar.polo (unsigned char *)&type,
506 f5e234d6 2018-05-18 omar.polo 1
507 f5e234d6 2018-05-18 omar.polo );
508 f5e234d6 2018-05-18 omar.polo
509 f5e234d6 2018-05-18 omar.polo /* some window managers honor this properties */
510 f5e234d6 2018-05-18 omar.polo type = XInternAtom(d, "_NET_WM_STATE_ABOVE", false);
511 f5e234d6 2018-05-18 omar.polo XChangeProperty(d,
512 f5e234d6 2018-05-18 omar.polo w,
513 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "_NET_WM_STATE", false),
514 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "ATOM", false),
515 f5e234d6 2018-05-18 omar.polo 32,
516 f5e234d6 2018-05-18 omar.polo PropModeReplace,
517 f5e234d6 2018-05-18 omar.polo (unsigned char *)&type,
518 f5e234d6 2018-05-18 omar.polo 1
519 f5e234d6 2018-05-18 omar.polo );
520 f5e234d6 2018-05-18 omar.polo
521 f5e234d6 2018-05-18 omar.polo type = XInternAtom(d, "_NET_WM_STATE_FOCUSED", false);
522 f5e234d6 2018-05-18 omar.polo XChangeProperty(d,
523 f5e234d6 2018-05-18 omar.polo w,
524 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "_NET_WM_STATE", false),
525 f5e234d6 2018-05-18 omar.polo XInternAtom(d, "ATOM", false),
526 f5e234d6 2018-05-18 omar.polo 32,
527 f5e234d6 2018-05-18 omar.polo PropModeAppend,
528 f5e234d6 2018-05-18 omar.polo (unsigned char *)&type,
529 f5e234d6 2018-05-18 omar.polo 1
530 f5e234d6 2018-05-18 omar.polo );
531 f5e234d6 2018-05-18 omar.polo
532 f5e234d6 2018-05-18 omar.polo // setting window hints
533 f5e234d6 2018-05-18 omar.polo XClassHint *class_hint = XAllocClassHint();
534 f5e234d6 2018-05-18 omar.polo if (class_hint == nil) {
535 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Could not allocate memory for class hint\n");
536 f5e234d6 2018-05-18 omar.polo exit(EX_UNAVAILABLE);
537 f5e234d6 2018-05-18 omar.polo }
538 7ef37ee0 2018-05-21 omar.polo class_hint->res_name = resname;
539 7ef37ee0 2018-05-21 omar.polo class_hint->res_class = resclass;
540 f5e234d6 2018-05-18 omar.polo XSetClassHint(d, w, class_hint);
541 f5e234d6 2018-05-18 omar.polo XFree(class_hint);
542 f5e234d6 2018-05-18 omar.polo
543 f5e234d6 2018-05-18 omar.polo XSizeHints *size_hint = XAllocSizeHints();
544 f5e234d6 2018-05-18 omar.polo if (size_hint == nil) {
545 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Could not allocate memory for size hint\n");
546 f5e234d6 2018-05-18 omar.polo exit(EX_UNAVAILABLE);
547 f5e234d6 2018-05-18 omar.polo }
548 7ef37ee0 2018-05-21 omar.polo size_hint->flags = PMinSize | PBaseSize;
549 f5e234d6 2018-05-18 omar.polo size_hint->min_width = width;
550 f5e234d6 2018-05-18 omar.polo size_hint->base_width = width;
551 f5e234d6 2018-05-18 omar.polo size_hint->min_height = height;
552 f5e234d6 2018-05-18 omar.polo size_hint->base_height = height;
553 f5e234d6 2018-05-18 omar.polo
554 f5e234d6 2018-05-18 omar.polo XFlush(d);
555 f5e234d6 2018-05-18 omar.polo }
556 f5e234d6 2018-05-18 omar.polo
557 f5e234d6 2018-05-18 omar.polo void get_wh(Display *d, Window *w, int *width, int *height) {
558 f5e234d6 2018-05-18 omar.polo XWindowAttributes win_attr;
559 f5e234d6 2018-05-18 omar.polo XGetWindowAttributes(d, *w, &win_attr);
560 f5e234d6 2018-05-18 omar.polo *height = win_attr.height;
561 f5e234d6 2018-05-18 omar.polo *width = win_attr.width;
562 f5e234d6 2018-05-18 omar.polo }
563 f5e234d6 2018-05-18 omar.polo
564 f5e234d6 2018-05-18 omar.polo // I know this may seem a little hackish BUT is the only way I managed
565 f5e234d6 2018-05-18 omar.polo // to actually grab that goddam keyboard. Only one call to
566 f5e234d6 2018-05-18 omar.polo // XGrabKeyboard does not always end up with the keyboard grabbed!
567 f5e234d6 2018-05-18 omar.polo int take_keyboard(Display *d, Window w) {
568 686e9237 2018-05-18 omar.polo int i;
569 686e9237 2018-05-18 omar.polo for (i = 0; i < 100; i++) {
570 686e9237 2018-05-18 omar.polo if (XGrabKeyboard(d, w, True, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
571 686e9237 2018-05-18 omar.polo return 1;
572 686e9237 2018-05-18 omar.polo usleep(1000);
573 686e9237 2018-05-18 omar.polo }
574 686e9237 2018-05-18 omar.polo return 0;
575 f5e234d6 2018-05-18 omar.polo }
576 f5e234d6 2018-05-18 omar.polo
577 f5e234d6 2018-05-18 omar.polo void release_keyboard(Display *d) {
578 686e9237 2018-05-18 omar.polo XUngrabKeyboard(d, CurrentTime);
579 f5e234d6 2018-05-18 omar.polo }
580 f5e234d6 2018-05-18 omar.polo
581 25bf99d2 2018-05-22 omar.polo int parse_integer(const char *str, int default_value) {
582 25bf99d2 2018-05-22 omar.polo /* int len = strlen(str); */
583 25bf99d2 2018-05-22 omar.polo /* if (len > 0 && str[len-1] == '%') { */
584 25bf99d2 2018-05-22 omar.polo /* char *cpy = strdup(str); */
585 25bf99d2 2018-05-22 omar.polo /* check_allocation(cpy); */
586 25bf99d2 2018-05-22 omar.polo /* cpy[len-1] = '\0'; */
587 25bf99d2 2018-05-22 omar.polo /* int val = parse_integer(cpy, default_value, max); */
588 25bf99d2 2018-05-22 omar.polo /* free(cpy); */
589 25bf99d2 2018-05-22 omar.polo /* return val * max / 100; */
590 25bf99d2 2018-05-22 omar.polo /* } */
591 f5e234d6 2018-05-18 omar.polo
592 f5e234d6 2018-05-18 omar.polo errno = 0;
593 f5e234d6 2018-05-18 omar.polo char *ep;
594 f5e234d6 2018-05-18 omar.polo long lval = strtol(str, &ep, 10);
595 f5e234d6 2018-05-18 omar.polo if (str[0] == '\0' || *ep != '\0') { // NaN
596 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "'%s' is not a valid number! Using %d as default.\n", str, default_value);
597 f5e234d6 2018-05-18 omar.polo return default_value;
598 f5e234d6 2018-05-18 omar.polo }
599 f5e234d6 2018-05-18 omar.polo if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
600 f5e234d6 2018-05-18 omar.polo (lval > INT_MAX || lval < INT_MIN)) {
601 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "%s out of range! Using %d as default.\n", str, default_value);
602 f5e234d6 2018-05-18 omar.polo return default_value;
603 f5e234d6 2018-05-18 omar.polo }
604 f5e234d6 2018-05-18 omar.polo return lval;
605 25bf99d2 2018-05-22 omar.polo }
606 25bf99d2 2018-05-22 omar.polo
607 25bf99d2 2018-05-22 omar.polo int parse_int_with_percentage(const char *str, int default_value, int max) {
608 25bf99d2 2018-05-22 omar.polo int len = strlen(str);
609 25bf99d2 2018-05-22 omar.polo if (len > 0 && str[len-1] == '%') {
610 25bf99d2 2018-05-22 omar.polo char *cpy = strdup(str);
611 25bf99d2 2018-05-22 omar.polo check_allocation(cpy);
612 25bf99d2 2018-05-22 omar.polo cpy[len-1] = '\0';
613 25bf99d2 2018-05-22 omar.polo int val = parse_integer(cpy, default_value);
614 25bf99d2 2018-05-22 omar.polo free(cpy);
615 25bf99d2 2018-05-22 omar.polo return val * max / 100;
616 25bf99d2 2018-05-22 omar.polo }
617 576ab141 2018-05-22 omar.polo return parse_integer(str, default_value);
618 f5e234d6 2018-05-18 omar.polo }
619 f5e234d6 2018-05-18 omar.polo
620 f5e234d6 2018-05-18 omar.polo int parse_int_with_middle(const char *str, int default_value, int max, int self) {
621 f5e234d6 2018-05-18 omar.polo if (!strcmp(str, "middle")) {
622 f5e234d6 2018-05-18 omar.polo return (max - self)/2;
623 f5e234d6 2018-05-18 omar.polo }
624 25bf99d2 2018-05-22 omar.polo return parse_int_with_percentage(str, default_value, max);
625 f5e234d6 2018-05-18 omar.polo }
626 f5e234d6 2018-05-18 omar.polo
627 f5e234d6 2018-05-18 omar.polo int main() {
628 95b27a5e 2018-05-19 omar.polo char **lines = calloc(INITIAL_ITEMS, sizeof(char*));
629 6da98882 2018-05-20 omar.polo int nlines = readlines(&lines, INITIAL_ITEMS);
630 f5e234d6 2018-05-18 omar.polo
631 f5e234d6 2018-05-18 omar.polo setlocale(LC_ALL, getenv("LANG"));
632 f5e234d6 2018-05-18 omar.polo
633 f5e234d6 2018-05-18 omar.polo enum state status = LOOPING;
634 f5e234d6 2018-05-18 omar.polo
635 f5e234d6 2018-05-18 omar.polo // where the monitor start (used only with xinerama)
636 f5e234d6 2018-05-18 omar.polo int offset_x = 0;
637 f5e234d6 2018-05-18 omar.polo int offset_y = 0;
638 f5e234d6 2018-05-18 omar.polo
639 f5e234d6 2018-05-18 omar.polo // width and height of the window
640 f5e234d6 2018-05-18 omar.polo int width = 400;
641 f5e234d6 2018-05-18 omar.polo int height = 20;
642 f5e234d6 2018-05-18 omar.polo
643 f5e234d6 2018-05-18 omar.polo // position on the screen
644 f5e234d6 2018-05-18 omar.polo int x = 0;
645 f5e234d6 2018-05-18 omar.polo int y = 0;
646 f5e234d6 2018-05-18 omar.polo
647 8758854a 2018-05-20 omar.polo char *ps1 = strdup("$ ");
648 8758854a 2018-05-20 omar.polo check_allocation(ps1);
649 8758854a 2018-05-20 omar.polo
650 9e94fcbe 2018-05-22 omar.polo char *fontname = strdup(default_fontname);
651 f5e234d6 2018-05-18 omar.polo check_allocation(fontname);
652 f5e234d6 2018-05-18 omar.polo
653 f5e234d6 2018-05-18 omar.polo int textlen = 10;
654 f5e234d6 2018-05-18 omar.polo char *text = malloc(textlen * sizeof(char));
655 f5e234d6 2018-05-18 omar.polo check_allocation(text);
656 f5e234d6 2018-05-18 omar.polo
657 f5e234d6 2018-05-18 omar.polo struct completions *cs = filter(text, lines);
658 f5e234d6 2018-05-18 omar.polo bool nothing_selected = true;
659 f5e234d6 2018-05-18 omar.polo
660 f5e234d6 2018-05-18 omar.polo // start talking to xorg
661 f5e234d6 2018-05-18 omar.polo Display *d = XOpenDisplay(nil);
662 f5e234d6 2018-05-18 omar.polo if (d == nil) {
663 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "Could not open display!\n");
664 f5e234d6 2018-05-18 omar.polo return EX_UNAVAILABLE;
665 f5e234d6 2018-05-18 omar.polo }
666 f5e234d6 2018-05-18 omar.polo
667 f5e234d6 2018-05-18 omar.polo // get display size
668 f5e234d6 2018-05-18 omar.polo // XXX: is getting the default root window dimension correct?
669 f5e234d6 2018-05-18 omar.polo XWindowAttributes xwa;
670 f5e234d6 2018-05-18 omar.polo XGetWindowAttributes(d, DefaultRootWindow(d), &xwa);
671 f5e234d6 2018-05-18 omar.polo int d_width = xwa.width;
672 f5e234d6 2018-05-18 omar.polo int d_height = xwa.height;
673 f5e234d6 2018-05-18 omar.polo
674 f5e234d6 2018-05-18 omar.polo #ifdef USE_XINERAMA
675 f5e234d6 2018-05-18 omar.polo if (XineramaIsActive(d)) {
676 7ca8829b 2018-05-21 omar.polo // find the mice
677 7ca8829b 2018-05-21 omar.polo int number_of_screens = XScreenCount(d);
678 7ca8829b 2018-05-21 omar.polo bool result;
679 7ca8829b 2018-05-21 omar.polo Window r;
680 7ca8829b 2018-05-21 omar.polo Window root;
681 7ca8829b 2018-05-21 omar.polo int root_x, root_y, win_x, win_y;
682 7ca8829b 2018-05-21 omar.polo unsigned int mask;
683 7ca8829b 2018-05-21 omar.polo bool res;
684 7ca8829b 2018-05-21 omar.polo for (int i = 0; i < number_of_screens; ++i) {
685 7ca8829b 2018-05-21 omar.polo root = XRootWindow(d, i);
686 7ca8829b 2018-05-21 omar.polo res = XQueryPointer(d, root, &r, &r, &root_x, &root_y, &win_x, &win_y, &mask);
687 7ca8829b 2018-05-21 omar.polo if (res) break;
688 7ca8829b 2018-05-21 omar.polo }
689 7ca8829b 2018-05-21 omar.polo if (!res) {
690 7ca8829b 2018-05-21 omar.polo fprintf(stderr, "No mouse found.\n");
691 7ca8829b 2018-05-21 omar.polo root_x = 0;
692 7ca8829b 2018-05-21 omar.polo root_y = 0;
693 7ca8829b 2018-05-21 omar.polo }
694 7ca8829b 2018-05-21 omar.polo
695 7ca8829b 2018-05-21 omar.polo // now find in which monitor the mice is on
696 f5e234d6 2018-05-18 omar.polo int monitors;
697 f5e234d6 2018-05-18 omar.polo XineramaScreenInfo *info = XineramaQueryScreens(d, &monitors);
698 7ca8829b 2018-05-21 omar.polo if (info) {
699 f5e234d6 2018-05-18 omar.polo for (int i = 0; i < monitors; ++i) {
700 7ca8829b 2018-05-21 omar.polo if (info[i].x_org <= root_x && root_x <= (info[i].x_org + info[i].width)
701 7ca8829b 2018-05-21 omar.polo && info[i].y_org <= root_y && root_y <= (info[i].y_org + info[i].height)) {
702 7ca8829b 2018-05-21 omar.polo offset_x = info[i].x_org;
703 7ca8829b 2018-05-21 omar.polo offset_y = info[i].y_org;
704 f5e234d6 2018-05-18 omar.polo d_width = info[i].width;
705 f5e234d6 2018-05-18 omar.polo d_height = info[i].height;
706 7ca8829b 2018-05-21 omar.polo break;
707 f5e234d6 2018-05-18 omar.polo }
708 f5e234d6 2018-05-18 omar.polo }
709 7ca8829b 2018-05-21 omar.polo }
710 f5e234d6 2018-05-18 omar.polo XFree(info);
711 f5e234d6 2018-05-18 omar.polo }
712 f5e234d6 2018-05-18 omar.polo #endif
713 f5e234d6 2018-05-18 omar.polo
714 f5e234d6 2018-05-18 omar.polo Colormap cmap = DefaultColormap(d, DefaultScreen(d));
715 f5e234d6 2018-05-18 omar.polo XColor p_fg, p_bg,
716 f5e234d6 2018-05-18 omar.polo compl_fg, compl_bg,
717 f5e234d6 2018-05-18 omar.polo compl_highlighted_fg, compl_highlighted_bg;
718 f5e234d6 2018-05-18 omar.polo
719 36a15a9f 2018-05-19 omar.polo bool horizontal_layout = true;
720 36a15a9f 2018-05-19 omar.polo
721 f5e234d6 2018-05-18 omar.polo // read resource
722 f5e234d6 2018-05-18 omar.polo XrmInitialize();
723 f5e234d6 2018-05-18 omar.polo char *xrm = XResourceManagerString(d);
724 347d23da 2018-05-19 omar.polo XrmDatabase xdb = nil;
725 f5e234d6 2018-05-18 omar.polo if (xrm != nil) {
726 347d23da 2018-05-19 omar.polo xdb = XrmGetStringDatabase(xrm);
727 f5e234d6 2018-05-18 omar.polo XrmValue value;
728 f5e234d6 2018-05-18 omar.polo char *datatype[20];
729 f5e234d6 2018-05-18 omar.polo
730 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.font", "*", datatype, &value) == true) {
731 f5e234d6 2018-05-18 omar.polo fontname = strdup(value.addr);
732 f5e234d6 2018-05-18 omar.polo check_allocation(fontname);
733 f5e234d6 2018-05-18 omar.polo }
734 f5e234d6 2018-05-18 omar.polo else
735 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "no font defined, using %s\n", fontname);
736 36a15a9f 2018-05-19 omar.polo
737 36a15a9f 2018-05-19 omar.polo if (XrmGetResource(xdb, "MyMenu.layout", "*", datatype, &value) == true) {
738 36a15a9f 2018-05-19 omar.polo char *v = strdup(value.addr);
739 36a15a9f 2018-05-19 omar.polo check_allocation(v);
740 36a15a9f 2018-05-19 omar.polo horizontal_layout = !strcmp(v, "horizontal");
741 36a15a9f 2018-05-19 omar.polo free(v);
742 36a15a9f 2018-05-19 omar.polo }
743 36a15a9f 2018-05-19 omar.polo else
744 36a15a9f 2018-05-19 omar.polo fprintf(stderr, "no layout defined, using horizontal\n");
745 f5e234d6 2018-05-18 omar.polo
746 8758854a 2018-05-20 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt", "*", datatype, &value) == true) {
747 8758854a 2018-05-20 omar.polo free(ps1);
748 8758854a 2018-05-20 omar.polo ps1 = normalize_str(value.addr);
749 8758854a 2018-05-20 omar.polo } else
750 8758854a 2018-05-20 omar.polo fprintf(stderr, "no prompt defined, using \"%s\" as default\n", ps1);
751 8758854a 2018-05-20 omar.polo
752 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.width", "*", datatype, &value) == true)
753 25bf99d2 2018-05-22 omar.polo width = parse_int_with_percentage(value.addr, width, d_width);
754 f5e234d6 2018-05-18 omar.polo else
755 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "no width defined, using %d\n", width);
756 f5e234d6 2018-05-18 omar.polo
757 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.height", "*", datatype, &value) == true)
758 25bf99d2 2018-05-22 omar.polo height = parse_int_with_percentage(value.addr, height, d_height);
759 f5e234d6 2018-05-18 omar.polo else
760 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "no height defined, using %d\n", height);
761 f5e234d6 2018-05-18 omar.polo
762 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.x", "*", datatype, &value) == true)
763 f5e234d6 2018-05-18 omar.polo x = parse_int_with_middle(value.addr, x, d_width, width);
764 f5e234d6 2018-05-18 omar.polo else
765 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "no x defined, using %d\n", width);
766 f5e234d6 2018-05-18 omar.polo
767 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.y", "*", datatype, &value) == true)
768 f5e234d6 2018-05-18 omar.polo y = parse_int_with_middle(value.addr, y, d_height, height);
769 f5e234d6 2018-05-18 omar.polo else
770 f5e234d6 2018-05-18 omar.polo fprintf(stderr, "no y defined, using %d\n", height);
771 f5e234d6 2018-05-18 omar.polo
772 f5e234d6 2018-05-18 omar.polo XColor tmp;
773 f5e234d6 2018-05-18 omar.polo // TODO: tmp needs to be free'd after every allocation?
774 f5e234d6 2018-05-18 omar.polo
775 f5e234d6 2018-05-18 omar.polo // prompt
776 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt.foreground", "*", datatype, &value) == true)
777 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &p_fg, &tmp);
778 f5e234d6 2018-05-18 omar.polo else
779 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &p_fg, &tmp);
780 f5e234d6 2018-05-18 omar.polo
781 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.prompt.background", "*", datatype, &value) == true)
782 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &p_bg, &tmp);
783 f5e234d6 2018-05-18 omar.polo else
784 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &p_bg, &tmp);
785 f5e234d6 2018-05-18 omar.polo
786 f5e234d6 2018-05-18 omar.polo // completion
787 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion.foreground", "*", datatype, &value) == true)
788 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &compl_fg, &tmp);
789 f5e234d6 2018-05-18 omar.polo else
790 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &compl_fg, &tmp);
791 f5e234d6 2018-05-18 omar.polo
792 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion.background", "*", datatype, &value) == true)
793 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &compl_bg, &tmp);
794 f5e234d6 2018-05-18 omar.polo else
795 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &compl_bg, &tmp);
796 f5e234d6 2018-05-18 omar.polo
797 f5e234d6 2018-05-18 omar.polo // completion highlighted
798 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion_highlighted.foreground", "*", datatype, &value) == true)
799 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &compl_highlighted_fg, &tmp);
800 f5e234d6 2018-05-18 omar.polo else
801 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &compl_highlighted_fg, &tmp);
802 f5e234d6 2018-05-18 omar.polo
803 f5e234d6 2018-05-18 omar.polo if (XrmGetResource(xdb, "MyMenu.completion_highlighted.background", "*", datatype, &value) == true)
804 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, value.addr, &compl_highlighted_bg, &tmp);
805 f5e234d6 2018-05-18 omar.polo else
806 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &compl_highlighted_bg, &tmp);
807 f5e234d6 2018-05-18 omar.polo } else {
808 f5e234d6 2018-05-18 omar.polo XColor tmp;
809 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &p_fg, &tmp);
810 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &p_bg, &tmp);
811 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &compl_fg, &tmp);
812 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &compl_bg, &tmp);
813 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "black", &compl_highlighted_fg, &tmp);
814 f5e234d6 2018-05-18 omar.polo XAllocNamedColor(d, cmap, "white", &compl_highlighted_bg, &tmp);
815 f5e234d6 2018-05-18 omar.polo }
816 f5e234d6 2018-05-18 omar.polo
817 f5e234d6 2018-05-18 omar.polo // load the font
818 347d23da 2018-05-19 omar.polo /* XFontStruct *font = XLoadQueryFont(d, fontname); */
819 347d23da 2018-05-19 omar.polo /* if (font == nil) { */
820 347d23da 2018-05-19 omar.polo /* fprintf(stderr, "Unable to load %s font\n", fontname); */
821 347d23da 2018-05-19 omar.polo /* font = XLoadQueryFont(d, "fixed"); */
822 347d23da 2018-05-19 omar.polo /* } */
823 347d23da 2018-05-19 omar.polo // load the font
824 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
825 c9a3bfaa 2018-05-21 omar.polo XftFont *font = XftFontOpenName(d, DefaultScreen(d), fontname);
826 c9a3bfaa 2018-05-21 omar.polo #else
827 347d23da 2018-05-19 omar.polo char **missing_charset_list;
828 347d23da 2018-05-19 omar.polo int missing_charset_count;
829 347d23da 2018-05-19 omar.polo XFontSet font = XCreateFontSet(d, fontname, &missing_charset_list, &missing_charset_count, nil);
830 f5e234d6 2018-05-18 omar.polo if (font == nil) {
831 347d23da 2018-05-19 omar.polo fprintf(stderr, "Unable to load the font(s) %s\n", fontname);
832 347d23da 2018-05-19 omar.polo return EX_UNAVAILABLE;
833 f5e234d6 2018-05-18 omar.polo }
834 c9a3bfaa 2018-05-21 omar.polo #endif
835 f5e234d6 2018-05-18 omar.polo
836 f5e234d6 2018-05-18 omar.polo // create the window
837 f5e234d6 2018-05-18 omar.polo XSetWindowAttributes attr;
838 55bd6842 2018-05-21 omar.polo attr.override_redirect = true;
839 f5e234d6 2018-05-18 omar.polo
840 f5e234d6 2018-05-18 omar.polo Window w = XCreateWindow(d, // display
841 f5e234d6 2018-05-18 omar.polo DefaultRootWindow(d), // parent
842 f5e234d6 2018-05-18 omar.polo x + offset_x, y + offset_y, // x y
843 f5e234d6 2018-05-18 omar.polo width, height, // w h
844 f5e234d6 2018-05-18 omar.polo 0, // border width
845 f5e234d6 2018-05-18 omar.polo DefaultDepth(d, DefaultScreen(d)), // depth
846 f5e234d6 2018-05-18 omar.polo InputOutput, // class
847 f5e234d6 2018-05-18 omar.polo DefaultVisual(d, DefaultScreen(d)), // visual
848 55bd6842 2018-05-21 omar.polo CWOverrideRedirect, // value mask
849 f5e234d6 2018-05-18 omar.polo &attr);
850 f5e234d6 2018-05-18 omar.polo
851 f5e234d6 2018-05-18 omar.polo set_win_atoms_hints(d, w, width, height);
852 f5e234d6 2018-05-18 omar.polo
853 f5e234d6 2018-05-18 omar.polo // we want some events
854 c9a3bfaa 2018-05-21 omar.polo XSelectInput(d, w, StructureNotifyMask | KeyPressMask | KeymapStateMask);
855 f5e234d6 2018-05-18 omar.polo
856 f5e234d6 2018-05-18 omar.polo // make the window appear on the screen
857 f5e234d6 2018-05-18 omar.polo XMapWindow(d, w);
858 f5e234d6 2018-05-18 omar.polo
859 f5e234d6 2018-05-18 omar.polo // wait for the MapNotify event (i.e. the event "window rendered")
860 f5e234d6 2018-05-18 omar.polo for (;;) {
861 f5e234d6 2018-05-18 omar.polo XEvent e;
862 f5e234d6 2018-05-18 omar.polo XNextEvent(d, &e);
863 f5e234d6 2018-05-18 omar.polo if (e.type == MapNotify)
864 f5e234d6 2018-05-18 omar.polo break;
865 f5e234d6 2018-05-18 omar.polo }
866 f5e234d6 2018-05-18 omar.polo
867 f5e234d6 2018-05-18 omar.polo // get the *real* width & height after the window was rendered
868 f5e234d6 2018-05-18 omar.polo get_wh(d, &w, &width, &height);
869 f5e234d6 2018-05-18 omar.polo
870 f5e234d6 2018-05-18 omar.polo // grab keyboard
871 f5e234d6 2018-05-18 omar.polo take_keyboard(d, w);
872 f5e234d6 2018-05-18 omar.polo
873 f5e234d6 2018-05-18 omar.polo // Create some graphics contexts
874 f5e234d6 2018-05-18 omar.polo XGCValues values;
875 347d23da 2018-05-19 omar.polo /* values.font = font->fid; */
876 f5e234d6 2018-05-18 omar.polo
877 f5e234d6 2018-05-18 omar.polo struct rendering r = {
878 f5e234d6 2018-05-18 omar.polo .d = d,
879 f5e234d6 2018-05-18 omar.polo .w = w,
880 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
881 c9a3bfaa 2018-05-21 omar.polo .font = font,
882 c9a3bfaa 2018-05-21 omar.polo #else
883 c9a3bfaa 2018-05-21 omar.polo .font = &font,
884 c9a3bfaa 2018-05-21 omar.polo #endif
885 347d23da 2018-05-19 omar.polo .prompt = XCreateGC(d, w, 0, &values),
886 347d23da 2018-05-19 omar.polo .prompt_bg = XCreateGC(d, w, 0, &values),
887 347d23da 2018-05-19 omar.polo .completion = XCreateGC(d, w, 0, &values),
888 347d23da 2018-05-19 omar.polo .completion_bg = XCreateGC(d, w, 0, &values),
889 347d23da 2018-05-19 omar.polo .completion_highlighted = XCreateGC(d, w, 0, &values),
890 347d23da 2018-05-19 omar.polo .completion_highlighted_bg = XCreateGC(d, w, 0, &values),
891 f5e234d6 2018-05-18 omar.polo .width = width,
892 f5e234d6 2018-05-18 omar.polo .height = height,
893 8758854a 2018-05-20 omar.polo .horizontal_layout = horizontal_layout,
894 8758854a 2018-05-20 omar.polo .ps1 = ps1,
895 8758854a 2018-05-20 omar.polo .ps1len = strlen(ps1)
896 f5e234d6 2018-05-18 omar.polo };
897 f5e234d6 2018-05-18 omar.polo
898 7ca8829b 2018-05-21 omar.polo #ifdef USE_XFT
899 7ca8829b 2018-05-21 omar.polo r.xftdraw = XftDrawCreate(d, w, DefaultVisual(d, 0), DefaultColormap(d, 0));
900 c9a3bfaa 2018-05-21 omar.polo
901 7ca8829b 2018-05-21 omar.polo // prompt
902 7ca8829b 2018-05-21 omar.polo XRenderColor xrcolor;
903 7ca8829b 2018-05-21 omar.polo xrcolor.red = p_fg.red;
904 7ca8829b 2018-05-21 omar.polo xrcolor.green = p_fg.red;
905 7ca8829b 2018-05-21 omar.polo xrcolor.blue = p_fg.red;
906 7ca8829b 2018-05-21 omar.polo xrcolor.alpha = 65535;
907 7ca8829b 2018-05-21 omar.polo XftColorAllocValue(d, DefaultVisual(d, 0), DefaultColormap(d, 0), &xrcolor, &r.xft_prompt);
908 c9a3bfaa 2018-05-21 omar.polo
909 7ca8829b 2018-05-21 omar.polo // completion
910 7ca8829b 2018-05-21 omar.polo xrcolor.red = compl_fg.red;
911 7ca8829b 2018-05-21 omar.polo xrcolor.green = compl_fg.green;
912 7ca8829b 2018-05-21 omar.polo xrcolor.blue = compl_fg.blue;
913 7ca8829b 2018-05-21 omar.polo xrcolor.alpha = 65535;
914 7ca8829b 2018-05-21 omar.polo XftColorAllocValue(d, DefaultVisual(d, 0), DefaultColormap(d, 0), &xrcolor, &r.xft_completion);
915 7ca8829b 2018-05-21 omar.polo
916 7ca8829b 2018-05-21 omar.polo // completion highlighted
917 7ca8829b 2018-05-21 omar.polo xrcolor.red = compl_highlighted_fg.red;
918 7ca8829b 2018-05-21 omar.polo xrcolor.green = compl_highlighted_fg.green;
919 7ca8829b 2018-05-21 omar.polo xrcolor.blue = compl_highlighted_fg.blue;
920 7ca8829b 2018-05-21 omar.polo xrcolor.alpha = 65535;
921 7ca8829b 2018-05-21 omar.polo XftColorAllocValue(d, DefaultVisual(d, 0), DefaultColormap(d, 0), &xrcolor, &r.xft_completion_highlighted);
922 c9a3bfaa 2018-05-21 omar.polo #endif
923 7ca8829b 2018-05-21 omar.polo
924 f5e234d6 2018-05-18 omar.polo // load the colors in our GCs
925 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.prompt, p_fg.pixel);
926 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.prompt_bg, p_bg.pixel);
927 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.completion, compl_fg.pixel);
928 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.completion_bg, compl_bg.pixel);
929 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.completion_highlighted, compl_highlighted_fg.pixel);
930 f5e234d6 2018-05-18 omar.polo XSetForeground(d, r.completion_highlighted_bg, compl_highlighted_bg.pixel);
931 347d23da 2018-05-19 omar.polo
932 347d23da 2018-05-19 omar.polo // open the X input method
933 347d23da 2018-05-19 omar.polo XIM xim = XOpenIM(d, xdb, resname, resclass);
934 347d23da 2018-05-19 omar.polo check_allocation(xim);
935 f5e234d6 2018-05-18 omar.polo
936 347d23da 2018-05-19 omar.polo XIMStyles *xis = nil;
937 347d23da 2018-05-19 omar.polo if (XGetIMValues(xim, XNQueryInputStyle, &xis, NULL) || !xis) {
938 347d23da 2018-05-19 omar.polo fprintf(stderr, "Input Styles could not be retrieved\n");
939 347d23da 2018-05-19 omar.polo return EX_UNAVAILABLE;
940 347d23da 2018-05-19 omar.polo }
941 347d23da 2018-05-19 omar.polo
942 347d23da 2018-05-19 omar.polo XIMStyle bestMatchStyle = 0;
943 347d23da 2018-05-19 omar.polo for (int i = 0; i < xis->count_styles; ++i) {
944 347d23da 2018-05-19 omar.polo XIMStyle ts = xis->supported_styles[i];
945 347d23da 2018-05-19 omar.polo if (ts == (XIMPreeditNothing | XIMStatusNothing)) {
946 347d23da 2018-05-19 omar.polo bestMatchStyle = ts;
947 347d23da 2018-05-19 omar.polo break;
948 347d23da 2018-05-19 omar.polo }
949 347d23da 2018-05-19 omar.polo }
950 347d23da 2018-05-19 omar.polo XFree(xis);
951 347d23da 2018-05-19 omar.polo
952 347d23da 2018-05-19 omar.polo if (!bestMatchStyle) {
953 347d23da 2018-05-19 omar.polo fprintf(stderr, "No matching input style could be determined\n");
954 347d23da 2018-05-19 omar.polo }
955 347d23da 2018-05-19 omar.polo
956 347d23da 2018-05-19 omar.polo XIC xic = XCreateIC(xim, XNInputStyle, bestMatchStyle, XNClientWindow, w, XNFocusWindow, w, NULL);
957 347d23da 2018-05-19 omar.polo check_allocation(xic);
958 347d23da 2018-05-19 omar.polo
959 f5e234d6 2018-05-18 omar.polo // draw the window for the first time
960 f5e234d6 2018-05-18 omar.polo draw(&r, text, cs);
961 f5e234d6 2018-05-18 omar.polo
962 f5e234d6 2018-05-18 omar.polo // main loop
963 f5e234d6 2018-05-18 omar.polo while (status == LOOPING) {
964 f5e234d6 2018-05-18 omar.polo XEvent e;
965 f5e234d6 2018-05-18 omar.polo XNextEvent(d, &e);
966 f5e234d6 2018-05-18 omar.polo
967 347d23da 2018-05-19 omar.polo if (XFilterEvent(&e, w))
968 347d23da 2018-05-19 omar.polo continue;
969 347d23da 2018-05-19 omar.polo
970 f5e234d6 2018-05-18 omar.polo switch (e.type) {
971 f5e234d6 2018-05-18 omar.polo case KeymapNotify:
972 f5e234d6 2018-05-18 omar.polo XRefreshKeyboardMapping(&e.xmapping);
973 f5e234d6 2018-05-18 omar.polo break;
974 f5e234d6 2018-05-18 omar.polo
975 347d23da 2018-05-19 omar.polo case KeyPress: {
976 347d23da 2018-05-19 omar.polo XKeyPressedEvent *ev = (XKeyPressedEvent*)&e;
977 f5e234d6 2018-05-18 omar.polo
978 347d23da 2018-05-19 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_Tab)) {
979 347d23da 2018-05-19 omar.polo bool shift = (ev->state & ShiftMask);
980 347d23da 2018-05-19 omar.polo struct completions *n = shift ? compl_select_prev(cs, nothing_selected)
981 347d23da 2018-05-19 omar.polo : compl_select_next(cs, nothing_selected);
982 347d23da 2018-05-19 omar.polo if (n != nil) {
983 347d23da 2018-05-19 omar.polo nothing_selected = false;
984 347d23da 2018-05-19 omar.polo free(text);
985 347d23da 2018-05-19 omar.polo text = strdup(n->completion);
986 347d23da 2018-05-19 omar.polo if (text == nil) {
987 347d23da 2018-05-19 omar.polo fprintf(stderr, "Memory allocation error!\n");
988 347d23da 2018-05-19 omar.polo status = ERR;
989 347d23da 2018-05-19 omar.polo break;
990 f5e234d6 2018-05-18 omar.polo }
991 347d23da 2018-05-19 omar.polo textlen = strlen(text);
992 f5e234d6 2018-05-18 omar.polo }
993 f5e234d6 2018-05-18 omar.polo draw(&r, text, cs);
994 f5e234d6 2018-05-18 omar.polo break;
995 f5e234d6 2018-05-18 omar.polo }
996 f5e234d6 2018-05-18 omar.polo
997 347d23da 2018-05-19 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_BackSpace)) {
998 f5e234d6 2018-05-18 omar.polo nothing_selected = true;
999 f5e234d6 2018-05-18 omar.polo popc(text, textlen);
1000 f5e234d6 2018-05-18 omar.polo update_completions(cs, text, lines);
1001 f5e234d6 2018-05-18 omar.polo draw(&r, text, cs);
1002 f5e234d6 2018-05-18 omar.polo break;
1003 347d23da 2018-05-19 omar.polo }
1004 f5e234d6 2018-05-18 omar.polo
1005 347d23da 2018-05-19 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_Return)) {
1006 347d23da 2018-05-19 omar.polo status = OK;
1007 347d23da 2018-05-19 omar.polo break;
1008 347d23da 2018-05-19 omar.polo }
1009 f5e234d6 2018-05-18 omar.polo
1010 347d23da 2018-05-19 omar.polo if (ev->keycode == XKeysymToKeycode(d, XK_Escape)) {
1011 347d23da 2018-05-19 omar.polo status = ERR;
1012 347d23da 2018-05-19 omar.polo break;
1013 347d23da 2018-05-19 omar.polo }
1014 f5e234d6 2018-05-18 omar.polo
1015 347d23da 2018-05-19 omar.polo // try to read what the user pressed
1016 347d23da 2018-05-19 omar.polo int symbol = 0;
1017 347d23da 2018-05-19 omar.polo Status s = 0;
1018 347d23da 2018-05-19 omar.polo Xutf8LookupString(xic, ev, (char*)&symbol, 4, 0, &s);
1019 347d23da 2018-05-19 omar.polo
1020 347d23da 2018-05-19 omar.polo if (s == XBufferOverflow) {
1021 347d23da 2018-05-19 omar.polo // should not happen since there are no utf-8 characters
1022 347d23da 2018-05-19 omar.polo // larger than 24bits, but is something to be aware of when
1023 347d23da 2018-05-19 omar.polo // used to directly write to a string buffer
1024 347d23da 2018-05-19 omar.polo fprintf(stderr, "Buffer overflow when trying to create keyboard symbol map.\n");
1025 347d23da 2018-05-19 omar.polo break;
1026 347d23da 2018-05-19 omar.polo }
1027 347d23da 2018-05-19 omar.polo char *str = (char*)&symbol;
1028 347d23da 2018-05-19 omar.polo
1029 347d23da 2018-05-19 omar.polo if (ev->state & ControlMask) {
1030 347d23da 2018-05-19 omar.polo // check for some key bindings
1031 347d23da 2018-05-19 omar.polo if (!strcmp(str, "")) { // C-u
1032 f5e234d6 2018-05-18 omar.polo nothing_selected = true;
1033 347d23da 2018-05-19 omar.polo for (int i = 0; i < textlen; ++i)
1034 347d23da 2018-05-19 omar.polo text[i] = 0;
1035 f5e234d6 2018-05-18 omar.polo update_completions(cs, text, lines);
1036 f5e234d6 2018-05-18 omar.polo }
1037 347d23da 2018-05-19 omar.polo if (!strcmp(str, "")) { // C-h
1038 347d23da 2018-05-19 omar.polo nothing_selected = true;
1039 347d23da 2018-05-19 omar.polo popc(text, textlen);
1040 347d23da 2018-05-19 omar.polo update_completions(cs, text, lines);
1041 347d23da 2018-05-19 omar.polo }
1042 347d23da 2018-05-19 omar.polo if (!strcmp(str, "")) { // C-w
1043 347d23da 2018-05-19 omar.polo nothing_selected = true;
1044 347d23da 2018-05-19 omar.polo
1045 347d23da 2018-05-19 omar.polo // `textlen` is the length of the allocated string, not the
1046 347d23da 2018-05-19 omar.polo // length of the ACTUAL string
1047 347d23da 2018-05-19 omar.polo int p = strlen(text) - 1;
1048 347d23da 2018-05-19 omar.polo if (p >= 0) { // delete the current char
1049 347d23da 2018-05-19 omar.polo text[p] = 0;
1050 347d23da 2018-05-19 omar.polo p--;
1051 347d23da 2018-05-19 omar.polo }
1052 347d23da 2018-05-19 omar.polo while (p >= 0 && isalnum(text[p])) {
1053 347d23da 2018-05-19 omar.polo text[p] = 0;
1054 347d23da 2018-05-19 omar.polo p--;
1055 347d23da 2018-05-19 omar.polo }
1056 347d23da 2018-05-19 omar.polo // erase also trailing white space
1057 347d23da 2018-05-19 omar.polo while (p >= 0 && isspace(text[p])) {
1058 347d23da 2018-05-19 omar.polo text[p] = 0;
1059 347d23da 2018-05-19 omar.polo p--;
1060 347d23da 2018-05-19 omar.polo }
1061 347d23da 2018-05-19 omar.polo update_completions(cs, text, lines);
1062 347d23da 2018-05-19 omar.polo }
1063 347d23da 2018-05-19 omar.polo if (!strcmp(str, "\r")) { // C-m
1064 347d23da 2018-05-19 omar.polo status = OK;
1065 347d23da 2018-05-19 omar.polo }
1066 347d23da 2018-05-19 omar.polo draw(&r, text, cs);
1067 f5e234d6 2018-05-18 omar.polo break;
1068 f5e234d6 2018-05-18 omar.polo }
1069 f5e234d6 2018-05-18 omar.polo
1070 347d23da 2018-05-19 omar.polo int str_len = strlen(str);
1071 347d23da 2018-05-19 omar.polo for (int i = 0; i < str_len; ++i) {
1072 347d23da 2018-05-19 omar.polo textlen = pushc(&text, textlen, str[i]);
1073 347d23da 2018-05-19 omar.polo if (textlen == -1) {
1074 347d23da 2018-05-19 omar.polo fprintf(stderr, "Memory allocation error\n");
1075 347d23da 2018-05-19 omar.polo status = ERR;
1076 347d23da 2018-05-19 omar.polo break;
1077 347d23da 2018-05-19 omar.polo }
1078 347d23da 2018-05-19 omar.polo nothing_selected = true;
1079 347d23da 2018-05-19 omar.polo update_completions(cs, text, lines);
1080 347d23da 2018-05-19 omar.polo }
1081 347d23da 2018-05-19 omar.polo
1082 347d23da 2018-05-19 omar.polo }
1083 f5e234d6 2018-05-18 omar.polo draw(&r, text, cs);
1084 f5e234d6 2018-05-18 omar.polo break;
1085 f5e234d6 2018-05-18 omar.polo
1086 f5e234d6 2018-05-18 omar.polo default:
1087 347d23da 2018-05-19 omar.polo fprintf(stderr, "Unknown event %d\n", e.type);
1088 f5e234d6 2018-05-18 omar.polo }
1089 f5e234d6 2018-05-18 omar.polo }
1090 f5e234d6 2018-05-18 omar.polo
1091 f5e234d6 2018-05-18 omar.polo release_keyboard(d);
1092 f5e234d6 2018-05-18 omar.polo
1093 f5e234d6 2018-05-18 omar.polo if (status == OK)
1094 f5e234d6 2018-05-18 omar.polo printf("%s\n", text);
1095 6da98882 2018-05-20 omar.polo
1096 6da98882 2018-05-20 omar.polo for (int i = 0; i < nlines; ++i) {
1097 6da98882 2018-05-20 omar.polo free(lines[i]);
1098 6da98882 2018-05-20 omar.polo }
1099 f5e234d6 2018-05-18 omar.polo
1100 c9a3bfaa 2018-05-21 omar.polo #ifdef USE_XFT
1101 c9a3bfaa 2018-05-21 omar.polo XftColorFree(d, DefaultVisual(d, 0), DefaultColormap(d, 0), &r.xft_prompt);
1102 c9a3bfaa 2018-05-21 omar.polo XftColorFree(d, DefaultVisual(d, 0), DefaultColormap(d, 0), &r.xft_completion);
1103 c9a3bfaa 2018-05-21 omar.polo XftColorFree(d, DefaultVisual(d, 0), DefaultColormap(d, 0), &r.xft_completion_highlighted);
1104 c9a3bfaa 2018-05-21 omar.polo #endif
1105 c9a3bfaa 2018-05-21 omar.polo
1106 8758854a 2018-05-20 omar.polo free(ps1);
1107 f5e234d6 2018-05-18 omar.polo free(fontname);
1108 f5e234d6 2018-05-18 omar.polo free(text);
1109 95b27a5e 2018-05-19 omar.polo free(lines);
1110 f5e234d6 2018-05-18 omar.polo compl_delete(cs);
1111 f5e234d6 2018-05-18 omar.polo
1112 8758854a 2018-05-20 omar.polo XDestroyWindow(d, w);
1113 f5e234d6 2018-05-18 omar.polo XCloseDisplay(d);
1114 f5e234d6 2018-05-18 omar.polo
1115 f5e234d6 2018-05-18 omar.polo return status == OK ? 0 : 1;
1116 f5e234d6 2018-05-18 omar.polo }