Menu

Main Menu
Talk Get Daily Search

Member's Online

    User Name
    Password

    Control the media player with the cam focus button (multiple "gestures")

    Reply
    thp | # 1 | 2010-05-31, 16:00 | Report

    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()

    Edit | Forward | Quote | Quick Reply | Thanks
    The Following 11 Users Say Thank You to thp For This Useful Post:
    Bec, blubbi, cheve, Dousan, kamil365, noobmonkey, qwerty12, thecubed, twaelti, vi_

     
    eiraku | # 2 | 2010-06-01, 06:05 | Report

    Can it be tuned so the script only loads when media player is running? And would it mess up if I have shortcutD installed.

    Nifty script by the way. Wonder if the same can be done with the headphone remote button - multiple key presses.

    Edit | Forward | Quote | Quick Reply | Thanks

     
    thecubed | # 3 | 2010-06-01, 06:18 | Report

    This script is exactly what I've been looking for! Thank you so much!

    Is there any way to allow this script to work while the screen is off?

    Edit | Forward | Quote | Quick Reply | Thanks

     
    blubbi | # 4 | 2010-06-08, 10:55 | Report

    Originally Posted by thecubed View Post
    This script is exactly what I've been looking for! Thank you so much!

    Is there any way to allow this script to work while the screen is off?
    As much as I can tell the sensor is turned off when the screen is turned of. You would have to prevent that.

    Cheers
    Bjoern

    Edit | Forward | Quote | Quick Reply | Thanks

     
    Bec | # 5 | 2010-07-20, 13:49 | Report

    how about long press the volume button for next/prev track?

    Edit | Forward | Quote | Quick Reply | Thanks

     
    ahmadamaj | # 6 | 2010-07-24, 22:36 | Report

    Originally Posted by Surstroemming View Post
    Is it possible? Also, how would I have to change the script to get the following functionality:

    1 click = play/pause
    2 clicks = next track
    3 clicks = previous track
    (without long clicks, accidental clicks aren't an issue for me)

    Thanks!
    i think you should edit the order in this line:
    METHODS = [None, 'next', 'previous', 'pause']

    just guessing, i know nothing about this.

    Edit | Forward | Quote | Quick Reply | Thanks

     
    gionni88 | # 7 | 2011-04-29, 20:28 | Report

    Originally Posted by thecubed View Post
    Is there any way to allow this script to work while the screen is off?
    /sys/devices/platform/gpio-switch/cam_focus/disable is set to 1 when screen is locked, and so button is disabled and dbus signal not emitted. Putting 0 while screen is locked works to keep the button active.

    Edit | Forward | Quote | Quick Reply | Thanks

    Last edited by gionni88; 2011-04-29 at 21:06.

     
vBulletin® Version 3.8.8
Normal Logout