from typing import List import logging import os import psutil from PIL import Image, ImageDraw, ImageFont from .screen import Screen class DebugScreen(Screen): def __init__(self, width: int, height: int, number_of_colour_bits: str): self.type = "Debug Screen" self.supports_custom_fonts = True self.width = width self.height = height self.image = Image.new(number_of_colour_bits, (self.width, self.height)) # Create a new image with 1 bit colour self.draw = ImageDraw.Draw(self.image) def setDefaultFont(self): logging.debug("setting font to default font") self.font = ImageFont.load_default() def setFont(self, font: str, size: int): logging.debug(f"setting font to {font} with size, {size}") self.font_size = size self.font = ImageFont.truetype(font, size) def clearBuffer(self): self.draw.rectangle((0, 0, self.width, self.height), outline=0, fill=0) def displayOnScreen(self): self.image.show() def displayLines(self, lines: List[str], x_margin: int = 2): # displays text on the screen, the line that is first in the list is displayed on the top self.clearBuffer() self.clearScreen() x_start = x_margin y_start = 0 x_padding = 0 y_padding = 0 for i, line in enumerate(lines): self.draw.text( ( x_start+(i*x_padding), y_start+y_padding ), line, font=self.font, fill=255 ) bounding_box = self.draw.textbbox( ( (x_start+x_padding), y_start+y_padding, ), line, font=self.font, ) bottom = bounding_box[3] y_padding = bottom self.displayOnScreen() def clearScreen(self): xdg_open_pid = -1 for p in psutil.process_iter(): if p.ppid() == os.getpid() and p.name() == "xdg-open": logging.debug(f"found xdg-open process with a process id {p.pid}") xdg_open_pid = p.pid elif xdg_open_pid != -1 and p.ppid() == xdg_open_pid: logging.debug(f"killing process, {p.name()} {p.cmdline()}") p.terminate() p.kill() self.clearBuffer() draw = ImageDraw.Draw(self.image) # black rectangle to clear screen, without clearing buffer draw.rectangle((0, 0, self.width, self.height), outline=0, fill=0)