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