forked from dersphere/script.screensaver.multi_slideshow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreensaver.py
580 lines (508 loc) · 19.5 KB
/
screensaver.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2013 Tristan Fischer ([email protected])
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import random
import sys
if sys.version_info >= (2, 7):
import json
else:
import simplejson as json
import xbmc
import xbmcaddon
import xbmcvfs
from xbmcgui import ControlImage, WindowDialog
addon = xbmcaddon.Addon()
ADDON_NAME = addon.getAddonInfo('name')
ADDON_PATH = addon.getAddonInfo('path')
MODES = (
'TableDrop',
'StarWars',
'RandomZoomIn',
'AppleTVLike',
'GridSwitch',
'Random',
)
SOURCES = (
'movies',
'image_folder',
'albums',
'shows',
)
PROPS = (
'fanart',
'thumbnail',
)
CHUNK_WAIT_TIME = 250
ACTION_IDS_EXIT = [9, 10, 13, 92]
class ScreensaverManager(object):
def __new__(cls):
mode = MODES[int(addon.getSetting('mode'))]
if mode == 'Random':
subcls = random.choice(ScreensaverBase.__subclasses__())
return subcls()
for subcls in ScreensaverBase.__subclasses__():
if subcls.MODE == mode:
return subcls()
raise ValueError('Not a valid ScreensaverBase subclass: %s' % mode)
class ExitMonitor(xbmc.Monitor):
def __init__(self, exit_callback):
self.exit_callback = exit_callback
def onScreensaverDeactivated(self):
self.exit_callback()
class ScreensaverWindow(WindowDialog):
def __init__(self, exit_callback):
self.exit_callback = exit_callback
def onAction(self, action):
action_id = action.getId()
if action_id in ACTION_IDS_EXIT:
self.exit_callback()
class ScreensaverBase(object):
MODE = None
IMAGE_CONTROL_COUNT = 10
FAST_IMAGE_COUNT = 0
NEXT_IMAGE_TIME = 2000
BACKGROUND_IMAGE = 'black.jpg'
def __init__(self):
self.log('__init__ start')
self.exit_requested = False
self.background_control = None
self.preload_control = None
self.image_count = 0
self.image_controls = []
self.global_controls = []
self.exit_monitor = ExitMonitor(self.stop)
self.xbmc_window = ScreensaverWindow(self.stop)
self.xbmc_window.show()
self.init_global_controls()
self.load_settings()
self.init_cycle_controls()
self.stack_cycle_controls()
self.log('__init__ end')
def init_global_controls(self):
self.log('init_global_controls start')
loading_img = xbmc.validatePath('/'.join((
ADDON_PATH, 'resources', 'media', 'loading.gif'
)))
self.loading_control = ControlImage(576, 296, 128, 128, loading_img)
self.preload_control = ControlImage(-1, -1, 1, 1, '')
self.background_control = ControlImage(0, 0, 1280, 720, '')
self.global_controls = [
self.preload_control, self.background_control, self.loading_control
]
self.xbmc_window.addControls(self.global_controls)
self.log('init_global_controls end')
def load_settings(self):
pass
def init_cycle_controls(self):
self.log('init_cycle_controls start')
for i in xrange(self.IMAGE_CONTROL_COUNT):
img_control = ControlImage(0, 0, 0, 0, '', aspectRatio=1)
self.image_controls.append(img_control)
self.log('init_cycle_controls end')
def stack_cycle_controls(self):
self.log('stack_cycle_controls start')
# add controls to the window in same order as image_controls list
# so any new image will be in front of all previous images
self.xbmc_window.addControls(self.image_controls)
self.log('stack_cycle_controls end')
def start_loop(self):
self.log('start_loop start')
images = self.get_images()
if addon.getSetting('random_order') == 'true':
random.shuffle(images)
image_url_cycle = cycle(images)
image_controls_cycle = cycle(self.image_controls)
self.hide_loading_indicator()
image_url = image_url_cycle.next()
while not self.exit_requested:
self.log('using image: %s' % repr(image_url))
image_control = image_controls_cycle.next()
self.process_image(image_control, image_url)
image_url = image_url_cycle.next()
if self.image_count < self.FAST_IMAGE_COUNT:
self.image_count += 1
else:
self.preload_image(image_url)
self.wait()
self.log('start_loop end')
def get_images(self):
self.log('get_images')
self.image_aspect_ratio = 16.0 / 9.0
source = SOURCES[int(addon.getSetting('source'))]
prop = PROPS[int(addon.getSetting('prop'))]
images = []
if source == 'movies':
images = self._get_json_images('VideoLibrary.GetMovies', 'movies', prop)
elif source == 'albums':
images = self._get_json_images('AudioLibrary.GetAlbums', 'albums', prop)
elif source == 'shows':
images = self._get_json_images('VideoLibrary.GetTVShows', 'tvshows', prop)
elif source == 'image_folder':
path = addon.getSetting('image_path')
if path:
images = self._get_folder_images(path)
if not images:
cmd = 'XBMC.Notification("{header}", "{message}")'.format(
header=addon.getLocalizedString(32500),
message=addon.getLocalizedString(32501)
)
xbmc.executebuiltin(cmd)
images = (
self._get_json_images('VideoLibrary.GetMovies', 'movies', 'fanart')
or self._get_json_images('AudioLibrary.GetArtists', 'artists', 'fanart')
)
return images
def _get_json_images(self, method, key, prop):
self.log('_get_json_images start')
query = {
'jsonrpc': '2.0',
'id': 0,
'method': method,
'params': {
'properties': [prop],
}
}
response = json.loads(xbmc.executeJSONRPC(json.dumps(query)))
images = [
element[prop] for element
in response.get('result', {}).get(key, [])
if element.get(prop)
]
self.log('_get_json_images end')
return images
def _get_folder_images(self, path):
self.log('_get_folder_images started with path: %s' % repr(path))
dirs, files = xbmcvfs.listdir(path)
images = [
xbmc.validatePath(path + f) for f in files
if f.lower()[-3:] in ('jpg', 'png')
]
if addon.getSetting('recursive') == 'true':
for directory in dirs:
if directory.startswith('.'):
continue
images.extend(
self._get_folder_images(
xbmc.validatePath('/'.join((path, directory, '')))
)
)
self.log('_get_folder_images ends')
return images
def hide_loading_indicator(self):
bg_img = xbmc.validatePath('/'.join((
ADDON_PATH, 'resources', 'media', self.BACKGROUND_IMAGE
)))
self.loading_control.setAnimations([(
'conditional',
'effect=fade start=100 end=0 time=500 condition=true'
)])
self.background_control.setAnimations([(
'conditional',
'effect=fade start=0 end=100 time=500 delay=500 condition=true'
)])
self.background_control.setImage(bg_img)
def process_image(self, image_control, image_url):
# Needs to be implemented in sub class
raise NotImplementedError
def preload_image(self, image_url):
# set the next image to an unvisible image-control for caching
self.log('preloading image: %s' % repr(image_url))
self.preload_control.setImage(image_url)
self.log('preloading done')
def wait(self):
# wait in chunks of 500ms to react earlier on exit request
chunk_wait_time = int(CHUNK_WAIT_TIME)
remaining_wait_time = int(self.NEXT_IMAGE_TIME)
while remaining_wait_time > 0:
if self.exit_requested:
self.log('wait aborted')
return
if remaining_wait_time < chunk_wait_time:
chunk_wait_time = remaining_wait_time
remaining_wait_time -= chunk_wait_time
xbmc.sleep(chunk_wait_time)
def stop(self):
self.log('stop')
self.exit_requested = True
self.exit_monitor = None
def close(self):
self.del_controls()
def del_controls(self):
self.log('del_controls start')
self.xbmc_window.removeControls(self.image_controls)
self.xbmc_window.removeControls(self.global_controls)
self.preload_control = None
self.background_control = None
self.loading_control = None
self.image_controls = []
self.global_controls = []
self.xbmc_window.close()
self.xbmc_window = None
self.log('del_controls end')
def log(self, msg):
xbmc.log(u'%s: %s' % (ADDON_NAME, msg))
class TableDropScreensaver(ScreensaverBase):
MODE = 'TableDrop'
BACKGROUND_IMAGE = 'table.jpg'
IMAGE_CONTROL_COUNT = 20
FAST_IMAGE_COUNT = 0
NEXT_IMAGE_TIME = 1500
MIN_WIDTH = 500
MAX_WIDTH = 700
def load_settings(self):
self.NEXT_IMAGE_TIME = int(addon.getSetting('tabledrop_wait'))
def process_image(self, image_control, image_url):
ROTATE_ANIMATION = (
'effect=rotate start=0 end=%d center=auto time=%d '
'delay=0 tween=circle condition=true'
)
DROP_ANIMATION = (
'effect=zoom start=%d end=100 center=auto time=%d '
'delay=0 tween=circle condition=true'
)
FADE_ANIMATION = (
'effect=fade start=0 end=100 time=200 '
'condition=true'
)
# hide the image
image_control.setVisible(False)
image_control.setImage('')
# re-stack it (to be on top)
self.xbmc_window.removeControl(image_control)
self.xbmc_window.addControl(image_control)
# calculate all parameters and properties
width = random.randint(self.MIN_WIDTH, self.MAX_WIDTH)
height = int(width / self.image_aspect_ratio)
x_position = random.randint(0, 1280 - width)
y_position = random.randint(0, 720 - height)
drop_height = random.randint(400, 800)
drop_duration = drop_height * 1.5
rotation_degrees = random.uniform(-20, 20)
rotation_duration = drop_duration
animations = [
('conditional', FADE_ANIMATION),
('conditional',
ROTATE_ANIMATION % (rotation_degrees, rotation_duration)),
('conditional',
DROP_ANIMATION % (drop_height, drop_duration)),
]
# set all parameters and properties
image_control.setImage(image_url)
image_control.setPosition(x_position, y_position)
image_control.setWidth(width)
image_control.setHeight(height)
image_control.setAnimations(animations)
# show the image
image_control.setVisible(True)
class StarWarsScreensaver(ScreensaverBase):
MODE = 'StarWars'
BACKGROUND_IMAGE = 'stars.jpg'
IMAGE_CONTROL_COUNT = 6
SPEED = 0.5
def load_settings(self):
self.SPEED = float(addon.getSetting('starwars_speed'))
self.EFFECT_TIME = 9000.0 / self.SPEED
self.NEXT_IMAGE_TIME = self.EFFECT_TIME / 7.6
def process_image(self, image_control, image_url):
TILT_ANIMATION = (
'effect=rotatex start=0 end=55 center=auto time=0 '
'condition=true'
)
MOVE_ANIMATION = (
'effect=slide start=0,1280 end=0,-2560 time=%d '
'tween=linear condition=true'
)
# hide the image
image_control.setImage('')
image_control.setVisible(False)
# re-stack it (to be on top)
self.xbmc_window.removeControl(image_control)
self.xbmc_window.addControl(image_control)
# calculate all parameters and properties
width = 1280
height = 720
x_position = 0
y_position = 0
animations = [
('conditional', TILT_ANIMATION),
('conditional', MOVE_ANIMATION % self.EFFECT_TIME),
]
# set all parameters and properties
image_control.setPosition(x_position, y_position)
image_control.setWidth(width)
image_control.setHeight(height)
image_control.setAnimations(animations)
image_control.setImage(image_url)
# show the image
image_control.setVisible(True)
class RandomZoomInScreensaver(ScreensaverBase):
MODE = 'RandomZoomIn'
IMAGE_CONTROL_COUNT = 7
NEXT_IMAGE_TIME = 2000
EFFECT_TIME = 5000
def load_settings(self):
self.NEXT_IMAGE_TIME = int(addon.getSetting('randomzoom_wait'))
self.EFFECT_TIME = int(addon.getSetting('randomzoom_effect'))
def process_image(self, image_control, image_url):
ZOOM_ANIMATION = (
'effect=zoom start=1 end=100 center=%d,%d time=%d '
'tween=quadratic condition=true'
)
# hide the image
image_control.setVisible(False)
image_control.setImage('')
# re-stack it (to be on top)
self.xbmc_window.removeControl(image_control)
self.xbmc_window.addControl(image_control)
# calculate all parameters and properties
width = 1280
height = 720
x_position = 0
y_position = 0
zoom_x = random.randint(0, 1280)
zoom_y = random.randint(0, 720)
animations = [
('conditional', ZOOM_ANIMATION % (zoom_x, zoom_y, self.EFFECT_TIME)),
]
# set all parameters and properties
image_control.setImage(image_url)
image_control.setPosition(x_position, y_position)
image_control.setWidth(width)
image_control.setHeight(height)
image_control.setAnimations(animations)
# show the image
image_control.setVisible(True)
class AppleTVLikeScreensaver(ScreensaverBase):
MODE = 'AppleTVLike'
IMAGE_CONTROL_COUNT = 35
FAST_IMAGE_COUNT = 2
DISTANCE_RATIO = 0.7
SPEED = 1.0
CONCURRENCY = 1.0
def load_settings(self):
self.SPEED = float(addon.getSetting('appletvlike_speed'))
self.CONCURRENCY = float(addon.getSetting('appletvlike_concurrency'))
self.MAX_TIME = int(15000 / self.SPEED)
self.NEXT_IMAGE_TIME = int(4500.0 / self.CONCURRENCY / self.SPEED)
def stack_cycle_controls(self):
# randomly generate a zoom in percent as betavariant
# between 10 and 70 and assign calculated width to control.
# Remove all controls from window and re-add sorted by size.
# This is needed because the bigger (=nearer) ones need to be in front
# of the smaller ones.
# Then shuffle image list again to have random size order.
for image_control in self.image_controls:
zoom = int(random.betavariate(2, 2) * 40) + 10
#zoom = int(random.randint(10, 70))
width = 1280 / 100 * zoom
image_control.setWidth(width)
self.image_controls = sorted(
self.image_controls, key=lambda c: c.getWidth()
)
self.xbmc_window.addControls(self.image_controls)
random.shuffle(self.image_controls)
def process_image(self, image_control, image_url):
MOVE_ANIMATION = (
'effect=slide start=0,720 end=0,-720 center=auto time=%s '
'tween=linear delay=0 condition=true'
)
image_control.setVisible(False)
image_control.setImage('')
# calculate all parameters and properties based on the already set
# width. We can not change the size again because all controls need
# to be added to the window in size order.
width = image_control.getWidth()
zoom = width * 100 / 1280
height = int(width / self.image_aspect_ratio)
# let images overlap max 1/2w left or right
center = random.randint(0, 1280)
x_position = center - width / 2
y_position = 0
time = self.MAX_TIME / zoom * self.DISTANCE_RATIO * 100
animations = [
('conditional', MOVE_ANIMATION % time),
]
# set all parameters and properties
image_control.setImage(image_url)
image_control.setPosition(x_position, y_position)
image_control.setWidth(width)
image_control.setHeight(height)
image_control.setAnimations(animations)
# show the image
image_control.setVisible(True)
class GridSwitchScreensaver(ScreensaverBase):
MODE = 'GridSwitch'
ROWS_AND_COLUMNS = 4
NEXT_IMAGE_TIME = 1000
EFFECT_TIME = 500
RANDOM_ORDER = False
IMAGE_CONTROL_COUNT = ROWS_AND_COLUMNS ** 2
FAST_IMAGE_COUNT = IMAGE_CONTROL_COUNT
def load_settings(self):
self.NEXT_IMAGE_TIME = int(addon.getSetting('gridswitch_wait'))
self.ROWS_AND_COLUMNS = int(addon.getSetting('gridswitch_rows_columns'))
self.RANDOM_ORDER = addon.getSetting('gridswitch_random') == 'true'
self.IMAGE_CONTROL_COUNT = self.ROWS_AND_COLUMNS ** 2
self.FAST_IMAGE_COUNT = self.IMAGE_CONTROL_COUNT
def stack_cycle_controls(self):
# Set position and dimensions based on stack position.
# Shuffle image list to have random order.
super(GridSwitchScreensaver, self).stack_cycle_controls()
for i, image_control in enumerate(self.image_controls):
current_row, current_col = divmod(i, self.ROWS_AND_COLUMNS)
width = 1280 / self.ROWS_AND_COLUMNS
height = 720 / self.ROWS_AND_COLUMNS
x_position = width * current_col
y_position = height * current_row
image_control.setPosition(x_position, y_position)
image_control.setWidth(width)
image_control.setHeight(height)
if self.RANDOM_ORDER:
random.shuffle(self.image_controls)
def process_image(self, image_control, image_url):
if not self.image_count < self.FAST_IMAGE_COUNT:
FADE_OUT_ANIMATION = (
'effect=fade start=100 end=0 time=%d condition=true' % self.EFFECT_TIME
)
animations = [
('conditional', FADE_OUT_ANIMATION),
]
image_control.setAnimations(animations)
xbmc.sleep(self.EFFECT_TIME)
image_control.setImage(image_url)
FADE_IN_ANIMATION = (
'effect=fade start=0 end=100 time=%d condition=true' % self.EFFECT_TIME
)
animations = [
('conditional', FADE_IN_ANIMATION),
]
image_control.setAnimations(animations)
def cycle(iterable):
saved = []
for element in iterable:
yield element
saved.append(element)
while saved:
for element in saved:
yield element
if __name__ == '__main__':
screensaver = ScreensaverManager()
screensaver.start_loop()
screensaver.close()
del screensaver
sys.modules.clear()