]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/lua/drawer.lua
MFV r331695, 331700: 9166 zfs storage pool checkpoint
[FreeBSD/FreeBSD.git] / stand / lua / drawer.lua
1 --
2 -- SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 --
4 -- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org>
5 -- Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
6 -- All rights reserved.
7 --
8 -- Redistribution and use in source and binary forms, with or without
9 -- modification, are permitted provided that the following conditions
10 -- are met:
11 -- 1. Redistributions of source code must retain the above copyright
12 --    notice, this list of conditions and the following disclaimer.
13 -- 2. Redistributions in binary form must reproduce the above copyright
14 --    notice, this list of conditions and the following disclaimer in the
15 --    documentation and/or other materials provided with the distribution.
16 --
17 -- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 -- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 -- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 -- ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 -- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 -- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 -- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 -- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 -- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 -- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 -- SUCH DAMAGE.
28 --
29 -- $FreeBSD$
30 --
31
32 local color = require("color")
33 local config = require("config")
34 local core = require("core")
35 local screen = require("screen")
36
37 local drawer = {}
38
39 local fbsd_logo
40 local beastie_color
41 local beastie
42 local fbsd_logo_v
43 local orb_color
44 local orb
45 local none
46
47 local function menuEntryName(drawing_menu, entry)
48         local name_handler = drawer.menu_name_handlers[entry.entry_type]
49
50         if name_handler ~= nil then
51                 return name_handler(drawing_menu, entry)
52         end
53         if type(entry.name) == "function" then
54                 return entry.name()
55         end
56         return entry.name
57 end
58
59 fbsd_logo = {
60 "  ______               ____   _____ _____  ",
61 " |  ____|             |  _ \\ / ____|  __ \\ ",
62 " | |___ _ __ ___  ___ | |_) | (___ | |  | |",
63 " |  ___| '__/ _ \\/ _ \\|  _ < \\___ \\| |  | |",
64 " | |   | | |  __/  __/| |_) |____) | |__| |",
65 " | |   | | |    |    ||     |      |      |",
66 " |_|   |_|  \\___|\\___||____/|_____/|_____/ "
67 }
68
69 beastie_color = {
70 "               \027[31m,        ,",
71 "              /(        )`",
72 "              \\ \\___   / |",
73 "              /- \027[37m_\027[31m  `-/  '",
74 "             (\027[37m/\\/ \\\027[31m \\   /\\",
75 "             \027[37m/ /   |\027[31m `    \\",
76 "             \027[34mO O   \027[37m) \027[31m/    |",
77 "             \027[37m`-^--'\027[31m`<     '",
78 "            (_.)  _  )   /",
79 "             `.___/`    /",
80 "               `-----' /",
81 "  \027[33m<----.\027[31m     __ / __   \\",
82 "  \027[33m<----|====\027[31mO)))\027[33m==\027[31m) \\) /\027[33m====|",
83 "  \027[33m<----'\027[31m    `--' `.__,' \\",
84 "               |        |",
85 "                \\       /       /\\",
86 "           \027[36m______\027[31m( (_  / \\______/",
87 "         \027[36m,'  ,-----'   |",
88 "         `--{__________)\027[37m"
89 }
90
91 beastie = {
92 "               ,        ,",
93 "              /(        )`",
94 "              \\ \\___   / |",
95 "              /- _  `-/  '",
96 "             (/\\/ \\ \\   /\\",
97 "             / /   | `    \\",
98 "             O O   ) /    |",
99 "             `-^--'`<     '",
100 "            (_.)  _  )   /",
101 "             `.___/`    /",
102 "               `-----' /",
103 "  <----.     __ / __   \\",
104 "  <----|====O)))==) \\) /====|",
105 "  <----'    `--' `.__,' \\",
106 "               |        |",
107 "                \\       /       /\\",
108 "           ______( (_  / \\______/",
109 "         ,'  ,-----'   |",
110 "         `--{__________)"
111 }
112
113 fbsd_logo_v = {
114 "  ______",
115 " |  ____| __ ___  ___ ",
116 " | |__ | '__/ _ \\/ _ \\",
117 " |  __|| | |  __/  __/",
118 " | |   | | |    |    |",
119 " |_|   |_|  \\___|\\___|",
120 "  ____   _____ _____",
121 " |  _ \\ / ____|  __ \\",
122 " | |_) | (___ | |  | |",
123 " |  _ < \\___ \\| |  | |",
124 " | |_) |____) | |__| |",
125 " |     |      |      |",
126 " |____/|_____/|_____/"
127 }
128
129 orb_color = {
130 "  \027[31m```                        \027[31;1m`\027[31m",
131 " s` `.....---...\027[31;1m....--.```   -/\027[31m",
132 " +o   .--`         \027[31;1m/y:`      +.\027[31m",
133 "  yo`:.            \027[31;1m:o      `+-\027[31m",
134 "   y/               \027[31;1m-/`   -o/\027[31m",
135 "  .-                  \027[31;1m::/sy+:.\027[31m",
136 "  /                     \027[31;1m`--  /\027[31m",
137 " `:                          \027[31;1m:`\027[31m",
138 " `:                          \027[31;1m:`\027[31m",
139 "  /                          \027[31;1m/\027[31m",
140 "  .-                        \027[31;1m-.\027[31m",
141 "   --                      \027[31;1m-.\027[31m",
142 "    `:`                  \027[31;1m`:`",
143 "      \027[31;1m.--             `--.",
144 "         .---.....----.\027[37m"
145 }
146
147 orb = {
148 "  ```                        `",
149 " s` `.....---.......--.```   -/",
150 " +o   .--`         /y:`      +.",
151 "  yo`:.            :o      `+-",
152 "   y/               -/`   -o/",
153 "  .-                  ::/sy+:.",
154 "  /                     `--  /",
155 " `:                          :`",
156 " `:                          :`",
157 "  /                          /",
158 "  .-                        -.",
159 "   --                      -.",
160 "    `:`                  `:`",
161 "      .--             `--.",
162 "         .---.....----."
163 }
164
165 none = {""}
166
167 -- Module exports
168 drawer.menu_name_handlers = {
169         -- Menu name handlers should take the menu being drawn and entry being
170         -- drawn as parameters, and return the name of the item.
171         -- This is designed so that everything, including menu separators, may
172         -- have their names derived differently. The default action for entry
173         -- types not specified here is to use entry.name directly.
174         [core.MENU_SEPARATOR] = function(_, entry)
175                 if entry.name ~= nil then
176                         if type(entry.name) == "function" then
177                                 return entry.name()
178                         end
179                         return entry.name
180                 end
181                 return ""
182         end,
183         [core.MENU_CAROUSEL_ENTRY] = function(_, entry)
184                 local carid = entry.carousel_id
185                 local caridx = config.getCarouselIndex(carid)
186                 local choices = entry.items
187                 if type(choices) == "function" then
188                         choices = choices()
189                 end
190                 if #choices < caridx then
191                         caridx = 1
192                 end
193                 return entry.name(caridx, choices[caridx], choices)
194         end,
195 }
196
197 drawer.brand_position = {x = 2, y = 1}
198 drawer.logo_position = {x = 46, y = 4}
199 drawer.menu_position = {x = 5, y = 10}
200 drawer.frame_size = {w = 42, h = 13}
201 drawer.default_shift = {x = 0, y = 0}
202 drawer.shift = drawer.default_shift
203
204 drawer.branddefs = {
205         -- Indexed by valid values for loader_brand in loader.conf(5). Valid
206         -- keys are: graphic (table depicting graphic)
207         ["fbsd"] = {
208                 graphic = fbsd_logo,
209         },
210         ["none"] = {
211                 graphic = none,
212         },
213 }
214
215 drawer.logodefs = {
216         -- Indexed by valid values for loader_logo in loader.conf(5). Valid keys
217         -- are: requires_color (boolean), graphic (table depicting graphic), and
218         -- shift (table containing x and y).
219         ["beastie"] = {
220                 requires_color = true,
221                 graphic = beastie_color,
222         },
223         ["beastiebw"] = {
224                 graphic = beastie,
225         },
226         ["fbsdbw"] = {
227                 graphic = fbsd_logo_v,
228                 shift = {x = 5, y = 4},
229         },
230         ["orb"] = {
231                 requires_color = true,
232                 graphic = orb_color,
233                 shift = {x = 2, y = 4},
234         },
235         ["orbbw"] = {
236                 graphic = orb,
237                 shift = {x = 2, y = 4},
238         },
239         ["tribute"] = {
240                 graphic = fbsd_logo,
241         },
242         ["tributebw"] = {
243                 graphic = fbsd_logo,
244         },
245         ["none"] = {
246                 graphic = none,
247                 shift = {x = 17, y = 0},
248         },
249 }
250
251 drawer.frame_styles = {
252         -- Indexed by valid values for loader_menu_frame in loader.conf(5).
253         -- All of the keys appearing below must be set for any menu frame style
254         -- added to drawer.frame_styles.
255         ["ascii"] = {
256                 horizontal      = "-",
257                 vertical        = "|",
258                 top_left        = "+",
259                 bottom_left     = "+",
260                 top_right       = "+",
261                 bottom_right    = "+",
262         },
263         ["single"] = {
264                 horizontal      = "\xC4",
265                 vertical        = "\xB3",
266                 top_left        = "\xDA",
267                 bottom_left     = "\xC0",
268                 top_right       = "\xBF",
269                 bottom_right    = "\xD9",
270         },
271         ["double"] = {
272                 horizontal      = "\xCD",
273                 vertical        = "\xBA",
274                 top_left        = "\xC9",
275                 bottom_left     = "\xC8",
276                 top_right       = "\xBB",
277                 bottom_right    = "\xBC",
278         },
279 }
280
281 function drawer.drawscreen(menu_opts)
282         -- drawlogo() must go first.
283         -- it determines the positions of other elements
284         drawer.drawlogo()
285         drawer.drawbrand()
286         drawer.drawbox()
287         return drawer.drawmenu(menu_opts)
288 end
289
290 function drawer.drawmenu(menudef)
291         local x = drawer.menu_position.x
292         local y = drawer.menu_position.y
293
294         x = x + drawer.shift.x
295         y = y + drawer.shift.y
296
297         -- print the menu and build the alias table
298         local alias_table = {}
299         local entry_num = 0
300         local menu_entries = menudef.entries
301         local effective_line_num = 0
302         if type(menu_entries) == "function" then
303                 menu_entries = menu_entries()
304         end
305         for _, e in ipairs(menu_entries) do
306                 -- Allow menu items to be conditionally visible by specifying
307                 -- a visible function.
308                 if e.visible ~= nil and not e.visible() then
309                         goto continue
310                 end
311                 effective_line_num = effective_line_num + 1
312                 if e.entry_type ~= core.MENU_SEPARATOR then
313                         entry_num = entry_num + 1
314                         screen.setcursor(x, y + effective_line_num)
315
316                         printc(entry_num .. ". " .. menuEntryName(menudef, e))
317
318                         -- fill the alias table
319                         alias_table[tostring(entry_num)] = e
320                         if e.alias ~= nil then
321                                 for _, a in ipairs(e.alias) do
322                                         alias_table[a] = e
323                                 end
324                         end
325                 else
326                         screen.setcursor(x, y + effective_line_num)
327                         printc(menuEntryName(menudef, e))
328                 end
329                 ::continue::
330         end
331         return alias_table
332 end
333
334 function drawer.drawbox()
335         local x = drawer.menu_position.x - 3
336         local y = drawer.menu_position.y - 1
337         local w = drawer.frame_size.w
338         local h = drawer.frame_size.h
339
340         local framestyle = loader.getenv("loader_menu_frame") or "double"
341         local framespec = drawer.frame_styles[framestyle]
342         -- If we don't have a framespec for the current frame style, just don't
343         -- draw a box.
344         if framespec == nil then
345                 return
346         end
347
348         local hl = framespec.horizontal
349         local vl = framespec.vertical
350
351         local tl = framespec.top_left
352         local bl = framespec.bottom_left
353         local tr = framespec.top_right
354         local br = framespec.bottom_right
355
356         x = x + drawer.shift.x
357         y = y + drawer.shift.y
358
359         screen.setcursor(x, y); printc(tl)
360         screen.setcursor(x, y + h); printc(bl)
361         screen.setcursor(x + w, y); printc(tr)
362         screen.setcursor(x + w, y + h); printc(br)
363
364         screen.setcursor(x + 1, y)
365         for _ = 1, w - 1 do
366                 printc(hl)
367         end
368
369         screen.setcursor(x + 1, y + h)
370         for _ = 1, w - 1 do
371                 printc(hl)
372         end
373
374         for i = 1, h - 1 do
375                 screen.setcursor(x, y + i)
376                 printc(vl)
377                 screen.setcursor(x + w, y + i)
378                 printc(vl)
379         end
380
381         local menu_header = loader.getenv("loader_menu_title") or
382             "Welcome to FreeBSD"
383         local menu_header_align = loader.getenv("loader_menu_title_align")
384         local menu_header_x
385
386         if menu_header_align ~= nil then
387                 menu_header_align = menu_header_align:lower()
388                 if menu_header_align == "left" then
389                         -- Just inside the left border on top
390                         menu_header_x = x + 1
391                 elseif menu_header_align == "right" then
392                         -- Just inside the right border on top
393                         menu_header_x = x + w - #menu_header
394                 end
395         end
396         if menu_header_x == nil then
397                 menu_header_x = x + (w / 2) - (#menu_header / 2)
398         end
399         screen.setcursor(menu_header_x, y)
400         printc(menu_header)
401 end
402
403 function drawer.draw(x, y, logo)
404         for i = 1, #logo do
405                 screen.setcursor(x, y + i - 1)
406                 printc(logo[i])
407         end
408 end
409
410 function drawer.drawbrand()
411         local x = tonumber(loader.getenv("loader_brand_x")) or
412             drawer.brand_position.x
413         local y = tonumber(loader.getenv("loader_brand_y")) or
414             drawer.brand_position.y
415
416         local graphic = drawer.branddefs[loader.getenv("loader_brand")]
417         if graphic == nil then
418                 graphic = fbsd_logo
419         end
420
421         x = x + drawer.shift.x
422         y = y + drawer.shift.y
423         drawer.draw(x, y, graphic)
424 end
425
426 function drawer.drawlogo()
427         local x = tonumber(loader.getenv("loader_logo_x")) or
428             drawer.logo_position.x
429         local y = tonumber(loader.getenv("loader_logo_y")) or
430             drawer.logo_position.y
431
432         local logo = loader.getenv("loader_logo")
433         local colored = color.isEnabled()
434
435         -- Lookup
436         local logodef = drawer.logodefs[logo]
437
438         if logodef == nil or logodef.graphic == nil or
439             (not colored and logodef.requires_color) then
440                 -- Choose a sensible default
441                 if colored then
442                         logodef = drawer.logodefs["orb"]
443                 else
444                         logodef = drawer.logodefs["orbbw"]
445                 end
446         end
447
448         if logodef ~= nil and logodef.graphic == none then
449                 drawer.shift = logodef.shift
450         else
451                 drawer.shift = drawer.default_shift
452         end
453
454         x = x + drawer.shift.x
455         y = y + drawer.shift.y
456
457         if logodef ~= nil and logodef.shift ~= nil then
458                 x = x + logodef.shift.x
459                 y = y + logodef.shift.y
460         end
461
462         drawer.draw(x, y, logodef.graphic)
463 end
464
465 return drawer