~ubuntu-branches/ubuntu/precise/remuco-server/precise

« back to all changes in this revision

Viewing changes to adapter/audacious/remuco-audacious

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2009-03-30 00:59:36 UTC
  • Revision ID: james.westby@ubuntu.com-20090330005936-hkxki384hm0d33gj
Tags: upstream-0.8.2.1
ImportĀ upstreamĀ versionĀ 0.8.2.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
# =============================================================================
 
4
#
 
5
#    Remuco - A remote control system for media players.
 
6
#    Copyright (C) 2006-2009 Oben Sonne <obensonne@googlemail.com>
 
7
#
 
8
#    This file is part of Remuco.
 
9
#
 
10
#    Remuco is free software: you can redistribute it and/or modify
 
11
#    it under the terms of the GNU General Public License as published by
 
12
#    the Free Software Foundation, either version 3 of the License, or
 
13
#    (at your option) any later version.
 
14
#
 
15
#    Remuco is distributed in the hope that it will be useful,
 
16
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
#    GNU General Public License for more details.
 
19
#
 
20
#    You should have received a copy of the GNU General Public License
 
21
#    along with Remuco.  If not, see <http://www.gnu.org/licenses/>.
 
22
#
 
23
# =============================================================================
 
24
 
 
25
"""Remuco player adapter for Audacious, implemented as an executable script."""
 
26
 
 
27
import dbus
 
28
from dbus.exceptions import DBusException
 
29
import gobject
 
30
 
 
31
import remuco
 
32
from remuco import log
 
33
 
 
34
# =============================================================================
 
35
# actions
 
36
# =============================================================================
 
37
 
 
38
IA_JUMP = remuco.ItemAction("Jump to")
 
39
PLAYLIST_ACTIONS = (IA_JUMP,)
 
40
 
 
41
# =============================================================================
 
42
# player adapter
 
43
# =============================================================================
 
44
 
 
45
class AudaciousAdapter(remuco.MPRISAdapter):
 
46
    
 
47
    def __init__(self):
 
48
        
 
49
        remuco.MPRISAdapter.__init__(self, "audacious", "Audacious",
 
50
                                     mime_types=("audio",),
 
51
                                     extra_playlist_actions=PLAYLIST_ACTIONS)
 
52
        
 
53
        self.__ad = None
 
54
        self.__poll_for_repeat_and_shuffle = False
 
55
        
 
56
    def start(self):
 
57
        
 
58
        remuco.MPRISAdapter.start(self)
 
59
        
 
60
        try:
 
61
            bus = dbus.SessionBus()
 
62
            proxy = bus.get_object("org.atheme.audacious", "/org/atheme/audacious")
 
63
            self.__ad = dbus.Interface(proxy, "org.atheme.audacious")
 
64
        except DBusException, e:
 
65
            raise StandardError("dbus error: %s" % e)
 
66
 
 
67
    def stop(self):
 
68
        
 
69
        remuco.MPRISAdapter.stop(self)
 
70
        
 
71
        self.__ad = None
 
72
        
 
73
    def poll(self):
 
74
 
 
75
        remuco.MPRISAdapter.poll(self)
 
76
 
 
77
        if self.__poll_for_repeat_and_shuffle:
 
78
            self.__poll_repeat()
 
79
            self.__poll_shuffle()
 
80
        
 
81
    def __poll_repeat(self):
 
82
        
 
83
        # used if audacious still does not provide this by signal "StatusChange"
 
84
        
 
85
        try:
 
86
            repeat = bool(self.__ad.Repeat())
 
87
        except DBusException, e:
 
88
            log.warning("dbus error: %s" % e)
 
89
            repeat = False
 
90
            
 
91
        self._repeat = repeat
 
92
        self.update_repeat(repeat)
 
93
        
 
94
    def __poll_shuffle(self):
 
95
        
 
96
        # used if audacious still does not provide this by signal "StatusChange"
 
97
        
 
98
        try:
 
99
            shuffle = bool(self.__ad.Shuffle())
 
100
        except DBusException, e:
 
101
            log.warning("dbus error: %s" % e)
 
102
            shuffle = False
 
103
            
 
104
        self._shuffle = shuffle
 
105
        self.update_shuffle(shuffle)
 
106
        
 
107
    # =========================================================================
 
108
    # control interface 
 
109
    # =========================================================================
 
110
    
 
111
    def ctrl_toggle_repeat(self):
 
112
        
 
113
        # audacious uses wrong method name for setting repeat mode
 
114
        
 
115
        try:
 
116
            self._mp_t.Loop(not self._repeat,
 
117
                            reply_handler=self._dbus_ignore,
 
118
                            error_handler=self._dbus_error)
 
119
        except DBusException, e:
 
120
            log.warning("dbus error: %s" % e)
 
121
            
 
122
        if self.__poll_for_repeat_and_shuffle:
 
123
            self.__poll_repeat()
 
124
    
 
125
    def ctrl_toggle_shuffle(self):
 
126
        
 
127
        # audacious uses wrong method name for setting shuffle mode
 
128
        
 
129
        try:
 
130
            self._mp_t.Random(not self._shuffle,
 
131
                              reply_handler=self._dbus_ignore,
 
132
                              error_handler=self._dbus_error)
 
133
        except DBusException, e:
 
134
            log.warning("dbus error: %s" % e)
 
135
        
 
136
        if self.__poll_for_repeat_and_shuffle:
 
137
            self.__poll_shuffle()
 
138
    
 
139
    # =========================================================================
 
140
    # actions interface
 
141
    # =========================================================================
 
142
    
 
143
    def action_playlist_item(self, action_id, positions, ids):
 
144
        
 
145
        if action_id == IA_JUMP.id:
 
146
            
 
147
            try:
 
148
                self.__ad.Jump(positions[0],
 
149
                               reply_handler=self._dbus_ignore,
 
150
                               error_handler=self._dbus_error)
 
151
            except DBusException, e:
 
152
                log.warning("dbus error: %s" % e)
 
153
            
 
154
        else:
 
155
            remuco.MPRISAdapter.action_playlist_item(self, action_id,
 
156
                                                     positions, ids)
 
157
        
 
158
        
 
159
    # =========================================================================
 
160
    # internal methods which must be adapted to provide MPRIS conformity
 
161
    # =========================================================================
 
162
    
 
163
    def _notify_status(self, status):
 
164
 
 
165
        if isinstance(status, int):
 
166
            # audacious only provides playback status here
 
167
            self.__poll_for_repeat_and_shuffle = True
 
168
            status = (status, self._shuffle, self._repeat, self._repeat)
 
169
        else:
 
170
            # it looks like audacious has fixed its MPRIS interface
 
171
            self.__poll_for_repeat_and_shuffle = False
 
172
        
 
173
        remuco.MPRISAdapter._notify_status(self, status)
 
174
        
 
175
    def _notify_track(self, track):
 
176
        
 
177
        # audacious provides length in 'length', not in 'time' or 'mtime'
 
178
        if "length" in track and not "mtime" in track:
 
179
            track["mtime"] = track["length"]
 
180
            
 
181
        # audacious provides length in 'URI', not in 'location'
 
182
        if "URI" in track:
 
183
            track["location"] = track["URI"]
 
184
            
 
185
        remuco.MPRISAdapter._notify_track(self, track)
 
186
 
 
187
# =============================================================================
 
188
# main
 
189
# =============================================================================
 
190
 
 
191
if __name__ == '__main__':
 
192
 
 
193
    pa = AudaciousAdapter()
 
194
    mg = remuco.Manager(pa, player_dbus_name="org.mpris.audacious")
 
195
    
 
196
    mg.run()
 
197