-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplayer.py
executable file
·165 lines (127 loc) · 4.55 KB
/
player.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
#!/usr/bin/env python2
from twisted.internet import gtk2reactor # for gtk-2.0
gtk2reactor.install()
from twisted.protocols.basic import LineReceiver
from twisted.internet.protocol import Factory
from twisted.internet import reactor
import gtk
import webkit
from os import path
class TcpControl(LineReceiver):
def connectionMade(self):
self.controls = self.factory.controls # self.factory isn't available in __init__
print("tcp client has connected")
self.controls.connect_new_clip(self.clip_loaded)
def connectionLost(self, reason):
self.controls.disconnect_new_clip(self.clip_loaded)
def request_clipno(self):
self.sendLine("CLIP")
def lineReceived(self, line):
cmd = line[:4].upper()
if cmd == "CLIP" or cmd == "JUMP":
try:
new_clip = int(line[5:])
self.controls.jump(new_clip)
except ValueError:
# no next clip number --> return active clip no
self.sendLine("CLIP " + str(self.controls.clip))
elif cmd == "NEXT":
self.controls.next()
elif cmd == "PREV":
self.controls.prev()
elif cmd == "LOAD":
self.controls.load()
elif cmd == "FULL":
self.controls.toggle_fullscreen()
elif cmd == "STOP":
self.controls.stop()
elif cmd == "QUIT":
self.controls.quit()
def clip_loaded(self, new_clip):
self.sendLine("CLIP " + str(self.controls.clip))
class TcpControlFactory(Factory):
protocol = TcpControl
def __init__(self, controls):
self.controls = controls
class FullscreenToggler(object):
""" from: http://stackoverflow.com/questions/5234434/simple-way-to-toggle-fullscreen-with-f11-in-pygtk """
def __init__(self, window, keysym=gtk.keysyms.F11):
self.window = window
self.keysym = keysym
self.window_is_fullscreen = False
self.window.connect_object('window-state-event', FullscreenToggler.on_window_state_change, self)
self.window.connect_object("key_press_event", FullscreenToggler.key_press, self)
def on_window_state_change(self, event):
self.window_is_fullscreen = bool(
gtk.gdk.WINDOW_STATE_FULLSCREEN & event.new_window_state)
def toggle(self):
if self.window_is_fullscreen:
self.window.unfullscreen()
else:
self.window.fullscreen()
def key_press(self, event):
if event.keyval == self.keysym:
self.toggle()
class Controls(object):
def __init__(self, window, view, fs_toggler):
self.clip = 0
self.view = view
self.fs_toggler = fs_toggler
window.connect_object("key_press_event", Controls.key_press, self)
self.callbacks_new_clip = []
self.load()
def connect_new_clip(self, callback):
self.callbacks_new_clip.append(callback)
def disconnect_new_clip(self, callback):
self.callbacks_new_clip.remove(callback)
def toggle_fullscreen(self):
self.fs_toggler.toggle()
def load(self):
uri = path.join(path.dirname(path.abspath(__file__)), "swf-files/" + str(self.clip) + ".swf")
self.view.open(uri)
print "loading clip " + str(self.clip) + " from " + uri + " (http://www.z0r.de/" + str(self.clip) + ")"
for callback in self.callbacks_new_clip:
callback(self.clip)
def jump(self, new_clip):
self.clip = new_clip
self.load()
def stop(self):
self.view.open("")
def prev(self):
self.clip -= 1
self.load()
def next(self):
self.clip += 1
self.load()
def key_press(self, event):
keyval = event.keyval
if keyval == 65363:
# right arrow
self.next()
elif keyval == 65361:
# left arrow
self.prev()
elif keyval == 65307:
# escape
self.stop()
elif keyval == 32:
# space
self.next()
def quit(self):
gtk.main_quit()
class Player(object):
def __init__(self):
w = gtk.Window(gtk.WINDOW_TOPLEVEL)
w.resize(800,600)
view = webkit.WebView()
sw = gtk.ScrolledWindow()
sw.add(view)
w.add(sw)
w.show_all()
w.connect("delete-event", gtk.main_quit)
fs_toggler = FullscreenToggler(w)
controls = Controls(w, view, fs_toggler)
reactor.listenTCP(9001, TcpControlFactory(controls))
reactor.run()
if __name__ == "__main__":
player = Player()