]> git.sesse.net Git - vlc/blob - bindings/python-ctypes/footer.py
2d00aba9815dd053002bdeac2ba4596b8def8fff
[vlc] / bindings / python-ctypes / footer.py
1 ### Start of footer.py ###
2
3 class MediaEvent(ctypes.Structure):
4     _fields_ = [
5         ('media_name', ctypes.c_char_p),
6         ('instance_name', ctypes.c_char_p),
7         ]
8
9 class EventUnion(ctypes.Union):
10     _fields_ = [
11         ('meta_type', ctypes.c_uint),
12         ('new_child', ctypes.c_uint),
13         ('new_duration', ctypes.c_longlong),
14         ('new_status', ctypes.c_int),
15         ('media', ctypes.c_void_p),
16         ('new_state', ctypes.c_uint),
17         # Media instance
18         ('new_position', ctypes.c_float),
19         ('new_time', ctypes.c_longlong),
20         ('new_title', ctypes.c_int),
21         ('new_seekable', ctypes.c_longlong),
22         ('new_pausable', ctypes.c_longlong),
23         # FIXME: Skipped MediaList and MediaListView...
24         ('filename', ctypes.c_char_p),
25         ('new_length', ctypes.c_longlong),
26         ('media_event', MediaEvent),
27         ]
28
29 class Event(ctypes.Structure):
30     _fields_ = [
31         ('type', EventType),
32         ('object', ctypes.c_void_p),
33         ('u', EventUnion),
34         ]
35
36 # Decorator for callback methods
37 callbackmethod=ctypes.CFUNCTYPE(None, Event, ctypes.c_void_p)
38
39 # Example callback method
40 @callbackmethod
41 def debug_callback(event, data):
42     print "Debug callback method"
43     print "Event:", event.type
44     print "Data", data
45
46 if __name__ == '__main__':
47     import sys
48     try:
49         from msvcrt import getch
50     except ImportError:
51         def getch():
52             import tty
53             import termios
54             fd=sys.stdin.fileno()
55             old_settings=termios.tcgetattr(fd)
56             try:
57                 tty.setraw(fd)
58                 ch=sys.stdin.read(1)
59             finally:
60                 termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
61             return ch
62
63     @callbackmethod
64     def end_callback(event, data):
65         print "End of stream"
66         sys.exit(0)
67
68     if sys.argv[1:]:
69         i=Instance()
70         m=i.media_new(sys.argv[1])
71         p=i.media_player_new()
72         p.set_media(m)
73         p.play()
74
75         e=p.event_manager()
76         e.event_attach(EventType.MediaPlayerStopped, end_callback, None)
77
78         def print_info():
79             """Print information about the media."""
80             m=p.get_media()
81             print "Playing ", m.get_mrl()
82             print "Current time:", p.get_time(), "/", m.get_duration()
83             print "Position", p.get_position()
84         
85         def forward():
86             """Go forward 1s"""
87             p.set_time(p.get_time() + 1000)
88
89         def backward():
90             """Go backward 1s"""
91             p.set_time(p.get_time() - 1000)
92
93         def print_help():
94             """Print help
95             """
96             print "Commands:"
97             for k, m in keybindings.iteritems():
98                 print "  %s: %s" % (k, (m.__doc__ or m.__name__).splitlines()[0])
99
100         def quit():
101             """Exit."""
102             sys.exit(0)
103
104         keybindings={
105             'f': p.toggle_fullscreen,
106             ' ': p.pause,
107             '+': forward,
108             '-': backward,
109             '?': print_help,
110             'i': print_info,
111             'q': quit,
112             }
113
114         while True:
115             k=getch()
116             method=keybindings.get(k, None)
117             if method is not None:
118                 method()