]> git.sesse.net Git - vlc/blob - extras/dbus-vlc.py
Missing files
[vlc] / 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     if a == "":
51         a = item["URI"] 
52     l_item.set_text(a)
53
54 #connect to the bus
55 bus = dbus.SessionBus()
56 player_o = bus.get_object("org.freedesktop.MediaPlayer", "/Player")
57 tracklist_o = bus.get_object("org.freedesktop.MediaPlayer", "/TrackList")
58
59 tracklist  = dbus.Interface(tracklist_o, "org.freedesktop.MediaPlayer")
60 player = dbus.Interface(player_o, "org.freedesktop.MediaPlayer")
61 try:
62     player_o.connect_to_signal("TrackChange", itemchange_handler, dbus_interface="org.freedesktop.MediaPlayer")
63 except:
64     True
65
66 #plays an element
67 def AddTrack(widget):
68     mrl = e_mrl.get_text()
69     if mrl != None and mrl != "":
70         tracklist.AddTrack(mrl, True)
71     else:
72         mrl = bt_file.get_filename()
73         if mrl != None and mrl != "":
74             tracklist.AddTrack("directory://" + mrl, True)
75     update(0)
76
77 #basic control
78 def Next(widget):
79     player.Next(reply_handler=(lambda *args: None), error_handler=(lambda *args: None))
80     update(0)
81
82 def Prev(widget):
83     player.Prev(reply_handler=(lambda *args: None), error_handler=(lambda *args: None))
84     update(0)
85
86 def Stop(widget):
87     player.Stop(reply_handler=(lambda *args: None), error_handler=(lambda *args: None))
88     update(0)
89
90 #update status display
91 def update(widget):
92     item = tracklist.GetMetadata(tracklist.GetCurrentTrack())
93     vol.set_value(player.VolumeGet())
94     try: 
95         a = item["artist"]
96     except:        a = ""
97     if a == "":
98         a = item["URI"] 
99     l_item.set_text(a)
100     GetPlayStatus(0)
101
102 #get playing status from remote vlc
103 def GetPlayStatus(widget):
104     global playing
105     status = player.GetStatus()
106     if status == 0:
107         img_bt_toggle.set_from_stock("gtk-media-pause", gtk.ICON_SIZE_SMALL_TOOLBAR)
108         playing = True
109     else:
110         img_bt_toggle.set_from_stock("gtk-media-play", gtk.ICON_SIZE_SMALL_TOOLBAR)
111         playing = False
112
113 def Quit(widget):
114     player.Quit(reply_handler=(lambda *args: None), error_handler=(lambda *args: None))
115     l_item.set_text("")
116
117 def Pause(widget):
118     player.Pause()
119     status = player.GetStatus()
120     if status == 0:
121         img_bt_toggle.set_from_stock(gtk.STOCK_MEDIA_PAUSE, gtk.ICON_SIZE_SMALL_TOOLBAR)
122         gobject.timeout_add( 2000, timeset)
123     else:
124         img_bt_toggle.set_from_stock(gtk.STOCK_MEDIA_PLAY, gtk.ICON_SIZE_SMALL_TOOLBAR)
125     update(0)
126
127 #callback for volume
128 def volchange(widget, data):
129     player.VolumeSet(vol.get_value_as_int(), reply_handler=(lambda *args: None), error_handler=(lambda *args: None))
130
131 #callback for position change
132 def timechange(widget, x=None, y=None):
133     player.PositionSet(int(time_s.get_value()), reply_handler=(lambda *args: None), error_handler=(lambda *args: None))
134
135 #refresh position
136 def timeset():
137     global playing
138     time_s.set_value(player.PositionGet())
139     return playing
140
141 #simple/full display
142 def expander(widget):
143     if exp.get_expanded() == False:
144         exp.set_label("Less")
145     else:
146         exp.set_label("More")
147
148 #close event
149 def delete_event(self, widget):
150     self.hide()
151     return True
152
153 def destroy(widget):
154     gtk.main_quit()
155
156 def key_release(widget, event):
157     global position
158     if event.keyval == gtk.keysyms.Escape:
159         position = window.get_position()
160         widget.hide()
161
162 #click on the tray icon
163 def tray_button(widget):
164     global position
165     if window.get_property('visible'):
166         position = window.get_position()
167         window.hide()
168     else:
169         window.move(position[0], position[1])
170         window.show()
171
172 xml = gtk.glade.XML('dbus-vlc.glade')
173
174 bt_close    = xml.get_widget('close')
175 bt_quit     = xml.get_widget('quit')
176 bt_file     = xml.get_widget('ChooseFile')
177 bt_mrl      = xml.get_widget('AddMRL')
178 bt_next     = xml.get_widget('next')
179 bt_prev     = xml.get_widget('prev')
180 bt_stop     = xml.get_widget('stop')
181 bt_toggle   = xml.get_widget('toggle')
182 l_item      = xml.get_widget('item')
183 e_mrl       = xml.get_widget('mrl')
184 window      = xml.get_widget('window1')
185 img_bt_toggle=xml.get_widget('image6')
186 exp         = xml.get_widget('expander2')
187 expvbox     = xml.get_widget('expandvbox')
188 vlcicon     = xml.get_widget('eventicon')
189 vol         = xml.get_widget('vol')
190 time_s      = xml.get_widget('time_s')
191 time_l      = xml.get_widget('time_l')
192
193 window.connect('delete_event',  delete_event)
194 window.connect('destroy',       destroy)
195 window.connect('key_release_event', key_release)
196
197 tray = gtk.status_icon_new_from_icon_name("vlc")
198 tray.connect('activate', tray_button)
199
200 def icon_clicked(widget, event):
201     update(0)
202
203 bt_close.connect('clicked',     destroy)
204 bt_quit.connect('clicked',      Quit)
205 bt_mrl.connect('clicked',       AddTrack)
206 bt_toggle.connect('clicked',    Pause)
207 bt_next.connect('clicked',      Next)
208 bt_prev.connect('clicked',      Prev)
209 bt_stop.connect('clicked',      Stop)
210 exp.connect('activate',         expander)
211 vol.connect('change-value',     volchange)
212 vol.connect('scroll-event',     volchange)
213 time_s.connect('adjust-bounds', timechange)
214 vlcicon.set_events(gtk.gdk.BUTTON_PRESS_MASK) 
215 vlcicon.connect('button_press_event', icon_clicked) 
216 time_s.set_update_policy(gtk.UPDATE_DISCONTINUOUS)
217 gobject.timeout_add( 2000, timeset)
218
219 library = "/media/mp3"
220
221 try:
222     os.chdir(library)
223     bt_file.set_current_folder(library)
224 except:
225     print "edit this file to point to your media library"
226
227 window.set_icon_name('vlc')
228 window.set_title("VLC - D-Bus ctrl")
229 window.show()
230
231 try:
232     update(0)
233 except:
234     True
235
236 icon_theme = gtk.icon_theme_get_default()
237 try:
238     pix = icon_theme.load_icon("vlc",24,0)
239     window.set_icon(pix)
240 except:
241     True
242 position = window.get_position()
243 vol.set_value(player.VolumeGet())
244
245 gtk.main()