139 lines
2.9 KiB
Python
Executable File
139 lines
2.9 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import gi, sys, os, threading, time
|
|
# import dbus
|
|
# import dbus.service, dbus.mainloop.glib
|
|
from pprint import pprint as print
|
|
|
|
|
|
gi.require_version('Gtk', '3.0')
|
|
|
|
from gi.repository import Gtk, Gdk, GLib, GdkPixbuf, GObject
|
|
|
|
from window import SonistWindow
|
|
|
|
from mpd.base import MPDClient
|
|
|
|
app_id = 'fun.wkit.sonist'
|
|
|
|
|
|
def run_async(func):
|
|
def wrapper(*args, **kwargs):
|
|
thread = threading.Thread(target=func, args=args, kwargs=kwargs)
|
|
thread.daemon = True
|
|
thread.start()
|
|
return thread
|
|
return wrapper
|
|
|
|
|
|
class Application(Gtk.Application):
|
|
|
|
__gsignals__ = {
|
|
'playing': (GObject.SignalFlags.RUN_FIRST, None, (bool,)),
|
|
'song_changed': (GObject.SignalFlags.RUN_FIRST, None, (bool,)),
|
|
'state_changed': (GObject.SignalFlags.RUN_FIRST, None, (str,))
|
|
}
|
|
|
|
|
|
def __init__(self):
|
|
Gtk.Application.__init__(self, application_id = app_id)
|
|
|
|
self.mpd_state = None
|
|
self.mpd_curr_song = None
|
|
|
|
self.mpd = MPDClient()
|
|
|
|
self.connect('window-removed', self.on_window_removed)
|
|
|
|
|
|
|
|
|
|
@run_async
|
|
def ping(self):
|
|
if self.mpd_is_online:
|
|
while True:
|
|
try:
|
|
self.mpd.ping()
|
|
stat = self.mpd.status()
|
|
song = self.mpd.currentsong() or {}
|
|
state = stat.get('state')
|
|
|
|
if self.mpd_curr_song != song.get('id'):
|
|
self.mpd_curr_song = song.get('id')
|
|
self.emit('song_changed', self.mpd_curr_song)
|
|
|
|
if state == 'play':
|
|
self.emit('playing', False)
|
|
|
|
if self.mpd_state != state:
|
|
self.emit('state_changed', state)
|
|
|
|
self.mpd_state = state
|
|
time.sleep(0.5)
|
|
|
|
except Exception as e:
|
|
self.mpd.kill()
|
|
time.sleep(2)
|
|
self.mpd_is_online = self.mpd_connect()
|
|
|
|
|
|
def mpd_connect(self):
|
|
try:
|
|
self.mpd.connect("127.0.0.1", 6600)
|
|
return True
|
|
except:
|
|
return False
|
|
|
|
|
|
def do_activate(self):
|
|
print('hello mpc')
|
|
self.set_app_menu(None)
|
|
self.set_menubar(None)
|
|
|
|
self.mpd_is_online = self.mpd_connect()
|
|
|
|
self.window = SonistWindow(self)
|
|
self.add_window(self.window)
|
|
self.window.show_all()
|
|
|
|
self.ping()
|
|
|
|
|
|
def on_window_removed(self, app, win):
|
|
if len(self.get_windows()) == 0:
|
|
print('朕要休息了~~~')
|
|
|
|
|
|
|
|
|
|
""" class ApplicationService(dbus.service.Object):
|
|
def __init__(self, app):
|
|
self.app = app
|
|
bus_name = dbus.service.BusName(app_id, bus = dbus.SessionBus())
|
|
dbus.service.Object.__init__(self, bus_name, '/')
|
|
|
|
|
|
@dbus.service.method(app_id)
|
|
def call_app(self):
|
|
self.app.present()
|
|
"""
|
|
|
|
if __name__ == "__main__":
|
|
# dbus.mainloop.glib.DBusGMainLoop(set_as_default = True)
|
|
# bus = dbus.SessionBus()
|
|
|
|
# try:
|
|
# obj = bus.get_object(app_id, '/')
|
|
# obj.call_app()
|
|
# sys.exit(0)
|
|
# except dbus.DBusException:
|
|
# pass
|
|
|
|
|
|
app = Application()
|
|
|
|
# ApplicationService(app)
|
|
app.run(sys.argv)
|
|
|
|
|