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 drawbox()
148 local x = menu_position.x - 3
149 local y = menu_position.y - 1
150 local w = frame_size.w
151 local h = frame_size.h
153 local framestyle = loader.getenv("loader_menu_frame") or "double"
154 local framespec = drawer.frame_styles[framestyle]
155 -- If we don't have a framespec for the current frame style, just don't
157 if framespec == nil then
161 local hl = framespec.horizontal
162 local vl = framespec.vertical
164 local tl = framespec.top_left
165 local bl = framespec.bottom_left
166 local tr = framespec.top_right
167 local br = framespec.bottom_right
172 screen.setcursor(x, y); printc(tl)
173 screen.setcursor(x, y + h); printc(bl)
174 screen.setcursor(x + w, y); printc(tr)
175 screen.setcursor(x + w, y + h); printc(br)
177 screen.setcursor(x + 1, y)
182 screen.setcursor(x + 1, y + h)
188 screen.setcursor(x, y + i)
190 screen.setcursor(x + w, y + i)
194 local menu_header = loader.getenv("loader_menu_title") or
196 local menu_header_align = loader.getenv("loader_menu_title_align")
199 if menu_header_align ~= nil then
200 menu_header_align = menu_header_align:lower()
201 if menu_header_align == "left" then
202 -- Just inside the left border on top
203 menu_header_x = x + 1
204 elseif menu_header_align == "right" then
205 -- Just inside the right border on top
206 menu_header_x = x + w - #menu_header
209 if menu_header_x == nil then
210 menu_header_x = x + (w / 2) - (#menu_header / 2)
212 screen.setcursor(menu_header_x, y)
216 local function drawbrand()
217 local x = tonumber(loader.getenv("loader_brand_x")) or
219 local y = tonumber(loader.getenv("loader_brand_y")) or
222 local branddef = getBranddef(loader.getenv("loader_brand"))
224 if branddef == nil then
225 branddef = getBranddef(drawer.default_brand)
228 local graphic = branddef.graphic
235 local function drawlogo()
236 local x = tonumber(loader.getenv("loader_logo_x")) or
238 local y = tonumber(loader.getenv("loader_logo_y")) or
241 local logo = loader.getenv("loader_logo")
242 local colored = color.isEnabled()
244 local logodef = getLogodef(logo)
246 if logodef == nil or logodef.graphic == nil or
247 (not colored and logodef.requires_color) then
248 -- Choose a sensible default
250 logodef = getLogodef(drawer.default_color_logodef)
252 logodef = getLogodef(drawer.default_bw_logodef)
256 if logodef ~= nil and logodef.graphic == none then
257 shift = logodef.shift
259 shift = default_shift
265 if logodef ~= nil and logodef.shift ~= nil then
266 x = x + logodef.shift.x
267 y = y + logodef.shift.y
270 draw(x, y, logodef.graphic)
274 " ______ ____ _____ _____ ",
275 " | ____| | _ \\ / ____| __ \\ ",
276 " | |___ _ __ ___ ___ | |_) | (___ | | | |",
277 " | ___| '__/ _ \\/ _ \\| _ < \\___ \\| | | |",
278 " | | | | | __/ __/| |_) |____) | |__| |",
279 " | | | | | | || | | |",
280 " |_| |_| \\___|\\___||____/|_____/|_____/ "
284 menu_name_handlers = {
285 -- Menu name handlers should take the menu being drawn and entry being
286 -- drawn as parameters, and return the name of the item.
287 -- This is designed so that everything, including menu separators, may
288 -- have their names derived differently. The default action for entry
289 -- types not specified here is to use entry.name directly.
290 [core.MENU_SEPARATOR] = function(_, entry)
291 if entry.name ~= nil then
292 if type(entry.name) == "function" then
299 [core.MENU_CAROUSEL_ENTRY] = function(_, entry)
300 local carid = entry.carousel_id
301 local caridx = config.getCarouselIndex(carid)
302 local choices = entry.items
303 if type(choices) == "function" then
306 if #choices < caridx then
309 return entry.name(caridx, choices[caridx], choices)
314 -- Indexed by valid values for loader_brand in loader.conf(5). Valid
315 -- keys are: graphic (table depicting graphic)
317 graphic = fbsd_brand,
325 -- Indexed by valid values for loader_logo in loader.conf(5). Valid keys
326 -- are: requires_color (boolean), graphic (table depicting graphic), and
327 -- shift (table containing x and y).
329 graphic = fbsd_brand,
332 graphic = fbsd_brand,
336 shift = {x = 17, y = 0},
340 brand_position = {x = 2, y = 1}
341 logo_position = {x = 46, y = 4}
342 menu_position = {x = 5, y = 10}
343 frame_size = {w = 42, h = 13}
344 default_shift = {x = 0, y = 0}
345 shift = default_shift
348 drawer.default_brand = 'fbsd'
349 drawer.default_color_logodef = 'orb'
350 drawer.default_bw_logodef = 'orbbw'
352 function drawer.addBrand(name, def)
353 branddefs[name] = def
356 function drawer.addLogo(name, def)
360 drawer.frame_styles = {
361 -- Indexed by valid values for loader_menu_frame in loader.conf(5).
362 -- All of the keys appearing below must be set for any menu frame style
363 -- added to drawer.frame_styles.
376 bottom_left = "\xC0",
378 bottom_right = "\xD9",
384 bottom_left = "\xC8",
386 bottom_right = "\xBC",
390 function drawer.drawscreen(menudef)
391 -- drawlogo() must go first.
392 -- it determines the positions of other elements
396 return drawmenu(menudef)