Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/*	$NetBSD: internals.c,v 1.17 2013/10/18 19:53:59 christos Exp $	*/

/*-
 * Copyright (c) 1998-1999 Brett Lymn (blymn@baea.com.au, brett_lymn@yahoo.com.au)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 */

#include <sys/cdefs.h>
__RCSID("$NetBSD: internals.c,v 1.17 2013/10/18 19:53:59 christos Exp $");

#include <menu.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "internals.h"

/* internal function prototypes */
static void
_menui_calc_neighbours(MENU *menu, int item_no);
static void _menui_redraw_menu(MENU *menu, int old_top_row, int old_cur_item);

  /*
   * Link all the menu items together to speed up navigation.  We need
   * to calculate the widest item entry, then work out how many columns
   * of items the window will accommodate and then how many rows there will
   * be.  Once the layout is determined the neighbours of each item is
   * calculated and the item structures updated.
   */
int
_menui_stitch_items(MENU *menu)
{
	int i, row_major;

	row_major = ((menu->opts & O_ROWMAJOR) == O_ROWMAJOR);

	if (menu->posted == 1)
		return E_POSTED;
	if (menu->items == NULL)
		return E_BAD_ARGUMENT;

	menu->item_rows = menu->item_count / menu->cols;
	menu->item_cols = menu->cols;
	if (menu->item_count > (menu->item_rows * menu->item_cols))
		menu->item_rows += 1;

	_menui_max_item_size(menu);

	for (i = 0; i < menu->item_count; i++) {
		  /* fill in the row and column value of the item */
		if (row_major) {
			menu->items[i]->row = i / menu->item_cols;
			menu->items[i]->col = i % menu->item_cols;
		} else {
			menu->items[i]->row = i % menu->item_rows;
			menu->items[i]->col = i / menu->item_rows;
		}

		_menui_calc_neighbours(menu, i);
	}

	return E_OK;
}

  /*
   * Calculate the neighbours for an item in menu.
   */
static void
_menui_calc_neighbours(MENU *menu, int item_no)
{
	int neighbour, cycle, row_major, edge;
	ITEM *item;

	row_major = ((menu->opts & O_ROWMAJOR) == O_ROWMAJOR);
	cycle = ((menu->opts & O_NONCYCLIC) != O_NONCYCLIC);
	item = menu->items[item_no];

	if (menu->item_rows < 2) {
		if (cycle) {
			item->up = item;
			item->down = item;
		} else {
			item->up = NULL;
			item->down = NULL;
		}
	} else {

		/* up */
		if (menu->item_cols < 2) {
			if (item_no == 0) {
				if (cycle)
					item->up =
					    menu->items[menu->item_count - 1];
				else
					item->up = NULL;
			} else
				item->up = menu->items[item_no - 1];
		} else {
			edge = 0;
			if (row_major) {
				if (item->row == 0) {
					neighbour =
				    	(menu->item_rows - 1) * menu->item_cols
						+ item->col;
					if (neighbour >= menu->item_count)
						neighbour -= menu->item_cols;
					edge = 1;
				} else
					neighbour = item_no - menu->item_cols;
			} else {
				if (item->row == 0) {
					neighbour = menu->item_rows * item->col
						+ menu->item_rows - 1;
					if (neighbour >= menu->item_count)
						neighbour = menu->item_count - 1;
					edge = 1;
				} else
					neighbour = item_no - 1;
			}


			item->up = menu->items[neighbour];
			if ((!cycle) && (edge == 1))
				item->up = NULL;
		}

		/* Down */
		if (menu->item_cols < 2) {
			if (item_no == (menu->item_count - 1)) {
				if (cycle)
					item->down = menu->items[0];
				else
					item->down = NULL;
			} else
				item->down = menu->items[item_no + 1];
		} else {
			edge = 0;
			if (row_major) {
				if (item->row == menu->item_rows - 1) {
					neighbour = item->col;
					edge = 1;
				} else {
					neighbour = item_no + menu->item_cols;
					if (neighbour >= menu->item_count) {
						neighbour = item->col;
						edge = 1;
					}
				}
			} else {
				if (item->row == menu->item_rows - 1) {
					neighbour = item->col * menu->item_rows;
					edge = 1;
				} else {
					neighbour = item_no + 1;
					if (neighbour >= menu->item_count) {
						neighbour = item->col
						    * menu->item_rows;
						edge = 1;
					}
				}
			}

			item->down = menu->items[neighbour];
			if ((!cycle) && (edge == 1))
				item->down = NULL;
		}
	}

	if (menu->item_cols < 2) {
		if (cycle) {
			item->left = item;
			item->right = item;
		} else {
			item->left = NULL;
			item->right = NULL;
		}
	} else {
		/* left */
		if (menu->item_rows < 2) {
			if (item_no == 0) {
				if (cycle)
					item->left =
					    menu->items[menu->item_count - 1];
				else
					item->left = NULL;
			} else
				item->left = menu->items[item_no - 1];
		} else {
			edge = 0;
			if (row_major) {
				if (item->col == 0) {
					neighbour = item_no + menu->cols - 1;
					if (neighbour >= menu->item_count)
						neighbour = menu->item_count - 1;
					edge = 1;
				} else
					neighbour = item_no - 1;
			} else {
				if (item->col == 0) {
					neighbour = menu->item_rows
					    * (menu->item_cols - 1) + item->row;
					if (neighbour >= menu->item_count)
						neighbour -= menu->item_rows;
					edge = 1;
				} else
					neighbour = item_no - menu->item_rows;
			}

			item->left = menu->items[neighbour];
			if ((!cycle) && (edge == 1))
				item->left = NULL;
		}

		/* right */
		if (menu->item_rows < 2) {
			if (item_no == menu->item_count - 1) {
				if (cycle)
					item->right = menu->items[0];
				else
					item->right = NULL;
			} else
				item->right = menu->items[item_no + 1];
		} else {
			edge = 0;
			if (row_major) {
				if (item->col == menu->item_cols - 1) {
					neighbour = item_no - menu->item_cols
					    + 1;
					edge = 1;
				} else if (item_no == menu->item_count - 1) {
					neighbour = item->row * menu->item_cols;
					edge = 1;
				} else
					neighbour = item_no + 1;
			} else {
				if (item->col == menu->item_cols - 1) {
					neighbour = item->row;
					edge = 1;
				} else {
					neighbour = item_no + menu->item_rows;
					if (neighbour >= menu->item_count) {
						neighbour = item->row;
						edge = 1;
					}
				}
			}

			item->right = menu->items[neighbour];
			if ((!cycle) && (edge == 1))
				item->right = NULL;
		}
	}
}

/*
 * Goto the item pointed to by item and adjust the menu structure
 * accordingly.  Call the term and init functions if required.
 */
int
_menui_goto_item(MENU *menu, ITEM *item, int new_top_row)
{
	int old_top_row = menu->top_row, old_cur_item = menu->cur_item;

	  /* If we get a null then the menu is not cyclic so deny request */
	if (item == NULL)
		return E_REQUEST_DENIED;

	menu->in_init = 1;
	if (menu->top_row != new_top_row) {
		if ((menu->posted == 1) && (menu->menu_term != NULL))
			menu->menu_term(menu);
		menu->top_row = new_top_row;

		if ((menu->posted == 1) && (menu->menu_init != NULL))
			menu->menu_init(menu);
	}

	  /* this looks like wasted effort but it can happen.... */
	if (menu->cur_item != item->index) {

		if ((menu->posted == 1) && (menu->item_term != NULL))
			menu->item_term(menu);

		menu->cur_item = item->index;
		menu->cur_row = item->row;
		menu->cur_col = item->col;

		if (menu->posted == 1)
			_menui_redraw_menu(menu, old_top_row, old_cur_item);

		if ((menu->posted == 1) && (menu->item_init != NULL))
			menu->item_init(menu);

	}

	menu->in_init = 0;
	return E_OK;
}

/*
 * Attempt to match items with the pattern buffer in the direction given
 * by iterating over the menu items.  If a match is found return E_OK
 * otherwise return E_NO_MATCH
 */
int
_menui_match_items(MENU *menu, int direction, int *item_matched)
{
	int i, caseless;

	caseless = ((menu->opts & O_IGNORECASE) == O_IGNORECASE);

	i = menu->cur_item;
	if (direction == MATCH_NEXT_FORWARD) {
		if (++i >= menu->item_count) i = 0;
	} else if (direction == MATCH_NEXT_REVERSE) {
		if (--i < 0) i = menu->item_count - 1;
	}


	do {
		if (menu->items[i]->name.length >= menu->plen) {
			  /* no chance if pattern is longer */
			if (caseless) {
				if (strncasecmp(menu->items[i]->name.string,
						menu->pattern,
						(size_t) menu->plen) == 0) {
					*item_matched = i;
					menu->match_len = menu->plen;
					return E_OK;
				}
			} else {
				if (strncmp(menu->items[i]->name.string,
					    menu->pattern,
					    (size_t) menu->plen) == 0) {
					*item_matched = i;
					menu->match_len = menu->plen;
					return E_OK;
				}
			}
		}

		if ((direction == MATCH_FORWARD) ||
		    (direction == MATCH_NEXT_FORWARD)) {
			if (++i >= menu->item_count) i = 0;
		} else {
			if (--i <= 0) i = menu->item_count - 1;
		}
	} while (i != menu->cur_item);

	menu->match_len = 0; /* match did not succeed - kill the match len. */
	return E_NO_MATCH;
}

/*
 * Attempt to match the pattern buffer against the items.  If c is a
 * printable character then add it to the pattern buffer prior to
 * performing the match.  Direction determines the direction of matching.
 * If the match is successful update the item_matched variable with the
 * index of the item that matched the pattern.
 */
int
_menui_match_pattern(MENU *menu, int c, int direction, int *item_matched)
{
	if (menu == NULL)
		return E_BAD_ARGUMENT;
	if (menu->items == NULL)
		return E_BAD_ARGUMENT;
	if (*menu->items == NULL)
		return E_BAD_ARGUMENT;

	if (isprint(c)) {
		  /* add char to buffer - first allocate room for it */
		if ((menu->pattern = (char *)
		     realloc(menu->pattern,
			     menu->plen + sizeof(char) +
			     ((menu->plen > 0)? 0 : 1)))
		    == NULL)
			return E_SYSTEM_ERROR;
		menu->pattern[menu->plen] = c;
		menu->pattern[++menu->plen] = '\0';

		  /* there is no chance of a match if pattern is longer
		     than all the items */
		if (menu->plen >= menu->max_item_width) {
			menu->pattern[--menu->plen] = '\0';
			return E_NO_MATCH;
		}

		if (_menui_match_items(menu, direction,
					item_matched) == E_NO_MATCH) {
			menu->pattern[--menu->plen] = '\0';
			return E_NO_MATCH;
		} else
			return E_OK;
	} else {
		if (_menui_match_items(menu, direction,
					item_matched) == E_OK) {
			return E_OK;
		} else {
			return E_NO_MATCH;
		}
	}
}

/*
 * Draw an item in the subwindow complete with appropriate highlighting.
 */
void
_menui_draw_item(MENU *menu, int item)
{
	int j, pad_len, mark_len;

	mark_len = max(menu->mark.length, menu->unmark.length);

	wmove(menu->scrwin,
	      menu->items[item]->row - menu->top_row,
	      menu->items[item]->col * (menu->col_width + 1));

	if (menu->cur_item == item)
		wattrset(menu->scrwin, menu->fore);
	if ((menu->items[item]->opts & O_SELECTABLE) != O_SELECTABLE)
		wattron(menu->scrwin, menu->grey);

	  /* deal with the menu mark, if  one is set.
	   * We mark the selected items and write blanks for
	   * all others unless the menu unmark string is set in which
	   * case the unmark string is written.
	   */
	if ((menu->items[item]->selected == 1) ||
	    (((menu->opts & O_ONEVALUE) == O_ONEVALUE) &&
		(menu->cur_item == item))) {
		if (menu->mark.string != NULL) {
			for (j = 0; j < menu->mark.length; j++) {
				waddch(menu->scrwin,
				       menu->mark.string[j]);
			}
		}
		  /* blank any length difference between mark & unmark */
		for (j = menu->mark.length; j < mark_len; j++)
			waddch(menu->scrwin, ' ');
	} else {
		if (menu->unmark.string != NULL) {
			for (j = 0; j < menu->unmark.length; j++) {
				waddch(menu->scrwin,
				       menu->unmark.string[j]);
			}
		}
		  /* blank any length difference between mark & unmark */
		for (j = menu->unmark.length; j < mark_len; j++)
			waddch(menu->scrwin, ' ');
	}

	  /* add the menu name */
	for (j=0; j < menu->items[item]->name.length; j++)
		waddch(menu->scrwin,
		       menu->items[item]->name.string[j]);

	pad_len = menu->col_width - menu->items[item]->name.length
		- mark_len - 1;
	if ((menu->opts & O_SHOWDESC) == O_SHOWDESC) {
		pad_len -= menu->items[item]->description.length - 1;
		for (j = 0; j < pad_len; j++)
			waddch(menu->scrwin, menu->pad);
		for (j = 0; j < menu->items[item]->description.length; j++) {
			waddch(menu->scrwin,
			       menu->items[item]->description.string[j]);
		}
	} else {
		for (j = 0; j < pad_len; j++)
			waddch(menu->scrwin, ' ');
	}
	menu->items[item]->visible = 1;

	  /* kill any special attributes... */
	wattrset(menu->scrwin, menu->back);

	  /*
	   * Fill in the spacing between items, annoying but it looks
	   * odd if the menu items are inverse because the spacings do not
	   * have the same attributes as the items.
	   */
	if ((menu->items[item]->col > 0) &&
	    (menu->items[item]->col < (menu->item_cols - 1))) {
		wmove(menu->scrwin,
		      menu->items[item]->row - menu->top_row,
		      menu->items[item]->col * (menu->col_width + 1) - 1);
		waddch(menu->scrwin, ' ');
	}

	  /* and position the cursor nicely */
	pos_menu_cursor(menu);
}

/*
 * Draw the menu in the subwindow provided.
 */
int
_menui_draw_menu(MENU *menu)
{
	int rowmajor, i, j, k, row = -1, stride;
	int incr, cur_row, offset, row_count;

	rowmajor = ((menu->opts & O_ROWMAJOR) == O_ROWMAJOR);

	if (rowmajor) {
		stride = 1;
		incr = menu->item_cols;
	} else {
		stride = menu->item_rows;
		incr = 1;
	}
	row_count = 0;

	for (i = 0;  i < menu->item_count; i += incr) {
		if (menu->items[i]->row == menu->top_row)
			break;
		row_count++;
		for (j = 0; j < menu->item_cols; j++) {
			offset = j * stride + i;
			if (offset >= menu->item_count)
				break; /* done */
			menu->items[offset]->visible = 0;
		}
	}

	wmove(menu->scrwin, 0, 0);

	menu->col_width = getmaxx(menu->scrwin) / menu->cols;

	for (cur_row = 0; cur_row < menu->rows; cur_row++) {
		for (j = 0; j < menu->cols; j++) {
			offset = j * stride + i;
			if (offset >= menu->item_count) {
			   /* no more items to draw, write background blanks */
				wattrset(menu->scrwin, menu->back);
				if (row < 0) {
					row = menu->items[menu->item_count - 1]->row;
				}

				wmove(menu->scrwin, cur_row,
				      j * (menu->col_width + 1));
				for (k = 0; k < menu->col_width; k++)
					waddch(menu->scrwin, ' ');
			} else {
				_menui_draw_item(menu, offset);
			}
		}

		i += incr;
		row_count++;
	}

	if (row_count < menu->item_rows) {
		for (cur_row = row_count;  cur_row < menu->item_rows; cur_row++) {
			for (j = 0; j < menu->item_cols; j++) {
				offset = j * stride + i;
				if (offset >= menu->item_count)
					break; /* done */
				menu->items[offset]->visible = 0;
			}
			i += incr;
		}
	}

	return E_OK;
}


/*
 * Calculate the widest menu item and stash it in the menu struct.
 *
 */
void
_menui_max_item_size(MENU *menu)
{
	int i, with_desc, width;

	with_desc = ((menu->opts & O_SHOWDESC) == O_SHOWDESC);

	for (i = 0; i < menu->item_count; i++) {
		width = menu->items[i]->name.length
			+ max(menu->mark.length, menu->unmark.length);
		if (with_desc)
			width += menu->items[i]->description.length + 1;

		menu->max_item_width = max(menu->max_item_width, width);
	}
}


/*
 * Redraw the menu on the screen.  If the current item has changed then
 * unhighlight the old item and highlight the new one.
 */
static void
_menui_redraw_menu(MENU *menu, int old_top_row, int old_cur_item)
{

	if (menu->top_row != old_top_row) {
		  /* top row changed - redo the whole menu
		   * XXXX this could be improved if we had wscrl implemented.

		   * XXXX we could scroll the window and just fill in the
		   * XXXX changed lines.
		   */
		wclear(menu->scrwin);
		_menui_draw_menu(menu);
	} else {
		if (menu->cur_item != old_cur_item) {
			  /* redo the old item as a normal one. */
			_menui_draw_item(menu, old_cur_item);
		}
		  /* and then redraw the current item */
		_menui_draw_item(menu, menu->cur_item);
	}
}