-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhme.py
1473 lines (1249 loc) · 45.8 KB
/
hme.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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# HME for Python, v0.20
# Copyright 2012 William McBrine
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You didn't receive a copy of the license with this library because
# you already have dozens of copies, don't you? If not, visit gnu.org.
""" HME for Python
An implementation of TiVo's HME ("Home Media Extensions") Protocol
for Python. This is based on the protocol specification, but it is
not a port of the Java SDK, and it does things somewhat differently.
(Starting from the spec, I did end up with a lot of similarities to
the SDK, and I then tweaked this module to bring them closer, in
some respects; but there are still a lot of differences.)
Basic usage: import hme (or "from hme import *"); subclass the
"Application" class; and override some of the stub functions that
appear at the end of this file. If you want to use it with the
included start.py, then your program should be in the form of a
module (see the included examples). Aside from the app class, you
may want to include TITLE and/or CLASS_NAME strings; TITLE is the
display title, and CLASS_NAME is the name of your main app class.
(Both will be derived from the module name if absent.)
startup() is called first, before any events are handled; then the
event loop runs until either it's out of events (i.e. the socket
closed), or you set self.active to False. Finally, cleanup() is
called.
Items not yet implemented from the spec:
* Event pushing other than key presses
Java SDK items not in the spec and not implemented here:
* Persistent data -- you can get the tsn and Cookie from
self.context.headers, if using start.py, but what you do with
them is outside the scope of this module
* Numerous specific methods -- but some of these are unneeded; e.g.,
set_bounds() substitutes for setLocation() as well as setBounds()
"""
__author__ = 'William McBrine <[email protected]>'
__version__ = '0.20'
__license__ = 'LGPL'
import time
import string
import struct
#--- Constants --------------------------------------------------------
#
# Mostly as defined in the HME Protocol Specification.
HME_MAJOR_VERSION = 0
HME_MINOR_VERSION = 49
SAFE_ACTION_H = 32
SAFE_ACTION_V = 24
SAFE_TITLE_H = 64
SAFE_TITLE_V = 48
# Default object IDs
ID_NULL = 0
ID_ROOT_STREAM = 1
ID_ROOT_VIEW = 2
ID_DEFAULT_TTF = 10
ID_SYSTEM_TTF = 11
ID_BONK_SOUND = 20
ID_UPDOWN_SOUND = 21
ID_THUMBSUP_SOUND = 22
ID_THUMBSDOWN_SOUND = 23
ID_SELECT_SOUND = 24
ID_TIVO_SOUND = 25
ID_LEFT_SOUND = 26
ID_RIGHT_SOUND = 27
ID_PAGEUP_SOUND = 28
ID_PAGEDOWN_SOUND = 29
ID_ALERT_SOUND = 30
ID_DESELECT_SOUND = 31
ID_ERROR_SOUND = 32
ID_SLOWDOWN1_SOUND = 33
ID_SPEEDUP1_SOUND = 34
ID_SPEEDUP2_SOUND = 35
ID_SPEEDUP3_SOUND = 36
ID_SPEEDUP4_SOUND = 37 # Series 4+ only
ID_CLIENT = 2048
# Key actions
KEY_PRESS = 1
KEY_REPEAT = 2
KEY_RELEASE = 3
# Key codes
KEY_UNKNOWN = 0
KEY_TIVO = 1 # not sent
KEY_UP = 2
KEY_DOWN = 3
KEY_LEFT = 4
KEY_RIGHT = 5
KEY_SELECT = 6
KEY_PLAY = 7
KEY_PAUSE = 8
KEY_SLOW = 9
KEY_REVERSE = 10
KEY_FORWARD = 11
KEY_REPLAY = 12
KEY_ADVANCE = 13
KEY_THUMBSUP = 14
KEY_THUMBSDOWN = 15
KEY_VOLUMEUP = 16
KEY_VOLUMEDOWN = 17
KEY_CHANNELUP = 18
KEY_CHANNELDOWN = 19
KEY_MUTE = 20
KEY_RECORD = 21
KEY_OPT_WINDOW = 22
KEY_OPT_PIP = KEY_OPT_WINDOW
KEY_OPT_ASPECT = KEY_OPT_WINDOW
KEY_OPT_ZOOM = KEY_OPT_WINDOW
KEY_LIVE_TV = 23 # not sent
KEY_OPT_EXIT = 24 # not sent
KEY_INFO = 25
KEY_DISPLAY = KEY_INFO
KEY_OPT_LIST = 26 # not sent
KEY_OPT_GUIDE = 27 # not sent
KEY_CLEAR = 28
KEY_ENTER = 29
KEY_NUM0 = 40
KEY_NUM1 = 41
KEY_NUM2 = 42
KEY_NUM3 = 43
KEY_NUM4 = 44
KEY_NUM5 = 45
KEY_NUM6 = 46
KEY_NUM7 = 47
KEY_NUM8 = 48
KEY_NUM9 = 49
KEY_OPT_STOP = 51
KEY_OPT_MENU = 52
KEY_OPT_TOP_MENU = 53
KEY_OPT_ANGLE = 54
KEY_OPT_DVD = 55 # not sent
KEY_OPT_A = 56
KEY_OPT_B = 57
KEY_OPT_C = 58
KEY_OPT_D = 59
KEY_OPT_TV_POWER = 60
KEY_OPT_TV_INPUT = 61
KEY_OPT_VOD = 62
KEY_OPT_POWER = 63
KEY_BACKSPACE = 65
# Transitions
TRANSITION_FORWARD = 1
TRANSITION_BACK = 2
TRANSITION_TELEPORT = 3
# Application errors
APP_ERROR_UNKNOWN = 0
APP_ERROR_BAD_ARGUMENT = 1
APP_ERROR_BAD_COMMAND = 2
APP_ERROR_RSRC_NOT_FOUND = 3
APP_ERROR_VIEW_NOT_FOUND = 4
APP_ERROR_OUT_OF_MEMORY = 5
APP_ERROR_INVALID_TRANSITION = 6
APP_ERROR_INVALID_RESOLUTION = 7
APP_ERROR_OTHER = 100
# Resource flags
RSRC_HALIGN_LEFT = 1
RSRC_HALIGN_CENTER = 2
RSRC_HALIGN_RIGHT = 4
RSRC_VALIGN_TOP = 0x10
RSRC_VALIGN_CENTER = 0x20
RSRC_VALIGN_BOTTOM = 0x40
RSRC_TEXT_WRAP = 0x0100
RSRC_IMAGE_HFIT = 0x1000
RSRC_IMAGE_VFIT = 0x2000
RSRC_IMAGE_BESTFIT = 0x4000
# Resource states
RSRC_STATUS_UNKNOWN = 0
RSRC_STATUS_CONNECTING = 1
RSRC_STATUS_CONNECTED = 2
RSRC_STATUS_LOADING = 3
RSRC_STATUS_READY = 4
RSRC_STATUS_PLAYING = 5
RSRC_STATUS_PAUSED = 6
RSRC_STATUS_SEEKING = 7
RSRC_STATUS_CLOSED = 8
RSRC_STATUS_COMPLETE = 9
RSRC_STATUS_ERROR = 10
RSRC_STATUS_END = 11
RSRC_STATUS_EOB = 12
# Resource errors
RSRC_ERROR_UNKNOWN = 0
RSRC_ERROR_BAD_DATA = 1
RSRC_ERROR_BAD_MAGIC = 2
RSRC_ERROR_BAD_VERSION = 3
RSRC_ERROR_CONNECTION_LOST = 4
RSRC_ERROR_CONNECTION_TIMEOUT = 5
RSRC_ERROR_CONNECT_FAILED = 6
RSRC_ERROR_HOST_NOT_FOUND = 7
RSRC_ERROR_INCOMPATIBLE = 8
RSRC_ERROR_NOT_SUPPORTED = 9
RSRC_ERROR_BAD_ARGUMENT = 20
RSRC_ERROR_BAD_STATE = 21
# Font styles
FONT_PLAIN = 0
FONT_BOLD = 1
FONT_ITALIC = 2
FONT_BOLDITALIC = 3
# Bitwise flags for _CMD_RSRC_ADD_FONT: data to return in _EVT_FONT_INFO.
# (This is not documented in the HME spec.) Without setting one or both
# of these flags, no _EVT_FONT_INFO is returned.
FONT_METRICS_BASIC = 1
FONT_METRICS_GLYPH = 2
#--- Private constants ------------------------------------------------
# Commands
_CMD_VIEW_ADD = 1
_CMD_VIEW_SET_BOUNDS = 2
_CMD_VIEW_SET_SCALE = 3
_CMD_VIEW_SET_TRANSLATION = 4
_CMD_VIEW_SET_TRANSPARENCY = 5
_CMD_VIEW_SET_VISIBLE = 6
_CMD_VIEW_SET_PAINTING = 7
_CMD_VIEW_SET_RESOURCE = 8
_CMD_VIEW_REMOVE = 9
_CMD_RSRC_ADD_COLOR = 20
_CMD_RSRC_ADD_TTF = 21
_CMD_RSRC_ADD_FONT = 22
_CMD_RSRC_ADD_TEXT = 23
_CMD_RSRC_ADD_IMAGE = 24
_CMD_RSRC_ADD_SOUND = 25
_CMD_RSRC_ADD_STREAM = 26
_CMD_RSRC_ADD_ANIM = 27
_CMD_RSRC_SET_ACTIVE = 40
_CMD_RSRC_SET_POSITION = 41
_CMD_RSRC_SET_SPEED = 42
_CMD_RSRC_SEND_EVENT = 44
_CMD_RSRC_CLOSE = 45
_CMD_RSRC_REMOVE = 46
_CMD_RECEIVER_ACKNOWLEDGE_IDLE = 60
_CMD_RECEIVER_TRANSITION = 61
_CMD_RECEIVER_SET_RESOLUTION = 62
# Events
_EVT_DEVICE_INFO = 1
_EVT_APP_INFO = 2
_EVT_RSRC_INFO = 3
_EVT_KEY = 4
_EVT_IDLE = 5
_EVT_FONT_INFO = 6
_EVT_INIT_INFO = 7
_EVT_RESOLUTION_INFO = 8
# Characters for codes returned by QWERTY input
_QWERTY_MAP = string.uppercase + "-=[]\;',./` "
def qwerty_map(rawcode):
""" Take the rawcode from a type 1 (KEY_UNKNOWN) direct text key
event, and return the ASCII equivalent, or "?" on error.
"""
try:
key = _QWERTY_MAP[((rawcode & 0xff00) >> 8) - 0x3c]
except:
key = '?'
return key
#--- Low-level stream handling ----------------------------------------
class _EventData:
""" Take raw event data and allow various Python types to be
extracted from it.
"""
def __init__(self, data):
self.data = data
self.index = 0
def next(self):
c = ord(self.data[self.index])
self.index += 1
return c
def unpack_bool(self):
""" HME boolean to bool """
return bool(self.next())
def unpack_vint(self):
""" HME variable-length integer to int """
value = 0
shift = 0
while True:
c = self.next()
if c & 0x80:
break
value += c << shift
shift += 7
value += (c & 0x3f) << shift
if c & 0x40:
value = -value
return value
def unpack_vuint(self):
""" HME variable-length unsigned integer to int """
value = 0
shift = 0
while True:
c = self.next()
if c & 0x80:
break
value += c << shift
shift += 7
value += (c & 0x7f) << shift
return value
def unpack_float(self):
""" HME float to float """
value = struct.unpack('!f', self.data[self.index:self.index + 4])[0]
self.index += 4
return value
def unpack_vdata(self):
""" HME variable-length data to str """
length = self.unpack_vuint()
result = self.data[self.index:self.index + length]
self.index += length
return result
def unpack_string(self):
""" HME string to unicode """
return self.unpack_vdata().decode('utf-8')
def unpack_dict(self):
""" HME dict to dict (each value may be a list)
Note that the HME dict type is referred to, but not
documented, in the HME protocol specification.
"""
d = {}
while True:
key = self.unpack_string()
if not key:
break
value = []
while True:
c = self.next()
if not c:
break
if c == 1:
value.append(self.unpack_string())
else:
value.append(self.unpack_dict())
if len(value) == 1:
value = value[0]
d[key] = value
return d
def unpack(self, format):
""" Unpack a list of types, based on a format string. """
func = {'b': self.unpack_bool,
'i': self.unpack_vint,
'f': self.unpack_float,
'v': self.unpack_vdata,
's': self.unpack_string,
'd': self.unpack_dict}
return [func[i]() for i in format]
def _get_chunked(stream):
""" Read HME-style chunked event data from the input stream. """
data = ''
while True:
# Get the next chunk length
try:
length = struct.unpack('!H', stream.read(2))[0]
except:
return None
# A zero-length chunk marks the end of the event
if not length:
return data
# Otherwise, append the new chunk
try:
data += stream.read(length)
except:
return None
def _pack_bool(value):
""" bool to HME boolean """
return chr(value)
def _pack_vint(value):
""" int to HME variable-length integer """
value = int(value)
result = ''
is_neg = value < 0
if is_neg:
value = -value
while value > 0x3f:
result += chr(value & 0x7f)
value >>= 7
if is_neg:
value |= 0x40
result += chr(value | 0x80)
return result
def _pack_vuint(value):
""" int to HME variable-length unsigned integer """
value = int(value)
result = ''
while value > 0x7f:
result += chr(value & 0x7f)
value >>= 7
result += chr(value | 0x80)
return result
def _pack_float(value):
""" float to HME float """
return struct.pack('!f', float(value))
def _pack_vdata(value):
""" str to HME variable-length data """
return _pack_vuint(len(value)) + value
def _pack_string(value):
""" unicode to HME string """
if type(value) is unicode:
value = value.encode('utf-8')
elif type(value) is not str:
value = str(value)
return _pack_vdata(value)
def _pack_dict(value):
""" dict (of lists) to HME dict """
result = ''
if type(value) != dict:
raise TypeError, 'must be a dict'
# The keys must be sorted, or the TiVo ignores the transition.
keys = value.keys()
keys.sort()
for key in keys:
result += _pack_string(key)
items = value[key]
if type(items) != list:
items = [items]
for item in items:
if type(item) == dict:
result += chr(2)
result += _pack_dict(item)
else:
result += chr(1)
result += _pack_string(item)
result += chr(0)
result += _pack_string('')
return result
def _pack_raw(value):
""" Return the data as-is. """
return value
def _pack(format, *values):
""" Pack a list of types, based on a format string. """
func = {'b': _pack_bool,
'i': _pack_vint,
'f': _pack_float,
'v': _pack_vdata,
's': _pack_string,
'd': _pack_dict,
'r': _pack_raw}
return ''.join(func[i](value) for i, value in zip(format, values))
def _put_chunked(stream, data):
""" Write HME-style chunked data to the output stream. """
MAXSIZE = 0xfffe
size = len(data)
index = 0
while size:
blocksize = min(size, MAXSIZE)
try:
stream.write(struct.pack('!H', blocksize))
stream.write(data[index:index + blocksize])
except:
return
index += blocksize
size -= blocksize
try:
stream.write('\0\0')
except:
pass
#--- Resource classes -------------------------------------------------
class _HMEObject:
""" Base class for Resources and Views
If the id is specified, it's used; otherwise the next available
id is fetched from the app.
The need to specify the app here results in a slew of "self"
parameters when _HMEObjects are being constructed -- probably
the ugliest aspect of this module.
"""
def __init__(self, app, id=None):
if id is None:
self.id = app.next_resnum()
else:
self.id = id
self.app = app
def put(self, cmd, format='', *params):
""" Send a command (cmd) with the current resource id and
specified parameters, if any. The parameters are packed
according to the format string.
"""
_put_chunked(self.app.wfile,
_pack('ii' + format, cmd, self.id, *params))
class Resource(_HMEObject):
""" Base class for Resources
Note that in this implementation, resources are never removed
automatically; you have to call the remove() method.
"""
def __init__(self, app, id=None):
_HMEObject.__init__(self, app, id)
self.speed = 0
def set_active(self, make_active=True):
self.put(_CMD_RSRC_SET_ACTIVE, 'b', make_active)
def set_position(self, position):
self.put(_CMD_RSRC_SET_POSITION, 'i', position)
def set_speed(self, speed):
self.put(_CMD_RSRC_SET_SPEED, 'f', speed)
self.speed = speed
try:
self.app.wfile.flush()
except:
pass
def close(self):
self.put(_CMD_RSRC_CLOSE)
def remove(self):
if self.id >= ID_CLIENT:
self.put(_CMD_RSRC_REMOVE)
self.id = -1
def play(self):
self.set_speed(1)
def pause(self):
self.set_speed(0)
class Color(Resource):
""" Color resource
The colornum is specified as an integer in standard web R/G/B
format (most convenient as hex). If none is given, white (the
typical font color for the TiVo interface) is used. Color
objects are cached in the app.colors dict, and the last one set
is also stored in app.last_color.
You can include an alpha value as the MSB. Zero is treated as
0xff (fully opacity) by this library; otherwise, lower numbers
mean greater transparency.
"""
def __init__(self, app, colornum=None):
if colornum is None:
colornum = 0xffffffff
if not (colornum & 0xff000000):
colornum |= 0xff000000
self.colornum = colornum
if colornum in app.colors:
Resource.__init__(self, app, app.colors[colornum].id)
else:
Resource.__init__(self, app)
self.put(_CMD_RSRC_ADD_COLOR, 'r', struct.pack('!I', colornum))
app.colors[colornum] = self
app.last_color = self
def remove(self):
Resource.remove(self)
self.app.colors.pop(self.colornum)
if self.app.last_color == self:
self.app.last_color = None
class TTF(Resource):
""" TrueType font file resource
Specified by data, file object, file name or id. If none is
given, ID_DEFAULT_TTF is used. TTF objects specified by name are
cached in the app.ttfs dict, and the last TTF set is stored in
app.last_ttf.
"""
def __init__(self, app, name=None, f=None, data=None, id=None):
if name is None and f is None and data is None and id is None:
id = ID_DEFAULT_TTF
self.name = name
if name and name in app.ttfs:
Resource.__init__(self, app, app.ttfs[name].id)
else:
Resource.__init__(self, app, id)
if id is None:
if data is None:
if f is None:
f = open(name, 'rb')
data = f.read()
self.put(_CMD_RSRC_ADD_TTF, 'r', data)
if name:
app.ttfs[name] = self
app.last_ttf = self
def remove(self):
if self.name:
Resource.remove(self)
self.app.ttfs.pop(self.name)
if self.app.last_ttf == self:
self.app.last_ttf = None
def __del__(self):
if not self.name:
Resource.remove(self)
class Font(Resource):
""" Font resource (with chosen point size and style)
ttf specifies an object of the TTF class, and defaults to the
ID_DEFAULT_TTF object. style can be FONT_ITALIC, FONT_BOLD, or
FONT_BOLDITALIC, and defaults to FONT_PLAIN. Font objects are
cached in the app.fonts dict, and the last Font set is stored in
app.last_font.
"""
def __init__(self, app, ttf=None, style=FONT_PLAIN, size=24, flags=0):
if ttf is None:
ttf = app.last_ttf
if ttf is None:
ttf = app.default_ttf
self.key = (ttf, style, size, flags)
if self.key in app.fonts:
Resource.__init__(self, app, app.fonts[self.key].id)
else:
Resource.__init__(self, app)
self.put(_CMD_RSRC_ADD_FONT, 'iifi', ttf.id, style, size, flags)
app.fonts[self.key] = self
app.last_font = self
def remove(self):
Resource.remove(self)
self.app.fonts.pop(self.key)
if self.app.last_font == self:
self.app.last_font = None
class Text(Resource):
""" Text resource (with chosen Color and Font)
If either the color or font is unspecified, the last ones set in
the app are used. The color can be specified by number (using
colornum=), or as an object of the Color class (color=). The
font can only be specified as an object of the Font class.
"""
def __init__(self, app, text, font=None, color=None, colornum=None):
Resource.__init__(self, app)
if color is None:
if colornum is not None:
color = Color(app, colornum)
else:
color = app.last_color
if color is None:
color = Color(app)
if font is None:
font = app.last_font
if font is None:
font = Font(app)
self.put(_CMD_RSRC_ADD_TEXT, 'iis', font.id, color.id, text)
def __del__(self):
Resource.remove(self)
class Image(Resource):
""" Image resource
Specified by data, file object or file name, one of which must
be given. Image objects specified by name are cached in the
app.images dict.
"""
def __init__(self, app, name=None, f=None, data=None):
self.name = name
if name is None and f is None and data is None:
raise Exception, 'No image specified for Image resource'
if name and name in app.images:
Resource.__init__(self, app, app.images[name].id)
else:
Resource.__init__(self, app)
if data is None:
if f is None:
f = open(name, 'rb')
data = f.read()
self.put(_CMD_RSRC_ADD_IMAGE, 'r', data)
if name:
app.images[name] = self
def remove(self):
Resource.remove(self)
if self.name:
self.app.images.pop(self.name)
def __del__(self):
if not self.name:
Resource.remove(self)
class Sound(Resource):
""" Sound resource
Specified by data, file object, file name or id. If none is
given, ID_UPDOWN_SOUND is used.
Note that, on a real TiVo, only the predefined sounds seem to
work. Use a Stream to play your own sounds.
"""
def __init__(self, app, name=None, f=None, data=None, id=None):
if data is None and f is None and name is None and id is None:
id = ID_UPDOWN_SOUND
Resource.__init__(self, app, id)
if id is None:
if data is None:
if f is None:
f = open(name, 'rb')
data = f.read()
self.put(_CMD_RSRC_ADD_SOUND, 'r', data)
class Stream(Resource):
""" Stream resource
Specified by url. You can also provide the MIME type here, but
it doesn't seem to be used. The default is to play the stream
automatically when the event is sent; you can change this by
specifying "play=False". However, streams seem to be playable
only once.
"""
def __init__(self, app, url, mime='', play=True, params={}):
Resource.__init__(self, app)
self.put(_CMD_RSRC_ADD_STREAM, 'ssbd', url, mime, play, params)
self.speed = int(play)
def __del__(self):
Resource.remove(self)
class Animation(Resource):
""" Animation resource
Specified by duration in seconds, with optional ease and id.
(The id is used to initalize a zero-duration object for
ID_NULL.) Animation objects are cached in the app.anims dict.
"""
def __init__(self, app, duration, ease=0, id=None):
self.key = (duration, ease)
if id is None:
if self.key in app.anims:
Resource.__init__(self, app, app.anims[self.key].id)
else:
Resource.__init__(self, app)
self.put(_CMD_RSRC_ADD_ANIM, 'if', duration * 1000, ease)
app.anims[self.key] = self
else:
Resource.__init__(self, app, id)
def remove(self):
Resource.remove(self)
self.app.anims.pop(self.key)
#--- The View class ---------------------------------------------------
class View(_HMEObject):
""" The View class
A view is the basic unit of the HME display. It has a size,
position, and can have an associated resource, which is either a
Color (i.e., a background color for the entire area), an Image,
or Text. It can also have children -- views within views. For
example, the parent view might be set to a background color,
with its child displaying text.
For this class, position, size, visibility, parent, and an
associated resource can be set on initialization; all have
default values. Each View maintains a list of child Views,
scale, transparency and translation.
"""
def __init__(self, app, xpos=0, ypos=0, width=None, height=None,
visible=True, parent=None, id=None, resource=None,
text=None, colornum=None, image=None, flags=0,
transparency=0):
_HMEObject.__init__(self, app, id)
self.children = []
self.resource = None
self.xscale = 1
self.yscale = 1
self.painting = True
self.transparency = 0
self.xtranslation = 0
self.ytranslation = 0
if id is None and parent is None:
parent = app.root
if parent:
if app is None:
app = parent.app
if width is None:
width = parent.width - xpos
if height is None:
height = parent.height - ypos
else:
width, height = app.current_resolution[:2]
self.xpos = xpos
self.ypos = ypos
self.width = width
self.height = height
if parent:
self.put(_CMD_VIEW_ADD, 'iiiiib', parent.id, xpos, ypos,
width, height, visible)
parent.children.append(self)
else:
visible = False # root view starts out not visible
self.parent = parent
self.visible = visible
if transparency:
self.set_transparency(transparency)
if resource:
self.set_resource(resource, flags)
elif text:
self.set_text(text, flags=flags)
elif image:
self.set_image(image, flags=flags)
elif colornum is not None:
self.set_color(colornum)
def set_bounds(self, xpos=None, ypos=None, width=None, height=None,
animation=None, animtime=0):
""" Change the size and/or shape of the view, optionally over a
period of time. The interval can be specified either in
number of seconds (animtime=), or as an Animation object
(animation=).
"""
if xpos is None:
xpos = self.xpos
if ypos is None:
ypos = self.ypos
if width is None:
width = self.width
if height is None:
height = self.height
if animation is None:
if animtime:
animation = Animation(self.app, animtime)
else:
animation = self.app.immediate
self.put(_CMD_VIEW_SET_BOUNDS, 'iiiii', xpos, ypos, width, height,
animation.id)
self.xpos = xpos
self.ypos = ypos
self.width = width
self.height = height
def set_scale(self, xscale=None, yscale=None, animation=None,
animtime=0):
""" Scale the view up or down, optionally over a period of time.
"""
if xscale is None:
xscale = self.xscale
if yscale is None:
yscale = self.yscale
if animation is None:
if animtime:
animation = Animation(self.app, animtime)
else:
animation = self.app.immediate
self.put(_CMD_VIEW_SET_SCALE, 'ffi', xscale, yscale,
animation.id)
self.xscale = xscale
self.yscale = yscale
def set_translation(self, xtranslation=0, ytranslation=0,
animation=None, animtime=0):
""" Set the "translation" of the view, optionally over a period
of time. What this does is move the contents within the
view, while the view itself stays in the same place.
"""
if (self.xtranslation != xtranslation or
self.ytranslation != ytranslation):
if animation is None:
if animtime:
animation = Animation(self.app, animtime)
else:
animation = self.app.immediate
self.put(_CMD_VIEW_SET_TRANSLATION, 'iii',
xtranslation, ytranslation, animation.id)
self.xtranslation = xtranslation
self.ytranslation = ytranslation
def translate(self, xincrement=0, yincrement=0, animation=None,
animtime=0):
""" Translate with relative coordinates, vs. the absolute
coordinates used in set_translation().
"""
self.set_translation(self.xtranslation + xincrement,
self.ytranslation + yincrement,
animation, animtime)
def set_transparency(self, transparency, animation=None, animtime=0):
""" Change the transparency of the view, optionally over a
period of time (i.e., fade in/fade out).
"""
if self.transparency != transparency:
if animation is None:
if animtime:
animation = Animation(self.app, animtime)
else:
animation = self.app.immediate
self.put(_CMD_VIEW_SET_TRANSPARENCY, 'fi',
transparency, animation.id)
self.transparency = transparency
def set_visible(self, visible=True, animation=None, animtime=0):
""" Make the view visible or invisible, optionally after a
period of time.
"""
if self.visible != visible:
if animation is None:
if animtime:
animation = Animation(self.app, animtime)
else:
animation = self.app.immediate
self.put(_CMD_VIEW_SET_VISIBLE, 'bi',
visible, animation.id)
self.visible = visible
def set_painting(self, painting=True):
""" Set the view to update on screen (painting=True) or not.
Use this to perform a series of changes, then make them
visible all at once. (painting=False differs from
invisibility in that the old contents of the view remain on
screen.)
"""
if self.painting != painting:
self.put(_CMD_VIEW_SET_PAINTING, 'b', painting)
self.painting = painting
def set_resource(self, resource, flags=0):
""" Set the view's associated resource to a Text, Color or Image
object.
"""