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