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