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