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