From 39de89fff5a92992288807f6804b06a5b5827040 Mon Sep 17 00:00:00 2001 From: chrys Date: Sun, 23 Apr 2017 01:41:24 +0200 Subject: [PATCH] add initial eventManager --- src/fenrir/core/eventManager.py | 73 +++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/fenrir/core/eventManager.py diff --git a/src/fenrir/core/eventManager.py b/src/fenrir/core/eventManager.py new file mode 100644 index 00000000..345645e5 --- /dev/null +++ b/src/fenrir/core/eventManager.py @@ -0,0 +1,73 @@ +#!/bin/python +# -*- coding: utf-8 -*- + +# Fenrir TTY screen reader +# By Chrys, Storm Dragon, and contributers. + +#from core import debug +from threading import Thread +from queue import Queue, Empty +import time +from enum import Enum + + +class fenrirEventType(Enum): + Ignore = 0 + ScreenUpdate = 1 + KeyboardInput = 2 + BrailleInput = 3 + PlugInputDevice = 4 + BrailleFlush = 5 + ScreenChanged = 6 + StopMainLoop = 7 + def __int__(self): + return self.value + def __str__(self): + return self.name + + +class eventQueue(Queue): + def clear(self): + try: + while True: + self.get_nowait() + except Empty: + pass + + +class eventManager(): + def __init__(self): + self._eventThreads = [] + self._eventQueue = eventQueue() + def initialize(self, environment): + self.env = environment + def shutdown(self): + self._eventQueue.clear() + def proceedEventLoop(self): + event = self._eventQueue.get() + print(event) + return(event != fenrirEventType.StopMainLoop) + def startMainEventLoop(self): + while(self.proceedEventLoop()): + pass + def addEventThread(self, event, function): + t = Thread(target=self.eventWorkerThread, args=(event, function)) + self._eventThreads.append(t) + t.start() + def eventWorkerThread(self, event, function): + for i in range(20): +# while True: + Data = function() + self._eventQueue.put({"EVENT":event,"DATA":Data}) + + +def p(): + time.sleep(0.2) + return("p") + + +e = eventManager() +e.addEventThread(fenrirEventType.ScreenUpdate,p) +e.addEventThread(fenrirEventType.BrailleInput,p) +e.addEventThread(fenrirEventType.PlugInputDevice,p) +e.addEventThread(fenrirEventType.ScreenChanged,p)