Updates dbus pygtk controller
[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 #
24 # NOTE: this controller is a SAMPLE, and thus doesn't implement all the D-Bus Media Player specification. http://wiki.videolan.org/index.php/DBus-spec
25 # This is an unfinished document (on the 12/06/2006) and has been designed to be as general as possible.
26 # So don't expect that much from this, but basic capabilities should work out of the box (Play/Pause/Next/Add)
27 #
28 # Also notice it has been designed first for a previous specificaiton, and thus some code may not work/be disabled
29 #
30 # You'll need pygtk >= 2.10 to use gtk.StatusIcon
31 #
32 import dbus
33 import dbus.glib
34 import gtk
35 import gtk.glade
36 import gobject
37 import os
38
39 global position
40 global timer
41 global playing
42 playing = False
43
44 def itemchange_handler(item):
45     gobject.timeout_add( 2000, timeset)
46     try:
47         a = item["artist"]
48     except:
49         a = ""
50     try:
51         t = item["title"]
52     except:
53         t = ""
54     if t == "":
55         t = item["URI"]
56     l_artist.set_text(a)
57     l_title.set_text(t)
58
59 #connect to the bus
60 bus = dbus.SessionBus()
61 player_o = bus.get_object("org.mpris.vlc", "/Player")
62 tracklist_o = bus.get_object("org.mpris.vlc", "/TrackList")
63
64 tracklist  = dbus.Interface(tracklist_o, "org.freedesktop.MediaPlayer")
65 player = dbus.Interface(player_o, "org.freedesktop.MediaPlayer")
66 try:
67     player_o.connect_to_signal("TrackChange", itemchange_handler, dbus_interface="org.freedesktop.MediaPlayer")
68 except:
69     True
70
71 #plays an element
72 def AddTrack(widget):
73     mrl = e_mrl.get_text()
74     if mrl != None and mrl != "":
75         tracklist.AddTrack(mrl, True)
76         e_mrl.set_text('')
77     else:
78         mrl = bt_file.get_filename()
79         if mrl != None and mrl != "":
80             tracklist.AddTrack("directory://" + mrl, True)
81     update(0)
82
83 #basic control
84 def Next(widget):
85     player.Next(reply_handler=(lambda *args: None), error_handler=(lambda *args: None))
86     update(0)
87
88 def Prev(widget):
89     player.Prev(reply_handler=(lambda *args: None), error_handler=(lambda *args: None))
90     update(0)
91
92 def Stop(widget):
93     player.Stop(reply_handler=(lambda *args: None), error_handler=(lambda *args: None))
94     update(0)
95
96 #update status display
97 def update(widget):
98     item = tracklist.GetMetadata(tracklist.GetCurrentTrack())
99     vol.set_value(player.VolumeGet())
100     try: 
101         a = item["artist"]
102     except:        a = ""
103     try:
104         t = item["title"]
105     except:        t = ""
106     if t == "":
107         t = item["URI"]
108     l_artist.set_text(a)
109     l_title.set_text(t)
110     GetPlayStatus(0)
111
112 #get playing status from remote vlc
113 def GetPlayStatus(widget):
114     global playing
115     status = player.GetStatus()
116     if status == 0:
117         img_bt_toggle.set_from_stock("gtk-media-pause", gtk.ICON_SIZE_SMALL_TOOLBAR)
118         playing = True
119     else:
120         img_bt_toggle.set_from_stock("gtk-media-play", gtk.ICON_SIZE_SMALL_TOOLBAR)
121         playing = False
122
123 def Quit(widget):
124     player.Quit(reply_handler=(lambda *args: None), error_handler=(lambda *args: None))
125     l_title.set_text("")
126
127 def Pause(widget):
128     player.Pause()
129     status = player.GetStatus()
130     if status == 0:
131         img_bt_toggle.set_from_stock(gtk.STOCK_MEDIA_PAUSE, gtk.ICON_SIZE_SMALL_TOOLBAR)
132         gobject.timeout_add( 2000, timeset)
133     else:
134         img_bt_toggle.set_from_stock(gtk.STOCK_MEDIA_PLAY, gtk.ICON_SIZE_SMALL_TOOLBAR)
135     update(0)
136
137 #callback for volume
138 def volchange(widget, data):
139     player.VolumeSet(vol.get_value_as_int(), reply_handler=(lambda *args: None), error_handler=(lambda *args: None))
140
141 #callback for position change
142 def timechange(widget, x=None, y=None):
143     player.PositionSet(int(time_s.get_value()), reply_handler=(lambda *args: None), error_handler=(lambda *args: None))
144
145 #refresh position
146 def timeset():
147     global playing
148     time_s.set_value(player.PositionGet())
149     return playing
150
151 #simple/full display
152 def expander(widget):
153     if exp.get_expanded() == False:
154         exp.set_label("Less")
155     else:
156         exp.set_label("More")
157
158 #close event
159 def delete_event(self, widget):
160     self.hide()
161     return True
162
163 def destroy(widget):
164     gtk.main_quit()
165
166 def key_release(widget, event):
167     global position
168     if event.keyval == gtk.keysyms.Escape:
169         position = window.get_position()
170         widget.hide()
171
172 #click on the tray icon
173 def tray_button(widget):
174     global position
175     if window.get_property('visible'):
176         position = window.get_position()
177         window.hide()
178     else:
179         window.move(position[0], position[1])
180         window.show()
181
182 xml = gtk.glade.XML('dbus-vlc.glade')
183
184 bt_close    = xml.get_widget('close')
185 bt_quit     = xml.get_widget('quit')
186 bt_file     = xml.get_widget('ChooseFile')
187 bt_next     = xml.get_widget('next')
188 bt_prev     = xml.get_widget('prev')
189 bt_stop     = xml.get_widget('stop')
190 bt_toggle   = xml.get_widget('toggle')
191 bt_mrl      = xml.get_widget('AddMRL')
192 l_artist    = xml.get_widget('l_artist')
193 l_title     = xml.get_widget('l_title')
194 e_mrl       = xml.get_widget('mrl')
195 window      = xml.get_widget('window1')
196 img_bt_toggle=xml.get_widget('image6')
197 exp         = xml.get_widget('expander2')
198 expvbox     = xml.get_widget('expandvbox')
199 vlcicon     = xml.get_widget('eventicon')
200 vol         = xml.get_widget('vol')
201 time_s      = xml.get_widget('time_s')
202 time_l      = xml.get_widget('time_l')
203
204 window.connect('delete_event',  delete_event)
205 window.connect('destroy',       destroy)
206 window.connect('key_release_event', key_release)
207
208 tray = gtk.status_icon_new_from_icon_name("vlc")
209 tray.connect('activate', tray_button)
210
211 def icon_clicked(widget, event):
212     update(0)
213
214 bt_close.connect('clicked',     destroy)
215 bt_quit.connect('clicked',      Quit)
216 bt_mrl.connect('clicked',       AddTrack)
217 bt_toggle.connect('clicked',    Pause)
218 bt_next.connect('clicked',      Next)
219 bt_prev.connect('clicked',      Prev)
220 bt_stop.connect('clicked',      Stop)
221 exp.connect('activate',         expander)
222 vol.connect('change-value',     volchange)
223 vol.connect('scroll-event',     volchange)
224 time_s.connect('adjust-bounds', timechange)
225 vlcicon.set_events(gtk.gdk.BUTTON_PRESS_MASK) 
226 vlcicon.connect('button_press_event', icon_clicked) 
227 time_s.set_update_policy(gtk.UPDATE_DISCONTINUOUS)
228 gobject.timeout_add( 2000, timeset)
229
230 library = "/media/mp3"
231
232 try:
233     os.chdir(library)
234     bt_file.set_current_folder(library)
235 except:
236     print "edit this file to point to your media library"
237
238 window.set_icon_name('vlc')
239 window.set_title("VLC - D-Bus ctrl")
240 window.show()
241
242 try:
243     update(0)
244 except:
245     True
246
247 icon_theme = gtk.icon_theme_get_default()
248 try:
249     pix = icon_theme.load_icon("vlc",24,0)
250     window.set_icon(pix)
251 except:
252     True
253 position = window.get_position()
254 vol.set_value(player.VolumeGet())
255
256 gtk.main()