1088b4f5fSWarner Losh-- 2088b4f5fSWarner Losh-- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org> 3*df74a61fSKyle Evans-- Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org> 4088b4f5fSWarner Losh-- All rights reserved. 5088b4f5fSWarner Losh-- 6088b4f5fSWarner Losh-- Redistribution and use in source and binary forms, with or without 7088b4f5fSWarner Losh-- modification, are permitted provided that the following conditions 8088b4f5fSWarner Losh-- are met: 9088b4f5fSWarner Losh-- 1. Redistributions of source code must retain the above copyright 10088b4f5fSWarner Losh-- notice, this list of conditions and the following disclaimer. 11088b4f5fSWarner Losh-- 2. Redistributions in binary form must reproduce the above copyright 12088b4f5fSWarner Losh-- notice, this list of conditions and the following disclaimer in the 13088b4f5fSWarner Losh-- documentation and/or other materials provided with the distribution. 14088b4f5fSWarner Losh-- 15088b4f5fSWarner Losh-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16088b4f5fSWarner Losh-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17088b4f5fSWarner Losh-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18088b4f5fSWarner Losh-- ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19088b4f5fSWarner Losh-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20088b4f5fSWarner Losh-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21088b4f5fSWarner Losh-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22088b4f5fSWarner Losh-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23088b4f5fSWarner Losh-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24088b4f5fSWarner Losh-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25088b4f5fSWarner Losh-- SUCH DAMAGE. 26088b4f5fSWarner Losh-- 27088b4f5fSWarner Losh-- $FreeBSD$ 28088b4f5fSWarner Losh-- 29088b4f5fSWarner Losh 30088b4f5fSWarner Loshlocal color = require("color"); 31a7cf0562SKyle Evanslocal core = require("core"); 32088b4f5fSWarner Loshlocal screen = require("screen"); 33088b4f5fSWarner Losh 34c8518398SKyle Evanslocal drawer = {}; 35c8518398SKyle Evans 3602122e53SKyle Evanslocal fbsd_logo; 3702122e53SKyle Evanslocal beastie_color; 3802122e53SKyle Evanslocal beastie; 3902122e53SKyle Evanslocal fbsd_logo_v; 4002122e53SKyle Evanslocal orb; 4102122e53SKyle Evanslocal none; 42bb26c57dSKyle Evanslocal none_shifted = false; 4302122e53SKyle Evans 44da56fe38SKyle Evansdrawer.menu_name_handlers = { 45da56fe38SKyle Evans -- Menu name handlers should take the menu being drawn and entry being 46da56fe38SKyle Evans -- drawn as parameters, and return the name of the item. 47da56fe38SKyle Evans -- This is designed so that everything, including menu separators, may 48da56fe38SKyle Evans -- have their names derived differently. The default action for entry 49da56fe38SKyle Evans -- types not specified here is to call and use entry.name(). 50da56fe38SKyle Evans [core.MENU_CAROUSEL_ENTRY] = function(drawing_menu, entry) 51da56fe38SKyle Evans local carid = entry.carousel_id; 52da56fe38SKyle Evans local caridx = menu.getCarouselIndex(carid); 53da56fe38SKyle Evans local choices = entry.items(); 54da56fe38SKyle Evans 55da56fe38SKyle Evans if (#choices < caridx) then 56da56fe38SKyle Evans caridx = 1; 57da56fe38SKyle Evans end 58da56fe38SKyle Evans return entry.name(caridx, choices[caridx], choices); 59da56fe38SKyle Evans end, 60da56fe38SKyle Evans}; 61da56fe38SKyle Evans 62088b4f5fSWarner Loshdrawer.brand_position = {x = 2, y = 1}; 6302122e53SKyle Evansdrawer.logo_position = {x = 46, y = 1}; 6402122e53SKyle Evansdrawer.menu_position = {x = 6, y = 11}; 6502122e53SKyle Evansdrawer.box_pos_dim = {x = 3, y = 10, w = 41, h = 11}; 6602122e53SKyle Evans 6702122e53SKyle Evansfbsd_logo = { 68088b4f5fSWarner Losh " ______ ____ _____ _____ ", 69088b4f5fSWarner Losh " | ____| | _ \\ / ____| __ \\ ", 70088b4f5fSWarner Losh " | |___ _ __ ___ ___ | |_) | (___ | | | |", 71088b4f5fSWarner Losh " | ___| '__/ _ \\/ _ \\| _ < \\___ \\| | | |", 72088b4f5fSWarner Losh " | | | | | __/ __/| |_) |____) | |__| |", 73088b4f5fSWarner Losh " | | | | | | || | | |", 74088b4f5fSWarner Losh " |_| |_| \\___|\\___||____/|_____/|_____/ " 75088b4f5fSWarner Losh}; 76088b4f5fSWarner Losh 7702122e53SKyle Evansbeastie_color = { 78088b4f5fSWarner Losh " \027[31m, ,", 79088b4f5fSWarner Losh " /( )`", 80088b4f5fSWarner Losh " \\ \\___ / |", 81088b4f5fSWarner Losh " /- \027[37m_\027[31m `-/ '", 82088b4f5fSWarner Losh " (\027[37m/\\/ \\\027[31m \\ /\\", 83088b4f5fSWarner Losh " \027[37m/ / |\027[31m ` \\", 84088b4f5fSWarner Losh " \027[34mO O \027[37m) \027[31m/ |", 85088b4f5fSWarner Losh " \027[37m`-^--'\027[31m`< '", 86088b4f5fSWarner Losh " (_.) _ ) /", 87088b4f5fSWarner Losh " `.___/` /", 88088b4f5fSWarner Losh " `-----' /", 89088b4f5fSWarner Losh " \027[33m<----.\027[31m __ / __ \\", 90088b4f5fSWarner Losh " \027[33m<----|====\027[31mO)))\027[33m==\027[31m) \\) /\027[33m====|", 91088b4f5fSWarner Losh " \027[33m<----'\027[31m `--' `.__,' \\", 92088b4f5fSWarner Losh " | |", 93088b4f5fSWarner Losh " \\ / /\\", 94088b4f5fSWarner Losh " \027[36m______\027[31m( (_ / \\______/", 95088b4f5fSWarner Losh " \027[36m,' ,-----' |", 96088b4f5fSWarner Losh " `--{__________)\027[37m" 97088b4f5fSWarner Losh}; 98088b4f5fSWarner Losh 9902122e53SKyle Evansbeastie = { 100088b4f5fSWarner Losh " , ,", 101088b4f5fSWarner Losh " /( )`", 102088b4f5fSWarner Losh " \\ \\___ / |", 103088b4f5fSWarner Losh " /- _ `-/ '", 104088b4f5fSWarner Losh " (/\\/ \\ \\ /\\", 105088b4f5fSWarner Losh " / / | ` \\", 106088b4f5fSWarner Losh " O O ) / |", 107088b4f5fSWarner Losh " `-^--'`< '", 108088b4f5fSWarner Losh " (_.) _ ) /", 109088b4f5fSWarner Losh " `.___/` /", 110088b4f5fSWarner Losh " `-----' /", 111088b4f5fSWarner Losh " <----. __ / __ \\", 112088b4f5fSWarner Losh " <----|====O)))==) \\) /====|", 113088b4f5fSWarner Losh " <----' `--' `.__,' \\", 114088b4f5fSWarner Losh " | |", 115088b4f5fSWarner Losh " \\ / /\\", 116088b4f5fSWarner Losh " ______( (_ / \\______/", 117088b4f5fSWarner Losh " ,' ,-----' |", 118088b4f5fSWarner Losh " `--{__________)" 119088b4f5fSWarner Losh}; 120088b4f5fSWarner Losh 12102122e53SKyle Evansfbsd_logo_v = { 122088b4f5fSWarner Losh " ______", 123088b4f5fSWarner Losh " | ____| __ ___ ___ ", 124088b4f5fSWarner Losh " | |__ | '__/ _ \\/ _ \\", 125088b4f5fSWarner Losh " | __|| | | __/ __/", 126088b4f5fSWarner Losh " | | | | | | |", 127088b4f5fSWarner Losh " |_| |_| \\___|\\___|", 128088b4f5fSWarner Losh " ____ _____ _____", 129088b4f5fSWarner Losh " | _ \\ / ____| __ \\", 130088b4f5fSWarner Losh " | |_) | (___ | | | |", 131088b4f5fSWarner Losh " | _ < \\___ \\| | | |", 132088b4f5fSWarner Losh " | |_) |____) | |__| |", 133088b4f5fSWarner Losh " | | | |", 134088b4f5fSWarner Losh " |____/|_____/|_____/" 135088b4f5fSWarner Losh}; 136088b4f5fSWarner Losh 13702122e53SKyle Evansorb_color = { 138088b4f5fSWarner Losh " \027[31m``` \027[31;1m`\027[31m", 139088b4f5fSWarner Losh " s` `.....---...\027[31;1m....--.``` -/\027[31m", 140088b4f5fSWarner Losh " +o .--` \027[31;1m/y:` +.\027[31m", 141088b4f5fSWarner Losh " yo`:. \027[31;1m:o `+-\027[31m", 142088b4f5fSWarner Losh " y/ \027[31;1m-/` -o/\027[31m", 143088b4f5fSWarner Losh " .- \027[31;1m::/sy+:.\027[31m", 144088b4f5fSWarner Losh " / \027[31;1m`-- /\027[31m", 145088b4f5fSWarner Losh " `: \027[31;1m:`\027[31m", 146088b4f5fSWarner Losh " `: \027[31;1m:`\027[31m", 147088b4f5fSWarner Losh " / \027[31;1m/\027[31m", 148088b4f5fSWarner Losh " .- \027[31;1m-.\027[31m", 149088b4f5fSWarner Losh " -- \027[31;1m-.\027[31m", 150088b4f5fSWarner Losh " `:` \027[31;1m`:`", 151088b4f5fSWarner Losh " \027[31;1m.-- `--.", 152088b4f5fSWarner Losh " .---.....----.\027[37m" 153088b4f5fSWarner Losh}; 154088b4f5fSWarner Losh 15502122e53SKyle Evansorb = { 156088b4f5fSWarner Losh " ``` `", 157088b4f5fSWarner Losh " s` `.....---.......--.``` -/", 158088b4f5fSWarner Losh " +o .--` /y:` +.", 159088b4f5fSWarner Losh " yo`:. :o `+-", 160088b4f5fSWarner Losh " y/ -/` -o/", 161088b4f5fSWarner Losh " .- ::/sy+:.", 162088b4f5fSWarner Losh " / `-- /", 163088b4f5fSWarner Losh " `: :`", 164088b4f5fSWarner Losh " `: :`", 165088b4f5fSWarner Losh " / /", 166088b4f5fSWarner Losh " .- -.", 167088b4f5fSWarner Losh " -- -.", 168088b4f5fSWarner Losh " `:` `:`", 169088b4f5fSWarner Losh " .-- `--.", 170088b4f5fSWarner Losh " .---.....----." 171088b4f5fSWarner Losh}; 172088b4f5fSWarner Losh 17302122e53SKyle Evansnone = {""}; 174088b4f5fSWarner Losh 17529aa5794SKyle Evansdrawer.branddefs = { 17629aa5794SKyle Evans ["fbsd"] = { 17729aa5794SKyle Evans graphic = fbsd_logo, 17829aa5794SKyle Evans }, 17929aa5794SKyle Evans ["none"] = { 18029aa5794SKyle Evans graphic = none, 18129aa5794SKyle Evans }, 18229aa5794SKyle Evans}; 18329aa5794SKyle Evans 184bb26c57dSKyle Evansdrawer.logodefs = { 185bb26c57dSKyle Evans -- Indexed by valid values for loader_logo in loader.conf(5). Valid keys 186752b2d40SKyle Evans -- are: requires_color (boolean), graphic (table depicting graphic), and 187bb26c57dSKyle Evans -- shift (table containing x and y). 188bb26c57dSKyle Evans ["beastie"] = { 189bb26c57dSKyle Evans requires_color = true, 190752b2d40SKyle Evans graphic = beastie_color, 191bb26c57dSKyle Evans }, 192bb26c57dSKyle Evans ["beastiebw"] = { 193752b2d40SKyle Evans graphic = beastie, 194bb26c57dSKyle Evans }, 195bb26c57dSKyle Evans ["fbsdbw"] = { 196752b2d40SKyle Evans graphic = fbsd_logo_v, 197bb26c57dSKyle Evans shift = {x = 5, y = 4}, 198bb26c57dSKyle Evans }, 199bb26c57dSKyle Evans ["orb"] = { 200bb26c57dSKyle Evans requires_color = true, 201752b2d40SKyle Evans graphic = orb_color, 202bb26c57dSKyle Evans shift = {x = 2, y = 4}, 203bb26c57dSKyle Evans }, 204bb26c57dSKyle Evans ["orbbw"] = { 205752b2d40SKyle Evans graphic = orb, 206bb26c57dSKyle Evans shift = {x = 2, y = 4}, 207bb26c57dSKyle Evans }, 208bb26c57dSKyle Evans ["tribute"] = { 209752b2d40SKyle Evans graphic = fbsd_logo, 210bb26c57dSKyle Evans }, 211bb26c57dSKyle Evans ["tributebw"] = { 212752b2d40SKyle Evans graphic = fbsd_logo, 213bb26c57dSKyle Evans }, 214bb26c57dSKyle Evans ["none"] = { 215752b2d40SKyle Evans graphic = none, 216bb26c57dSKyle Evans shift = {x = 17, y = 0}, 217bb26c57dSKyle Evans }, 218bb26c57dSKyle Evans}; 219bb26c57dSKyle Evans 220088b4f5fSWarner Loshfunction drawer.drawscreen(menu_opts) 221088b4f5fSWarner Losh -- drawlogo() must go first. 222088b4f5fSWarner Losh -- it determines the positions of other elements 223088b4f5fSWarner Losh drawer.drawlogo(); 224088b4f5fSWarner Losh drawer.drawbrand(); 225088b4f5fSWarner Losh drawer.drawbox(); 226088b4f5fSWarner Losh return drawer.drawmenu(menu_opts); 227088b4f5fSWarner Loshend 228088b4f5fSWarner Losh 2292413c411SKyle Evansfunction menu_entry_name(drawing_menu, entry) 2302413c411SKyle Evans local name_handler = drawer.menu_name_handlers[entry.entry_type]; 2312413c411SKyle Evans 2322413c411SKyle Evans if (name_handler ~= nil) then 2332413c411SKyle Evans return name_handler(drawing_menu, entry); 2342413c411SKyle Evans end 2352413c411SKyle Evans return entry.name(); 2362413c411SKyle Evansend 2372413c411SKyle Evans 238088b4f5fSWarner Loshfunction drawer.drawmenu(m) 239088b4f5fSWarner Losh x = drawer.menu_position.x; 240088b4f5fSWarner Losh y = drawer.menu_position.y; 241088b4f5fSWarner Losh 242088b4f5fSWarner Losh -- print the menu and build the alias table 243088b4f5fSWarner Losh local alias_table = {}; 244088b4f5fSWarner Losh local entry_num = 0; 245d8757746SKyle Evans local menu_entries = m.entries; 2462e716cecSKyle Evans if (type(menu_entries) == "function") then 2472e716cecSKyle Evans menu_entries = menu_entries(); 2482e716cecSKyle Evans end 249d8757746SKyle Evans for line_num, e in ipairs(menu_entries) do 2504a4fb4f8SKyle Evans -- Allow menu items to be conditionally visible by specifying 2514a4fb4f8SKyle Evans -- a visible function. 2524a4fb4f8SKyle Evans if (e.visible ~= nil) and (not e.visible()) then 253ddb76e07SKyle Evans goto continue; 2544a4fb4f8SKyle Evans end 255a7cf0562SKyle Evans if (e.entry_type ~= core.MENU_SEPARATOR) then 256088b4f5fSWarner Losh entry_num = entry_num + 1; 257088b4f5fSWarner Losh screen.setcursor(x, y + line_num); 258ada26c4aSKyle Evans 2592413c411SKyle Evans print(entry_num .. ". " .. menu_entry_name(m, e)); 260088b4f5fSWarner Losh 261088b4f5fSWarner Losh -- fill the alias table 262088b4f5fSWarner Losh alias_table[tostring(entry_num)] = e; 263196ba166SKyle Evans if (e.alias ~= nil) then 264088b4f5fSWarner Losh for n, a in ipairs(e.alias) do 265088b4f5fSWarner Losh alias_table[a] = e; 266088b4f5fSWarner Losh end 267196ba166SKyle Evans end 268088b4f5fSWarner Losh else 269088b4f5fSWarner Losh screen.setcursor(x, y + line_num); 2702413c411SKyle Evans print(menu_entry_name(m, e)); 271088b4f5fSWarner Losh end 2724a4fb4f8SKyle Evans ::continue:: 273088b4f5fSWarner Losh end 274088b4f5fSWarner Losh return alias_table; 275088b4f5fSWarner Loshend 276088b4f5fSWarner Losh 277088b4f5fSWarner Losh 278088b4f5fSWarner Loshfunction drawer.drawbox() 279088b4f5fSWarner Losh x = drawer.box_pos_dim.x; 280088b4f5fSWarner Losh y = drawer.box_pos_dim.y; 281088b4f5fSWarner Losh w = drawer.box_pos_dim.w; 282088b4f5fSWarner Losh h = drawer.box_pos_dim.h; 283088b4f5fSWarner Losh 284088b4f5fSWarner Losh local hl = string.char(0xCD); 285088b4f5fSWarner Losh local vl = string.char(0xBA); 286088b4f5fSWarner Losh 287088b4f5fSWarner Losh local tl = string.char(0xC9); 288088b4f5fSWarner Losh local bl = string.char(0xC8); 289088b4f5fSWarner Losh local tr = string.char(0xBB); 290088b4f5fSWarner Losh local br = string.char(0xBC); 291088b4f5fSWarner Losh 292088b4f5fSWarner Losh screen.setcursor(x, y); print(tl); 293088b4f5fSWarner Losh screen.setcursor(x, y+h); print(bl); 294088b4f5fSWarner Losh screen.setcursor(x+w, y); print(tr); 295088b4f5fSWarner Losh screen.setcursor(x+w, y+h); print(br); 296088b4f5fSWarner Losh 297088b4f5fSWarner Losh for i = 1, w-1 do 298088b4f5fSWarner Losh screen.setcursor(x+i, y); 299088b4f5fSWarner Losh print(hl); 300088b4f5fSWarner Losh screen.setcursor(x+i, y+h); 301088b4f5fSWarner Losh print(hl); 302088b4f5fSWarner Losh end 303088b4f5fSWarner Losh 304088b4f5fSWarner Losh for i = 1, h-1 do 305088b4f5fSWarner Losh screen.setcursor(x, y+i); 306088b4f5fSWarner Losh print(vl); 307088b4f5fSWarner Losh screen.setcursor(x+w, y+i); 308088b4f5fSWarner Losh print(vl); 309088b4f5fSWarner Losh end 310088b4f5fSWarner Losh 311088b4f5fSWarner Losh screen.setcursor(x+(w/2)-9, y); 312088b4f5fSWarner Losh print("Welcome to FreeBSD"); 313088b4f5fSWarner Loshend 314088b4f5fSWarner Losh 315088b4f5fSWarner Loshfunction drawer.draw(x, y, logo) 316088b4f5fSWarner Losh for i = 1, #logo do 317088b4f5fSWarner Losh screen.setcursor(x, y + i); 318088b4f5fSWarner Losh print(logo[i]); 319088b4f5fSWarner Losh end 320088b4f5fSWarner Loshend 321088b4f5fSWarner Losh 322088b4f5fSWarner Loshfunction drawer.drawbrand() 32324a1bd54SKyle Evans local x = tonumber(loader.getenv("loader_brand_x")) or 32424a1bd54SKyle Evans drawer.brand_position.x; 32524a1bd54SKyle Evans local y = tonumber(loader.getenv("loader_brand_y")) or 32624a1bd54SKyle Evans drawer.brand_position.y; 327088b4f5fSWarner Losh 32829aa5794SKyle Evans local graphic = drawer.branddefs[loader.getenv("loader_brand")]; 32929aa5794SKyle Evans if (graphic == nil) then 33029aa5794SKyle Evans graphic = fbsd_logo; 33129aa5794SKyle Evans end 33229aa5794SKyle Evans drawer.draw(x, y, graphic); 333088b4f5fSWarner Loshend 334088b4f5fSWarner Losh 335bb26c57dSKyle Evansfunction shift_brand_text(shift) 336bb26c57dSKyle Evans drawer.brand_position.x = drawer.brand_position.x + shift.x; 337bb26c57dSKyle Evans drawer.brand_position.y = drawer.brand_position.y + shift.y; 338bb26c57dSKyle Evans drawer.menu_position.x = drawer.menu_position.x + shift.x; 339bb26c57dSKyle Evans drawer.menu_position.y = drawer.menu_position.y + shift.y; 340bb26c57dSKyle Evans drawer.box_pos_dim.x = drawer.box_pos_dim.x + shift.x; 341bb26c57dSKyle Evans drawer.box_pos_dim.y = drawer.box_pos_dim.y + shift.y; 342bb26c57dSKyle Evansend 343bb26c57dSKyle Evans 344088b4f5fSWarner Loshfunction drawer.drawlogo() 34524a1bd54SKyle Evans local x = tonumber(loader.getenv("loader_logo_x")) or 34624a1bd54SKyle Evans drawer.logo_position.x; 34724a1bd54SKyle Evans local y = tonumber(loader.getenv("loader_logo_y")) or 34824a1bd54SKyle Evans drawer.logo_position.y; 349088b4f5fSWarner Losh 350088b4f5fSWarner Losh local logo = loader.getenv("loader_logo"); 351088b4f5fSWarner Losh local colored = color.isEnabled(); 352088b4f5fSWarner Losh 353bb26c57dSKyle Evans -- Lookup 354bb26c57dSKyle Evans local logodef = drawer.logodefs[logo]; 355bb26c57dSKyle Evans 356752b2d40SKyle Evans if (logodef ~= nil) and (logodef.graphic == none) then 357088b4f5fSWarner Losh -- centre brand and text if no logo 358bb26c57dSKyle Evans if (not none_shifted) then 359bb26c57dSKyle Evans shift_brand_text(logodef.shift); 360bb26c57dSKyle Evans none_shifted = true; 361088b4f5fSWarner Losh end 362752b2d40SKyle Evans elseif (logodef == nil) or (logodef.graphic == nil) or 363bb26c57dSKyle Evans ((not colored) and logodef.requires_color) then 364bb26c57dSKyle Evans -- Choose a sensible default 36524a1bd54SKyle Evans if (colored) then 366bb26c57dSKyle Evans logodef = drawer.logodefs["orb"]; 367088b4f5fSWarner Losh else 368bb26c57dSKyle Evans logodef = drawer.logodefs["orbbw"]; 369088b4f5fSWarner Losh end 370088b4f5fSWarner Losh end 371bb26c57dSKyle Evans if (logodef.shift ~= nil) then 372bb26c57dSKyle Evans x = x + logodef.shift.x; 373bb26c57dSKyle Evans y = y + logodef.shift.y; 374bb26c57dSKyle Evans end 375752b2d40SKyle Evans drawer.draw(x, y, logodef.graphic); 376088b4f5fSWarner Loshend 377088b4f5fSWarner Losh 37824a1bd54SKyle Evansreturn drawer; 379