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