Skip to content

Instantly share code, notes, and snippets.

@Peetz0r
Last active July 25, 2020 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Peetz0r/54d1a39b5ca45e1f657762ef74ee85ac to your computer and use it in GitHub Desktop.
Save Peetz0r/54d1a39b5ca45e1f657762ef74ee85ac to your computer and use it in GitHub Desktop.
"post-boot" menu
#!/usr/bin/python
import sys, os, pygame, time
from pygame.locals import *
pygame.display.init()
pygame.font.init()
pygame.mouse.set_visible(False)
pygame.key.set_repeat(500, 30)
size = width,height = (pygame.display.Info().current_w, pygame.display.Info().current_h)
screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
iconsize = int(width/12)
margin = iconsize
print("Size: ", size)
class Icon(object):
im = None
rect = None
name = ''
cmd = []
def __init__(self, name, file, cmd):
self.im = pygame.transform.smoothscale(
pygame.image.load(file).convert_alpha(),
(iconsize, iconsize)
)
self.rect = self.im.get_rect()
self.name = name
self.cmd = cmd
icons = [
Icon('Gnome', 'gnome.png', ['sh', '-c', 'modprobe amdgpu; systemctl start gdm']),
Icon('Headless', 'headl.png', ['sh', '-c', 'systemctl start plymouth-quit; systemctl start getty@tty1']),
Icon('macOS VM', 'macos.png', ['virsh', 'start', 'macOS']),
Icon('Windows 10 VM', 'win10.png', ['virsh', 'start', 'win10']),
Icon('Power Off', 'power.png', ['poweroff']),
]
bgrt = pygame.image.load('/sys/firmware/acpi/bgrt/image').convert()
bgrt_rect = (
tuple([int(open('/sys/firmware/acpi/bgrt/%soffset'%a).read()) for a in ['x','y']]+
bgrt.get_rect()[2:])
)
selectsize = oldselected = selected = 0
largefont = pygame.font.SysFont(pygame.font.get_default_font(), int(height/12))
smallfont = pygame.font.SysFont(pygame.font.get_default_font(), int(height/60))
while True:
if selected != oldselected:
selectsize = iconsize * -.25
oldselected = selected
screen.fill((0, 0, 0))
screen.blit(bgrt, bgrt_rect)
for i, icon in enumerate(icons):
x = width/2 - (iconsize * len(icons) + margin * (len(icons)-1))/2
x = int(x + i * (iconsize + margin))
y = int(height*.7 - iconsize/2)
if i == selected:
if selectsize < iconsize*.25:
selectsize = min(iconsize*.25, selectsize + (iconsize*.25 - selectsize)*0.2)
screen.fill(
(127, 127, 127),
(
int(x-selectsize),
int(y-selectsize),
int(iconsize+selectsize*2),
int(iconsize+selectsize*2),
),
)
text = largefont.render(icon.name, True, (255, 255, 255))
screen.blit(text, (int(width/2 - text.get_rect()[2]/2), int(height*.90)))
text = smallfont.render(str(icon.cmd), True, (127, 127, 127))
screen.blit(text, (int(width/2 - text.get_rect()[2]/2), int(height*.975)))
screen.blit(icon.im, (x, y, iconsize, iconsize))
pygame.display.update()
try:
for e in pygame.event.get():
if e.type == QUIT:
pygame.quit()
sys.exit()
if e.type == KEYDOWN:
if e.key in [K_ESCAPE, K_q]:
pygame.quit()
sys.exit()
if e.key in [K_RETURN]:
pygame.quit()
if not icons[selected].cmd:
sys.exit()
os.execvp(icons[selected].cmd[0], icons[selected].cmd)
if e.key in [K_LEFT, K_UP]:
selected = selected -1
if e.key in [K_RIGHT, K_DOWN]:
selected = selected +1
if e.key in [K_PAGEUP, K_HOME]:
selected = 0
if e.key in [K_PAGEDOWN, K_END]:
selected = len(icons)-1
if K_0 <= e.key <= K_9:
selected = min(e.key-K_0, len(icons)) -1
except KeyboardInterrupt:
print('int')
pygame.quit()
sys.exit()
time.sleep(0.02)
selected = selected % len(icons)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment