Blame


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