View Single Post
thp's Avatar
Posts: 1,391 | Thanked: 4,272 times | Joined on Sep 2007 @ Vienna, Austria
#1
Here's a small Python script that monitors the cam focus button state and allows you to navigate through your music and play/pause it. Simply start it and then you can two-press to go to the next song, three-press to go to the previous song or four-press to toggle play/pause. The last press must be held until the action is carried out. This avoids accidental presses to be recognized.

Code:
# N900 focus button Media Player control
# thp 2010-05-31
#
# The last press must be held for about 1 second,
# so for a three-press, you would do short-short-long.
#
# two-press: next
# three-press: previous
# four-press: toggle pause
#

import gtk
import gobject
import dbus
import dbus.glib
import hildon

FOCUS_FILE = '/sys/devices/platform/camera_button/bus/devices/cam_focus/state'
TIMEOUT = 1000

MAFW_OBJECT = 'com.nokia.mafw.renderer.Mafw-Gst-Renderer-Plugin.gstrenderer'
MAFW_PATH = '/com/nokia/mafw/renderer/gstrenderer'
MAFW_INTF = 'com.nokia.mafw.renderer'

METHODS = [None, 'next', 'previous', 'pause']

class FocusMonitor(object):
    def __init__(self):
        self.fp = open(FOCUS_FILE, 'r')
        self.fp.read()
        self.source_id = gobject.io_add_watch(self.fp, \
                gobject.IO_PRI, \
                self.on_state_changed)
        self.timeout_id = None
        self.active = False
        self.event_count = 0
        self.session_bus = dbus.SessionBus()
        self.widget = gtk.Label()

    def on_state_changed(self, source, cb_condition):
        self.fp.seek(0, 0)
        self.active = (self.fp.read().strip() == 'active')
        if self.active:
            self.event_count += 1
            if self.timeout_id is not None:
                gobject.source_remove(self.timeout_id)
            self.timeout_id = gobject.timeout_add(TIMEOUT, self.on_timeout)
        return True

    def banner(self, message):
        hildon.hildon_banner_show_information(self.widget, '', message)

    def on_timeout(self):
        if not self.active:
            self.event_count = 0
            self.timeout_id = None
            return False

        if self.event_count > 1 and self.event_count <= len(METHODS):
            o = self.session_bus.get_object(MAFW_OBJECT, MAFW_PATH)
            i = dbus.Interface(o, MAFW_INTF)
            method_name = METHODS[self.event_count-1]
            method = getattr(i, method_name)
            try:
                method()
                self.banner(method_name)
            except Exception, e:
                if method_name == 'pause':
                    try:
                        i.play()
                        self.banner('play')
                    except:
                        pass
        self.event_count = 0
        self.timeout_id = None
        return False

FocusMonitor()
gtk.main()
 

The Following 11 Users Say Thank You to thp For This Useful Post: