summaryrefslogtreecommitdiffstats
path: root/py3status/modules/player_control.py
blob: 4270356be51dead7b406c2bddd5d518b831113db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# -*- coding: utf-8 -*-
"""
Control music/video players.

Provides an icon to control simple functions of audio/video players:
 - start (left click)
 - stop  (left click)
 - pause (middle click)

Configuration parameters:
    - debug: enable verbose logging (bool) (default: False)
    - supported_players: supported players (str) (comma separated list)
    - volume_tick: percentage volume change on mouse wheel (int) (positive number or None to disable it)

@author Federico Ceratto <federico.ceratto@gmail.com>, rixx
@license BSD
"""
# Any contributor to this module should add his/her name to the @author
# line, comma separated.

from syslog import syslog, LOG_INFO
from time import time, sleep
import os
import subprocess

try:
    import dbus
    dbus_available = True
except:
    dbus_available = False


def log(msg):
    syslog(LOG_INFO, "player_control: %s" % msg[:100])


class Py3status:
    """
    """
    # available configuration parameters
    debug = False
    pause_icon = u'❚❚'
    play_icon = u'▶'
    stop_icon = u'◼'
    supported_players = 'audacious,vlc'
    volume_tick = 1

    def __init__(self):
        self.status = 'stop'
        self.icon = self.play_icon

    def on_click(self, i3s_output_list, i3s_config, event):
        """
        """
        buttons = (None, 'left', 'middle', 'right', 'up', 'down')
        try:
            button = buttons[event['button']]
        except IndexError:
            return

        if button in ('up', 'down'):
            if self.volume_tick is None:
                return

            self._change_volume(button == 'up')
            return

        if self.status == 'play':
            if button == 'left':
                self._stop()

            elif button == 'middle':
                self._pause()

        elif self.status == 'stop':
            if button == 'left':
                self._play()

        elif self.status == 'pause':
            if button in ('left', 'right'):
                self._play()

    def _run(self, *args):
        if self.debug:
            log('running %s' % repr(*args))

        subprocess.check_output(*args, stderr=subprocess.STDOUT)

    def _play(self):
        self.status = 'play'
        self.icon = self.stop_icon
        player_name = self._detect_running_player()
        if player_name == 'audacious':
            self._run(['/usr/bin/audacious', '-p'])
        elif player_name == 'vlc':
            player = self._get_vlc()
            if player:
                player.Play()

    def _stop(self):
        self.status = 'stop'
        self.icon = self.play_icon
        player_name = self._detect_running_player()
        if player_name == 'audacious':
            self._run(['/usr/bin/audacious', '-s'])
        elif player_name == 'vlc':
            player = self._get_vlc()
            if player:
                player.Stop()

    def _pause(self):
        self.status = 'pause'
        self.icon = self.pause_icon
        player_name = self._detect_running_player()
        if player_name == 'audacious':
            self._run(['/usr/bin/audacious', '-u'])
        elif player_name == 'vlc':
            player = self._get_vlc()
            if player:
                player.Pause()

    def _change_volume(self, increase):
        """Change volume using amixer
        """
        sign = '+' if increase else '-'
        delta = "%d%%%s" % (self.volume_tick, sign)
        self._run(('/usr/bin/amixer', '-q', 'sset', 'Master', delta))

    def _detect_running_player(self):
        """Detect running player process, if any
        """
        supported_players = self.supported_players.split(',')
        running_players = []
        for pid in os.listdir('/proc'):
            if not pid.isdigit():
                continue

            fn = os.path.join('/proc', pid, 'comm')
            try:
                with open(fn, 'rb') as f:
                    player_name = f.read().decode().rstrip()

            except:
                continue

            if player_name in supported_players:
                running_players.append(player_name)

        # Pick which player to use based on the order in self.supported_players
        for player_name in supported_players:
            if player_name in running_players:
                if self.debug:
                    log('found player: %s' % player_name)

                # those players need the dbus module
                if player_name in ['vlc'] and not dbus_available:
                    log('%s requires the dbus python module' % player_name)
                    return None

                return player_name

        return None

    def _get_vlc(self):
        mpris = 'org.mpris.MediaPlayer2'
        mpris_slash = '/' + mpris.replace('.', '/')
        bus = dbus.SessionBus()
        proxy = bus.get_object(mpris+'.vlc', mpris_slash)
        return dbus.Interface(proxy, dbus_interface=mpris+'.Player')

    def player_control(self, i3s_output_list, i3s_config):
        return dict(
            full_text=self.icon,
            cached_until=time(),
        )


if __name__ == "__main__":
    x = Py3status()
    config = {
        'color_good': '#00FF00',
        'color_bad': '#FF0000',
    }
    while True:
        print(x.player_control([], config))
        sleep(1)