snakeware

Python-based Linux distro for x86-64 and RPi
git clone git://squid-tech.com/snakeware.git
Log | Files | Refs | README

commit fa12656edf3e48a113bbd67b186efc24f252bbc1
parent 6ba966819ce8bf2aeb7b0ba623a677014c087a36
Author: Josh Moore <jxm5210@rit.edu>
Date:   Wed, 27 May 2020 19:24:04 -0400

Add loadapp() func, refactor

Diffstat:
MREADME.md | 4++--
Msnakeware/py_modules.sh | 3++-
Asnakewm/.gitignore | 1+
Asnakewm/__init__.py | 1+
Asnakewm/apps/test/HelloWorld/__init__.py | 14++++++++++++++
Dsnakewm/snakewm.py | 82-------------------------------------------------------------------------------
Asnakewm/wm.py | 90+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 110 insertions(+), 85 deletions(-)

diff --git a/README.md b/README.md @@ -20,7 +20,7 @@ Then, burn the image file to a flash drive and boot it, or launch it in QEMU wit Once you are booted into the Python environment, launch snakewm with these commands: ``` ->>> from snakewm import SnakeWM +>>> from snakewm.wm import SnakeWM >>> SnakeWM().run() ``` @@ -34,7 +34,7 @@ The build scripts currently steal shared object files from the host system to pa we will likely build these libraries from source instead of copying them from the host. **NOTE:** If you are only contributing apps or other code to snakewm, you don't need to build a whole snakeware distro -image to test your changes. Simply make your changes to snakewm then run `sudo python snakewm.py` in the `snakewm/` +image to test your changes. Simply make your changes to snakewm then run `sudo python wm.py` in the `snakewm/` directory. snakewm will then start drawing itself directly to the framebuffer and you can test out your changes. Press `ALT+ESC` to return to your normal desktop. diff --git a/snakeware/py_modules.sh b/snakeware/py_modules.sh @@ -1,7 +1,8 @@ # Install python modules # Run this after the build.sh script finishes. +# install pip modules build/usr/bin/python3 -m pip install -r config/pip_modules.txt # snakeware python packages -cp ../snakewm/snakewm.py build/usr/lib/python3.8/ +cp -r ../snakewm/ build/usr/lib/python3.8/ diff --git a/snakewm/.gitignore b/snakewm/.gitignore @@ -0,0 +1 @@ +__pycache__ diff --git a/snakewm/__init__.py b/snakewm/__init__.py @@ -0,0 +1 @@ + diff --git a/snakewm/apps/test/HelloWorld/__init__.py b/snakewm/apps/test/HelloWorld/__init__.py @@ -0,0 +1,14 @@ +import pygame +import pygame_gui + +def load(manager, params): + """ + An example implementation of the load() function called by the snakewm + loadapp() function. + """ + pygame_gui.windows.UIMessageWindow( + rect=pygame.Rect((10, 10), (300, 160)), + window_title="snakeware", + html_message="Hello World!", + manager=manager + ) diff --git a/snakewm/snakewm.py b/snakewm/snakewm.py @@ -1,82 +0,0 @@ -""" -Snake Window Manager -""" - -import os -import sys - -import pygame, pygame_gui - -BG_COLOR = (0, 128, 128) - -class SnakeWM: - SCREEN = None - DIMS = None - BG = None - MANAGER = None - - def __init__(self): - pygame.init() - - # initialize pygame to framebuffer - os.putenv('SDL_FBDEV', '/dev/fb0') - pygame.display.init() - - # get screen dimensions - self.DIMS = ( - pygame.display.Info().current_w, - pygame.display.Info().current_h - ) - - # init screen - self.SCREEN = pygame.display.set_mode( - self.DIMS, - pygame.FULLSCREEN - ) - - # init background - self.BG = pygame.Surface((self.DIMS)) - self.BG.fill(BG_COLOR) - - # init UI manager - self.MANAGER = pygame_gui.UIManager(self.DIMS) - - pygame.mouse.set_visible(True) - pygame.display.update() - - def alert(self, title, msg, pos): - pygame_gui.windows.UIMessageWindow( - rect=pygame.Rect(pos, (300, 160)), - window_title=title, - html_message=msg, - manager=self.MANAGER - ) - - def run(self): - clock = pygame.time.Clock() - running = True - - self.alert('Test', 'Hello World!', (100, 100)) - self.alert('lol', 'It just werks', (500, 500)) - - while running: - delta = clock.tick(60) / 1000.0 - - pressed = pygame.key.get_pressed() - - for event in pygame.event.get(): - if event.type == pygame.KEYDOWN: - if event.key == pygame.K_ESCAPE and pressed[pygame.K_LALT]: - running = False - - self.MANAGER.process_events(event) - - self.MANAGER.update(delta) - - self.SCREEN.blit(self.BG, (0, 0)) - self.MANAGER.draw_ui(self.SCREEN) - pygame.display.update() - -if __name__ == '__main__': - wm = SnakeWM() - wm.run() diff --git a/snakewm/wm.py b/snakewm/wm.py @@ -0,0 +1,90 @@ +""" +Snake Window Manager +""" + +import os +import sys +import importlib + +import pygame, pygame_gui + +BG_COLOR = (0, 128, 128) + +class SnakeWM: + SCREEN = None + DIMS = None + BG = None + MANAGER = None + + def __init__(self): + pygame.init() + + # initialize pygame to framebuffer + os.putenv('SDL_FBDEV', '/dev/fb0') + pygame.display.init() + + # get screen dimensions + self.DIMS = ( + pygame.display.Info().current_w, + pygame.display.Info().current_h + ) + + # init screen + self.SCREEN = pygame.display.set_mode( + self.DIMS, + pygame.FULLSCREEN + ) + + # init background + self.BG = pygame.Surface((self.DIMS)) + self.BG.fill(BG_COLOR) + + # init UI manager + self.MANAGER = pygame_gui.UIManager(self.DIMS) + + pygame.mouse.set_visible(True) + pygame.display.update() + + def loadapp(self, app, params=None): + """ + Load and run an app based on its location (ie app="apps.test.HelloWorld"). + Apps are basically just Python packages. The loaded app directory must contain an __init__.py + with a load() function that accepts a UIManager parameter and a params list parameter. + + The load() function should create an instance of the app to load and add the app UI to + the passed UIManager object. See existing apps for examples. + """ + if __name__ != '__main__': + app = 'snakewm.' + app + + _app = importlib.import_module(app) + _app.load(self.MANAGER, params) + + def run(self): + clock = pygame.time.Clock() + running = True + + while running: + delta = clock.tick(60) / 1000.0 + + pressed = pygame.key.get_pressed() + + for event in pygame.event.get(): + if event.type == pygame.KEYDOWN: + if pressed[pygame.K_LALT]: + if event.key == pygame.K_ESCAPE: + running = False + elif event.key == pygame.K_h: + self.loadapp("apps.test.HelloWorld") + + self.MANAGER.process_events(event) + + self.MANAGER.update(delta) + + self.SCREEN.blit(self.BG, (0, 0)) + self.MANAGER.draw_ui(self.SCREEN) + pygame.display.update() + +if __name__ == '__main__': + wm = SnakeWM() + wm.run()