Implement input position control
[vlc.git] / extras / dbus-vlc.py
1 #!/usr/bin/python
2 # -*- coding: utf8 -*-
3 #
4 # Copyright (C) 2006 Rafaël Carré <funman at videolanorg>
5 #
6 # $Id$
7
8 #  This program is free software; you can redistribute it and/or modify
9 #  it under the terms of the GNU General Public License as published by
10 #  the Free Software Foundation; either version 2 of the License, or
11 #  (at your option) any later version.
12
13 #  This program is distributed in the hope that it will be useful,
14 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #  GNU General Public License for more details.
17
18 #  You should have received a copy of the GNU General Public License
19 #  along with this program; if not, write to the Free Software
20 #  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 #
22
23 import dbus
24 import dbus.glib
25 import gtk
26 import gtk.glade
27 import egg.trayicon
28 import gobject
29 import os
30
31 global position
32 global timer
33
34 def itemchange_handler(item):
35     l_item.set_text(item)
36
37 bus = dbus.SessionBus()
38 remote_object = bus.get_object("org.videolan.vlc", "/org/videolan/vlc")
39 interface  = dbus.Interface(remote_object, "org.videolan.vlc")
40 try:
41     remote_object.connect_to_signal("ItemChange", itemchange_handler, dbus_interface="org.videolan.vlc")
42 except:
43     True
44
45 def AddMRL(widget):
46     mrl = e_mrl.get_text()
47     if mrl != None and mrl != "":
48         interface.AddMRL(mrl, True)
49     else:
50         mrl = bt_file.get_filename()
51         if mrl != None and mrl != "":
52             interface.AddMRL("directory://" + mrl, True)
53
54 def Next(widget):
55     interface.Next()
56     update(0)
57
58 def Prev(widget):
59     interface.Prev()
60     update(0)
61
62 def Stop(widget):
63     interface.Stop()
64     update(0)
65
66 def update(widget):
67     itemchange_handler(str(interface.GetPlayingItem()))
68     vol.set_value(interface.VolumeGet())
69     GetPlayStatus(0)
70
71 def GetPlayStatus(widget):
72     global timer
73     status = str(interface.GetPlayStatus())
74     if status == "playing":
75         img_bt_toggle.set_from_stock("gtk-media-pause", gtk.ICON_SIZE_SMALL_TOOLBAR)
76         timer = gobject.timeout_add( 2000, timeset)
77     else:
78         img_bt_toggle.set_from_stock("gtk-media-play", gtk.ICON_SIZE_SMALL_TOOLBAR)
79         gobject.timeout_remove(timer)
80
81 def Quit(widget):
82     interface.Quit()
83     l_item.set_text("")
84
85 def TogglePause(widget):
86     if interface.TogglePause() == True:
87         img_bt_toggle.set_from_stock(gtk.STOCK_MEDIA_PAUSE, gtk.ICON_SIZE_SMALL_TOOLBAR)
88     else:
89         img_bt_toggle.set_from_stock(gtk.STOCK_MEDIA_PLAY, gtk.ICON_SIZE_SMALL_TOOLBAR)
90     update(0)
91
92 def volchange(widget, data):
93     interface.VolumeSet(vol.get_value_as_int(), reply_handler=(lambda: None), error_handler=(lambda: None))
94
95 def timechange(widget, x=None, y=None):
96     interface.PositionSet(time_s.get_value() * 10, reply_handler=(lambda: None), error_handler=(lambda: None))
97
98 def timeset():
99     time_s.set_value(interface.PositionGet() / 10)
100     return True
101
102 def expander(widget):
103     if exp.get_expanded() == False:
104         exp.set_label("Less")
105     else:
106         exp.set_label("More")
107
108 def delete_event(self, widget):
109     widget.hide()
110     return True
111
112 def destroy(widget):
113     gtk.main_quit()
114
115 def key_release(widget, event):
116     global position
117     if event.keyval == gtk.keysyms.Escape:
118         position = window.get_position()
119         widget.hide()
120
121 def tray_button(widget,event):
122     global position
123     if event.button == 1:
124         if window.get_property('visible'):
125             position = window.get_position()
126             window.hide()
127         else:
128             window.move(position[0], position[1])
129             window.show()
130     if event.button == 3:
131         menu.popup(None,None,None,event.button,event.time)
132
133 xml = gtk.glade.XML('dbus-vlc.glade')
134
135 bt_close    = xml.get_widget('close')
136 bt_quit     = xml.get_widget('quit')
137 bt_file     = xml.get_widget('ChooseFile')
138 bt_mrl      = xml.get_widget('AddMRL')
139 bt_next     = xml.get_widget('next')
140 bt_prev     = xml.get_widget('prev')
141 bt_stop     = xml.get_widget('stop')
142 bt_toggle   = xml.get_widget('toggle')
143 l_item      = xml.get_widget('item')
144 e_mrl       = xml.get_widget('mrl')
145 window      = xml.get_widget('window1')
146 img_bt_toggle=xml.get_widget('image6')
147 exp         = xml.get_widget('expander2')
148 expvbox     = xml.get_widget('expandvbox')
149 menu        = xml.get_widget('menu1')
150 menuitem    = xml.get_widget('menuquit')
151 vlcicon     = xml.get_widget('eventicon')
152 vol         = xml.get_widget('vol')
153 time_s      = xml.get_widget('time_s')
154 time_l      = xml.get_widget('time_l')
155
156 window.connect('delete_event',  delete_event)
157 window.connect('destroy',       destroy)
158 window.connect('key_release_event', key_release)
159
160 tray = egg.trayicon.TrayIcon("VLC")
161 eventbox = gtk.EventBox()
162 tray.add(eventbox)
163 eventbox.set_events(gtk.gdk.BUTTON_PRESS_MASK)
164 eventbox.connect('button_press_event', tray_button)
165 image = gtk.Image()
166 eventbox.add(image)
167 image.set_from_icon_name("vlc", gtk.ICON_SIZE_MENU)
168 tray.show_all()
169
170 def icon_clicked(widget, event):
171     update(0)
172
173 menu.attach_to_widget(eventbox,None)
174
175 bt_close.connect('clicked',     destroy)
176 bt_quit.connect('clicked',      Quit)
177 bt_mrl.connect('clicked',       AddMRL)
178 bt_toggle.connect('clicked',    TogglePause)
179 bt_next.connect('clicked',      Next)
180 bt_prev.connect('clicked',      Prev)
181 bt_stop.connect('clicked',      Stop)
182 exp.connect('activate',         expander)
183 menuitem.connect('activate',    destroy)
184 vlcicon.set_events(gtk.gdk.BUTTON_PRESS_MASK)
185 vlcicon.connect('button_press_event', icon_clicked)
186 vol.connect('change-value',     volchange)
187 vol.connect('scroll-event',     volchange)
188 time_s.connect('adjust-bounds', timechange)
189
190 time_s.set_update_policy(gtk.UPDATE_DISCONTINUOUS)
191
192 library = "/media/mp3"
193
194 try:
195     os.chdir(library)
196     bt_file.set_current_folder(library)
197 except:
198     print "edit this file to point to your media library"
199
200 window.set_icon_name('vlc')
201 window.set_title("VLC - D-Bus ctrl")
202 window.show()
203
204 try:
205     update(0)
206 except:
207     True
208
209 gtk.main()