]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/lua/drawer.lua
lualoader: Stop exporting drawer.draw
[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_brand
40 local none
41
42 local function menuEntryName(drawing_menu, entry)
43         local name_handler = drawer.menu_name_handlers[entry.entry_type]
44
45         if name_handler ~= nil then
46                 return name_handler(drawing_menu, entry)
47         end
48         if type(entry.name) == "function" then
49                 return entry.name()
50         end
51         return entry.name
52 end
53
54 local function getBranddef(brand)
55         if brand == nil then
56                 return nil
57         end
58         -- Look it up
59         local branddef = drawer.branddefs[brand]
60
61         -- Try to pull it in
62         if branddef == nil then
63                 try_include('brand-' .. brand)
64                 branddef = drawer.branddefs[brand]
65         end
66
67         return branddef
68 end
69
70 local function getLogodef(logo)
71         if logo == nil then
72                 return nil
73         end
74         -- Look it up
75         local logodef = drawer.logodefs[logo]
76
77         -- Try to pull it in
78         if logodef == nil then
79                 try_include('logo-' .. logo)
80                 logodef = drawer.logodefs[logo]
81         end
82
83         return logodef
84 end
85
86 local function draw(x, y, logo)
87         for i = 1, #logo do
88                 screen.setcursor(x, y + i - 1)
89                 printc(logo[i])
90         end
91 end
92
93 fbsd_brand = {
94 "  ______               ____   _____ _____  ",
95 " |  ____|             |  _ \\ / ____|  __ \\ ",
96 " | |___ _ __ ___  ___ | |_) | (___ | |  | |",
97 " |  ___| '__/ _ \\/ _ \\|  _ < \\___ \\| |  | |",
98 " | |   | | |  __/  __/| |_) |____) | |__| |",
99 " | |   | | |    |    ||     |      |      |",
100 " |_|   |_|  \\___|\\___||____/|_____/|_____/ "
101 }
102 none = {""}
103
104 -- Module exports
105 drawer.default_brand = 'fbsd'
106
107 drawer.menu_name_handlers = {
108         -- Menu name handlers should take the menu being drawn and entry being
109         -- drawn as parameters, and return the name of the item.
110         -- This is designed so that everything, including menu separators, may
111         -- have their names derived differently. The default action for entry
112         -- types not specified here is to use entry.name directly.
113         [core.MENU_SEPARATOR] = function(_, entry)
114                 if entry.name ~= nil then
115                         if type(entry.name) == "function" then
116                                 return entry.name()
117                         end
118                         return entry.name
119                 end
120                 return ""
121         end,
122         [core.MENU_CAROUSEL_ENTRY] = function(_, entry)
123                 local carid = entry.carousel_id
124                 local caridx = config.getCarouselIndex(carid)
125                 local choices = entry.items
126                 if type(choices) == "function" then
127                         choices = choices()
128                 end
129                 if #choices < caridx then
130                         caridx = 1
131                 end
132                 return entry.name(caridx, choices[caridx], choices)
133         end,
134 }
135
136 drawer.brand_position = {x = 2, y = 1}
137 drawer.logo_position = {x = 46, y = 4}
138 drawer.menu_position = {x = 5, y = 10}
139 drawer.frame_size = {w = 42, h = 13}
140 drawer.default_shift = {x = 0, y = 0}
141 drawer.shift = drawer.default_shift
142
143 drawer.branddefs = {
144         -- Indexed by valid values for loader_brand in loader.conf(5). Valid
145         -- keys are: graphic (table depicting graphic)
146         ["fbsd"] = {
147                 graphic = fbsd_brand,
148         },
149         ["none"] = {
150                 graphic = none,
151         },
152 }
153
154 function drawer.addBrand(name, def)
155         drawer.branddefs[name] = def
156 end
157
158 function drawer.addLogo(name, def)
159         drawer.logodefs[name] = def
160 end
161
162 drawer.logodefs = {
163         -- Indexed by valid values for loader_logo in loader.conf(5). Valid keys
164         -- are: requires_color (boolean), graphic (table depicting graphic), and
165         -- shift (table containing x and y).
166         ["tribute"] = {
167                 graphic = fbsd_brand,
168         },
169         ["tributebw"] = {
170                 graphic = fbsd_brand,
171         },
172         ["none"] = {
173                 graphic = none,
174                 shift = {x = 17, y = 0},
175         },
176 }
177
178 drawer.frame_styles = {
179         -- Indexed by valid values for loader_menu_frame in loader.conf(5).
180         -- All of the keys appearing below must be set for any menu frame style
181         -- added to drawer.frame_styles.
182         ["ascii"] = {
183                 horizontal      = "-",
184                 vertical        = "|",
185                 top_left        = "+",
186                 bottom_left     = "+",
187                 top_right       = "+",
188                 bottom_right    = "+",
189         },
190         ["single"] = {
191                 horizontal      = "\xC4",
192                 vertical        = "\xB3",
193                 top_left        = "\xDA",
194                 bottom_left     = "\xC0",
195                 top_right       = "\xBF",
196                 bottom_right    = "\xD9",
197         },
198         ["double"] = {
199                 horizontal      = "\xCD",
200                 vertical        = "\xBA",
201                 top_left        = "\xC9",
202                 bottom_left     = "\xC8",
203                 top_right       = "\xBB",
204                 bottom_right    = "\xBC",
205         },
206 }
207
208 function drawer.drawscreen(menu_opts)
209         -- drawlogo() must go first.
210         -- it determines the positions of other elements
211         drawer.drawlogo()
212         drawer.drawbrand()
213         drawer.drawbox()
214         return drawer.drawmenu(menu_opts)
215 end
216
217 function drawer.drawmenu(menudef)
218         local x = drawer.menu_position.x
219         local y = drawer.menu_position.y
220
221         x = x + drawer.shift.x
222         y = y + drawer.shift.y
223
224         -- print the menu and build the alias table
225         local alias_table = {}
226         local entry_num = 0
227         local menu_entries = menudef.entries
228         local effective_line_num = 0
229         if type(menu_entries) == "function" then
230                 menu_entries = menu_entries()
231         end
232         for _, e in ipairs(menu_entries) do
233                 -- Allow menu items to be conditionally visible by specifying
234                 -- a visible function.
235                 if e.visible ~= nil and not e.visible() then
236                         goto continue
237                 end
238                 effective_line_num = effective_line_num + 1
239                 if e.entry_type ~= core.MENU_SEPARATOR then
240                         entry_num = entry_num + 1
241                         screen.setcursor(x, y + effective_line_num)
242
243                         printc(entry_num .. ". " .. menuEntryName(menudef, e))
244
245                         -- fill the alias table
246                         alias_table[tostring(entry_num)] = e
247                         if e.alias ~= nil then
248                                 for _, a in ipairs(e.alias) do
249                                         alias_table[a] = e
250                                 end
251                         end
252                 else
253                         screen.setcursor(x, y + effective_line_num)
254                         printc(menuEntryName(menudef, e))
255                 end
256                 ::continue::
257         end
258         return alias_table
259 end
260
261 function drawer.drawbox()
262         local x = drawer.menu_position.x - 3
263         local y = drawer.menu_position.y - 1
264         local w = drawer.frame_size.w
265         local h = drawer.frame_size.h
266
267         local framestyle = loader.getenv("loader_menu_frame") or "double"
268         local framespec = drawer.frame_styles[framestyle]
269         -- If we don't have a framespec for the current frame style, just don't
270         -- draw a box.
271         if framespec == nil then
272                 return
273         end
274
275         local hl = framespec.horizontal
276         local vl = framespec.vertical
277
278         local tl = framespec.top_left
279         local bl = framespec.bottom_left
280         local tr = framespec.top_right
281         local br = framespec.bottom_right
282
283         x = x + drawer.shift.x
284         y = y + drawer.shift.y
285
286         screen.setcursor(x, y); printc(tl)
287         screen.setcursor(x, y + h); printc(bl)
288         screen.setcursor(x + w, y); printc(tr)
289         screen.setcursor(x + w, y + h); printc(br)
290
291         screen.setcursor(x + 1, y)
292         for _ = 1, w - 1 do
293                 printc(hl)
294         end
295
296         screen.setcursor(x + 1, y + h)
297         for _ = 1, w - 1 do
298                 printc(hl)
299         end
300
301         for i = 1, h - 1 do
302                 screen.setcursor(x, y + i)
303                 printc(vl)
304                 screen.setcursor(x + w, y + i)
305                 printc(vl)
306         end
307
308         local menu_header = loader.getenv("loader_menu_title") or
309             "Welcome to FreeBSD"
310         local menu_header_align = loader.getenv("loader_menu_title_align")
311         local menu_header_x
312
313         if menu_header_align ~= nil then
314                 menu_header_align = menu_header_align:lower()
315                 if menu_header_align == "left" then
316                         -- Just inside the left border on top
317                         menu_header_x = x + 1
318                 elseif menu_header_align == "right" then
319                         -- Just inside the right border on top
320                         menu_header_x = x + w - #menu_header
321                 end
322         end
323         if menu_header_x == nil then
324                 menu_header_x = x + (w / 2) - (#menu_header / 2)
325         end
326         screen.setcursor(menu_header_x, y)
327         printc(menu_header)
328 end
329
330 function drawer.drawbrand()
331         local x = tonumber(loader.getenv("loader_brand_x")) or
332             drawer.brand_position.x
333         local y = tonumber(loader.getenv("loader_brand_y")) or
334             drawer.brand_position.y
335
336         local branddef = getBranddef(loader.getenv("loader_brand"))
337
338         if branddef == nil then
339                 branddef = getBranddef(drawer.default_brand)
340         end
341
342         local graphic = branddef.graphic
343
344         x = x + drawer.shift.x
345         y = y + drawer.shift.y
346         draw(x, y, graphic)
347 end
348
349 function drawer.drawlogo()
350         local x = tonumber(loader.getenv("loader_logo_x")) or
351             drawer.logo_position.x
352         local y = tonumber(loader.getenv("loader_logo_y")) or
353             drawer.logo_position.y
354
355         local logo = loader.getenv("loader_logo")
356         local colored = color.isEnabled()
357
358         local logodef = getLogodef(logo)
359
360         if logodef == nil or logodef.graphic == nil or
361             (not colored and logodef.requires_color) then
362                 -- Choose a sensible default
363                 if colored then
364                         logodef = getLogodef("orb")
365                 else
366                         logodef = getLogodef("orbbw")
367                 end
368         end
369
370         if logodef ~= nil and logodef.graphic == none then
371                 drawer.shift = logodef.shift
372         else
373                 drawer.shift = drawer.default_shift
374         end
375
376         x = x + drawer.shift.x
377         y = y + drawer.shift.y
378
379         if logodef ~= nil and logodef.shift ~= nil then
380                 x = x + logodef.shift.x
381                 y = y + logodef.shift.y
382         end
383
384         draw(x, y, logodef.graphic)
385 end
386
387 return drawer