Example #1
0
File: aesop.py Project: psev/aesop
 def __init__(self, rootwin):
     log.info("Initialized")
     self.windows = WindowController(rootwin)
     self.handlers = NamedDict()
     self.player = MPlayer(["-vo", "vdpau", "-ao", "pulse"])
     self.setup()
     self.run()
Example #2
0
File: aesop.py Project: sevra/aesop
 def __init__(self, rootwin):
    log.info('Initialized')
    self.windows = WindowController(rootwin)
    self.handlers = NamedDict()
    self.player = MPlayer(['-vo', 'vdpau', '-ao', 'pulse'])
    self.setup()
    self.run()
Example #3
0
File: aesop.py Project: psev/aesop
class Aesop(Signaler, PlayerState):
    @property
    def active(self):
        return self._active

    @active.setter
    def active(self, handler):
        self._active = handler
        self.windows.active = handler.win

    def __init__(self, rootwin):
        log.info("Initialized")
        self.windows = WindowController(rootwin)
        self.handlers = NamedDict()
        self.player = MPlayer(["-vo", "vdpau", "-ao", "pulse"])
        self.setup()
        self.run()

    def setup(self):
        self.timeout = None
        self.player.defaults["default"] = 0

        curses.curs_set(False)
        curses.mousemask(curses.ALL_MOUSE_EVENTS | curses.REPORT_MOUSE_POSITION)
        curses.mouseinterval(0)

        curses.use_default_colors()
        curses.init_pair(1, curses.COLOR_BLUE, -1)
        curses.init_pair(2, curses.COLOR_CYAN, -1)
        curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_WHITE)
        curses.init_pair(4, curses.COLOR_YELLOW, -1)

        self.windows.add("help", 3, 0, self.windows.max.y - 6, self.windows.max.x - 1)
        self.handlers.help = HelpHandler(self.windows.help, self.player, self.windows)

        self.windows.add("state", 0, 0, 3, self.windows.max.x)
        self.handlers.state = StateHandler(self.windows.state, self.player)

        self.windows.add("progress", self.windows.max.y - 3, 0)
        self.handlers.progress = ProgressHandler(self.windows.progress, self.player)

        exts = ["avi", "wmv", "mkv", "mpg"]
        self.windows.add("browser", 3, 0, self.windows.max.y - 6, self.windows.max.x - 1)
        self.handlers.browser = BrowserHandler(self.windows.browser, self.player, exts)

        self.windows.apply("keypad", 1)  # turn keypad on for all windows

        self.active = self.handlers.browser
        active = cycle([self.handlers.help, self.handlers.browser])

        self.windows.root.keymap.add(
            Bind(None, ["KEY_RESIZE"], self.resize),
            Bind(None, ["KEY_MOUSE"], self.mouse),
            Bind("Close Aesop.", ["q"], self.quit),
            Bind("Toggle pause.", ["p"], self.player.pause),
            Bind("Stop play.", ["x"], self.player.stop),
            Bind("Seek forward 10 seconds", ["KEY_RIGHT", "s"], self.handlers.progress.seek, 10),
            Bind("Seek backward 10 seconds", ["KEY_LEFT", "h"], self.handlers.progress.seek, -10),
            Bind("Cycle between help, browser and playlist windows.", ["\t"], self.set_active, lambda: next(active)),
        )

    def keypress(self, key):
        bind = self.windows.key_lookup(key)
        bind.func(*bind.args, **bind.kargs)

    def run(self):
        rfds = [sys.stdin, self.player.notifier]

        while True:
            try:
                rl, wl, el = select(rfds, [], [], self.timeout)

                if sys.stdin in rl:
                    self.keypress(self.windows.active.getkey())
                    curses.flushinp()

                if self.player.notifier in rl:
                    self.apply_state()  # set current state
                    self.player.notifier.get()  # clear notifier queue

                if self.playing:
                    self.handle("playing")
            except Exception as err:
                try:
                    self.keypress(self.windows.root.getkey())
                except:
                    continue
                finally:
                    log.error(err)
                    type, value, trace = sys.exc_info()
                    log.error("".join(traceback.format_exception(type, value, trace)))
                continue

    def apply_state(self):
        self.handle("change")
        if self.playing:
            self.player.defaults["prefix"] = ""
            self.handle("play")
            self.timeout = 1
        elif self.paused:
            self.player.defaults["prefix"] = "pausing_keep"
            self.handle("pause")
            self.timeout = None
        elif self.stopped:
            self.player.defaults["prefix"] = ""
            self.handle("stop")
            self.timeout = None
        elif self.loading:
            self.player.defaults["prefix"] = ""
            self.handle("load")
            self.timeout = None
        self.windows.refresh()

    def handle(self, action, *args, **kargs):
        for handler in self.handlers:
            if action == "playing":
                prefix = "while_"
            else:
                prefix = "on_"
            getattr(self.handlers[handler], prefix + action)(*args, **kargs)

    def set_active(self, handler):
        if callable(handler):
            handler = handler()
        self.active = handler
        handler.display()

    def mouse(self):
        id, x, y, z, state = event = curses.getmouse()
        if self.active.win.enclose(y, x):
            self.active.on_mouse(event)

    def resize(self, *args):
        curses.endwin()
        self.windows.refresh()
        self.handle("resize")

    def quit(self):
        self.player.kill()
        raise SystemExit()
Example #4
0
File: aesop.py Project: sevra/aesop
class Aesop(Signaler, PlayerState):
   @property
   def active(self):
      return self._active

   @active.setter
   def active(self, handler):
      self._active = handler
      self.windows.active = handler.win

   def __init__(self, rootwin):
      log.info('Initialized')
      self.windows = WindowController(rootwin)
      self.handlers = NamedDict()
      self.player = MPlayer(['-vo', 'vdpau', '-ao', 'pulse'])
      self.setup()
      self.run()

   def setup(self):
      self.timeout = None
      self.player.defaults['default'] = 0

      curses.curs_set(False)
      curses.mousemask(curses.ALL_MOUSE_EVENTS | curses.REPORT_MOUSE_POSITION)
      curses.mouseinterval(0)

      curses.use_default_colors()
      curses.init_pair(1, curses.COLOR_BLUE, -1)
      curses.init_pair(2, curses.COLOR_CYAN, -1)
      curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_WHITE)
      curses.init_pair(4, curses.COLOR_YELLOW, -1)

      self.windows.add('help', 3, 0, self.windows.max.y-6, self.windows.max.x-1)
      self.handlers.help = HelpHandler(self.windows.help, self.player, self.windows)

      self.windows.add('state', 0, 0, 3, self.windows.max.x)
      self.handlers.state = StateHandler(self.windows.state, self.player)

      self.windows.add('progress', self.windows.max.y-3, 0)
      self.handlers.progress = ProgressHandler(self.windows.progress, self.player)

      exts = [ 'avi', 'wmv', 'mkv', 'mpg', ]
      self.windows.add('browser', 3, 0, self.windows.max.y-6, self.windows.max.x-1)
      self.handlers.browser = BrowserHandler(self.windows.browser, self.player, exts)

      self.windows.apply('keypad', 1) # turn keypad on for all windows

      self.active = self.handlers.browser
      active = cycle([self.handlers.help, self.handlers.browser])

      signal.signal(signal.SIGWINCH, lambda *args : self.handle('resize'))
      self.windows.root.keymap.add(
         Bind(None, ['KEY_RESIZE'], lambda : self.handle('resize')),
         Bind(None, ['KEY_MOUSE'], self.mouse),
         Bind('Close Aesop.', ['q'], self.quit),
         Bind('Toggle pause.', ['p'], self.player.pause),
         Bind('Stop play.', ['x'], self.player.stop),
         Bind('Seek forward 10 seconds', ['KEY_RIGHT', 's'], self.handlers.progress.seek, 10),
         Bind('Seek backward 10 seconds', ['KEY_LEFT', 'h'], self.handlers.progress.seek, -10),
         Bind('Cycle between help, browser and playlist windows.', ['\t'],  self.set_active, lambda : next(active)),
      )

   def run(self):
      rfds = [
         sys.stdin,
         self.player.notifier,
      ]

      while True:
         try: rl, wl, el = select(rfds, [], [], self.timeout)
         except error: continue

         if sys.stdin in rl:
            try:
               bind = self.windows.key_lookup(self.windows.active.getkey())
               bind.func(*bind.args, **bind.kargs)
            except Exception as err:
               log.warning(err)
            curses.flushinp()

         if self.player.notifier in rl:
            self.apply_state(self.player.notifier.get())

         if self.playing:
            self.handle('playing')

   def apply_state(self, state):
      self.handle('change')
      if self.playing:
         self.player.defaults['prefix'] = ''
         self.handle('play')
         self.timeout = 1
      elif self.paused:
         self.player.defaults['prefix'] = 'pausing_keep'
         self.handle('pause')
         self.timeout = None
      elif self.stopped:
         self.player.defaults['prefix'] = ''
         self.handle('stop')
         self.timeout = None
      elif self.loading:
         self.player.defaults['prefix'] = ''
         self.handle('load')
         self.timeout = None
      self.windows.refresh()

   def handle(self, action, *args, **kargs):
      for handler in self.handlers:
         if action == 'playing':
            prefix = 'while_'
         else:
            prefix = 'on_'
         getattr(self.handlers[handler], prefix + action)(*args, **kargs)

   def set_active(self, handler):
      if callable(handler): handler = handler()
      self.active = handler
      handler.display()
         
   def mouse(self):
      id, x, y, z, state = event = curses.getmouse()
      if self.active.win.enclose(y, x):
         self.active.on_mouse(event)

   def quit(self):
      self.player.kill()
      raise SystemExit()