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