Notices


Reply
Thread Tools
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:
Posts: 105 | Thanked: 26 times | Joined on May 2010
#2
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.
 
thecubed's Avatar
Posts: 91 | Thanked: 32 times | Joined on Jan 2008 @ Near: Gilroy, CA
#3
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?
__________________
[Nokia N900] [Lenovo T61p]
15GB Movies / 5GB Music. I'm ready to go!
Loving this device, bugs and all.
http://thecubed.com
 
blubbi's Avatar
Posts: 288 | Thanked: 113 times | Joined on Dec 2009 @ Germany
#4
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
 
Bec's Avatar
Posts: 876 | Thanked: 396 times | Joined on Dec 2009
#5
how about long press the volume button for next/prev track?
__________________
 
Posts: 223 | Thanked: 79 times | Joined on Apr 2010 @ Lebanon- Beirut
#6
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.
 
Posts: 435 | Thanked: 769 times | Joined on Apr 2010
#7
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.

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

Thread Tools

 
Forum Jump


All times are GMT. The time now is 18:47.