2 -- SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 -- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org>
5 -- Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
6 -- All rights reserved.
8 -- Redistribution and use in source and binary forms, with or without
9 -- modification, are permitted provided that the following conditions
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.
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
32 local color = require("color")
33 local config = require("config")
34 local core = require("core")
35 local screen = require("screen")
42 local menu_name_handlers
52 local function menuEntryName(drawing_menu, entry)
53 local name_handler = menu_name_handlers[entry.entry_type]
55 if name_handler ~= nil then
56 return name_handler(drawing_menu, entry)
58 if type(entry.name) == "function" then
64 local function getBranddef(brand)
69 local branddef = branddefs[brand]
72 if branddef == nil then
73 try_include('brand-' .. brand)
74 branddef = branddefs[brand]
80 local function getLogodef(logo)
85 local logodef = logodefs[logo]
88 if logodef == nil then
89 try_include('logo-' .. logo)
90 logodef = logodefs[logo]
96 local function draw(x, y, logo)
98 screen.setcursor(x, y + i - 1)
103 local function drawmenu(menudef)
104 local x = menu_position.x
105 local y = menu_position.y
110 -- print the menu and build the alias table
111 local alias_table = {}
113 local menu_entries = menudef.entries
114 local effective_line_num = 0
115 if type(menu_entries) == "function" then
116 menu_entries = menu_entries()
118 for _, e in ipairs(menu_entries) do
119 -- Allow menu items to be conditionally visible by specifying
120 -- a visible function.
121 if e.visible ~= nil and not e.visible() then
124 effective_line_num = effective_line_num + 1
125 if e.entry_type ~= core.MENU_SEPARATOR then
126 entry_num = entry_num + 1
127 screen.setcursor(x, y + effective_line_num)
129 printc(entry_num .. ". " .. menuEntryName(menudef, e))
131 -- fill the alias table
132 alias_table[tostring(entry_num)] = e
133 if e.alias ~= nil then
134 for _, a in ipairs(e.alias) do
139 screen.setcursor(x, y + effective_line_num)
140 printc(menuEntryName(menudef, e))
147 local function defaultframe()
148 if core.isSerialConsole() then
154 local function drawbox()
155 local x = menu_position.x - 3
156 local y = menu_position.y - 1
157 local w = frame_size.w
158 local h = frame_size.h
160 local framestyle = loader.getenv("loader_menu_frame") or defaultframe()
161 local framespec = drawer.frame_styles[framestyle]
162 -- If we don't have a framespec for the current frame style, just don't
164 if framespec == nil then
168 local hl = framespec.horizontal
169 local vl = framespec.vertical
171 local tl = framespec.top_left
172 local bl = framespec.bottom_left
173 local tr = framespec.top_right
174 local br = framespec.bottom_right
179 screen.setcursor(x, y); printc(tl)
180 screen.setcursor(x, y + h); printc(bl)
181 screen.setcursor(x + w, y); printc(tr)
182 screen.setcursor(x + w, y + h); printc(br)
184 screen.setcursor(x + 1, y)
189 screen.setcursor(x + 1, y + h)
195 screen.setcursor(x, y + i)
197 screen.setcursor(x + w, y + i)
201 local menu_header = loader.getenv("loader_menu_title") or
203 local menu_header_align = loader.getenv("loader_menu_title_align")
206 if menu_header_align ~= nil then
207 menu_header_align = menu_header_align:lower()
208 if menu_header_align == "left" then
209 -- Just inside the left border on top
210 menu_header_x = x + 1
211 elseif menu_header_align == "right" then
212 -- Just inside the right border on top
213 menu_header_x = x + w - #menu_header
216 if menu_header_x == nil then
217 menu_header_x = x + (w / 2) - (#menu_header / 2)
219 screen.setcursor(menu_header_x, y)
223 local function drawbrand()
224 local x = tonumber(loader.getenv("loader_brand_x")) or
226 local y = tonumber(loader.getenv("loader_brand_y")) or
229 local branddef = getBranddef(loader.getenv("loader_brand"))
231 if branddef == nil then
232 branddef = getBranddef(drawer.default_brand)
235 local graphic = branddef.graphic
242 local function drawlogo()
243 local x = tonumber(loader.getenv("loader_logo_x")) or
245 local y = tonumber(loader.getenv("loader_logo_y")) or
248 local logo = loader.getenv("loader_logo")
249 local colored = color.isEnabled()
251 local logodef = getLogodef(logo)
253 if logodef == nil or logodef.graphic == nil or
254 (not colored and logodef.requires_color) then
255 -- Choose a sensible default
257 logodef = getLogodef(drawer.default_color_logodef)
259 logodef = getLogodef(drawer.default_bw_logodef)
263 if logodef ~= nil and logodef.graphic == none then
264 shift = logodef.shift
266 shift = default_shift
272 if logodef ~= nil and logodef.shift ~= nil then
273 x = x + logodef.shift.x
274 y = y + logodef.shift.y
277 draw(x, y, logodef.graphic)
281 " ______ ____ _____ _____ ",
282 " | ____| | _ \\ / ____| __ \\ ",
283 " | |___ _ __ ___ ___ | |_) | (___ | | | |",
284 " | ___| '__/ _ \\/ _ \\| _ < \\___ \\| | | |",
285 " | | | | | __/ __/| |_) |____) | |__| |",
286 " | | | | | | || | | |",
287 " |_| |_| \\___|\\___||____/|_____/|_____/ "
291 menu_name_handlers = {
292 -- Menu name handlers should take the menu being drawn and entry being
293 -- drawn as parameters, and return the name of the item.
294 -- This is designed so that everything, including menu separators, may
295 -- have their names derived differently. The default action for entry
296 -- types not specified here is to use entry.name directly.
297 [core.MENU_SEPARATOR] = function(_, entry)
298 if entry.name ~= nil then
299 if type(entry.name) == "function" then
306 [core.MENU_CAROUSEL_ENTRY] = function(_, entry)
307 local carid = entry.carousel_id
308 local caridx = config.getCarouselIndex(carid)
309 local choices = entry.items
310 if type(choices) == "function" then
313 if #choices < caridx then
316 return entry.name(caridx, choices[caridx], choices)
321 -- Indexed by valid values for loader_brand in loader.conf(5). Valid
322 -- keys are: graphic (table depicting graphic)
324 graphic = fbsd_brand,
332 -- Indexed by valid values for loader_logo in loader.conf(5). Valid keys
333 -- are: requires_color (boolean), graphic (table depicting graphic), and
334 -- shift (table containing x and y).
336 graphic = fbsd_brand,
339 graphic = fbsd_brand,
343 shift = {x = 17, y = 0},
347 brand_position = {x = 2, y = 1}
348 logo_position = {x = 46, y = 4}
349 menu_position = {x = 5, y = 10}
350 frame_size = {w = 42, h = 13}
351 default_shift = {x = 0, y = 0}
352 shift = default_shift
355 drawer.default_brand = 'fbsd'
356 drawer.default_color_logodef = 'orb'
357 drawer.default_bw_logodef = 'orbbw'
359 function drawer.addBrand(name, def)
360 branddefs[name] = def
363 function drawer.addLogo(name, def)
367 drawer.frame_styles = {
368 -- Indexed by valid values for loader_menu_frame in loader.conf(5).
369 -- All of the keys appearing below must be set for any menu frame style
370 -- added to drawer.frame_styles.
383 bottom_left = "\xC0",
385 bottom_right = "\xD9",
391 bottom_left = "\xC8",
393 bottom_right = "\xBC",
397 function drawer.drawscreen(menudef)
398 -- drawlogo() must go first.
399 -- it determines the positions of other elements
403 return drawmenu(menudef)