forked from TomW1605/EDAutopilot
-
Notifications
You must be signed in to change notification settings - Fork 2
/
dev_tray.py
96 lines (71 loc) · 2.38 KB
/
dev_tray.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import keyboard
import threading
from PIL import Image
from pystray import Icon # , MenuItem, Menu
from os.path import join, abspath
from ctypes import pythonapi, py_object
from dev_autopilot import autopilot, get_bindings, clear_input, kill_ed, safe_net
STATE = 1
ICON = None
main_thread, safeNet_thread = None, None
class ThreadWithException(threading.Thread):
def __init__(self, name):
threading.Thread.__init__(self)
self.name = name
def run(self):
# target function of the thread class
if self.name == 'EDAutopilot':
autopilot(stop_action)
elif self.name == 'SafeNet':
safe_net(stop_action)
def get_id(self):
# returns id of the respective thread
if hasattr(self, '_thread_id'):
return self._thread_id
for t_id, thread in threading._active.items():
if thread is self:
return t_id
def raise_exception(self):
thread_id = self.get_id()
res = pythonapi.PyThreadState_SetAsyncExc(thread_id, py_object(SystemExit))
if res > 1:
pythonapi.PyThreadState_SetAsyncExc(thread_id, 0)
print('Exception raise failure')
def setup(icon):
pass
# icon.visible = True
def exit_action():
stop_action()
ICON.visible = False
ICON.stop()
def start_action():
global main_thread
main_thread = ThreadWithException("EDAutopilot")
main_thread.start()
main_thread.isAlive = main_thread.is_alive() # KThread workaround
global safeNet_thread
safeNet_thread = ThreadWithException("SafeNet")
safeNet_thread.start()
safeNet_thread.isAlive = safeNet_thread.is_alive()
def stop_action():
global main_thread, safeNet_thread
if main_thread != None and main_thread.isAlive:
main_thread.raise_exception()
main_thread.join()
if safeNet_thread != None and safeNet_thread.isAlive:
safeNet_thread.raise_exception()
safeNet_thread.join()
clear_input(get_bindings())
def tray():
global ICON
name = 'ED - Autopilot'
ICON = Icon(name=name, title=name)
logo = Image.open(join(abspath("."), 'src/logo.png'))
ICON.icon = logo
keyboard.add_hotkey('page up', start_action)
keyboard.add_hotkey('page down', stop_action)
keyboard.add_hotkey('end', kill_ed)
# keyboard.wait()
ICON.run(setup)
if __name__ == '__main__':
tray()