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