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