diff options
Diffstat (limited to '')
-rw-r--r-- | scripts/kconfig/libcurses/slk.c | 673 |
1 files changed, 673 insertions, 0 deletions
diff --git a/scripts/kconfig/libcurses/slk.c b/scripts/kconfig/libcurses/slk.c new file mode 100644 index 000000000..e8dde752c --- /dev/null +++ b/scripts/kconfig/libcurses/slk.c | |||
@@ -0,0 +1,673 @@ | |||
1 | /* PDCurses */ | ||
2 | |||
3 | #include "curspriv.h" | ||
4 | |||
5 | /*man-start************************************************************** | ||
6 | |||
7 | slk | ||
8 | --- | ||
9 | |||
10 | ### Synopsis | ||
11 | |||
12 | int slk_init(int fmt); | ||
13 | int slk_set(int labnum, const char *label, int justify); | ||
14 | int slk_refresh(void); | ||
15 | int slk_noutrefresh(void); | ||
16 | char *slk_label(int labnum); | ||
17 | int slk_clear(void); | ||
18 | int slk_restore(void); | ||
19 | int slk_touch(void); | ||
20 | int slk_attron(const chtype attrs); | ||
21 | int slk_attr_on(const attr_t attrs, void *opts); | ||
22 | int slk_attrset(const chtype attrs); | ||
23 | int slk_attr_set(const attr_t attrs, short color_pair, void *opts); | ||
24 | int slk_attroff(const chtype attrs); | ||
25 | int slk_attr_off(const attr_t attrs, void *opts); | ||
26 | int slk_color(short color_pair); | ||
27 | |||
28 | int slk_wset(int labnum, const wchar_t *label, int justify); | ||
29 | |||
30 | int PDC_mouse_in_slk(int y, int x); | ||
31 | void PDC_slk_free(void); | ||
32 | void PDC_slk_initialize(void); | ||
33 | |||
34 | wchar_t *slk_wlabel(int labnum) | ||
35 | |||
36 | ### Description | ||
37 | |||
38 | These functions manipulate a window that contain Soft Label Keys | ||
39 | (SLK). To use the SLK functions, a call to slk_init() must be made | ||
40 | BEFORE initscr() or newterm(). slk_init() removes 1 or 2 lines from | ||
41 | the useable screen, depending on the format selected. | ||
42 | |||
43 | The line(s) removed from the screen are used as a separate window, in | ||
44 | which SLKs are displayed. | ||
45 | |||
46 | slk_init() requires a single parameter which describes the format of | ||
47 | the SLKs as follows: | ||
48 | |||
49 | 0 3-2-3 format | ||
50 | 1 4-4 format | ||
51 | 2 4-4-4 format (ncurses extension) | ||
52 | 3 4-4-4 format with index line (ncurses extension) | ||
53 | 2 lines used | ||
54 | 55 5-5 format (pdcurses format) | ||
55 | |||
56 | slk_refresh(), slk_noutrefresh() and slk_touch() are analogous to | ||
57 | refresh(), noutrefresh() and touch(). | ||
58 | |||
59 | ### Return Value | ||
60 | |||
61 | All functions return OK on success and ERR on error. | ||
62 | |||
63 | ### Portability | ||
64 | |||
65 | Function | X/Open | ncurses | NetBSD | ||
66 | :---------------------|:------:|:-------:|:------: | ||
67 | slk_init | Y | Y | Y | ||
68 | slk_set | Y | Y | Y | ||
69 | slk_refresh | Y | Y | Y | ||
70 | slk_noutrefresh | Y | Y | Y | ||
71 | slk_label | Y | Y | Y | ||
72 | slk_clear | Y | Y | Y | ||
73 | slk_restore | Y | Y | Y | ||
74 | slk_touch | Y | Y | Y | ||
75 | slk_attron | Y | Y | Y | ||
76 | slk_attrset | Y | Y | Y | ||
77 | slk_attroff | Y | Y | Y | ||
78 | slk_attr_on | Y | Y | Y | ||
79 | slk_attr_set | Y | Y | Y | ||
80 | slk_attr_off | Y | Y | Y | ||
81 | slk_wset | Y | Y | Y | ||
82 | PDC_mouse_in_slk | - | - | - | ||
83 | PDC_slk_free | - | - | - | ||
84 | PDC_slk_initialize | - | - | - | ||
85 | slk_wlabel | - | - | - | ||
86 | |||
87 | **man-end****************************************************************/ | ||
88 | |||
89 | #include <stdlib.h> | ||
90 | |||
91 | enum { LABEL_NORMAL = 8, LABEL_EXTENDED = 10, LABEL_NCURSES_EXTENDED = 12 }; | ||
92 | |||
93 | static int label_length = 0; | ||
94 | static int labels = 0; | ||
95 | static int label_fmt = 0; | ||
96 | static int label_line = 0; | ||
97 | static bool hidden = FALSE; | ||
98 | |||
99 | static struct SLK { | ||
100 | chtype label[32]; | ||
101 | int len; | ||
102 | int format; | ||
103 | int start_col; | ||
104 | } *slk = (struct SLK *)NULL; | ||
105 | |||
106 | /* slk_init() is the slk initialization routine. | ||
107 | This must be called before initscr(). | ||
108 | |||
109 | label_fmt = 0, 1 or 55. | ||
110 | 0 = 3-2-3 format | ||
111 | 1 = 4 - 4 format | ||
112 | 2 = 4-4-4 format (ncurses extension for PC 12 function keys) | ||
113 | 3 = 4-4-4 format (ncurses extension for PC 12 function keys - | ||
114 | with index line) | ||
115 | 55 = 5 - 5 format (extended for PC, 10 function keys) */ | ||
116 | |||
117 | int slk_init(int fmt) | ||
118 | { | ||
119 | PDC_LOG(("slk_init() - called\n")); | ||
120 | |||
121 | if (SP) | ||
122 | return ERR; | ||
123 | |||
124 | switch (fmt) | ||
125 | { | ||
126 | case 0: /* 3 - 2 - 3 */ | ||
127 | labels = LABEL_NORMAL; | ||
128 | break; | ||
129 | |||
130 | case 1: /* 4 - 4 */ | ||
131 | labels = LABEL_NORMAL; | ||
132 | break; | ||
133 | |||
134 | case 2: /* 4 4 4 */ | ||
135 | labels = LABEL_NCURSES_EXTENDED; | ||
136 | break; | ||
137 | |||
138 | case 3: /* 4 4 4 with index */ | ||
139 | labels = LABEL_NCURSES_EXTENDED; | ||
140 | break; | ||
141 | |||
142 | case 55: /* 5 - 5 */ | ||
143 | labels = LABEL_EXTENDED; | ||
144 | break; | ||
145 | |||
146 | default: | ||
147 | return ERR; | ||
148 | } | ||
149 | |||
150 | label_fmt = fmt; | ||
151 | |||
152 | slk = calloc(labels, sizeof(struct SLK)); | ||
153 | |||
154 | if (!slk) | ||
155 | labels = 0; | ||
156 | |||
157 | return slk ? OK : ERR; | ||
158 | } | ||
159 | |||
160 | /* draw a single button */ | ||
161 | |||
162 | static void _drawone(int num) | ||
163 | { | ||
164 | int i, col, slen; | ||
165 | |||
166 | if (hidden) | ||
167 | return; | ||
168 | |||
169 | slen = slk[num].len; | ||
170 | |||
171 | switch (slk[num].format) | ||
172 | { | ||
173 | case 0: /* LEFT */ | ||
174 | col = 0; | ||
175 | break; | ||
176 | |||
177 | case 1: /* CENTER */ | ||
178 | col = (label_length - slen) / 2; | ||
179 | |||
180 | if (col + slen > label_length) | ||
181 | --col; | ||
182 | break; | ||
183 | |||
184 | default: /* RIGHT */ | ||
185 | col = label_length - slen; | ||
186 | } | ||
187 | |||
188 | wmove(SP->slk_winptr, label_line, slk[num].start_col); | ||
189 | |||
190 | for (i = 0; i < label_length; ++i) | ||
191 | waddch(SP->slk_winptr, (i >= col && i < (col + slen)) ? | ||
192 | slk[num].label[i - col] : ' '); | ||
193 | } | ||
194 | |||
195 | /* redraw each button */ | ||
196 | |||
197 | static void _redraw(void) | ||
198 | { | ||
199 | int i; | ||
200 | |||
201 | for (i = 0; i < labels; ++i) | ||
202 | _drawone(i); | ||
203 | } | ||
204 | |||
205 | /* slk_set() Used to set a slk label to a string. | ||
206 | |||
207 | labnum = 1 - 8 (or 10) (number of the label) | ||
208 | label = string (8 or 7 bytes total), or NULL | ||
209 | justify = 0 : left, 1 : center, 2 : right */ | ||
210 | |||
211 | int slk_set(int labnum, const char *label, int justify) | ||
212 | { | ||
213 | #ifdef PDC_WIDE | ||
214 | wchar_t wlabel[32]; | ||
215 | |||
216 | PDC_mbstowcs(wlabel, label, 31); | ||
217 | return slk_wset(labnum, wlabel, justify); | ||
218 | #else | ||
219 | PDC_LOG(("slk_set() - called\n")); | ||
220 | |||
221 | if (labnum < 1 || labnum > labels || justify < 0 || justify > 2) | ||
222 | return ERR; | ||
223 | |||
224 | labnum--; | ||
225 | |||
226 | if (!label || !(*label)) | ||
227 | { | ||
228 | /* Clear the label */ | ||
229 | |||
230 | *slk[labnum].label = 0; | ||
231 | slk[labnum].format = 0; | ||
232 | slk[labnum].len = 0; | ||
233 | } | ||
234 | else | ||
235 | { | ||
236 | int i, j = 0; | ||
237 | |||
238 | /* Skip leading spaces */ | ||
239 | |||
240 | while (label[j] == ' ') | ||
241 | j++; | ||
242 | |||
243 | /* Copy it */ | ||
244 | |||
245 | for (i = 0; i < label_length; i++) | ||
246 | { | ||
247 | chtype ch = label[i + j]; | ||
248 | |||
249 | slk[labnum].label[i] = ch; | ||
250 | |||
251 | if (!ch) | ||
252 | break; | ||
253 | } | ||
254 | |||
255 | /* Drop trailing spaces */ | ||
256 | |||
257 | while ((i + j) && (label[i + j - 1] == ' ')) | ||
258 | i--; | ||
259 | |||
260 | slk[labnum].label[i] = 0; | ||
261 | slk[labnum].format = justify; | ||
262 | slk[labnum].len = i; | ||
263 | } | ||
264 | |||
265 | _drawone(labnum); | ||
266 | |||
267 | return OK; | ||
268 | #endif | ||
269 | } | ||
270 | |||
271 | int slk_refresh(void) | ||
272 | { | ||
273 | PDC_LOG(("slk_refresh() - called\n")); | ||
274 | |||
275 | return (slk_noutrefresh() == ERR) ? ERR : doupdate(); | ||
276 | } | ||
277 | |||
278 | int slk_noutrefresh(void) | ||
279 | { | ||
280 | PDC_LOG(("slk_noutrefresh() - called\n")); | ||
281 | |||
282 | if (!SP) | ||
283 | return ERR; | ||
284 | |||
285 | return wnoutrefresh(SP->slk_winptr); | ||
286 | } | ||
287 | |||
288 | char *slk_label(int labnum) | ||
289 | { | ||
290 | static char temp[33]; | ||
291 | #ifdef PDC_WIDE | ||
292 | wchar_t *wtemp = slk_wlabel(labnum); | ||
293 | |||
294 | PDC_wcstombs(temp, wtemp, 32); | ||
295 | #else | ||
296 | chtype *p; | ||
297 | int i; | ||
298 | |||
299 | PDC_LOG(("slk_label() - called\n")); | ||
300 | |||
301 | if (labnum < 1 || labnum > labels) | ||
302 | return (char *)0; | ||
303 | |||
304 | for (i = 0, p = slk[labnum - 1].label; *p; i++) | ||
305 | temp[i] = *p++; | ||
306 | |||
307 | temp[i] = '\0'; | ||
308 | #endif | ||
309 | return temp; | ||
310 | } | ||
311 | |||
312 | int slk_clear(void) | ||
313 | { | ||
314 | PDC_LOG(("slk_clear() - called\n")); | ||
315 | |||
316 | if (!SP) | ||
317 | return ERR; | ||
318 | |||
319 | hidden = TRUE; | ||
320 | werase(SP->slk_winptr); | ||
321 | return wrefresh(SP->slk_winptr); | ||
322 | } | ||
323 | |||
324 | int slk_restore(void) | ||
325 | { | ||
326 | PDC_LOG(("slk_restore() - called\n")); | ||
327 | |||
328 | if (!SP) | ||
329 | return ERR; | ||
330 | |||
331 | hidden = FALSE; | ||
332 | _redraw(); | ||
333 | return wrefresh(SP->slk_winptr); | ||
334 | } | ||
335 | |||
336 | int slk_touch(void) | ||
337 | { | ||
338 | PDC_LOG(("slk_touch() - called\n")); | ||
339 | |||
340 | if (!SP) | ||
341 | return ERR; | ||
342 | |||
343 | return touchwin(SP->slk_winptr); | ||
344 | } | ||
345 | |||
346 | int slk_attron(const chtype attrs) | ||
347 | { | ||
348 | int rc; | ||
349 | |||
350 | PDC_LOG(("slk_attron() - called\n")); | ||
351 | |||
352 | if (!SP) | ||
353 | return ERR; | ||
354 | |||
355 | rc = wattron(SP->slk_winptr, attrs); | ||
356 | _redraw(); | ||
357 | |||
358 | return rc; | ||
359 | } | ||
360 | |||
361 | int slk_attr_on(const attr_t attrs, void *opts) | ||
362 | { | ||
363 | PDC_LOG(("slk_attr_on() - called\n")); | ||
364 | |||
365 | return slk_attron(attrs); | ||
366 | } | ||
367 | |||
368 | int slk_attroff(const chtype attrs) | ||
369 | { | ||
370 | int rc; | ||
371 | |||
372 | PDC_LOG(("slk_attroff() - called\n")); | ||
373 | |||
374 | if (!SP) | ||
375 | return ERR; | ||
376 | |||
377 | rc = wattroff(SP->slk_winptr, attrs); | ||
378 | _redraw(); | ||
379 | |||
380 | return rc; | ||
381 | } | ||
382 | |||
383 | int slk_attr_off(const attr_t attrs, void *opts) | ||
384 | { | ||
385 | PDC_LOG(("slk_attr_off() - called\n")); | ||
386 | |||
387 | return slk_attroff(attrs); | ||
388 | } | ||
389 | |||
390 | int slk_attrset(const chtype attrs) | ||
391 | { | ||
392 | int rc; | ||
393 | |||
394 | PDC_LOG(("slk_attrset() - called\n")); | ||
395 | |||
396 | if (!SP) | ||
397 | return ERR; | ||
398 | |||
399 | rc = wattrset(SP->slk_winptr, attrs); | ||
400 | _redraw(); | ||
401 | |||
402 | return rc; | ||
403 | } | ||
404 | |||
405 | int slk_color(short color_pair) | ||
406 | { | ||
407 | int rc; | ||
408 | |||
409 | PDC_LOG(("slk_color() - called\n")); | ||
410 | |||
411 | if (!SP) | ||
412 | return ERR; | ||
413 | |||
414 | rc = wcolor_set(SP->slk_winptr, color_pair, NULL); | ||
415 | _redraw(); | ||
416 | |||
417 | return rc; | ||
418 | } | ||
419 | |||
420 | int slk_attr_set(const attr_t attrs, short color_pair, void *opts) | ||
421 | { | ||
422 | PDC_LOG(("slk_attr_set() - called\n")); | ||
423 | |||
424 | return slk_attrset(attrs | COLOR_PAIR(color_pair)); | ||
425 | } | ||
426 | |||
427 | static void _slk_calc(void) | ||
428 | { | ||
429 | int i, center, col = 0; | ||
430 | label_length = COLS / labels; | ||
431 | |||
432 | if (label_length > 31) | ||
433 | label_length = 31; | ||
434 | |||
435 | switch (label_fmt) | ||
436 | { | ||
437 | case 0: /* 3 - 2 - 3 F-Key layout */ | ||
438 | |||
439 | --label_length; | ||
440 | |||
441 | slk[0].start_col = col; | ||
442 | slk[1].start_col = (col += label_length); | ||
443 | slk[2].start_col = (col += label_length); | ||
444 | |||
445 | center = COLS / 2; | ||
446 | |||
447 | slk[3].start_col = center - label_length + 1; | ||
448 | slk[4].start_col = center + 1; | ||
449 | |||
450 | col = COLS - (label_length * 3) + 1; | ||
451 | |||
452 | slk[5].start_col = col; | ||
453 | slk[6].start_col = (col += label_length); | ||
454 | slk[7].start_col = (col += label_length); | ||
455 | break; | ||
456 | |||
457 | case 1: /* 4 - 4 F-Key layout */ | ||
458 | |||
459 | for (i = 0; i < 8; i++) | ||
460 | { | ||
461 | slk[i].start_col = col; | ||
462 | col += label_length; | ||
463 | |||
464 | if (i == 3) | ||
465 | col = COLS - (label_length * 4) + 1; | ||
466 | } | ||
467 | |||
468 | break; | ||
469 | |||
470 | case 2: /* 4 4 4 F-Key layout */ | ||
471 | case 3: /* 4 4 4 F-Key layout with index */ | ||
472 | |||
473 | for (i = 0; i < 4; i++) | ||
474 | { | ||
475 | slk[i].start_col = col; | ||
476 | col += label_length; | ||
477 | } | ||
478 | |||
479 | center = COLS / 2; | ||
480 | |||
481 | slk[4].start_col = center - (label_length * 2) + 1; | ||
482 | slk[5].start_col = center - label_length + 1; | ||
483 | slk[6].start_col = center + 1; | ||
484 | slk[7].start_col = center + label_length + 1; | ||
485 | |||
486 | col = COLS - (label_length * 4) + 1; | ||
487 | |||
488 | for (i = 8; i < 12; i++) | ||
489 | { | ||
490 | slk[i].start_col = col; | ||
491 | col += label_length; | ||
492 | } | ||
493 | |||
494 | break; | ||
495 | |||
496 | default: /* 5 - 5 F-Key layout */ | ||
497 | |||
498 | for (i = 0; i < 10; i++) | ||
499 | { | ||
500 | slk[i].start_col = col; | ||
501 | col += label_length; | ||
502 | |||
503 | if (i == 4) | ||
504 | col = COLS - (label_length * 5) + 1; | ||
505 | } | ||
506 | } | ||
507 | |||
508 | --label_length; | ||
509 | |||
510 | /* make sure labels are all in window */ | ||
511 | |||
512 | _redraw(); | ||
513 | } | ||
514 | |||
515 | void PDC_slk_initialize(void) | ||
516 | { | ||
517 | if (slk) | ||
518 | { | ||
519 | if (label_fmt == 3) | ||
520 | { | ||
521 | SP->slklines = 2; | ||
522 | label_line = 1; | ||
523 | } | ||
524 | else | ||
525 | SP->slklines = 1; | ||
526 | |||
527 | if (!SP->slk_winptr) | ||
528 | { | ||
529 | SP->slk_winptr = newwin(SP->slklines, COLS, | ||
530 | LINES - SP->slklines, 0); | ||
531 | if (!SP->slk_winptr) | ||
532 | return; | ||
533 | |||
534 | wattrset(SP->slk_winptr, A_REVERSE); | ||
535 | } | ||
536 | |||
537 | _slk_calc(); | ||
538 | |||
539 | /* if we have an index line, display it now */ | ||
540 | |||
541 | if (label_fmt == 3) | ||
542 | { | ||
543 | chtype save_attr; | ||
544 | int i; | ||
545 | |||
546 | save_attr = SP->slk_winptr->_attrs; | ||
547 | wattrset(SP->slk_winptr, A_NORMAL); | ||
548 | wmove(SP->slk_winptr, 0, 0); | ||
549 | whline(SP->slk_winptr, 0, COLS); | ||
550 | |||
551 | for (i = 0; i < labels; i++) | ||
552 | mvwprintw(SP->slk_winptr, 0, slk[i].start_col, "F%d", i + 1); | ||
553 | |||
554 | SP->slk_winptr->_attrs = save_attr; | ||
555 | } | ||
556 | |||
557 | touchwin(SP->slk_winptr); | ||
558 | } | ||
559 | } | ||
560 | |||
561 | void PDC_slk_free(void) | ||
562 | { | ||
563 | if (slk) | ||
564 | { | ||
565 | if (SP->slk_winptr) | ||
566 | { | ||
567 | delwin(SP->slk_winptr); | ||
568 | SP->slk_winptr = (WINDOW *)NULL; | ||
569 | } | ||
570 | |||
571 | free(slk); | ||
572 | slk = (struct SLK *)NULL; | ||
573 | |||
574 | label_length = 0; | ||
575 | labels = 0; | ||
576 | label_fmt = 0; | ||
577 | label_line = 0; | ||
578 | hidden = FALSE; | ||
579 | } | ||
580 | } | ||
581 | |||
582 | int PDC_mouse_in_slk(int y, int x) | ||
583 | { | ||
584 | int i; | ||
585 | |||
586 | PDC_LOG(("PDC_mouse_in_slk() - called: y->%d x->%d\n", y, x)); | ||
587 | |||
588 | /* If the line on which the mouse was clicked is NOT the last line | ||
589 | of the screen, we are not interested in it. */ | ||
590 | |||
591 | if (!slk || !SP->slk_winptr || (y != SP->slk_winptr->_begy + label_line)) | ||
592 | return 0; | ||
593 | |||
594 | for (i = 0; i < labels; i++) | ||
595 | if (x >= slk[i].start_col && x < (slk[i].start_col + label_length)) | ||
596 | return i + 1; | ||
597 | |||
598 | return 0; | ||
599 | } | ||
600 | |||
601 | #ifdef PDC_WIDE | ||
602 | int slk_wset(int labnum, const wchar_t *label, int justify) | ||
603 | { | ||
604 | PDC_LOG(("slk_wset() - called\n")); | ||
605 | |||
606 | if (labnum < 1 || labnum > labels || justify < 0 || justify > 2) | ||
607 | return ERR; | ||
608 | |||
609 | labnum--; | ||
610 | |||
611 | if (!label || !(*label)) | ||
612 | { | ||
613 | /* Clear the label */ | ||
614 | |||
615 | *slk[labnum].label = 0; | ||
616 | slk[labnum].format = 0; | ||
617 | slk[labnum].len = 0; | ||
618 | } | ||
619 | else | ||
620 | { | ||
621 | int i, j = 0; | ||
622 | |||
623 | /* Skip leading spaces */ | ||
624 | |||
625 | while (label[j] == L' ') | ||
626 | j++; | ||
627 | |||
628 | /* Copy it */ | ||
629 | |||
630 | for (i = 0; i < label_length; i++) | ||
631 | { | ||
632 | chtype ch = label[i + j]; | ||
633 | |||
634 | slk[labnum].label[i] = ch; | ||
635 | |||
636 | if (!ch) | ||
637 | break; | ||
638 | } | ||
639 | |||
640 | /* Drop trailing spaces */ | ||
641 | |||
642 | while ((i + j) && (label[i + j - 1] == L' ')) | ||
643 | i--; | ||
644 | |||
645 | slk[labnum].label[i] = 0; | ||
646 | slk[labnum].format = justify; | ||
647 | slk[labnum].len = i; | ||
648 | } | ||
649 | |||
650 | _drawone(labnum); | ||
651 | |||
652 | return OK; | ||
653 | } | ||
654 | |||
655 | wchar_t *slk_wlabel(int labnum) | ||
656 | { | ||
657 | static wchar_t temp[33]; | ||
658 | chtype *p; | ||
659 | int i; | ||
660 | |||
661 | PDC_LOG(("slk_wlabel() - called\n")); | ||
662 | |||
663 | if (labnum < 1 || labnum > labels) | ||
664 | return (wchar_t *)0; | ||
665 | |||
666 | for (i = 0, p = slk[labnum - 1].label; *p; i++) | ||
667 | temp[i] = *p++; | ||
668 | |||
669 | temp[i] = '\0'; | ||
670 | |||
671 | return temp; | ||
672 | } | ||
673 | #endif | ||