1
0
mirror of https://github.com/slgobinath/SafeEyes.git synced 2024-12-25 23:52:42 +01:00
SafeEyes/safeeyes/plugins/mediacontrol/plugin.py

95 lines
2.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# Safe Eyes is a utility to remind you to take break frequently
# to protect your eyes from eye strain.
# Copyright (C) 2019 Gobinath
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
2021-01-27 04:55:59 +01:00
Media Control plugin lets users to pause currently playing media player from the break screen.
"""
import logging
import os
import re
import gi
from safeeyes.model import TrayAction
gi.require_version('Gtk', '3.0')
2023-12-31 00:01:57 +01:00
from gi.repository import Gtk, Gio
2019-01-17 00:15:31 +01:00
tray_icon_path = None
def __active_players():
"""
List of all media players which are playing now.
"""
players = []
2023-12-31 00:01:57 +01:00
dbus_proxy = Gio.DBusProxy.new_for_bus_sync(
bus_type=Gio.BusType.SESSION,
flags=Gio.DBusProxyFlags.DO_NOT_LOAD_PROPERTIES,
info=None,
name='org.freedesktop.DBus',
object_path='/org/freedesktop/DBus',
interface_name='org.freedesktop.DBus',
cancellable=None,
)
for service in dbus_proxy.ListNames():
if re.match('org.mpris.MediaPlayer2.', service):
2023-12-31 00:01:57 +01:00
player = Gio.DBusProxy.new_for_bus_sync(
bus_type=Gio.BusType.SESSION,
flags=Gio.DBusProxyFlags.NONE,
info=None,
name=service,
object_path='/org/mpris/MediaPlayer2',
interface_name='org.mpris.MediaPlayer2.Player',
cancellable=None,
)
status = player.get_cached_property('PlaybackStatus').unpack().lower()
if status == "playing":
players.append(player)
return players
def __pause_players(players):
"""
Pause all playing media players using dbus.
"""
for player in players:
2023-12-31 00:01:57 +01:00
player.Pause()
def init(ctx, safeeyes_config, plugin_config):
"""
Initialize the screensaver plugin.
"""
2019-01-17 00:15:31 +01:00
global tray_icon_path
tray_icon_path = os.path.join(plugin_config['path'], "resource/pause.png")
def get_tray_action(break_obj):
"""
Return TrayAction only if there is a media player currently playing.
"""
players = __active_players()
if players:
2019-01-17 00:15:31 +01:00
return TrayAction.build("Pause media",
tray_icon_path,
Gtk.STOCK_MEDIA_PAUSE,
lambda: __pause_players(players))