xref: /illumos-gate/usr/src/boot/forth/menu.4th (revision 1fa2a66491e7d8ae0be84e7da4da8e812480c710)
1\ Copyright (c) 2003 Scott Long <scottl@FreeBSD.org>
2\ Copyright (c) 2003 Aleksander Fafula <alex@fafula.com>
3\ Copyright (c) 2006-2015 Devin Teske <dteske@FreeBSD.org>
4\ Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
5\ All rights reserved.
6\
7\ Redistribution and use in source and binary forms, with or without
8\ modification, are permitted provided that the following conditions
9\ are met:
10\ 1. Redistributions of source code must retain the above copyright
11\    notice, this list of conditions and the following disclaimer.
12\ 2. Redistributions in binary form must reproduce the above copyright
13\    notice, this list of conditions and the following disclaimer in the
14\    documentation and/or other materials provided with the distribution.
15\
16\ THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17\ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18\ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19\ ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20\ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21\ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22\ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23\ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24\ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25\ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26\ SUCH DAMAGE.
27
28marker task-menu.4th
29
30\ Frame drawing
31include /boot/forth/frames.4th
32
33vocabulary menu-infrastructure
34vocabulary menu-namespace
35vocabulary menu-command-helpers
36
37only forth also menu-infrastructure definitions
38
39f_double        \ Set frames to double (see frames.4th). Replace with
40                \ f_single if you want single frames.
4146 constant dot \ ASCII definition of a period (in decimal)
42
43 5 constant menu_default_x         \ default column position of timeout
4410 constant menu_default_y         \ default row position of timeout msg
45 4 constant menu_timeout_default_x \ default column position of timeout
4623 constant menu_timeout_default_y \ default row position of timeout msg
4710 constant menu_timeout_default   \ default timeout (in seconds)
48
49\ Customize the following values with care
50
51  1 constant menu_start \ Numerical prefix of first menu item
52dot constant bullet     \ Menu bullet (appears after numerical prefix)
53  5 constant menu_x     \ Row position of the menu (from the top)
54 10 constant menu_y     \ Column position of the menu (from left side)
55
56\ Menu Appearance
57variable menuidx   \ Menu item stack for number prefixes
58variable menurow   \ Menu item stack for positioning
59variable menubllt  \ Menu item bullet
60
61\ Menu Positioning
62variable menuX     \ Menu X offset (columns)
63variable menuY     \ Menu Y offset (rows)
64
65\ Menu-item elements
66variable menurebootadded
67
68\ Menu timer [count-down] variables
69variable menu_timeout_enabled \ timeout state (internal use only)
70variable menu_time            \ variable for tracking the passage of time
71variable menu_timeout         \ determined configurable delay duration
72variable menu_timeout_x       \ column position of timeout message
73variable menu_timeout_y       \ row position of timeout message
74
75only forth also menu-namespace definitions
76
77\ Menu-item key association/detection
78variable menukey1
79variable menukey2
80variable menukey3
81variable menukey4
82variable menukey5
83variable menukey6
84variable menukey7
85variable menukey8
86variable menureboot
87variable menuacpi
88variable menuosconsole
89variable menukmdb
90variable menuoptions
91
92\ Menu initialization status variables
93variable init_state1
94variable init_state2
95variable init_state3
96variable init_state4
97variable init_state5
98variable init_state6
99variable init_state7
100variable init_state8
101
102\ Boolean option status variables
103variable toggle_state1
104variable toggle_state2
105variable toggle_state3
106variable toggle_state4
107variable toggle_state5
108variable toggle_state6
109variable toggle_state7
110variable toggle_state8
111
112\ Array option status variables
113variable cycle_state1
114variable cycle_state2
115variable cycle_state3
116variable cycle_state4
117variable cycle_state5
118variable cycle_state6
119variable cycle_state7
120variable cycle_state8
121
122\ Containers for storing the initial caption text
123create init_text1 64 allot
124create init_text2 64 allot
125create init_text3 64 allot
126create init_text4 64 allot
127create init_text5 64 allot
128create init_text6 64 allot
129create init_text7 64 allot
130create init_text8 64 allot
131
132only forth definitions
133
134: arch-i386? ( -- BOOL ) \ Returns TRUE (-1) on i386, FALSE (0) otherwise.
135	s" arch-i386" environment? dup if
136		drop
137	then
138;
139
140: acpipresent? ( -- flag ) \ Returns TRUE if ACPI is present, FALSE otherwise
141	s" hint.acpi.0.rsdp" getenv
142	dup -1 = if
143		drop false exit
144	then
145	2drop
146	true
147;
148
149: acpienabled? ( -- flag ) \ Returns TRUE if ACPI is enabled, FALSE otherwise
150	s" hint.acpi.0.disabled" getenv
151	dup -1 <> if
152		s" 0" compare 0<> if
153			false exit
154		then
155	else
156		drop
157	then
158	true
159;
160
161: +c! ( N C-ADDR/U K -- C-ADDR/U )
162	3 pick 3 pick	( n c-addr/u k -- n c-addr/u k n c-addr )
163	rot + c!	( n c-addr/u k n c-addr -- n c-addr/u )
164	rot drop	( n c-addr/u -- c-addr/u )
165;
166
167only forth also menu-namespace definitions
168
169\ Forth variables
170: namespace     ( C-ADDR/U N -- ) also menu-namespace +c! evaluate previous ;
171: menukeyN      ( N -- ADDR )   s" menukeyN"       7 namespace ;
172: init_stateN   ( N -- ADDR )   s" init_stateN"   10 namespace ;
173: toggle_stateN ( N -- ADDR )   s" toggle_stateN" 12 namespace ;
174: cycle_stateN  ( N -- ADDR )   s" cycle_stateN"  11 namespace ;
175: init_textN    ( N -- C-ADDR ) s" init_textN"     9 namespace ;
176
177\ Environment variables
178: menu_init[x]       ( N -- C-ADDR/U )   s" menu_init[x]"       10 +c! ;
179: menu_command[x]    ( N -- C-ADDR/U )   s" menu_command[x]"    13 +c! ;
180: menu_caption[x]    ( N -- C-ADDR/U )   s" menu_caption[x]"    13 +c! ;
181: ansi_caption[x]    ( N -- C-ADDR/U )   s" ansi_caption[x]"    13 +c! ;
182: menu_keycode[x]    ( N -- C-ADDR/U )   s" menu_keycode[x]"    13 +c! ;
183: toggled_text[x]    ( N -- C-ADDR/U )   s" toggled_text[x]"    13 +c! ;
184: toggled_ansi[x]    ( N -- C-ADDR/U )   s" toggled_ansi[x]"    13 +c! ;
185: menu_caption[x][y] ( N M -- C-ADDR/U ) s" menu_caption[x][y]" 16 +c! 13 +c! ;
186: ansi_caption[x][y] ( N M -- C-ADDR/U ) s" ansi_caption[x][y]" 16 +c! 13 +c! ;
187
188also menu-infrastructure definitions
189
190\ This function prints a menu item at menuX (row) and menuY (column), returns
191\ the incremental decimal ASCII value associated with the menu item, and
192\ increments the cursor position to the next row for the creation of the next
193\ menu item. This function is called by the menu-create function. You need not
194\ call it directly.
195\
196: printmenuitem ( menu_item_str -- ascii_keycode )
197
198	loader_color? if [char] ^ escc! then
199
200	menurow dup @ 1+ swap ! ( increment menurow )
201	menuidx dup @ 1+ swap ! ( increment menuidx )
202
203	\ Calculate the menuitem row position
204	menurow @ menuY @ +
205
206	\ Position the cursor at the menuitem position
207	dup menuX @ swap at-xy
208
209	\ Print the value of menuidx
210	loader_color? dup ( -- bool bool )
211	if b then
212	menuidx @ .
213	if me then
214
215	\ Move the cursor forward 1 column
216	dup menuX @ 1+ swap at-xy
217
218	menubllt @ emit	\ Print the menu bullet using the emit function
219
220	\ Move the cursor to the 3rd column from the current position
221	\ to allow for a space between the numerical prefix and the
222	\ text caption
223	menuX @ 3 + swap at-xy
224
225	\ Print the menu caption (we expect a string to be on the stack
226	\ prior to invoking this function)
227	type
228
229	\ Here we will add the ASCII decimal of the numerical prefix
230	\ to the stack (decimal ASCII for `1' is 49) as a "return value"
231	menuidx @ 48 +
232;
233
234: delim? ( C -- BOOL )
235	dup  32 =		( c -- c bool )		\ [sp] space
236	over  9 = or		( c bool -- c bool )	\ [ht] horizontal tab
237	over 10 = or		( c bool -- c bool )	\ [nl] newline
238	over 13 = or		( c bool -- c bool )	\ [cr] carriage return
239	over [char] , =	or	( c bool -- c bool )	\ comma
240	swap drop		( c bool -- bool )	\ return boolean
241;
242
243\ illumos kernel acpi-user-options has following values:
244\ default:	0 - system will enable acpi based on bios date
245\ on:		1 - acpi is set on
246\ off:		2 - acpi is set off
247\ madt:		4 - use only MADT
248\ legacy:	8 - use legacy mode
249
250: acpi-captions ( N -- )
251  \ first entry
252  dup s" [A]CPI................ default" rot 48 menu_caption[x][y] setenv
253  dup s" ^[1mA^[mCPI.............. ^[32;7mdefault^[m" rot 48 ansi_caption[x][y] setenv
254
255  dup s" [A]CPI................ On" rot 49 menu_caption[x][y] setenv
256  dup s" ^[1mA^[mCPI.............. ^[34;1mOn^[m" rot 49 ansi_caption[x][y] setenv
257
258  dup s" [A]CPI................ Off" rot 50 menu_caption[x][y] setenv
259  dup s" ^[1mA^[mCPI.............. ^[34;1mOff^[m" rot 50 ansi_caption[x][y] setenv
260
261  dup s" [A]CPI................ MADT" rot 51 menu_caption[x][y] setenv
262  dup s" ^[1mA^[mCPI.............. ^[34;1mMADT^[m" rot 51 ansi_caption[x][y] setenv
263
264  dup s" [A]CPI................ Legacy" rot 52 menu_caption[x][y] setenv
265  s" ^[1mA^[mCPI.............. ^[34;1mLegacy^[m" rot 52 ansi_caption[x][y] setenv
266;
267
268\ illumos console has following values:
269\ text, ttya, ttyb, ttyc, ttyd
270
271: osconsole-captions ( N -- )
272  \ first entry
273  dup s" Os[C]onsole........... text" rot 48 menu_caption[x][y] setenv
274  dup s" Os^[1mC^[monsole............ ^[32;7mtext^[m" rot 48 ansi_caption[x][y] setenv
275
276  dup s" Os[C]onsole........... ttya" rot 49 menu_caption[x][y] setenv
277  dup s" Os^[1mC^[monsole............ ^[34;1mttya^[m" rot 49 ansi_caption[x][y] setenv
278
279  dup s" Os[C]onsole........... ttyb" rot 50 menu_caption[x][y] setenv
280  dup s" Os^[1mC^[monsole............ ^[34;1mttyb^[m" rot 50 ansi_caption[x][y] setenv
281
282  dup s" Os[C]onsole........... ttyc" rot 51 menu_caption[x][y] setenv
283  dup s" Os^[1mC^[monsole............ ^[34;1mttyc^[m" rot 51 ansi_caption[x][y] setenv
284
285  dup s" Os[C]onsole........... ttyd" rot 52 menu_caption[x][y] setenv
286  s" Os^[1mC^[monsole............ ^[34;1mttyd^[m" rot 52 ansi_caption[x][y] setenv
287;
288
289\ kmdb options are as follows
290\ default:	0 - disabled
291\		1 - boot with -k option
292\		2 - as 1 + configure NMI to drop to kmdb
293\		3 - boot with -k and -d options
294\		4 - as 3 + configure NMI to drop to kmdb
295
296: kmdb-captions ( N -- )
297  \ first entry
298  dup s" [k]mdb Mode........... Off" rot 48 menu_caption[x][y] setenv
299  dup s" ^[1mk^[mmdb Mode............. ^[34;1mOff^[m" rot 48 ansi_caption[x][y] setenv
300
301  dup s" [k]mdb Mode........... Loaded" rot 49 menu_caption[x][y] setenv
302  dup s" ^[1mk^[mmdb Mode............. ^[32;7mLoaded^[m" rot 49 ansi_caption[x][y] setenv
303
304  dup s" [k]mdb Mode........... On NMI" rot 50 menu_caption[x][y] setenv
305  dup s" ^[1mk^[mmdb Mode............. ^[32;7mOn NMI^[m" rot 50 ansi_caption[x][y] setenv
306
307  dup s" [k]mdb Mode........... On Boot" rot 51 menu_caption[x][y] setenv
308  dup s" ^[1mk^[mmdb Mode............. ^[32;7mOn Boot^[m" rot 51 ansi_caption[x][y] setenv
309
310  dup s" [k]mdb Mode........... On Boot/NMI" rot 52 menu_caption[x][y] setenv
311  s" ^[1mk^[mmdb Mode............. ^[32;7mOn Boot/NMI^[m" rot 52 ansi_caption[x][y] setenv
312;
313
314: set-captions ( x y - x y )
315	\ Set the current non-ANSI caption
316	2dup swap dup ( x y -- x y y x x )
317	s" set menu_caption[x]=$menu_caption[x][y]"
318	17 +c! 34 +c! 37 +c! evaluate
319	( x y y x x c-addr/u -- x y  )
320
321	\ Set the current ANSI caption
322	2dup swap dup ( x y -- x y y x x )
323	s" set ansi_caption[x]=$ansi_caption[x][y]"
324	17 +c! 34 +c! 37 +c! evaluate
325	( x y y x x c-addr/u -- x y )
326;
327
328\ This function creates the list of menu items. This function is called by the
329\ menu-display function. You need not call it directly.
330\
331: menu-create ( -- )
332
333	\ Print the frame caption at (x,y)
334	s" loader_menu_title" getenv dup -1 = if
335		drop s" Welcome to illumos"
336	then
337	TRUE ( use default alignment )
338	s" loader_menu_title_align" getenv dup -1 <> if
339		2dup s" left" compare-insensitive 0= if ( 1 )
340			2drop ( c-addr/u ) drop ( bool )
341			menuX @ menuY @ 1-
342			FALSE ( don't use default alignment )
343		else ( 1 ) 2dup s" right" compare-insensitive 0= if ( 2 )
344			2drop ( c-addr/u ) drop ( bool )
345			menuX @ 42 + 4 - over - menuY @ 1-
346			FALSE ( don't use default alignment )
347		else ( 2 ) 2drop ( c-addr/u ) then ( 1 ) then
348	else
349		drop ( getenv cruft )
350	then
351	if ( use default center alignement? )
352		menuX @ 19 + over 2 / - menuY @ 1-
353	then
354	at-xy type
355
356	\ If $menu_init is set, evaluate it (allowing for whole menus to be
357	\ constructed dynamically -- as this function could conceivably set
358	\ the remaining environment variables to construct the menu entirely).
359	\
360	s" menu_init" getenv dup -1 <> if
361		evaluate
362	else
363		drop
364	then
365
366	\ Print our menu options with respective key/variable associations.
367	\ `printmenuitem' ends by adding the decimal ASCII value for the
368	\ numerical prefix to the stack. We store the value left on the stack
369	\ to the key binding variable for later testing against a character
370	\ captured by the `getkey' function.
371
372	\ Note that any menu item beyond 9 will have a numerical prefix on the
373	\ screen consisting of the first digit (ie. 1 for the tenth menu item)
374	\ and the key required to activate that menu item will be the decimal
375	\ ASCII of 48 plus the menu item (ie. 58 for the tenth item, aka. `:')
376	\ which is misleading and not desirable.
377	\
378	\ Thus, we do not allow more than 8 configurable items on the menu
379	\ (with "Reboot" as the optional ninth and highest numbered item).
380
381	\
382	\ Initialize the OsConsole option status.
383	\
384	0 menuosconsole !
385	s" menu_osconsole" getenv -1 <> if
386		c@ dup 48 > over 57 < and if ( '1' <= c1 <= '8' )
387			dup menuosconsole !
388			dup osconsole-captions
389
390			s" init_osconsole" evaluate
391
392			\ Get the current cycle state (entry to use)
393			s" osconsole_state" evaluate @ 48 + ( n -- n y )
394
395			set-captions
396
397			\ Initialize cycle state from stored value
398			48 - ( n y -- n k )
399			s" init_cyclestate" evaluate ( n k -- n )
400
401			\ Set $os_console
402			s" activate_osconsole" evaluate ( n -- n )
403		then
404		drop
405	then
406
407	\
408	\ Initialize the ACPI option status.
409	\
410	0 menuacpi !
411	s" menu_acpi" getenv -1 <> if
412		c@ dup 48 > over 57 < and if ( '1' <= c1 <= '8' )
413			dup menuacpi !
414			dup acpi-captions
415
416			s" init_acpi" evaluate
417
418			\ Get the current cycle state (entry to use)
419			s" acpi_state" evaluate @ 48 + ( n -- n y )
420
421			set-captions
422
423			\ Initialize cycle state from stored value
424			48 - ( n y -- n k )
425			s" init_cyclestate" evaluate ( n k -- n )
426
427			\ Set $acpi-user-options
428			s" activate_acpi" evaluate ( n -- n )
429		then
430		drop
431	then
432
433	\
434	\ Initialize the kmdb option status.
435	\
436	0 menukmdb !
437	s" menu_kmdb" getenv -1 <> if
438		c@ dup 48 > over 57 < and if ( '1' <= c1 <= '8' )
439			dup menukmdb !
440			dup kmdb-captions
441
442			s" init_kmdb" evaluate
443
444			\ Get the current cycle state (entry to use)
445			s" kmdb_state" evaluate @ 48 + ( n -- n y )
446
447			set-captions
448
449			\ Initialize cycle state from stored value
450			48 - ( n y -- n k )
451			s" init_cyclestate" evaluate ( n k -- n )
452
453			\ Activate the current option
454			s" activate_kmdb" evaluate ( n -- n )
455		then
456		drop
457	then
458
459	\
460	\ Initialize the menu_options visual separator.
461	\
462	0 menuoptions !
463	s" menu_options" getenv -1 <> if
464		c@ dup 48 > over 57 < and if ( '1' <= c1 <= '8' )
465			menuoptions !
466		else
467			drop
468		then
469	then
470
471	\ Initialize "Reboot" menu state variable (prevents double-entry)
472	false menurebootadded !
473
474	menu_start
475	1- menuidx !    \ Initialize the starting index for the menu
476	0 menurow !     \ Initialize the starting position for the menu
477
478	49 \ Iterator start (loop range 49 to 56; ASCII '1' to '8')
479	begin
480		\ If the "Options:" separator, print it.
481		dup menuoptions @ = if
482			\ Optionally add a reboot option to the menu
483			s" menu_reboot" getenv -1 <> if
484				drop
485				s" Reboot" printmenuitem menureboot !
486				true menurebootadded !
487			then
488
489			menuX @
490			menurow @ 2 + menurow !
491			menurow @ menuY @ +
492			at-xy
493			s" menu_optionstext" getenv dup -1 <> if
494				type
495			else
496				drop ." Options:"
497			then
498		then
499
500		\ make sure we have not already initialized this item
501		dup init_stateN dup @ 0= if
502			1 swap !
503
504			\ If this menuitem has an initializer, run it
505			dup menu_init[x]
506			getenv dup -1 <> if
507				evaluate
508			else
509				drop
510			then
511		else
512			drop
513		then
514
515		dup
516		loader_color? if
517			ansi_caption[x]
518		else
519			menu_caption[x]
520		then
521
522		dup -1 <> if
523			\ test for environment variable
524			getenv dup -1 <> if
525				printmenuitem ( c-addr/u -- n )
526				dup menukeyN !
527			else
528				drop
529			then
530		else
531			drop
532		then
533
534		1+ dup 56 > \ add 1 to iterator, continue if less than 57
535	until
536	drop \ iterator
537
538	\ Optionally add a reboot option to the menu
539	menurebootadded @ true <> if
540		s" menu_reboot" getenv -1 <> if
541			drop       \ no need for the value
542			s" Reboot" \ menu caption (required by printmenuitem)
543
544			printmenuitem
545			menureboot !
546		else
547			0 menureboot !
548		then
549	then
550;
551
552\ Takes an integer on the stack and updates the timeout display.
553\
554: menu-timeout-update ( N -- )
555
556	\ Enforce minimum
557	dup 0 < if drop 0 then
558
559	menu_timeout_x @ menu_timeout_y @ at-xy \ position cursor
560
561	dup 0> if
562		s" Autoboot in " type
563		dup . s" second" type
564		1 > if [char] s emit then
565		s" . [Space] to pause " type
566	else
567		drop 40 spaces \ erase message
568	then
569
570	at-bl
571;
572
573\ This function blocks program flow (loops forever) until a key is pressed.
574\ The key that was pressed is added to the top of the stack in the form of its
575\ decimal ASCII representation. This function is called by the menu-display
576\ function. You need not call it directly.
577\ note, the esc sequences will be dropped, this needs to be changed if
578\ menu is built based on arrow keys.
579\
580: getkey ( -- ascii_keycode )
581
582	begin \ loop forever
583
584		menu_timeout_enabled @ 1 = if
585			( -- )
586			seconds ( get current time: -- N )
587			dup menu_time @ <> if ( has time elapsed?: N N N -- N )
588
589				\ At least 1 second has elapsed since last loop
590				\ so we will decrement our "timeout" (really a
591				\ counter, insuring that we do not proceed too
592				\ fast) and update our timeout display.
593
594				menu_time ! ( update time record: N -- )
595				menu_timeout @ ( "time" remaining: -- N )
596				dup 0> if ( greater than 0?: N N 0 -- N )
597					1- ( decrement counter: N -- N )
598					dup menu_timeout !
599						( re-assign: N N Addr -- N )
600				then
601				( -- N )
602
603				dup 0= swap 0< or if ( N <= 0?: N N -- )
604					\ halt the timer
605					0 menu_timeout ! ( 0 Addr -- )
606					0 menu_timeout_enabled ! ( 0 Addr -- )
607				then
608
609				\ update the timer display ( N -- )
610				menu_timeout @ menu-timeout-update
611
612				menu_timeout @ 0= if
613					\ We've reached the end of the timeout
614					\ (user did not cancel by pressing ANY
615					\ key)
616
617					s" menu_timeout_command"  getenv dup
618					-1 = if
619						drop \ clean-up
620					else
621						evaluate
622					then
623				then
624
625			else ( -- N )
626				\ No [detectable] time has elapsed (in seconds)
627				drop ( N -- )
628			then
629			( -- )
630		then
631
632		key? if \ Was a key pressed? (see loader(8))
633
634			\ An actual key was pressed (if the timeout is running,
635			\ kill it regardless of which key was pressed)
636			menu_timeout @ 0<> if
637				0 menu_timeout !
638				0 menu_timeout_enabled !
639
640				\ clear screen of timeout message
641				0 menu-timeout-update
642			then
643
644			\ get the key that was pressed and exit (if we
645			\ get a non-zero ASCII code)
646			key dup 0<> if
647				dup 0x1b = if
648					key? if ( is it sequence? )
649						drop
650						begin
651							key?
652						while
653							key drop
654						repeat
655					else
656						exit
657					then
658				else
659					exit
660				then
661			else
662				drop
663			then
664		then
665		50 ms \ sleep for 50 milliseconds (see loader(8))
666
667	again
668;
669
670: menu-erase ( -- ) \ Erases menu and resets positioning variable to position 1.
671
672	\ Clear the screen area associated with the interactive menu
673	menuX @ menuY @
674	2dup at-xy 38 spaces 1+		2dup at-xy 38 spaces 1+
675	2dup at-xy 38 spaces 1+		2dup at-xy 38 spaces 1+
676	2dup at-xy 38 spaces 1+		2dup at-xy 38 spaces 1+
677	2dup at-xy 38 spaces 1+		2dup at-xy 38 spaces 1+
678	2dup at-xy 38 spaces 1+		2dup at-xy 38 spaces 1+
679	2dup at-xy 38 spaces 1+		2dup at-xy 38 spaces
680	2drop
681
682	\ Reset the starting index and position for the menu
683	menu_start 1- menuidx !
684	0 menurow !
685;
686
687only forth
688also menu-infrastructure
689also menu-namespace
690also menu-command-helpers definitions
691
692: toggle_menuitem ( N -- N ) \ toggles caption text and internal menuitem state
693
694	\ ASCII numeral equal to user-selected menu item must be on the stack.
695	\ We do not modify the stack, so the ASCII numeral is left on top.
696
697	dup init_textN c@ 0= if
698		\ NOTE: no need to check toggle_stateN since the first time we
699		\ are called, we will populate init_textN. Further, we don't
700		\ need to test whether menu_caption[x] (ansi_caption[x] when
701		\ loader_color?=1) is available since we would not have been
702		\ called if the caption was NULL.
703
704		\ base name of environment variable
705		dup ( n -- n n ) \ key pressed
706		loader_color? if
707			ansi_caption[x]
708		else
709			menu_caption[x]
710		then
711		getenv dup -1 <> if
712
713			2 pick ( n c-addr/u -- n c-addr/u n )
714			init_textN ( n c-addr/u n -- n c-addr/u c-addr )
715
716			\ now we have the buffer c-addr on top
717			\ ( followed by c-addr/u of current caption )
718
719			\ Copy the current caption into our buffer
720			2dup c! -rot \ store strlen at first byte
721			begin
722				rot 1+    \ bring alt addr to top and increment
723				-rot -rot \ bring buffer addr to top
724				2dup c@ swap c! \ copy current character
725				1+     \ increment buffer addr
726				rot 1- \ bring buffer len to top and decrement
727				dup 0= \ exit loop if buffer len is zero
728			until
729			2drop \ buffer len/addr
730			drop  \ alt addr
731
732		else
733			drop
734		then
735	then
736
737	\ Now we are certain to have init_textN populated with the initial
738	\ value of menu_caption[x] (ansi_caption[x] with loader_color enabled).
739	\ We can now use init_textN as the untoggled caption and
740	\ toggled_text[x] (toggled_ansi[x] with loader_color enabled) as the
741	\ toggled caption and store the appropriate value into menu_caption[x]
742	\ (again, ansi_caption[x] with loader_color enabled). Last, we'll
743	\ negate the toggled state so that we reverse the flow on subsequent
744	\ calls.
745
746	dup toggle_stateN @ 0= if
747		\ state is OFF, toggle to ON
748
749		dup ( n -- n n ) \ key pressed
750		loader_color? if
751			toggled_ansi[x]
752		else
753			toggled_text[x]
754		then
755		getenv dup -1 <> if
756			\ Assign toggled text to menu caption
757			2 pick ( n c-addr/u -- n c-addr/u n ) \ key pressed
758			loader_color? if
759				ansi_caption[x]
760			else
761				menu_caption[x]
762			then
763			setenv
764		else
765			\ No toggled text, keep the same caption
766			drop ( n -1 -- n ) \ getenv cruft
767		then
768
769		true \ new value of toggle state var (to be stored later)
770	else
771		\ state is ON, toggle to OFF
772
773		dup init_textN count ( n -- n c-addr/u )
774
775		\ Assign init_textN text to menu caption
776		2 pick ( n c-addr/u -- n c-addr/u n ) \ key pressed
777		loader_color? if
778			ansi_caption[x]
779		else
780			menu_caption[x]
781		then
782		setenv
783
784		false \ new value of toggle state var (to be stored below)
785	then
786
787	\ now we'll store the new toggle state (on top of stack)
788	over toggle_stateN !
789;
790
791: cycle_menuitem ( N -- N ) \ cycles through array of choices for a menuitem
792
793	\ ASCII numeral equal to user-selected menu item must be on the stack.
794	\ We do not modify the stack, so the ASCII numeral is left on top.
795
796	dup cycle_stateN dup @ 1+ \ get value and increment
797
798	\ Before assigning the (incremented) value back to the pointer,
799	\ let's test for the existence of this particular array element.
800	\ If the element exists, we'll store index value and move on.
801	\ Otherwise, we'll loop around to zero and store that.
802
803	dup 48 + ( n addr k -- n addr k k' )
804	         \ duplicate array index and convert to ASCII numeral
805
806	3 pick swap ( n addr k k' -- n addr k n k' ) \ (n,k') as (x,y)
807	loader_color? if
808		ansi_caption[x][y]
809	else
810		menu_caption[x][y]
811	then
812	( n addr k n k' -- n addr k c-addr/u )
813
814	\ Now test for the existence of our incremented array index in the
815	\ form of $menu_caption[x][y] ($ansi_caption[x][y] with loader_color
816	\ enabled) as set in loader.rc(5), et. al.
817
818	getenv dup -1 = if
819		\ No caption set for this array index. Loop back to zero.
820
821		drop ( n addr k -1 -- n addr k ) \ getenv cruft
822		drop 0 ( n addr k -- n addr 0 )  \ new value to store later
823
824		2 pick [char] 0 ( n addr 0 -- n addr 0 n 48 ) \ (n,48) as (x,y)
825		loader_color? if
826			ansi_caption[x][y]
827		else
828			menu_caption[x][y]
829		then
830		( n addr 0 n 48 -- n addr 0 c-addr/u )
831		getenv dup -1 = if
832			\ Highly unlikely to occur, but to ensure things move
833			\ along smoothly, allocate a temporary NULL string
834			drop ( cruft ) s" "
835		then
836	then
837
838	\ At this point, we should have the following on the stack (in order,
839	\ from bottom to top):
840	\
841	\    n        - Ascii numeral representing the menu choice (inherited)
842	\    addr     - address of our internal cycle_stateN variable
843	\    k        - zero-based number we intend to store to the above
844	\    c-addr/u - string value we intend to store to menu_caption[x]
845	\               (or ansi_caption[x] with loader_color enabled)
846	\
847	\ Let's perform what we need to with the above.
848
849	\ Assign array value text to menu caption
850	4 pick ( n addr k c-addr/u -- n addr k c-addr/u n )
851	loader_color? if
852		ansi_caption[x]
853	else
854		menu_caption[x]
855	then
856	setenv
857
858	swap ! ( n addr k -- n ) \ update array state variable
859;
860
861only forth definitions also menu-infrastructure
862
863\ Erase and redraw the menu. Useful if you change a caption and want to
864\ update the menu to reflect the new value.
865\
866: menu-redraw ( -- )
867	menu-erase
868	menu-create
869;
870
871: menu-box ( -- )
872	\ Interpret a custom frame type for the menu
873	TRUE ( draw a box? default yes, but might be altered below )
874	s" loader_menu_frame" getenv dup -1 = if ( 1 )
875		drop \ no custom frame type
876	else ( 1 )  2dup s" single" compare-insensitive 0= if ( 2 )
877		f_single ( see frames.4th )
878	else ( 2 )  2dup s" double" compare-insensitive 0= if ( 3 )
879		f_double ( see frames.4th )
880	else ( 3 ) s" none" compare-insensitive 0= if ( 4 )
881		drop FALSE \ don't draw a box
882	( 4 ) then ( 3 ) then ( 2 )  then ( 1 ) then
883	if
884		42 13 menuX @ 3 - menuY @ 1- box \ Draw frame (w,h,x,y)
885	then
886;
887
888\ This function initializes the menu. Call this from your `loader.rc' file
889\ before calling any other menu-related functions.
890\
891: menu-init ( -- )
892	menu_start
893	1- menuidx !    \ Initialize the starting index for the menu
894	0 menurow !     \ Initialize the starting position for the menu
895
896	\ Assign configuration values
897	s" loader_menu_y" getenv dup -1 = if
898		drop \ no custom row position
899		menu_default_y
900	else
901		\ make sure custom position is a number
902		?number 0= if
903			menu_default_y \ or use default
904		then
905	then
906	menuY !
907	s" loader_menu_x" getenv dup -1 = if
908		drop \ no custom column position
909		menu_default_x
910	else
911		\ make sure custom position is a number
912		?number 0= if
913			menu_default_x \ or use default
914		then
915	then
916	menuX !
917
918	['] menu-box console-iterate
919	at-bl
920;
921
922also menu-namespace
923
924\ Main function. Call this from your `loader.rc' file.
925\
926: menu-display ( -- )
927
928	0 menu_timeout_enabled ! \ start with automatic timeout disabled
929
930	\ check indication that automatic execution after delay is requested
931	s" menu_timeout_command" getenv -1 <> if ( Addr C -1 -- | Addr )
932		drop ( just testing existence right now: Addr -- )
933
934		\ initialize state variables
935		seconds menu_time ! ( store the time we started )
936		1 menu_timeout_enabled ! ( enable automatic timeout )
937
938		\ read custom time-duration (if set)
939		s" autoboot_delay" getenv dup -1 = if
940			drop \ no custom duration (remove dup'd bunk -1)
941			menu_timeout_default \ use default setting
942		else
943			2dup ?number 0= if ( if not a number )
944				\ disable timeout if "NO", else use default
945				s" NO" compare-insensitive 0= if
946					0 menu_timeout_enabled !
947					0 ( assigned to menu_timeout below )
948				else
949					menu_timeout_default
950				then
951			else
952				-rot 2drop
953
954				\ boot immediately if less than zero
955				dup 0< if
956					drop
957					menu-create
958					at-bl
959					0 boot
960				then
961			then
962		then
963		menu_timeout ! ( store value on stack from above )
964
965		menu_timeout_enabled @ 1 = if
966			\ read custom column position (if set)
967			s" loader_menu_timeout_x" getenv dup -1 = if
968				drop \ no custom column position
969				menu_timeout_default_x \ use default setting
970			else
971				\ make sure custom position is a number
972				?number 0= if
973					menu_timeout_default_x \ or use default
974				then
975			then
976			menu_timeout_x ! ( store value on stack from above )
977
978			\ read custom row position (if set)
979			s" loader_menu_timeout_y" getenv dup -1 = if
980				drop \ no custom row position
981				menu_timeout_default_y \ use default setting
982			else
983				\ make sure custom position is a number
984				?number 0= if
985					menu_timeout_default_y \ or use default
986				then
987			then
988			menu_timeout_y ! ( store value on stack from above )
989		then
990	then
991
992	menu-create
993
994	begin \ Loop forever
995
996		at-bl
997		getkey     \ Block here, waiting for a key to be pressed
998
999		dup -1 = if
1000			drop exit \ Caught abort (abnormal return)
1001		then
1002
1003		\ Boot if the user pressed Enter/Ctrl-M (13) or
1004		\ Ctrl-Enter/Ctrl-J (10)
1005		dup over 13 = swap 10 = or if
1006			drop ( no longer needed )
1007			s" boot" evaluate
1008			exit ( pedantic; never reached )
1009		then
1010
1011		dup menureboot @ = if 0 reboot then
1012
1013		\ Evaluate the decimal ASCII value against known menu item
1014		\ key associations and act accordingly
1015
1016		49 \ Iterator start (loop range 49 to 56; ASCII '1' to '8')
1017		begin
1018			dup menukeyN @
1019			rot tuck = if
1020
1021				\ Adjust for missing ACPI menuitem on non-i386
1022\				arch-i386? true <> menuacpi @ 0<> and if
1023\					menuacpi @ over 2dup < -rot = or
1024\					over 58 < and if
1025\					( key >= menuacpi && key < 58: N -- N )
1026\						1+
1027\					then
1028\				then
1029
1030				\ Test for the environment variable
1031				dup menu_command[x]
1032				getenv dup -1 <> if
1033					\ Execute the stored procedure
1034					evaluate
1035
1036					\ We expect there to be a non-zero
1037					\  value left on the stack after
1038					\ executing the stored procedure.
1039					\ If so, continue to run, else exit.
1040
1041					0= if
1042						drop \ key pressed
1043						drop \ loop iterator
1044						exit
1045					else
1046						swap \ need iterator on top
1047					then
1048				then
1049
1050				\ Re-adjust for missing ACPI menuitem
1051\				arch-i386? true <> menuacpi @ 0<> and if
1052\					swap
1053\					menuacpi @ 1+ over 2dup < -rot = or
1054\					over 59 < and if
1055\						1-
1056\					then
1057\					swap
1058\				then
1059			else
1060				swap \ need iterator on top
1061			then
1062
1063			\
1064			\ Check for menu keycode shortcut(s)
1065			\
1066			dup menu_keycode[x]
1067			getenv dup -1 = if
1068				drop
1069			else
1070				?number 0<> if
1071					rot tuck = if
1072						swap
1073						dup menu_command[x]
1074						getenv dup -1 <> if
1075							evaluate
1076							0= if
1077								2drop
1078								exit
1079							then
1080						else
1081							drop
1082						then
1083					else
1084						swap
1085					then
1086				then
1087			then
1088
1089			1+ dup 56 > \ increment iterator
1090			            \ continue if less than 57
1091		until
1092		drop \ loop iterator
1093		drop \ key pressed
1094
1095	again	\ Non-operational key was pressed; repeat
1096;
1097
1098\ This function unsets all the possible environment variables associated with
1099\ creating the interactive menu.
1100\
1101: menu-unset ( -- )
1102
1103	49 \ Iterator start (loop range 49 to 56; ASCII '1' to '8')
1104	begin
1105		dup menu_init[x]    unsetenv	\ menu initializer
1106		dup menu_command[x] unsetenv	\ menu command
1107		dup menu_caption[x] unsetenv	\ menu caption
1108		dup ansi_caption[x] unsetenv	\ ANSI caption
1109		dup menu_keycode[x] unsetenv	\ menu keycode
1110		dup toggled_text[x] unsetenv	\ toggle_menuitem caption
1111		dup toggled_ansi[x] unsetenv	\ toggle_menuitem ANSI caption
1112
1113		48 \ Iterator start (inner range 48 to 57; ASCII '0' to '9')
1114		begin
1115			\ cycle_menuitem caption and ANSI caption
1116			2dup menu_caption[x][y] unsetenv
1117			2dup ansi_caption[x][y] unsetenv
1118			1+ dup 57 >
1119		until
1120		drop \ inner iterator
1121
1122		0 over menukeyN      !	\ used by menu-create, menu-display
1123		0 over init_stateN   !	\ used by menu-create
1124		0 over toggle_stateN !	\ used by toggle_menuitem
1125		0 over init_textN   c!	\ used by toggle_menuitem
1126		0 over cycle_stateN  !	\ used by cycle_menuitem
1127
1128		1+ dup 56 >	\ increment, continue if less than 57
1129	until
1130	drop \ iterator
1131
1132	s" menu_timeout_command" unsetenv	\ menu timeout command
1133	s" menu_reboot"          unsetenv	\ Reboot menu option flag
1134	s" menu_acpi"            unsetenv	\ ACPI menu option flag
1135	s" menu_kmdb"            unsetenv	\ kmdb menu option flag
1136	s" menu_osconsole"       unsetenv	\ osconsole menu option flag
1137	s" menu_options"         unsetenv	\ Options separator flag
1138	s" menu_optionstext"     unsetenv	\ separator display text
1139	s" menu_init"            unsetenv	\ menu initializer
1140
1141	0 menureboot !
1142	0 menuacpi !
1143	0 menukmdb !
1144	0 menuosconsole !
1145	0 menuoptions !
1146;
1147
1148only forth definitions also menu-infrastructure
1149
1150\ This function both unsets menu variables and visually erases the menu area
1151\ in-preparation for another menu.
1152\
1153: menu-clear ( -- )
1154	menu-unset
1155	menu-erase
1156;
1157
1158bullet menubllt !
1159
1160also menu-namespace
1161
1162\ Initialize our menu initialization state variables
11630 init_state1 !
11640 init_state2 !
11650 init_state3 !
11660 init_state4 !
11670 init_state5 !
11680 init_state6 !
11690 init_state7 !
11700 init_state8 !
1171
1172\ Initialize our boolean state variables
11730 toggle_state1 !
11740 toggle_state2 !
11750 toggle_state3 !
11760 toggle_state4 !
11770 toggle_state5 !
11780 toggle_state6 !
11790 toggle_state7 !
11800 toggle_state8 !
1181
1182\ Initialize our array state variables
11830 cycle_state1 !
11840 cycle_state2 !
11850 cycle_state3 !
11860 cycle_state4 !
11870 cycle_state5 !
11880 cycle_state6 !
11890 cycle_state7 !
11900 cycle_state8 !
1191
1192\ Initialize string containers
11930 init_text1 c!
11940 init_text2 c!
11950 init_text3 c!
11960 init_text4 c!
11970 init_text5 c!
11980 init_text6 c!
11990 init_text7 c!
12000 init_text8 c!
1201
1202only forth definitions
1203