043da442dfdf8336db1e08c9f830b5e56db5485f
[vlc.git] / extras / mpris.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
25 # Media Player Remote Interface Specification (MPRIS for short) available at
26 # http://wiki.xmms2.xmms.se/index.php/Media_Player_Interfaces
27 #
28 # You'll need pygtk >= 2.10 to use gtk.StatusIcon
29 #
30 import dbus
31 import dbus.glib
32 import gtk
33 import gtk.glade
34 import gobject
35 import os
36
37 global position
38 global timer
39 global playing
40 playing = False
41
42 global shuffle
43 global repeat
44 global loop
45 #mpris doesn't support getting the status of these (at the moment)
46 shuffle = False
47 repeat = False
48 loop = False
49
50 global root
51 global player
52 global tracklist
53
54 global bus
55
56 def player_change(newname, a, b):
57     if b != "" and "org.mpris." in newname:
58         Connect(newname)
59
60 def itemchange_handler(item):
61     try:
62         a = item["artist"]
63     except:
64         a = ""
65     try:
66         t = item["title"]
67     except:
68         t = ""
69     if t == "":
70         t = item["URI"]
71     l_artist.set_text(a)
72     l_title.set_text(t)
73
74 #find the first media player available
75 def Connect(name):
76     global root
77     global player
78     global tracklist
79     global bus
80     global playing
81
82     root_o = bus.get_object(name, "/")
83     player_o = bus.get_object(name, "/Player")
84     tracklist_o = bus.get_object(name, "/TrackList")
85
86     root = dbus.Interface(root_o, "org.freedesktop.MediaPlayer")
87     tracklist  = dbus.Interface(tracklist_o, "org.freedesktop.MediaPlayer")
88     player = dbus.Interface(player_o, "org.freedesktop.MediaPlayer")
89     player_o.connect_to_signal("TrackChange", itemchange_handler, dbus_interface="org.freedesktop.MediaPlayer")
90     if player.GetStatus() == 0:
91         playing = True
92     window.set_title(root.Identity())
93
94 #plays an element
95 def AddTrack(widget):
96     mrl = e_mrl.get_text()
97     if mrl != None and mrl != "":
98         tracklist.AddTrack(mrl, True)
99         e_mrl.set_text('')
100     else:
101         mrl = bt_file.get_filename()
102         if mrl != None and mrl != "":
103             tracklist.AddTrack("directory://" + mrl, True)
104     update(0)
105
106 #basic control
107 def Next(widget):
108     player.Next(reply_handler=(lambda *args: None), error_handler=(lambda *args: None))
109     update(0)
110
111 def Prev(widget):
112     player.Prev(reply_handler=(lambda *args: None), error_handler=(lambda *args: None))
113     update(0)
114
115 def Stop(widget):
116     player.Stop(reply_handler=(lambda *args: None), error_handler=(lambda *args: None))
117     update(0)
118
119 #update status display
120 def update(widget):
121     item = tracklist.GetMetadata(tracklist.GetCurrentTrack())
122     vol.set_value(player.VolumeGet())
123     try: 
124         a = item["artist"]
125     except:        a = ""
126     try:
127         t = item["title"]
128     except:        t = ""
129     if t == "":
130         try:
131             t = item["URI"]
132         except:
133             t = ""
134     l_artist.set_text(a)
135     l_title.set_text(t)
136     GetPlayStatus(0)
137
138 #get playing status from remote player
139 def GetPlayStatus(widget):
140     global playing
141     status = player.GetStatus()
142     if status == 0:
143         img_bt_toggle.set_from_stock("gtk-media-pause", gtk.ICON_SIZE_SMALL_TOOLBAR)
144         playing = True
145     else:
146         img_bt_toggle.set_from_stock("gtk-media-play", gtk.ICON_SIZE_SMALL_TOOLBAR)
147         playing = False
148
149 def Quit(widget):
150     player.Quit(reply_handler=(lambda *args: None), error_handler=(lambda *args: None))
151     l_title.set_text("")
152
153 def Pause(widget):
154     player.Pause()
155     status = player.GetStatus()
156     if status == 0:
157         img_bt_toggle.set_from_stock(gtk.STOCK_MEDIA_PAUSE, gtk.ICON_SIZE_SMALL_TOOLBAR)
158     else:
159         img_bt_toggle.set_from_stock(gtk.STOCK_MEDIA_PLAY, gtk.ICON_SIZE_SMALL_TOOLBAR)
160     update(0)
161
162 def Repeat(widget):
163     global repeat
164     repeat = not repeat
165     player.Repeat(repeat)
166
167 def Shuffle(widget):
168     global shuffle
169     shuffle = not shuffle
170     tracklist.Random(shuffle)
171
172 def Loop(widget):
173     global loop
174     loop = not loop
175     tracklist.Loop(loop)
176
177 #callback for volume
178 def volchange(widget, data):
179     player.VolumeSet(vol.get_value_as_int(), reply_handler=(lambda *args: None), error_handler=(lambda *args: None))
180
181 #callback for position change
182 def timechange(widget, x=None, y=None):
183     player.PositionSet(int(time_s.get_value()), reply_handler=(lambda *args: None), error_handler=(lambda *args: None))
184
185 #refresh position
186 def timeset():
187     global playing
188     if playing == True:
189         time_s.set_value(player.PositionGet())
190     return True
191
192 #simple/full display
193 def expander(widget):
194     if exp.get_expanded() == False:
195         exp.set_label("Less")
196     else:
197         exp.set_label("More")
198
199 #close event
200 def delete_event(self, widget):
201     self.hide()
202     return True
203
204 def destroy(widget):
205     gtk.main_quit()
206
207 def key_release(widget, event):
208     global position
209     if event.keyval == gtk.keysyms.Escape:
210         position = window.get_position()
211         widget.hide()
212
213 #click on the tray icon
214 def tray_button(widget):
215     global position
216     if window.get_property('visible'):
217         position = window.get_position()
218         window.hide()
219     else:
220         window.move(position[0], position[1])
221         window.show()
222
223 #loads glade file from the directory where the script is,
224 #so we can use /path/to/mpris.py to execute it.
225 import sys
226 xml = gtk.glade.XML(os.path.dirname(sys.argv[0]) + '/mpris.glade')
227
228 #ui setup
229 bt_close    = xml.get_widget('close')
230 bt_quit     = xml.get_widget('quit')
231 bt_file     = xml.get_widget('ChooseFile')
232 bt_next     = xml.get_widget('next')
233 bt_prev     = xml.get_widget('prev')
234 bt_stop     = xml.get_widget('stop')
235 bt_toggle   = xml.get_widget('toggle')
236 bt_mrl      = xml.get_widget('AddMRL')
237 bt_shuffle  = xml.get_widget('shuffle')
238 bt_repeat   = xml.get_widget('repeat')
239 bt_loop     = xml.get_widget('loop')
240 l_artist    = xml.get_widget('l_artist')
241 l_title     = xml.get_widget('l_title')
242 e_mrl       = xml.get_widget('mrl')
243 window      = xml.get_widget('window1')
244 img_bt_toggle=xml.get_widget('image6')
245 exp         = xml.get_widget('expander2')
246 expvbox     = xml.get_widget('expandvbox')
247 audioicon   = xml.get_widget('eventicon')
248 vol         = xml.get_widget('vol')
249 time_s      = xml.get_widget('time_s')
250 time_l      = xml.get_widget('time_l')
251
252 window.connect('delete_event',  delete_event)
253 window.connect('destroy',       destroy)
254 window.connect('key_release_event', key_release)
255
256 tray = gtk.status_icon_new_from_icon_name("audio-x-generic")
257 tray.connect('activate', tray_button)
258
259 def icon_clicked(widget, event):
260     update(0)
261
262 bt_close.connect('clicked',     destroy)
263 bt_quit.connect('clicked',      Quit)
264 bt_mrl.connect('clicked',       AddTrack)
265 bt_toggle.connect('clicked',    Pause)
266 bt_next.connect('clicked',      Next)
267 bt_prev.connect('clicked',      Prev)
268 bt_stop.connect('clicked',      Stop)
269 bt_loop.connect('clicked',      Loop)
270 bt_repeat.connect('clicked',    Repeat)
271 bt_shuffle.connect('clicked',   Shuffle)
272 exp.connect('activate',         expander)
273 vol.connect('change-value',     volchange)
274 vol.connect('scroll-event',     volchange)
275 time_s.connect('adjust-bounds', timechange)
276 audioicon.set_events(gtk.gdk.BUTTON_PRESS_MASK) 
277 audioicon.connect('button_press_event', icon_clicked) 
278 time_s.set_update_policy(gtk.UPDATE_DISCONTINUOUS)
279
280 library = "/media/mp3" #editme
281
282 try:
283     os.chdir(library)
284     bt_file.set_current_folder(library)
285 except:
286     bt_file.set_current_folder(os.path.expanduser("~"))
287
288 #connect to the bus
289 bus = dbus.SessionBus()
290 dbus_names = bus.get_object( "org.freedesktop.DBus", "/org/freedesktop/DBus" )
291 dbus_names.connect_to_signal("NameOwnerChanged", player_change, dbus_interface="org.freedesktop.DBus")
292
293 dbus_o = bus.get_object("org.freedesktop.DBus", "/")
294 dbus_intf = dbus.Interface(dbus_o, "org.freedesktop.DBus")
295 name_list = dbus_intf.ListNames()
296 name = ""
297 for n in name_list:
298     if "org.mpris." in n:
299         name = n
300         break
301
302 if name != "":
303     Connect(name)
304     window.set_title(root.Identity())
305     vol.set_value(player.VolumeGet())
306
307 #runs timer to update position
308 gobject.timeout_add( 1000, timeset)
309
310 window.set_icon_name('audio-x-generic')
311 window.show()
312
313 try:
314     update(0)
315 except:
316     True
317
318 icon_theme = gtk.icon_theme_get_default()
319 try:
320     pix = icon_theme.load_icon("audio-x-generic",24,0)
321     window.set_icon(pix)
322 except:
323     True
324 position = window.get_position()
325
326 gtk.main()