-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpidisplay.py
317 lines (261 loc) · 14.7 KB
/
pidisplay.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
"""
PI Display
(c) Steven Babineau - [email protected]
2022
"""
import inspect
import sys
import os
import time
import importlib
import datetime
import pygame
import pygame.ftfont
from enum import Enum
from lib import helper
from lib.fullscreen_plugin import FullScreenPlugin
from lib.plugin import Plugin, Singleton
import configparser
from lib.widget_plugin import WidgetPlugin
class Direction(Enum):
FORWARD = "forward"
BACKWARDS = "backwards"
def main():
""" Main is what Main is """
pygame.init()
if not pygame.ftfont.get_init():
pygame.ftfont.init()
pygame.display.set_caption('PiDisplay')
pygame.event.set_allowed([pygame.QUIT, pygame.KEYDOWN])
clock = pygame.time.Clock()
config = configparser.RawConfigParser()
config.read(os.path.abspath(os.path.join(os.path.dirname(__file__), "config.ini")))
appconfig = config["pidisplay"]
debug = appconfig.getboolean("debug")
if pygame.version.vernum[0] == 2:
helper.log(debug, "Running with pygame 2.X options.")
flags = 0
else:
helper.log(debug, "Running with pygame 1.X options.")
flags = pygame.HWSURFACE | pygame.DOUBLEBUF
screen_width = appconfig.getint("screen_width")
screen_height = appconfig.getint("screen_height")
if appconfig.getboolean("fullscreen_mode"):
flags |= pygame.FULLSCREEN
if appconfig.getboolean("fullscreen_uses_current_resolution"):
screen_width = 0
screen_height = 0
if pygame.version.vernum[0] == 2:
pygame.display.set_mode([screen_width, screen_height], flags, vsync=1)
else:
pygame.display.set_mode([screen_width, screen_height], flags)
canvas = pygame.display.get_surface()
pygame.mouse.set_pos((int(canvas.get_width()/2), int(canvas.get_height()/2)))
pygame.mouse.set_visible(False)
plugin_modules = get_plugins(debug)
plugins = []
for i in config.sections():
if i == "pidisplay":
continue
plugin_class_name = config[i]["class"] if "class" in config[i] else ""
plugin_class = None
for j in plugin_modules:
if j["class_name"] == plugin_class_name:
plugin_class = j["class"]
break
plugin_widget_location = config[i]["widget_location"] if "widget_location" in config[i] else ""
plugin_autoswitch_timer = int(config[i]["autoswitch_timer"]) if "autoswitch_timer" in config[i] else sys.maxsize
if plugin_class is not None:
plugins.append({"internal_name": i,
"class": plugin_class,
"widget_location": plugin_widget_location,
"autoswitch_timer": plugin_autoswitch_timer,
"widget_height": config[i].getint("widget_height")})
else:
helper.log(debug, "Couldn't find class for {}".format(plugin_class_name))
helper.log(debug, "Total modules found: {}".format(len(plugins)))
running = True
update = True
fps = appconfig.getint("frames_per_second")
top_widget_plugins = []
top_bar_canvases = []
bottom_widget_plugins = []
bottom_bar_canvases = []
full_screen_plugins = []
top_bar_y = 0
bottom_bar_y = canvas.get_height()
for i in range(len(plugins)):
plugin = plugins[i]
if WidgetPlugin in plugin["class"].__bases__:
if plugin["widget_location"] == helper.WIDGET_LOCATION_TOP:
top_bar_canvas = canvas.subsurface(pygame.Rect(0, top_bar_y, canvas.get_width(), plugin["widget_height"]))
top_widget_plugins.append({"location": plugin["widget_location"],
"instance": plugin["class"](helper, top_bar_canvas, config[plugin["internal_name"]])})
top_bar_y += plugin["widget_height"]
top_bar_canvases.append(top_bar_canvas)
elif plugin["widget_location"] == helper.WIDGET_LOCATION_BOTTOM:
bottom_bar_y -= plugin["widget_height"]
bottom_bar_canvas = canvas.subsurface(pygame.Rect(0, bottom_bar_y, canvas.get_width(), plugin["widget_height"]))
bottom_widget_plugins.append({"location": plugin["widget_location"],
"instance": plugin["class"](helper, bottom_bar_canvas, config[plugin["internal_name"]])})
bottom_bar_canvases.append(bottom_bar_canvas)
elif FullScreenPlugin in plugin["class"].__bases__:
full_screen_plugins.append({"class": plugin["class"], "autoswitch_timer": plugin["autoswitch_timer"], "internal_name": config[plugin["internal_name"]]})
top_offset = top_bar_y
bottom_offset = canvas.get_height() - bottom_bar_y
doubleclick_timer = 0
current_plugin = -1
message_font = pygame.font.SysFont(appconfig["default_font_face"], appconfig.getint("default_font_size"))
message_step = 255.0 / (fps * appconfig.getint("message_popup_fade_time"))
full_screen_rect = pygame.Rect(0, top_offset, canvas.get_width(), canvas.get_height() - top_offset - bottom_offset)
helper.log(debug, str(full_screen_rect))
helper.log(debug, "WxH = {}x{}".format(canvas.get_width(), canvas.get_height()))
full_screen_canvas_small = canvas.subsurface(full_screen_rect)
if len(full_screen_plugins) > 0:
current_plugin, tick, full_screen_plugin, start_time = switch_plugin(current_plugin,
full_screen_plugins,
canvas,
full_screen_canvas_small,
Direction.FORWARD)
while running:
clock.tick(fps)
if update:
full_screen_plugin.update(tick, fps)
full_screen_plugin.just_in = False
if full_screen_plugins[current_plugin]["internal_name"].getboolean("show_widgets"):
for i in range(len(top_widget_plugins)):
top_widget_plugins[i]["instance"].update(tick, fps)
for i in range(len(bottom_widget_plugins)):
bottom_widget_plugins[i]["instance"].update(tick, fps)
if tick == fps:
tick = 1
else:
tick += 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
update = not update
elif event.key == pygame.K_ESCAPE or event.key == pygame.K_q:
running = False
elif event.key == pygame.K_RIGHT:
current_plugin, tick, full_screen_plugin, start_time = switch_plugin(current_plugin,
full_screen_plugins,
canvas,
full_screen_canvas_small,
Direction.FORWARD)
elif event.key == pygame.K_LEFT:
current_plugin, tick, full_screen_plugin, start_time = switch_plugin(current_plugin,
full_screen_plugins,
canvas,
full_screen_canvas_small,
Direction.BACKWARDS)
elif event.type == pygame.MOUSEBUTTONDOWN:
timer_set = False
if doubleclick_timer == 0:
pygame.time.set_timer(helper.EVENT_DOUBLECLICK, appconfig.getint("doubleclick_delay"))
timer_set = True
elif doubleclick_timer == 1:
pygame.time.set_timer(helper.EVENT_DOUBLECLICK, 0)
# Switch plugins
current_plugin, tick, full_screen_plugin, start_time = switch_plugin(current_plugin,
full_screen_plugins,
canvas,
full_screen_canvas_small,
Direction.FORWARD)
timer_set = False
if timer_set:
doubleclick_timer = 1
else:
doubleclick_timer = 0
elif event.type == helper.EVENT_DOUBLECLICK:
# timer timed out
full_screen_plugin.handle_click(pygame.mouse.get_pos())
pygame.time.set_timer(helper.EVENT_DOUBLECLICK, 0)
doubleclick_timer = 0
elif event.type == helper.EVENT_MESSAGE:
message = event.message[0]
if len(event.message) == 1:
opacity = 255 + (message_step * fps * appconfig.getint("message_popup_fade_delay"))
else:
opacity = event.message[1]
if opacity > 0:
opacity -= message_step
surf_message_text = message_font.render(event.message[0], True, (200, 200, 200))
surf_message = pygame.Surface((surf_message_text.get_width() + 40, # margin
surf_message_text.get_height() + 40))
surf_message.fill((32, 32, 32))
surf_message.blit(surf_message_text, (int(surf_message.get_width()/2 - surf_message_text.get_width()/2),
int(surf_message.get_height()/2 - surf_message_text.get_height()/2)))
pygame.draw.rect(surf_message, (200, 200, 200),
(0, 0, surf_message.get_width(), surf_message.get_height()), 3)
if opacity > 255:
surf_message.set_alpha(255)
else:
surf_message.set_alpha(opacity)
canvas.blit(surf_message, (int(canvas.get_width()/2 - surf_message.get_width()/2), canvas.get_height() - surf_message.get_height() - 50)) # bottom margin
my_event = pygame.event.Event(helper.EVENT_MESSAGE, message=[message, opacity])
pygame.event.post(my_event)
if update:
pygame.display.flip()
if time.time() - start_time > full_screen_plugins[current_plugin]["autoswitch_timer"] or full_screen_plugin.READY_TO_SWITCH:
full_screen_plugin.READY_TO_SWITCH = False
# Switch plugins
if appconfig.getboolean("take_screenshots"):
screenshot_file = os.path.abspath(os.path.join(os.path.dirname(__file__),
appconfig["screenshot_dir"],
"screenshot_{}.png".format(datetime.datetime.now().timestamp())))
pygame.image.save(canvas, screenshot_file)
helper.log(debug, "Saved screenshot to {}".format(screenshot_file))
current_plugin, tick, full_screen_plugin, start_time = switch_plugin(current_plugin,
full_screen_plugins,
canvas,
full_screen_canvas_small,
Direction.FORWARD)
else:
print("Enable a plugin first (make sure to specify the class key in config.ini)!")
pygame.quit()
sys.exit()
def switch_plugin(current_plugin, full_screen_plugins, canvas, canvas_small, direction):
if direction == Direction.FORWARD:
current_plugin += 1
if current_plugin == len(full_screen_plugins):
current_plugin = 0
elif direction == Direction.BACKWARDS:
current_plugin -= 1
if current_plugin < 0:
current_plugin = len(full_screen_plugins) - 1
else:
raise ValueError("Incorrect direction specified while switching full screen plugins!")
plugin_config_name = full_screen_plugins[current_plugin]["internal_name"]
if plugin_config_name.getboolean("show_widgets"):
full_screen_plugin = full_screen_plugins[current_plugin]["class"](helper, canvas_small,
plugin_config_name)
else:
full_screen_plugin = full_screen_plugins[current_plugin]["class"](helper, canvas,
plugin_config_name)
tick = 0
start_time = time.time()
pygame.display.flip()
return current_plugin, tick, full_screen_plugin, start_time
def get_plugins(debug):
plugins = []
plugin_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "plugins"))
helper.log(debug, "Plugin path: {}".format(plugin_path))
plugin_folders = os.listdir(plugin_path)
for folder in plugin_folders:
full_path = os.path.join(plugin_path, folder)
if os.path.isdir(full_path):
files = os.listdir(full_path)
for file in files:
if file[-3:] == ".py":
module = importlib.import_module("." + file[:-3], "plugins.{}".format(folder))
for name, obj in inspect.getmembers(module):
if (type(obj) == type or type(obj) == Singleton) and issubclass(obj, Plugin) and obj.__module__[:8] == "plugins.":
# Get the config
helper.log(debug, "Found plugin {}".format(obj.__name__))
plugins.append({"class_name": obj.__name__, "class": obj})
return plugins
if __name__ == '__main__':
main()