Blame


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