예제 #1
0
class Master(object):
    """
    Master Monitor. This monitor will connect to the db and will call all
    monitors with the changes. This class will make sure they don't re-query
    all at once and have a small delay between them to keep the load down.
    """
    def __init__(self, db):
        self.monitors = []
        self.timer = Timer(self.check)
        db.signals['changed'].connect(self.changed)


    def connect(self, monitor):
        """
        Connect a new monitor.
        """
        self.monitors.append((weakref(monitor), [ False, [] ]))


    def changed(self, changes):
        """
        Database callback with changed ids.
        """
        if len(changes) == 1 and changes[0][0] == 'media':
            for m, c in self.monitors:
                c[0] = True
        for m, c in self.monitors:
            c[1].extend(changes)
        if not self.timer.active:
            # TODO: should use scheduler to get interval
            self.timer.start(0.02)


    def check(self):
        """
        Timed callback to call the connected monitor update functions.
        """
        if not self.monitors:
            return False
        monitor, (force, changes) = self.monitors.pop(0)
        if monitor == None:
            return True
        if changes or force:
            monitor.check(changes)
        self.monitors.append((monitor, [ False, [] ]))
        return len(changes) > 0 or force
예제 #2
0
class Master(object):
    """
    Master Monitor. This monitor will connect to the db and will call all
    monitors with the changes. This class will make sure they don't re-query
    all at once and have a small delay between them to keep the load down.
    """
    def __init__(self, db):
        self.monitors = []
        self.timer = Timer(self.check)
        db.signals['changed'].connect(self.changed)

    def connect(self, monitor):
        """
        Connect a new monitor.
        """
        self.monitors.append((weakref(monitor), [False, []]))

    def changed(self, changes):
        """
        Database callback with changed ids.
        """
        if len(changes) == 1 and changes[0][0] == 'media':
            for m, c in self.monitors:
                c[0] = True
        for m, c in self.monitors:
            c[1].extend(changes)
        if not self.timer.active:
            # TODO: should use scheduler to get interval
            self.timer.start(0.02)

    def check(self):
        """
        Timed callback to call the connected monitor update functions.
        """
        if not self.monitors:
            return False
        monitor, (force, changes) = self.monitors.pop(0)
        if monitor == None:
            return True
        if changes or force:
            monitor.check(changes)
        self.monitors.append((monitor, [False, []]))
        return len(changes) > 0 or force
예제 #3
0
class PluginInterface(plugin.DaemonPlugin):
    """
    This plugin enables a small bar showing information about audio being played
    when detached with the detach plugin.

    If the idlebar is loaded and there is enough space left there, this plugin
    will draw itself there, otherwise it will draw at the right bottom of the
    screen.

    A dot (graphvis) diagram of the states the bar has
    dot -Tpng -odetach.png detach.dot

    | digraph finite_state_machine {
    |   rankdir=TB;
    |   size="8,5"
    |   node [shape = doublecircle]; Hide;
    |   node [shape = circle];
    |   { rank = same; "Wait"; "Show"; }
    |   Hide -> Show [ label = "detach(start timer)" ];
    |   Show -> Wait [ label = "play_end" ];
    |   Show -> Hide [ label = "stop(stop timer)" ];
    |   Wait -> Hide [ label = "stop(stop timer)" ];
    |   Show -> Show [ label = "play_start" ];
    |   Wait -> Show [ label = "play_start" ];
    |   Wait -> Hide [ label = "timeout(stop timer)" ];
    | }
    """
    detached = False

    def __init__(self):
        """initialise the DaemonPlugin interface"""
        logger.log( 9, 'detachbar.PluginInterface.__init__()')
        plugin.DaemonPlugin.__init__(self)
        self.plugin_name = 'audio.detachbar'
        self.player = None
        self.timer = Timer(self._timer_handler)
        self.event = EventHandler(self._event_handler)
        self.event.register()
        self.state = BAR_NOTSET
        self.update(BAR_HIDE)
        # tunables
        self.wait_timeout = 3  # 3 seconds till we hide the bar


    def _event_handler(self, event):
        logger.log( 9, '_event_handler(event=%s)', event)
        if plugin.isevent(event) == 'DETACH':
            PluginInterface.detached = True
            self.update(BAR_SHOW)
        elif plugin.isevent(event) == 'ATTACH':
            PluginInterface.detached = False
            self.update(BAR_HIDE)
        elif event == STOP:
            PluginInterface.detached = False
            self.update(BAR_HIDE)
        elif event == BUTTON and event.arg == 'STOP':
            PluginInterface.detached = False
            self.update(BAR_HIDE)
        elif PluginInterface.detached:
            if event == PLAY_START:
                self.update(BAR_SHOW)
            elif event == PLAY_END:
                self.update(BAR_WAIT)


    def _timer_handler(self):
        logger.log( 9, '_timer_handler()')
        self.update()


    def update(self, state=None):
        """
        update the bar according to bar state
        """
        logger.log( 8, 'update()')
        if state is not None:
            if state == BAR_SHOW:
                self.show()
                self.timer.start(1.0)
            elif state == BAR_HIDE:
                self.timer.stop()
                self.hide()
            elif state == BAR_WAIT:
                self.time = time.time()
            self.state = state

        if self.state == BAR_SHOW:
            skin.redraw()
        elif self.state == BAR_HIDE:
            skin.redraw()
        elif self.state == BAR_WAIT:
            if self.time and (time.time() - self.time) > self.wait_timeout:
                self.update(BAR_HIDE)


    def show(self):
        """
        used when showing for the first time
        """
        logger.log( 9, 'show()')
        self.player = audio.player.get()
        if self.player:
            self.getinfo()


    def hide(self):
        """
        used when hiding the bar
        """
        logger.log( 9, 'hide()')
        self.render = []
        self.player = None
        self.time   = None
        self.bar    = None


    def stop(self):
        """
        stops the player, waiting for timeout
        """
        logger.log( 9, 'stop()')
        #self.state = BAR_WAIT
        #self.time  = time.time()


    def draw(self, (type, object), osd):
        """
        draws the bar
        called from the skin's redraw method
        """
        logger.log( 8, 'draw((type=%r, object=%r), osd=%r)', type, object, osd)
        if self.player is None:
            return

        #if self.state == BAR_WAIT:
        #    # when idle, wait for a new player
        #    if audio.player.get():
        #        self.show()

        #elif self.state == BAR_SHOW:
        #    if self.player and not self.player.running:
        #        # player stopped, we also stop
        #        # and wait for a new one
        #        self.player = None
        #        self.stop()
        #        return

        #    if type == 'player':
        #        # Oops, showing the player again, stop me
        #        self.stop()
        #        self.hide()
        #        return

        font = osd.get_font('detachbar')

        if font == osd.get_font('default'):
            font = osd.get_font('info value')

        self.calculatesizes(osd, font)

        if self.image:
            x = self.x - self.h
            width  = self.w + 70 - 10
        else:
            x = self.x
            width = self.w

        if not self.idlebar:
            y = self.y - 10
            height = self.h
            osd.drawroundbox(x, y, width, height, (0xf0ffffffL, 5, 0xb0000000L, 10))

        if self.image:
            osd.draw_image(self.image, (x+5, self.y, 50, 50))

        y = self.t_y

        for r in self.render:
            osd.write_text(r, font, None, self.t_x, y, self.t_w, self.font_h, 'center', 'center')
            y += self.font_h

        try:
            if self.player.item.length:
                progress = '%s/%s' % (self.formattime(self.player.item.elapsed),
                    self.formattime(self.player.item.length))
            else:
                progress = '%s' % self.formattime(self.player.item.elapsed)
        except AttributeError:
            progress = ''

        osd.write_text(progress, font, None, self.t_x, y, self.t_w, self.font_h, 'center', 'center')
예제 #4
0
class PluginInterface(plugin.DaemonPlugin):
    """
    This plugin enables a small bar showing information about audio being played
    when detached with the detach plugin.

    If the idlebar is loaded and there is enough space left there, this plugin
    will draw itself there, otherwise it will draw at the right bottom of the
    screen.

    A dot (graphvis) diagram of the states the bar has
    dot -Tpng -odetach.png detach.dot

    | digraph finite_state_machine {
    |   rankdir=TB;
    |   size="8,5"
    |   node [shape = doublecircle]; Hide;
    |   node [shape = circle];
    |   { rank = same; "Wait"; "Show"; }
    |   Hide -> Show [ label = "detach(start timer)" ];
    |   Show -> Wait [ label = "play_end" ];
    |   Show -> Hide [ label = "stop(stop timer)" ];
    |   Wait -> Hide [ label = "stop(stop timer)" ];
    |   Show -> Show [ label = "play_start" ];
    |   Wait -> Show [ label = "play_start" ];
    |   Wait -> Hide [ label = "timeout(stop timer)" ];
    | }
    """
    detached = False

    def __init__(self):
        """initialise the DaemonPlugin interface"""
        logger.log(9, 'detachbar.PluginInterface.__init__()')
        plugin.DaemonPlugin.__init__(self)
        self.plugin_name = 'audio.detachbar'
        self.player = None
        self.timer = Timer(self._timer_handler)
        self.event = EventHandler(self._event_handler)
        self.event.register()
        self.state = BAR_NOTSET
        self.update(BAR_HIDE)
        # tunables
        self.wait_timeout = 3  # 3 seconds till we hide the bar

    def _event_handler(self, event):
        logger.log(9, '_event_handler(event=%s)', event)
        if plugin.isevent(event) == 'DETACH':
            PluginInterface.detached = True
            self.update(BAR_SHOW)
        elif plugin.isevent(event) == 'ATTACH':
            PluginInterface.detached = False
            self.update(BAR_HIDE)
        elif event == STOP:
            PluginInterface.detached = False
            self.update(BAR_HIDE)
        elif event == BUTTON and event.arg == 'STOP':
            PluginInterface.detached = False
            self.update(BAR_HIDE)
        elif PluginInterface.detached:
            if event == PLAY_START:
                self.update(BAR_SHOW)
            elif event == PLAY_END:
                self.update(BAR_WAIT)

    def _timer_handler(self):
        logger.log(9, '_timer_handler()')
        self.update()

    def update(self, state=None):
        """
        update the bar according to bar state
        """
        logger.log(8, 'update()')
        if state is not None:
            if state == BAR_SHOW:
                self.show()
                self.timer.start(1.0)
            elif state == BAR_HIDE:
                self.timer.stop()
                self.hide()
            elif state == BAR_WAIT:
                self.time = time.time()
            self.state = state

        if self.state == BAR_SHOW:
            skin.redraw()
        elif self.state == BAR_HIDE:
            skin.redraw()
        elif self.state == BAR_WAIT:
            if self.time and (time.time() - self.time) > self.wait_timeout:
                self.update(BAR_HIDE)

    def show(self):
        """
        used when showing for the first time
        """
        logger.log(9, 'show()')
        self.player = audio.player.get()
        if self.player:
            self.getinfo()

    def hide(self):
        """
        used when hiding the bar
        """
        logger.log(9, 'hide()')
        self.render = []
        self.player = None
        self.time = None
        self.bar = None

    def stop(self):
        """
        stops the player, waiting for timeout
        """
        logger.log(9, 'stop()')
        #self.state = BAR_WAIT
        #self.time  = time.time()

    def draw(self, (type, object), osd):
        """
        draws the bar
        called from the skin's redraw method
        """
        logger.log(8, 'draw((type=%r, object=%r), osd=%r)', type, object, osd)
        if self.player is None:
            return

        #if self.state == BAR_WAIT:
        #    # when idle, wait for a new player
        #    if audio.player.get():
        #        self.show()

        #elif self.state == BAR_SHOW:
        #    if self.player and not self.player.running:
        #        # player stopped, we also stop
        #        # and wait for a new one
        #        self.player = None
        #        self.stop()
        #        return

        #    if type == 'player':
        #        # Oops, showing the player again, stop me
        #        self.stop()
        #        self.hide()
        #        return

        font = osd.get_font('detachbar')

        if font == osd.get_font('default'):
            font = osd.get_font('info value')

        self.calculatesizes(osd, font)

        if self.image:
            x = self.x - self.h
            width = self.w + 70 - 10
        else:
            x = self.x
            width = self.w

        if not self.idlebar:
            y = self.y - 10
            height = self.h
            osd.drawroundbox(x, y, width, height,
                             (0xf0ffffffL, 5, 0xb0000000L, 10))

        if self.image:
            osd.draw_image(self.image, (x + 5, self.y, 50, 50))

        y = self.t_y

        for r in self.render:
            osd.write_text(r, font, None, self.t_x, y, self.t_w, self.font_h,
                           'center', 'center')
            y += self.font_h

        try:
            if self.player.item.length:
                progress = '%s/%s' % (self.formattime(
                    self.player.item.elapsed),
                                      self.formattime(self.player.item.length))
            else:
                progress = '%s' % self.formattime(self.player.item.elapsed)
        except AttributeError:
            progress = ''

        osd.write_text(progress, font, None, self.t_x, y, self.t_w,
                       self.font_h, 'center', 'center')