Exemple #1
0
class PluginInterface(plugin.DaemonPlugin):
    """
    This plugin shows the current encoding status
    Activate with:
    | plugin.activate('idlebar.encoding')
    """
    def __init__(self):
        logger.log(9, 'encoding.PluginInterface.__init__(self)')
        plugin.DaemonPlugin.__init__(self)
        #IdleBarPlugin.__init__(self)
        self.poll_interval = 1
        self.draw_interval = self.poll_interval
        self.last_interval = self.poll_interval
        self.lastdraw = 0
        self.lastpoll = 0
        self.plugin_name = 'video.encodingstatus'
        self.server = EncodingClientActions()

        self.skin = skin.get_singleton()
        self.barimg = os.path.join(config.ICON_DIR, 'status/encoding_bar.png')
        self.boximg = os.path.join(config.ICON_DIR, 'status/encoding_box.png')
        self.boxborder = 3
        self.padding = 5  # internal padding for box vs text
        self.image = None
        self.cacheimg = {}
        self.muted = False
        self.encoding = -1
        self.progress = 0
        self.jobname = ''
        self.calculate = True
        self.jobs = ''
        self.mode = 'Not Running'
        self.text = []
        self.percent = 0.0
        self.running = False
        self.drawtime = 0
        self.polltime = 0
        self.state = 'noserver'
        self.laststate = None
        self.font = self.skin.get_font(config.ENCODING_IDLEBAR_FONT)

    def config(self):
        return [
            ('ENCODING_IDLEBAR', True, 'Use the idlebar'),
            ('ENCODING_IDLEBAR_FONT', 'small0',
             'Font to use in the idlebar, tiny0 is another good choice'),
        ]

    def getimage(self, image, osd, cache=False):
        logger.log(9, 'getimage(self, image, osd, cache=False)')
        if image.find(config.ICON_DIR) == 0 and image.find(
                osd.settings.icon_dir) == -1:
            new_image = os.path.join(osd.settings.icon_dir,
                                     image[len(config.ICON_DIR) + 1:])
            if os.path.isfile(new_image):
                image = new_image
        if cache:
            if image not in self.cacheimg.keys():
                self.cacheimg[image] = pygame.image.load(image)
            return self.cacheimg[image]

        return pygame.image.load(image)

    def settext(self):
        logger.log(9, 'settext(self)')
        """
        set the text
        """
        (status, jobs) = self.server.listJobs()
        if not status:
            self.state = 'noserver'
            self.jobs = _('encoding server not running')
            self.draw_interval = 5000
            self.running = False
            return 0
        if not jobs:
            self.state = 'nojobs'
            self.jobs = _('encoding server has no jobs')
            self.draw_interval = 5000
            self.running = False
            return 0
        self.state = 'active'
        (idnr, jobname, jobstate) = jobs[0]
        joblist = jobname
        for job in jobs[1:]:
            (idnr, jobname, jobstate) = job
            joblist += ', ' + jobname
        self.jobs = joblist
        self.running = True

        self.text = []
        (status, progress) = self.server.getProgress()
        if status:
            if progress[1] == 0:
                self.mode = 'Not started'
                self.state = 'active'
                self.draw_interval = 5000
            elif progress[1] == 1:
                self.mode = 'Audio'
                self.state = 'audio'
                self.draw_interval = 200
            elif progress[1] == 2:
                self.mode = 'Video-1'
                self.state = 'video'
                self.draw_interval = 1000
            elif progress[1] == 3:
                self.mode = 'Video-2'
                self.state = 'video'
                self.draw_interval = 1000
            elif progress[1] == 4:
                self.mode = 'Multiplexing'
                self.state = 'multiplexing'
                self.draw_interval = 1000
            self.text.append("%s %s%% %s" %
                             (self.mode, progress[2], progress[3]))
            self.percent = progress[2] / 100.0

    def calculatesizes(self, osd, font):
        logger.log(9, 'calculatesizes(self, osd, font)')
        """
        sizecalcs is not necessery on every pass
        """
        if config.ENCODING_IDLEBAR:
            if not hasattr(self, 'idlebar'):
                self.idlebar = plugin.getbyname('idlebar')
                if self.idlebar:
                    self.idlebar_max = osd.width + osd.x
                    logger.log(9, 'idlebar_max=%s, free_space=%s',
                               self.idlebar_max, self.idlebar.free_space)
                    for p in plugin.get('idlebar'):
                        if hasattr(p, 'clock_left_position'):
                            self.idlebar_max = p.clock_left_position

                    if self.idlebar_max - self.idlebar.free_space < 250:
                        logger.debug(
                            'free space in idlebar to small, using normal detach'
                        )
                        self.idlebar = None
                    else:
                        # this doesn't work, but needs to for the detachbar
                        self.idlebar.take_space(250)
        else:
            self.idlebar = None

        if self.idlebar:
            self.boxborder = 0
            self.padding = 0

        if self.calculate:
            self.calculate = False
            self.font_h = font.font.height

            logger.log(9, 'osd.width=%s, osd.height=%s, osd.x=%s, osd.y=%s',
                       osd.width, osd.height, osd.x, osd.y)
            screen_width = osd.width + 2 * osd.x
            screen_height = osd.height + 2 * osd.y

            used_width = font.font.stringsize(self.jobs) - 1
            # ensure that the box width is between min and max
            used_width = max(used_width, 200)
            used_width = min(used_width, 280)
            used_height = self.font_h
            if self.running:
                w, h = self.getimage(self.boximg, osd).get_size()
                used_width = max(used_width, w)
                used_height += h
                for text in self.text:
                    used_width = max(used_width,
                                     font.font.stringsize(text)) - 1
                    used_height += self.font_h

            logger.log(
                9,
                'screen_width=%s, screen_height=%s, used_width=%s, used_height=%s, font_h=%s',
                screen_width, screen_height, used_width, used_height,
                self.font_h)

            self.boxw = used_width + (self.padding + self.boxborder) * 2
            self.boxh = used_height + (self.padding + self.boxborder) * 2
            self.bx = osd.x
            self.by = screen_height - osd.y - self.boxh
            self.textw = used_width
            self.texth = used_height
            self.tx = self.bx + self.boxborder + self.padding
            self.ty = self.by + self.boxborder + self.padding

        if self.idlebar:
            if self.image:
                self.bx = self.idlebar.free_space + 250 + 70
            else:
                self.bx = self.idlebar.free_space + 250
            self.by = osd.y
            self.tx = self.bx + self.boxborder + self.padding
            self.ty = self.by + self.boxborder + self.padding
            self.textw = min(self.textw, self.idlebar_max - self.bx - 30)

    def draw(self, (type, object), osd):
        logger.log(9, 'draw(self, (type, object), osd)')
        now = time.time()
        duration = now - self.drawtime
        logger.log(9, "draw=%.2f, interval=%s, state=%s", duration,
                   self.draw_interval, self.state)
        self.drawtime = now
        self.lastdraw = now

        self.calculate = True
        self.settext()
        self.calculatesizes(osd, self.font)

        logger.log(
            9, 'self:bx=%s, by=%s, boxh=%s, boxw=%s, border=%s, padding=%s',
            self.bx, self.by, self.boxh, self.boxw, self.boxborder,
            self.padding)

        if self.idlebar:
            osd.drawroundbox(self.bx, self.by, self.boxw, self.boxh,
                             (0xffffffffL, self.boxborder, 0x40ffff00L, 0))
            #  A R G B                      A R G B
            #  background border_width      border     border_radius
        else:
            osd.drawroundbox(
                self.bx, self.by, self.boxw, self.boxh,
                (0xf0ffffffL, self.boxborder, 0xb0000000L, self.boxborder))

        logger.log(9, 'self:tx=%s, ty=%s, texth=%s, textw=%s', self.tx,
                   self.ty, self.texth, self.textw)
        y = self.ty
        osd.write_text(self.jobs, self.font, None, self.tx, y, self.textw,
                       self.font_h, 'center', 'center')
        if self.running:
            y += self.font_h
            encbar = self.getimage(self.barimg, osd, True)
            encbox = self.getimage(self.boximg, osd)
            w, h = encbox.get_size()
            encbox.blit(encbar, (3, 3), (0, 0, (w * self.percent), h))
            x = (self.textw - w) / 2
            #osd.drawroundbox(self.tx+x-2, y-2, w+4, h+4,
            #    (0xf0ffffffL, 2, 0xb000ffffL, 0))
            osd.drawimage(encbox, (self.tx + x, y, -1, -1))[0]
            y += h
            for text in self.text:
                osd.write_text(text, self.font, None, self.tx, y, self.textw,
                               self.font_h, 'center', 'center')
                y += self.font_h

        return self.textw
Exemple #2
0
class PluginInterface(IdleBarPlugin):
    """
    Shows the status of the current encoding job

    Activate with::

        | plugin.activate('idlebar.transcode')
    """
    def __init__(self):
        """ Initialise the transcode idlebar plug-in """
        logger.log(9, 'transcode.PluginInterface.__init__()')
        IdleBarPlugin.__init__(self)
        self.plugin_name = 'idlebar.transcode'

        self.background = os.path.join(config.ICON_DIR,
                                       'status/enc_background.png')
        self.leftclamp = os.path.join(config.ICON_DIR,
                                      'status/enc_leftclamp.png')
        self.rightclamp = os.path.join(config.ICON_DIR,
                                       'status/enc_rightclamp.png')
        self.notrunning = os.path.join(config.ICON_DIR,
                                       'status/enc_notrunning.png')
        self.nojobs = os.path.join(config.ICON_DIR, 'status/enc_nojobs.png')
        self.audio = os.path.join(config.ICON_DIR, 'status/enc_audio.png')
        self.video = os.path.join(config.ICON_DIR, 'status/enc_video.png')
        self.video1 = os.path.join(config.ICON_DIR, 'status/enc_video1.png')
        self.video2 = os.path.join(config.ICON_DIR, 'status/enc_video2.png')
        self.video3 = os.path.join(config.ICON_DIR, 'status/enc_video3.png')
        self.multiplex = os.path.join(config.ICON_DIR,
                                      'status/enc_multiplex.png')

        self.cacheimg = {}
        self.background_w, self.background_h = (0, 0)
        self.leftclamp_w, self.leftclamp_h = (0, 0)
        self.rightclamp_w, self.rightclamp_h = (0, 0)
        self.remaining_min = re.compile('[0-9]*')
        self.remaining = ''
        self.progress = 0
        self.progress_x = None
        self.leftclamp_x = 0
        self.rightclamp_x = 0

        self.poll_interval = 1  # 1 sec should be same as most frequent
        self.draw_interval = 5.0  # 5 secs
        self.last_interval = self.poll_interval
        self.lastdraw = 0
        self.lastpoll = 0
        self.drawtime = 0
        self.server = EncodingClientActions()

        self.skin = skin.get_singleton()
        self.calculate = True
        self.jobs = ''
        self.mode = 'Not Running'
        self.state = 'noserver'
        self.laststate = None
        self.percent = 0.0
        self.running = False
        self.font = self.skin.get_font(config.OSD_IDLEBAR_FONT)
        self.timer = kaa.Timer(self._timerhandler)
        self.timer.start(self.poll_interval)
        logger.debug('transcode.PluginInterface.__init__() done.')

    def config(self):
        logger.log(9, 'config()')
        return []

    def getimage(self, image, osd, cache=False):
        """
        Load the image from the cache when available otherwise load the image and save
        in the cache.
        """
        logger.log(9, 'getimage(image=%r, osd=%r, cache=%s)', image, osd,
                   cache)
        if image.find(config.ICON_DIR) == 0 and image.find(
                osd.settings.icon_dir) == -1:
            new_image = os.path.join(osd.settings.icon_dir,
                                     image[len(config.ICON_DIR) + 1:])
            if os.path.isfile(new_image):
                image = new_image
        if cache:
            if image not in self.cacheimg.keys():
                self.cacheimg[image] = pygame.image.load(image)
            return self.cacheimg[image]

        return pygame.image.load(image)

    def set_sprite(self):
        """ set the sprite image name and the drawing interval """
        logger.log(9, 'set_sprite()')
        (status, jobs) = self.server.listJobs()
        if not status:
            self.sprite = self.notrunning
            self.state = 'noserver'
            self.jobs = _('encoding server not running')
            self.draw_interval = 5.0
            self.running = False
            return (self.background_w, self.background_h)
        if not jobs:
            self.sprite = self.nojobs
            self.state = 'nojobs'
            self.jobs = _('encoding server has no jobs')
            self.draw_interval = 5.0
            self.running = False
            return (self.background_w, self.background_h)
        self.state = 'active'
        (idnr, jobname, jobstate) = jobs[0]
        joblist = jobname
        for job in jobs[1:]:
            (idnr, jobname, jobstate) = job
            joblist += ', ' + jobname
        self.jobs = joblist
        self.running = True

        (status, progress) = self.server.getProgress()
        if status:
            if progress[1] == 0:
                self.sprite = self.nojobs
                self.mode = 'Not started'
                self.state = 'active'
                self.draw_interval = 5.0
            elif progress[1] == 1:
                self.sprite = self.audio
                self.mode = 'Audio'
                self.state = 'audio'
                self.draw_interval = 0.2
            elif progress[1] == 2:
                self.sprite = self.video1
                self.mode = 'Video-1'
                self.state = 'video'
                self.draw_interval = 1.0
            elif progress[1] == 3:
                self.sprite = self.video2
                self.mode = 'Video-2'
                self.state = 'video'
                self.draw_interval = 1.0
            elif progress[1] == 4:
                self.sprite = self.multiplex
                self.mode = 'Multiplexing'
                self.state = 'multiplexing'
                self.draw_interval = 1.0
            if isinstance(progress[3], basestring):
                remaining = self.remaining_min.search(progress[3])
            else:
                remaining = 0
            self.remaining = remaining and remaining.group() or ''
            self.progress = progress[2]
            self.percent = progress[2] / 100.0
            return (self.background_w, self.background_h)

    def calculatesizes(self, osd, font):
        """size calcs is not necessery on every pass
        There are some shortcuts here, the left and right clamps are the same with
        all sprites are the same size and the background
        return true when the progress has changed, false otherwise
        """
        logger.log(9, 'calculatesizes(osd, font)')
        if self.progress_x == None:
            background = self.getimage(self.background, osd)
            rightclamp = self.getimage(self.rightclamp, osd, True)
            leftclamp = self.getimage(self.leftclamp, osd, True)
            self.background_w, self.background_h = background.get_size()
            self.leftclamp_w, self.leftclamp_h = leftclamp.get_size()
            self.rightclamp_w, self.rightclamp_h = rightclamp.get_size()
            logger.log(9, 'background: w=%s, h=%s', self.background_w,
                       self.background_h)
            logger.log(9, 'leftclamp: w=%s, h=%s', self.leftclamp_w,
                       self.leftclamp_h)
            logger.log(9, 'rightclamp: w=%s, h=%s', self.rightclamp_w,
                       self.rightclamp_h)

        progress_x = ((self.background_w -
                       (2 * self.leftclamp_w)) * self.progress) / 200
        logger.log(
            8, 'progress_x=%s, background_w=%s, leftclamp_w=%s, progress=%s',
            progress_x, self.background_w, self.leftclamp_w, self.progress)

        if self.progress_x != progress_x:
            self.progress_x = progress_x
            self.leftclamp_x = self.progress_x
            self.rightclamp_x = self.background_w - self.rightclamp_w - self.progress_x
            logger.log(9, 'progress_x=%s, leftclamp_x=%s, rightclamp_x=%s',
                       self.progress_x, self.leftclamp_x, self.rightclamp_x)

            return True
        return False

    def draw(self, (type, object), x, osd):
        """ Build the image by blitting sub images on the background and draw the background """
        logger.log(8, 'draw((type=%r, object=), x=%r, osd=)', type, x)
        now = time.time()
        duration = now - self.drawtime
        logger.log(9, "draw=%.2f, interval=%s, state=%s", duration,
                   self.draw_interval, self.state)
        self.drawtime = now
        self.lastdraw = now

        (sprite_w, sprite_h) = self.set_sprite()
        self.calculatesizes(osd, self.font)

        background = self.getimage(self.background, osd)
        if self.sprite:
            sprite = self.getimage(self.sprite, osd)
            background.blit(sprite, (0, 0),
                            (0, 0, self.background_w, self.background_h))
        leftclamp = self.getimage(self.leftclamp, osd, True)
        rightclamp = self.getimage(self.rightclamp, osd, True)
        background.blit(leftclamp, (self.leftclamp_x, 1),
                        (0, 0, self.leftclamp_w, self.leftclamp_h))
        background.blit(rightclamp, (self.rightclamp_x, 1),
                        (0, 0, self.rightclamp_w, self.rightclamp_h))
        osd.drawimage(background, (x, osd.y, -1, -1))[0]
        if self.remaining:
            font = osd.get_font(config.OSD_IDLEBAR_FONT)
            widthdf = font.stringsize(self.remaining)
            remaining_x = x + ((self.background_w - widthdf) / 2)
            remaining_y = osd.y + self.background_h - font.h + 10
            logger.debug('remaining=%r x=%s y=%s widthdf=%s font.h=%s',
                         self.remaining, remaining_x, remaining_y, widthdf,
                         font.h)

            osd.write_text(self.remaining, font, None, remaining_x,
                           remaining_y, widthdf, font.h, 'center', 'top')

        return self.background_w
Exemple #3
0
class PluginInterface(IdleBarPlugin):
    """
    Shows the status of the current encoding job

    Activate with::

        | plugin.activate('idlebar.transcode')
    """

    def __init__(self):
        """ Initialise the transcode idlebar plug-in """
        logger.log( 9, 'transcode.PluginInterface.__init__()')
        IdleBarPlugin.__init__(self)
        self.plugin_name = 'idlebar.transcode'

        self.background = os.path.join(config.ICON_DIR, 'status/enc_background.png')
        self.leftclamp  = os.path.join(config.ICON_DIR, 'status/enc_leftclamp.png')
        self.rightclamp = os.path.join(config.ICON_DIR, 'status/enc_rightclamp.png')
        self.notrunning = os.path.join(config.ICON_DIR, 'status/enc_notrunning.png')
        self.nojobs     = os.path.join(config.ICON_DIR, 'status/enc_nojobs.png')
        self.audio      = os.path.join(config.ICON_DIR, 'status/enc_audio.png')
        self.video      = os.path.join(config.ICON_DIR, 'status/enc_video.png')
        self.video1     = os.path.join(config.ICON_DIR, 'status/enc_video1.png')
        self.video2     = os.path.join(config.ICON_DIR, 'status/enc_video2.png')
        self.video3     = os.path.join(config.ICON_DIR, 'status/enc_video3.png')
        self.multiplex  = os.path.join(config.ICON_DIR, 'status/enc_multiplex.png')

        self.cacheimg = {}
        self.background_w, self.background_h = (0, 0)
        self.leftclamp_w, self.leftclamp_h = (0, 0)
        self.rightclamp_w, self.rightclamp_h = (0, 0)
        self.remaining_min = re.compile('[0-9]*')
        self.remaining = ''
        self.progress  = 0
        self.progress_x = None
        self.leftclamp_x = 0
        self.rightclamp_x = 0

        self.poll_interval = 1 # 1 sec should be same as most frequent
        self.draw_interval = 5.0 # 5 secs
        self.last_interval = self.poll_interval
        self.lastdraw  = 0
        self.lastpoll  = 0
        self.drawtime  = 0
        self.server    = EncodingClientActions()

        self.skin      = skin.get_singleton()
        self.calculate = True
        self.jobs      = ''
        self.mode      = 'Not Running'
        self.state     = 'noserver'
        self.laststate = None
        self.percent   = 0.0
        self.running   = False
        self.font      = self.skin.get_font(config.OSD_IDLEBAR_FONT)
        self.timer     = kaa.Timer(self._timerhandler)
        self.timer.start(self.poll_interval)
        logger.debug('transcode.PluginInterface.__init__() done.')


    def config(self):
        logger.log( 9, 'config()')
        return [
        ]


    def getimage(self, image, osd, cache=False):
        """
        Load the image from the cache when available otherwise load the image and save
        in the cache.
        """
        logger.log( 9, 'getimage(image=%r, osd=%r, cache=%s)', image, osd, cache)
        if image.find(config.ICON_DIR) == 0 and image.find(osd.settings.icon_dir) == -1:
            new_image = os.path.join(osd.settings.icon_dir, image[len(config.ICON_DIR)+1:])
            if os.path.isfile(new_image):
                image = new_image
        if cache:
            if image not in self.cacheimg.keys():
                self.cacheimg[image] = pygame.image.load(image)
            return self.cacheimg[image]

        return pygame.image.load(image)


    def set_sprite(self):
        """ set the sprite image name and the drawing interval """
        logger.log( 9, 'set_sprite()')
        (status, jobs) = self.server.listJobs()
        if not status:
            self.sprite = self.notrunning
            self.state = 'noserver'
            self.jobs = _('encoding server not running')
            self.draw_interval = 5.0
            self.running = False
            return (self.background_w, self.background_h);
        if not jobs:
            self.sprite = self.nojobs
            self.state = 'nojobs'
            self.jobs = _('encoding server has no jobs')
            self.draw_interval = 5.0
            self.running = False
            return (self.background_w, self.background_h);
        self.state = 'active'
        (idnr, jobname, jobstate) = jobs[0]
        joblist = jobname
        for job in jobs[1:]:
            (idnr, jobname, jobstate) = job
            joblist += ', ' + jobname
        self.jobs = joblist
        self.running = True

        (status, progress) = self.server.getProgress();
        if status:
            if progress[1] == 0:
                self.sprite = self.nojobs
                self.mode = 'Not started'
                self.state = 'active'
                self.draw_interval = 5.0
            elif progress[1] == 1:
                self.sprite = self.audio
                self.mode = 'Audio'
                self.state = 'audio'
                self.draw_interval = 0.2
            elif progress[1] == 2:
                self.sprite = self.video1
                self.mode = 'Video-1'
                self.state = 'video'
                self.draw_interval = 1.0
            elif progress[1] == 3:
                self.sprite = self.video2
                self.mode = 'Video-2'
                self.state = 'video'
                self.draw_interval = 1.0
            elif progress[1] == 4:
                self.sprite = self.multiplex
                self.mode = 'Multiplexing'
                self.state = 'multiplexing'
                self.draw_interval = 1.0
            if isinstance(progress[3], basestring):
                remaining = self.remaining_min.search(progress[3])
            else:
                remaining = 0
            self.remaining = remaining and remaining.group() or ''
            self.progress = progress[2]
            self.percent = progress[2] / 100.0
            return (self.background_w, self.background_h);


    def calculatesizes(self, osd, font):
        """size calcs is not necessery on every pass
        There are some shortcuts here, the left and right clamps are the same with
        all sprites are the same size and the background
        return true when the progress has changed, false otherwise
        """
        logger.log( 9, 'calculatesizes(osd, font)')
        if self.progress_x == None:
            background = self.getimage(self.background, osd)
            rightclamp = self.getimage(self.rightclamp, osd, True)
            leftclamp = self.getimage(self.leftclamp, osd, True)
            self.background_w, self.background_h = background.get_size()
            self.leftclamp_w, self.leftclamp_h = leftclamp.get_size()
            self.rightclamp_w, self.rightclamp_h = rightclamp.get_size()
            logger.log( 9, 'background: w=%s, h=%s', self.background_w, self.background_h)
            logger.log( 9, 'leftclamp: w=%s, h=%s', self.leftclamp_w, self.leftclamp_h)
            logger.log( 9, 'rightclamp: w=%s, h=%s', self.rightclamp_w, self.rightclamp_h)

        progress_x = ((self.background_w - (2 * self.leftclamp_w)) * self.progress) / 200
        logger.log( 8, 'progress_x=%s, background_w=%s, leftclamp_w=%s, progress=%s', progress_x, self.background_w, self.leftclamp_w, self.progress)


        if self.progress_x != progress_x:
            self.progress_x = progress_x
            self.leftclamp_x = self.progress_x
            self.rightclamp_x = self.background_w - self.rightclamp_w - self.progress_x
            logger.log( 9, 'progress_x=%s, leftclamp_x=%s, rightclamp_x=%s', self.progress_x, self.leftclamp_x, self.rightclamp_x)

            return True
        return False


    def draw(self, (type, object), x, osd):
        """ Build the image by blitting sub images on the background and draw the background """
        logger.log( 8, 'draw((type=%r, object=), x=%r, osd=)', type, x)
        now = time.time()
        duration = now - self.drawtime
        logger.log( 9, "draw=%.2f, interval=%s, state=%s", duration, self.draw_interval, self.state)
        self.drawtime = now
        self.lastdraw = now

        (sprite_w, sprite_h) = self.set_sprite()
        self.calculatesizes(osd, self.font)

        background = self.getimage(self.background, osd)
        if self.sprite:
            sprite = self.getimage(self.sprite, osd)
            background.blit(sprite, (0, 0), (0, 0, self.background_w, self.background_h))
        leftclamp = self.getimage(self.leftclamp, osd, True)
        rightclamp = self.getimage(self.rightclamp, osd, True)
        background.blit(leftclamp, (self.leftclamp_x, 1), (0, 0, self.leftclamp_w, self.leftclamp_h))
        background.blit(rightclamp, (self.rightclamp_x, 1), (0, 0, self.rightclamp_w, self.rightclamp_h))
        osd.drawimage(background, (x, osd.y, -1, -1) )[0]
        if self.remaining:
            font = osd.get_font(config.OSD_IDLEBAR_FONT)
            widthdf = font.stringsize(self.remaining)
            remaining_x = x + ((self.background_w - widthdf) / 2)
            remaining_y = osd.y + self.background_h - font.h + 10
            logger.debug('remaining=%r x=%s y=%s widthdf=%s font.h=%s', self.remaining, remaining_x, remaining_y, widthdf, font.h)

            osd.write_text(self.remaining, font, None, remaining_x, remaining_y, widthdf, font.h, 'center', 'top')

        return self.background_w
Exemple #4
0
class PluginInterface(plugin.DaemonPlugin):
    """
    This plugin shows the current encoding status
    Activate with:
    | plugin.activate('idlebar.encoding')
    """

    def __init__(self):
        logger.log( 9, 'encoding.PluginInterface.__init__(self)')
        plugin.DaemonPlugin.__init__(self)
        #IdleBarPlugin.__init__(self)
        self.poll_interval = 1
        self.draw_interval = self.poll_interval
        self.last_interval = self.poll_interval
        self.lastdraw  = 0
        self.lastpoll  = 0
        self.plugin_name = 'video.encodingstatus'
        self.server    = EncodingClientActions()

        self.skin      = skin.get_singleton()
        self.barimg    = os.path.join(config.ICON_DIR, 'status/encoding_bar.png')
        self.boximg    = os.path.join(config.ICON_DIR, 'status/encoding_box.png')
        self.boxborder = 3
        self.padding   = 5 # internal padding for box vs text
        self.image     = None
        self.cacheimg  = {}
        self.muted     = False
        self.encoding  = -1
        self.progress  = 0
        self.jobname   = ''
        self.calculate = True
        self.jobs      = ''
        self.mode      = 'Not Running'
        self.text      = []
        self.percent   = 0.0
        self.running   = False
        self.drawtime  = 0
        self.polltime  = 0
        self.state     = 'noserver'
        self.laststate = None
        self.font      = self.skin.get_font(config.ENCODING_IDLEBAR_FONT)


    def config(self):
        return [
            ('ENCODING_IDLEBAR', True, 'Use the idlebar'),
            ('ENCODING_IDLEBAR_FONT', 'small0', 'Font to use in the idlebar, tiny0 is another good choice'),
        ]


    def getimage(self, image, osd, cache=False):
        logger.log( 9, 'getimage(self, image, osd, cache=False)')
        if image.find(config.ICON_DIR) == 0 and image.find(osd.settings.icon_dir) == -1:
            new_image = os.path.join(osd.settings.icon_dir, image[len(config.ICON_DIR)+1:])
            if os.path.isfile(new_image):
                image = new_image
        if cache:
            if image not in self.cacheimg.keys():
                self.cacheimg[image] = pygame.image.load(image)
            return self.cacheimg[image]

        return pygame.image.load(image)


    def settext(self):
        logger.log( 9, 'settext(self)')
        """
        set the text
        """
        (status, jobs) = self.server.listJobs()
        if not status:
            self.state = 'noserver'
            self.jobs = _('encoding server not running')
            self.draw_interval = 5000
            self.running = False
            return 0;
        if not jobs:
            self.state = 'nojobs'
            self.jobs = _('encoding server has no jobs')
            self.draw_interval = 5000
            self.running = False
            return 0;
        self.state = 'active'
        (idnr, jobname, jobstate) = jobs[0]
        joblist = jobname
        for job in jobs[1:]:
            (idnr, jobname, jobstate) = job
            joblist += ', ' + jobname
        self.jobs = joblist
        self.running = True

        self.text = []
        (status, progress) = self.server.getProgress();
        if status:
            if progress[1] == 0:
                self.mode = 'Not started'
                self.state = 'active'
                self.draw_interval = 5000
            elif progress[1] == 1:
                self.mode = 'Audio'
                self.state = 'audio'
                self.draw_interval = 200
            elif progress[1] == 2:
                self.mode = 'Video-1'
                self.state = 'video'
                self.draw_interval = 1000
            elif progress[1] == 3:
                self.mode = 'Video-2'
                self.state = 'video'
                self.draw_interval = 1000
            elif progress[1] == 4:
                self.mode = 'Multiplexing'
                self.state = 'multiplexing'
                self.draw_interval = 1000
            self.text.append("%s %s%% %s" % (self.mode, progress[2], progress[3]))
            self.percent = progress[2] / 100.0


    def calculatesizes(self, osd, font):
        logger.log( 9, 'calculatesizes(self, osd, font)')
        """
        sizecalcs is not necessery on every pass
        """
        if config.ENCODING_IDLEBAR:
            if not hasattr(self, 'idlebar'):
                self.idlebar = plugin.getbyname('idlebar')
                if self.idlebar:
                    self.idlebar_max = osd.width + osd.x
                    logger.log( 9, 'idlebar_max=%s, free_space=%s', self.idlebar_max, self.idlebar.free_space)
                    for p in plugin.get('idlebar'):
                        if hasattr(p, 'clock_left_position'):
                            self.idlebar_max = p.clock_left_position

                    if self.idlebar_max - self.idlebar.free_space < 250:
                        logger.debug('free space in idlebar to small, using normal detach')
                        self.idlebar = None
                    else:
                        # this doesn't work, but needs to for the detachbar
                        self.idlebar.take_space(250)
        else:
            self.idlebar = None

        if self.idlebar:
            self.boxborder = 0
            self.padding = 0

        if self.calculate:
            self.calculate = False
            self.font_h = font.font.height

            logger.log( 9, 'osd.width=%s, osd.height=%s, osd.x=%s, osd.y=%s', osd.width, osd.height, osd.x, osd.y)
            screen_width = osd.width + 2*osd.x
            screen_height = osd.height + 2*osd.y

            used_width = font.font.stringsize(self.jobs) - 1
            # ensure that the box width is between min and max
            used_width = max(used_width, 200)
            used_width = min(used_width, 280)
            used_height = self.font_h
            if self.running:
                w, h = self.getimage(self.boximg, osd).get_size()
                used_width = max(used_width, w)
                used_height += h
                for text in self.text:
                    used_width = max(used_width, font.font.stringsize(text)) - 1
                    used_height += self.font_h

            logger.log( 9, 'screen_width=%s, screen_height=%s, used_width=%s, used_height=%s, font_h=%s', screen_width, screen_height, used_width, used_height, self.font_h)

            self.boxw = used_width + (self.padding + self.boxborder) * 2
            self.boxh = used_height + (self.padding + self.boxborder) * 2
            self.bx = osd.x
            self.by = screen_height - osd.y - self.boxh
            self.textw = used_width
            self.texth = used_height
            self.tx = self.bx + self.boxborder + self.padding
            self.ty = self.by + self.boxborder + self.padding

        if self.idlebar:
            if self.image:
                self.bx = self.idlebar.free_space + 250 + 70
            else:
                self.bx = self.idlebar.free_space + 250
            self.by = osd.y
            self.tx = self.bx + self.boxborder + self.padding
            self.ty = self.by + self.boxborder + self.padding
            self.textw = min(self.textw, self.idlebar_max - self.bx - 30)


    def draw(self, (type, object), osd):
        logger.log( 9, 'draw(self, (type, object), osd)')
        now = time.time()
        duration = now - self.drawtime
        logger.log( 9, "draw=%.2f, interval=%s, state=%s", duration, self.draw_interval, self.state)
        self.drawtime = now
        self.lastdraw = now

        self.calculate = True
        self.settext()
        self.calculatesizes(osd, self.font)

        logger.log( 9, 'self:bx=%s, by=%s, boxh=%s, boxw=%s, border=%s, padding=%s', self.bx, self.by, self.boxh, self.boxw, self.boxborder, self.padding)

        if self.idlebar:
            osd.drawroundbox(self.bx, self.by, self.boxw, self.boxh,
                (0xffffffffL, self.boxborder, 0x40ffff00L, 0))
                #  A R G B                      A R G B
                #  background border_width      border     border_radius
        else:
            osd.drawroundbox(self.bx, self.by, self.boxw, self.boxh,
                (0xf0ffffffL, self.boxborder, 0xb0000000L, self.boxborder))

        logger.log( 9, 'self:tx=%s, ty=%s, texth=%s, textw=%s', self.tx, self.ty, self.texth, self.textw)
        y = self.ty
        osd.write_text(self.jobs, self.font, None, self.tx, y, self.textw, self.font_h, 'center', 'center')
        if self.running:
            y += self.font_h
            encbar = self.getimage(self.barimg, osd, True)
            encbox = self.getimage(self.boximg, osd)
            w, h = encbox.get_size()
            encbox.blit(encbar, (3, 3), (0, 0, (w * self.percent), h))
            x = (self.textw - w) / 2
            #osd.drawroundbox(self.tx+x-2, y-2, w+4, h+4,
            #    (0xf0ffffffL, 2, 0xb000ffffL, 0))
            osd.drawimage(encbox, (self.tx+x, y, -1, -1) )[0]
            y += h
            for text in self.text:
                osd.write_text(text, self.font, None, self.tx, y, self.textw, self.font_h, 'center', 'center')
                y += self.font_h

        return self.textw