drivers/wifi: Remove unnecessary data structure copy
[coreboot2.git] / payloads / libpayload / curses / PDCurses / pdcurses / slk.c
blob1491e2bbf009eb5db962a855c7b392537d5c091a
1 /* Public Domain Curses */
3 #include <curspriv.h>
5 RCSID("$Id: slk.c,v 1.61 2008/07/13 16:08:18 wmcbrine Exp $")
7 /*man-start**************************************************************
9 Name: slk
11 Synopsis:
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);
28 int slk_wset(int labnum, const wchar_t *label, int justify);
30 int PDC_mouse_in_slk(int y, int x);
31 void PDC_slk_free(void);
32 void PDC_slk_initialize(void);
34 wchar_t *slk_wlabel(int labnum)
36 Description:
37 These functions manipulate a window that contain Soft Label Keys
38 (SLK). To use the SLK functions, a call to slk_init() must be
39 made BEFORE initscr() or newterm(). slk_init() removes 1 or 2
40 lines from the useable screen, depending on the format selected.
42 The line(s) removed from the screen are used as a separate
43 window, in which SLKs are displayed.
45 slk_init() requires a single parameter which describes the
46 format of the SLKs as follows:
48 0 3-2-3 format
49 1 4-4 format
50 2 4-4-4 format (ncurses extension)
51 3 4-4-4 format with index line (ncurses extension)
52 2 lines used
53 55 5-5 format (pdcurses format)
55 slk_refresh(), slk_noutrefresh() and slk_touch() are analogous
56 to refresh(), noutrefresh() and touch().
58 Return Value:
59 All functions return OK on success and ERR on error.
61 Portability X/Open BSD SYS V
62 slk_init Y - Y
63 slk_set Y - Y
64 slk_refresh Y - Y
65 slk_noutrefresh Y - Y
66 slk_label Y - Y
67 slk_clear Y - Y
68 slk_restore Y - Y
69 slk_touch Y - Y
70 slk_attron Y - Y
71 slk_attrset Y - Y
72 slk_attroff Y - Y
73 slk_attr_on Y
74 slk_attr_set Y
75 slk_attr_off Y
76 slk_wset Y
77 PDC_mouse_in_slk - - -
78 PDC_slk_free - - -
79 PDC_slk_initialize - - -
80 slk_wlabel - - -
82 **man-end****************************************************************/
84 #include <stdlib.h>
86 enum { LABEL_NORMAL = 8, LABEL_EXTENDED = 10, LABEL_NCURSES_EXTENDED = 12 };
88 static int label_length = 0;
89 static int labels = 0;
90 static int label_fmt = 0;
91 static int label_line = 0;
92 static bool hidden = FALSE;
94 static struct SLK {
95 chtype label[32];
96 int len;
97 int format;
98 int start_col;
99 } *slk = (struct SLK *)NULL;
101 /* slk_init() is the slk initialization routine.
102 This must be called before initscr().
104 label_fmt = 0, 1 or 55.
105 0 = 3-2-3 format
106 1 = 4 - 4 format
107 2 = 4-4-4 format (ncurses extension for PC 12 function keys)
108 3 = 4-4-4 format (ncurses extension for PC 12 function keys -
109 with index line)
110 55 = 5 - 5 format (extended for PC, 10 function keys) */
112 int slk_init(int fmt)
114 PDC_LOG(("slk_init() - called\n"));
116 if (SP)
117 return ERR;
119 switch (fmt)
121 case 0: /* 3 - 2 - 3 */
122 labels = LABEL_NORMAL;
123 break;
125 case 1: /* 4 - 4 */
126 labels = LABEL_NORMAL;
127 break;
129 case 2: /* 4 4 4 */
130 labels = LABEL_NCURSES_EXTENDED;
131 break;
133 case 3: /* 4 4 4 with index */
134 labels = LABEL_NCURSES_EXTENDED;
135 break;
137 case 55: /* 5 - 5 */
138 labels = LABEL_EXTENDED;
139 break;
141 default:
142 return ERR;
145 label_fmt = fmt;
147 slk = calloc(labels, sizeof(struct SLK));
149 if (!slk)
150 labels = 0;
152 return slk ? OK : ERR;
155 /* draw a single button */
157 static void _drawone(int num)
159 int i, col, slen;
161 if (hidden)
162 return;
164 slen = slk[num].len;
166 switch (slk[num].format)
168 case 0: /* LEFT */
169 col = 0;
170 break;
172 case 1: /* CENTER */
173 col = (label_length - slen) / 2;
175 if (col + slen > label_length)
176 --col;
177 break;
179 default: /* RIGHT */
180 col = label_length - slen;
183 wmove(SP->slk_winptr, label_line, slk[num].start_col);
185 for (i = 0; i < label_length; ++i)
186 waddch(SP->slk_winptr, (i >= col && i < (col + slen)) ?
187 slk[num].label[i - col] : ' ');
190 /* redraw each button */
192 static void _redraw(void)
194 int i;
196 for (i = 0; i < labels; ++i)
197 _drawone(i);
200 /* slk_set() Used to set a slk label to a string.
202 labnum = 1 - 8 (or 10) (number of the label)
203 label = string (8 or 7 bytes total), or NULL
204 justify = 0 : left, 1 : center, 2 : right */
206 int slk_set(int labnum, const char *label, int justify)
208 #ifdef PDC_WIDE
209 wchar_t wlabel[32];
211 PDC_mbstowcs(wlabel, label, 31);
212 return slk_wset(labnum, wlabel, justify);
213 #else
214 PDC_LOG(("slk_set() - called\n"));
216 if (labnum < 1 || labnum > labels || justify < 0 || justify > 2)
217 return ERR;
219 labnum--;
221 if (!label || !(*label))
223 /* Clear the label */
225 *slk[labnum].label = 0;
226 slk[labnum].format = 0;
227 slk[labnum].len = 0;
229 else
231 int i, j = 0;
233 /* Skip leading spaces */
235 while (label[j] == ' ')
236 j++;
238 /* Copy it */
240 for (i = 0; i < label_length; i++)
242 chtype ch = label[i + j];
244 slk[labnum].label[i] = ch;
246 if (!ch)
247 break;
250 /* Drop trailing spaces */
252 while ((i + j) && (label[i + j - 1] == ' '))
253 i--;
255 slk[labnum].label[i] = 0;
256 slk[labnum].format = justify;
257 slk[labnum].len = i;
260 _drawone(labnum);
262 return OK;
263 #endif
266 int slk_refresh(void)
268 PDC_LOG(("slk_refresh() - called\n"));
270 return (slk_noutrefresh() == ERR) ? ERR : doupdate();
273 int slk_noutrefresh(void)
275 PDC_LOG(("slk_noutrefresh() - called\n"));
277 return wnoutrefresh(SP->slk_winptr);
280 char *slk_label(int labnum)
282 static char temp[33];
283 #ifdef PDC_WIDE
284 wchar_t *wtemp = slk_wlabel(labnum);
286 PDC_wcstombs(temp, wtemp, 32);
287 #else
288 chtype *p;
289 int i;
291 PDC_LOG(("slk_label() - called\n"));
293 if (labnum < 1 || labnum > labels)
294 return (char *)0;
296 for (i = 0, p = slk[labnum - 1].label; *p; i++)
297 temp[i] = *p++;
299 temp[i] = '\0';
300 #endif
301 return temp;
304 int slk_clear(void)
306 PDC_LOG(("slk_clear() - called\n"));
308 hidden = TRUE;
309 werase(SP->slk_winptr);
310 return wrefresh(SP->slk_winptr);
313 int slk_restore(void)
315 PDC_LOG(("slk_restore() - called\n"));
317 hidden = FALSE;
318 _redraw();
319 return wrefresh(SP->slk_winptr);
322 int slk_touch(void)
324 PDC_LOG(("slk_touch() - called\n"));
326 return touchwin(SP->slk_winptr);
329 int slk_attron(const chtype attrs)
331 int rc;
333 PDC_LOG(("slk_attron() - called\n"));
335 rc = wattron(SP->slk_winptr, attrs);
336 _redraw();
338 return rc;
341 int slk_attr_on(const attr_t attrs, void *opts)
343 PDC_LOG(("slk_attr_on() - called\n"));
345 return slk_attron(attrs);
348 int slk_attroff(const chtype attrs)
350 int rc;
352 PDC_LOG(("slk_attroff() - called\n"));
354 rc = wattroff(SP->slk_winptr, attrs);
355 _redraw();
357 return rc;
360 int slk_attr_off(const attr_t attrs, void *opts)
362 PDC_LOG(("slk_attr_off() - called\n"));
364 return slk_attroff(attrs);
367 int slk_attrset(const chtype attrs)
369 int rc;
371 PDC_LOG(("slk_attrset() - called\n"));
373 rc = wattrset(SP->slk_winptr, attrs);
374 _redraw();
376 return rc;
379 int slk_color(short color_pair)
381 int rc;
383 PDC_LOG(("slk_color() - called\n"));
385 rc = wcolor_set(SP->slk_winptr, color_pair, NULL);
386 _redraw();
388 return rc;
391 int slk_attr_set(const attr_t attrs, short color_pair, void *opts)
393 PDC_LOG(("slk_attr_set() - called\n"));
395 return slk_attrset(attrs | COLOR_PAIR(color_pair));
398 static void _slk_calc(void)
400 int i, center, col = 0;
401 label_length = COLS / labels;
403 if (label_length > 31)
404 label_length = 31;
406 switch (label_fmt)
408 case 0: /* 3 - 2 - 3 F-Key layout */
410 --label_length;
412 slk[0].start_col = col;
413 slk[1].start_col = (col += label_length);
414 slk[2].start_col = (col += label_length);
416 center = COLS / 2;
418 slk[3].start_col = center - label_length + 1;
419 slk[4].start_col = center + 1;
421 col = COLS - (label_length * 3) + 1;
423 slk[5].start_col = col;
424 slk[6].start_col = (col += label_length);
425 slk[7].start_col = (col += label_length);
426 break;
428 case 1: /* 4 - 4 F-Key layout */
430 for (i = 0; i < 8; i++)
432 slk[i].start_col = col;
433 col += label_length;
435 if (i == 3)
436 col = COLS - (label_length * 4) + 1;
439 break;
441 case 2: /* 4 4 4 F-Key layout */
442 case 3: /* 4 4 4 F-Key layout with index */
444 for (i = 0; i < 4; i++)
446 slk[i].start_col = col;
447 col += label_length;
450 center = COLS/2;
452 slk[4].start_col = center - (label_length * 2) + 1;
453 slk[5].start_col = center - label_length - 1;
454 slk[6].start_col = center + 1;
455 slk[7].start_col = center + label_length + 1;
457 col = COLS - (label_length * 4) + 1;
459 for (i = 8; i < 12; i++)
461 slk[i].start_col = col;
462 col += label_length;
465 break;
467 default: /* 5 - 5 F-Key layout */
469 for (i = 0; i < 10; i++)
471 slk[i].start_col = col;
472 col += label_length;
474 if (i == 4)
475 col = COLS - (label_length * 5) + 1;
479 --label_length;
481 /* make sure labels are all in window */
483 _redraw();
486 void PDC_slk_initialize(void)
488 if (slk)
490 if (label_fmt == 3)
492 SP->slklines = 2;
493 label_line = 1;
495 else
496 SP->slklines = 1;
498 if (!SP->slk_winptr)
500 if ( !(SP->slk_winptr = newwin(SP->slklines, COLS,
501 LINES - SP->slklines, 0)) )
502 return;
504 wattrset(SP->slk_winptr, A_REVERSE);
507 _slk_calc();
509 /* if we have an index line, display it now */
511 if (label_fmt == 3)
513 chtype save_attr;
514 int i;
516 save_attr = SP->slk_winptr->_attrs;
517 wattrset(SP->slk_winptr, A_NORMAL);
518 wmove(SP->slk_winptr, 0, 0);
519 whline(SP->slk_winptr, 0, COLS);
521 for (i = 0; i < labels; i++)
522 mvwprintw(SP->slk_winptr, 0, slk[i].start_col, "F%d", i + 1);
524 SP->slk_winptr->_attrs = save_attr;
527 touchwin(SP->slk_winptr);
531 void PDC_slk_free(void)
533 if (slk)
535 if (SP->slk_winptr)
537 delwin(SP->slk_winptr);
538 SP->slk_winptr = (WINDOW *)NULL;
541 free(slk);
542 slk = (struct SLK *)NULL;
544 label_length = 0;
545 labels = 0;
546 label_fmt = 0;
547 label_line = 0;
548 hidden = FALSE;
552 int PDC_mouse_in_slk(int y, int x)
554 int i;
556 PDC_LOG(("PDC_mouse_in_slk() - called: y->%d x->%d\n", y, x));
558 /* If the line on which the mouse was clicked is NOT the last line
559 of the screen, we are not interested in it. */
561 if (!slk || !SP->slk_winptr || (y != SP->slk_winptr->_begy + label_line))
562 return 0;
564 for (i = 0; i < labels; i++)
565 if (x >= slk[i].start_col && x < (slk[i].start_col + label_length))
566 return i + 1;
568 return 0;
571 #ifdef PDC_WIDE
572 int slk_wset(int labnum, const wchar_t *label, int justify)
574 PDC_LOG(("slk_wset() - called\n"));
576 if (labnum < 1 || labnum > labels || justify < 0 || justify > 2)
577 return ERR;
579 labnum--;
581 if (!label || !(*label))
583 /* Clear the label */
585 *slk[labnum].label = 0;
586 slk[labnum].format = 0;
587 slk[labnum].len = 0;
589 else
591 int i, j = 0;
593 /* Skip leading spaces */
595 while (label[j] == L' ')
596 j++;
598 /* Copy it */
600 for (i = 0; i < label_length; i++)
602 chtype ch = label[i + j];
604 slk[labnum].label[i] = ch;
606 if (!ch)
607 break;
610 /* Drop trailing spaces */
612 while ((i + j) && (label[i + j - 1] == L' '))
613 i--;
615 slk[labnum].label[i] = 0;
616 slk[labnum].format = justify;
617 slk[labnum].len = i;
620 _drawone(labnum);
622 return OK;
625 wchar_t *slk_wlabel(int labnum)
627 static wchar_t temp[33];
628 chtype *p;
629 int i;
631 PDC_LOG(("slk_wlabel() - called\n"));
633 if (labnum < 1 || labnum > labels)
634 return (wchar_t *)0;
636 for (i = 0, p = slk[labnum - 1].label; *p; i++)
637 temp[i] = *p++;
639 temp[i] = '\0';
641 return temp;
643 #endif