Page 4 of 6 FirstFirst ... 23456 LastLast
Results 31 to 40 of 57

Thread: [HOWTO] Run scripts for laptop lid open/close and dock/undock events

  1. #31
    Join Date
    Sep 2008
    Location
    United Kingdom, Kent
    Beans
    129
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: [HOWTO] Run scripts for laptop lid open/close and dock/undock events

    the test file just remains blank
    If alter the permissions for the lid.sh file to not allow it to run, the screen is still blanked which leads me to believe that closing the screen doesn't run the lid.sh file, like you said. However, it was there, I didn't just create it myself.

    I've managed to make my laptop play a sound when I click the power button by editing the powerbtn.sh file in the same folder as lid.sh
    Customize bash commands/Unknown Process/I love LoL/'Keep Aligned' Stuck
    Red/cyan* means top priority
    *only if you have compiz negative turned on

  2. #32
    Join Date
    Oct 2005
    Location
    Adelaide, South Australia
    Beans
    746
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: [HOWTO] Run scripts for laptop lid open/close and dock/undock events

    Hi,

    I notice that in your initial edits of lid.sh, you hardcode the path to the users home folder....

    This means that for every new user on the laptop, you'd need to enter yet another line..which would end up calling many scripts and doing many things...

    not good.

    any way to dynamically call the user that is logged in ?

    update : 04:46 PM

    After some discussion on #wowuidev about the problem of multiple users, the scripts they would have and hardcoding paths to such scripts in each user on a system, it was concluded that using dbus would be a far better approach.

    Get the signal
    So, first... test that opening and closing the laptop lid fires off a dbus signal. Using dbus-monitor to get results while i closed and opened the laptop lid resutled in :
    Code:
    $ dbus-monitor
    ...
    signal sender=:1.19 -> dest=(null destination) serial=233 path=/org/gnome/PowerManager/Backlight; interface=org.gnome.PowerManager.Backlight; member=BrightnessChanged
       uint32 50
    method call sender=:1.19 -> dest=:1.20 serial=234 path=/; interface=org.gnome.ScreenSaver; member=UnThrottle
       uint32 707299091
    method return sender=:1.20 -> dest=:1.19 reply_serial=234
    signal sender=:1.19 -> dest=(null destination) serial=235 path=/org/gnome/PowerManager/Backlight; interface=org.gnome.PowerManager.Backlight; member=BrightnessChanged
       uint32 50
    ...
    confirmed.

    Act on it
    This part requires a script to run when the user logins in.

    It's purpose will be to watch for dbus events.

    when org.gnome.PowerManager.Backlight fires off a signal that interests us, we act on it.

    So far all i have is this (i have no experience with python what so ever, so this will take me a while to conjour up)

    This file i created : ~/bin/laptop-lid/event-listener
    Code:
    #!/usr/bin/env python
    import dbus, gobject
    from dbus.mainloop.glib import DBusGMainLoop
    
    def cb_func(message,sender=None):
        print "got signal ({0})  from {1} " .format(message,sender)
        # determine if lid is closed or open:
        # if open {
        # run bash script in ~/bin/laptop-lid/open 
        # }elseif closed {
        # run bash script in ~/bin/laptop-lid/closed
        # }
    
    dbusSystemObject = "org.gnome.PowerManager.Backlight"
    dbusSystemObjectPath = "/org/gnome/PowerManager/Backlight"
    
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
    bus = dbus.SessionBus()
    bus.add_signal_receiver(cb_func,
                            dbus_interface=dbusSystemObject,
                            path=dbusSystemObjectPath,
                            sender_keyword='sender')
    
    loop = gobject.MainLoop()
    loop.run()
    So far i have it printing the following when i close and open the laptop lid :
    Code:
    got signal (50)  from :1.19 
    got signal (50)  from :1.19
    Last edited by airtonix; February 27th, 2010 at 07:45 AM.
    Fear is the mindkiller....
    The little death that obliterates...

  3. #33
    Join Date
    Oct 2005
    Location
    Adelaide, South Australia
    Beans
    746
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: [HOWTO] Run scripts for laptop lid open/close and dock/undock events

    More improvements (working version)

    1. Create the following three files at these locations (next version i'll include ability to specify your open/close script locations as parameters

    ~/bin/laptop-lid/open

    Code:
     #!/bin/bash
    notify-send "lid opened"
    #play a open sound
    aplay /usr/lib/openoffice/basis3.1/share/gallery/sounds/sparcle.wav
    #change Pidgin status
    purple-remote 'setstatus?status=available&message=I am here...'
    ~/bin/laptop-lid/close
    Code:
    #!/bin/bash
    aplay /usr/lib/openoffice/basis3.1/share/gallery/sounds/falling.wav
    #change Pidgin status
    purple-remote 'setstatus?status=away&message=My laptop lid is closed...'


    ~/bin/laptop-lid/event

    Code:
    #!/usr/bin/env python
    import os
    import dbus, gobject
    from dbus.mainloop.glib import DBusGMainLoop
    
    class LaptopLid:
    
        def __init__(self):
            self.dbusSystemObject = "org.gnome.PowerManager.Backlight"
            self.dbusSystemObjectPath = "/org/gnome/PowerManager/Backlight"
            self.acpiLIDstatePath = "/proc/acpi/button/lid/LID0/state"                            #this may vary dending on your system
            self.scriptPath="~/bin/laptop-lid"
            self.state="closed"
    
            dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
            self.bus = dbus.SessionBus()
            self.bus.add_signal_receiver(self.cb_func, dbus_interface=self.dbusSystemObject, path=self.dbusSystemObjectPath)
    
            self.loop = gobject.MainLoop()
            self.loop.run()
    
        def cb_func(self,message,sender=None):
            #    if(laptopLidState==)
            f = open(self.acpiLIDstatePath, 'r').read()
            if f.count("open") > 0 : #and self.state=="closed" :
                print "opened"
                os.system('sh '+self.scriptPath+'/open')
                self.state = "open"
            elif f.count("closed") > 0 : # and self.state=="open":
                print "closed"
                os.system('sh '+self.scriptPath+'/close')
                self.state = "closed"
    
    l = LaptopLid()
    Make it run after login..
    1 open : system > preferences > startup applications
    2. click add
    3. name : DBUS Laptop Lid Event Detector
    4. command : python ~/bin/laptop-lid/event

    Testing...
    Test it by opening terminal and typing :
    Code:
    python ~/bin/laptop-lid/event
    Last edited by airtonix; February 27th, 2010 at 09:08 AM.
    Fear is the mindkiller....
    The little death that obliterates...

  4. #34
    Join Date
    Dec 2007
    Location
    Galway, Ireland
    Beans
    316
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: [HOWTO] Run scripts for laptop lid open/close and dock/undock events

    Excellent contribution airtonix!

    When I wrote this I never thought about a multi-user scenario, this was done on my personal work laptop, no one else will ever use it other than me.

    I'll edit the main post to guide users wanting multi-user support for the lid event to come to your post.

  5. #35
    Join Date
    Sep 2008
    Location
    United Kingdom, Kent
    Beans
    129
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: [HOWTO] Run scripts for laptop lid open/close and dock/undock events

    when running dbus-monitor I do get some output but nothing when I open and close the lid.
    This is the output I get
    Code:
    signal sender=org.freedesktop.DBus -> dest=:1.97 serial=2 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameAcquired
       string ":1.97"
    method call sender=:1.97 -> dest=org.freedesktop.DBus serial=3 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=AddMatch
       string "type='method_call'"
    method call sender=:1.97 -> dest=org.freedesktop.DBus serial=4 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=AddMatch
       string "type='method_return'"
    method call sender=:1.97 -> dest=org.freedesktop.DBus serial=5 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=AddMatch
       string "type='error'"
    Customize bash commands/Unknown Process/I love LoL/'Keep Aligned' Stuck
    Red/cyan* means top priority
    *only if you have compiz negative turned on

  6. #36
    Join Date
    Oct 2005
    Location
    Adelaide, South Australia
    Beans
    746
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: [HOWTO] Run scripts for laptop lid open/close and dock/undock events

    Quote Originally Posted by SlickRick View Post
    when running dbus-monitor I do get some output but nothing when I open and close the lid.
    But i assume you do have a lid entry in :
    Code:
    /proc/acpi/button/lid
    This is what mine looks like :
    Code:
    $ tree /proc/acpi/button
    /proc/acpi/button
    |-- lid
    |   `-- LID0
    |       |-- info
    |       `-- state
    |-- power
    |   |-- PWRB
    |   |   `-- info
    |   `-- PWRF
    |       `-- info
    `-- sleep
        `-- SLPB
    Not sure that it matters, but i have my lid event in gnome-power-manager set to blank the screen.

    I assume you are running gnome ?
    Attached Images Attached Images
    Last edited by airtonix; February 28th, 2010 at 01:50 AM.
    Fear is the mindkiller....
    The little death that obliterates...

  7. #37
    Join Date
    Oct 2005
    Location
    Adelaide, South Australia
    Beans
    746
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: [HOWTO] Run scripts for laptop lid open/close and dock/undock events

    I just discovered something else that might cause problems :

    In the above python dbus event listener, I am sniffing for an event signal sent by "org.gnome.PowerManager.Backlight", but this only fires when i close/open the lid if my power cable is plugged in.

    Only org.gnome.Screensaver fires regardless of whether or not the power cable is plugged in.

    problem is that I need a service :
    Code:
            self.dbusSystemObject = "org.gnome.PowerManager.Backlight"
    And a path :
    Code:
            self.dbusSystemObjectPath = "/org/gnome/PowerManager/Backlight"
    however if you look at the output from dbus-monitor when i close/open the laptop lid :
    Code:
    method call sender=:1.19 -> dest=:1.20 serial=571 path=/; interface=org.gnome.ScreenSaver; member=Throttle
       string "Power screensaver"
       string "Laptop lid is closed"
    method return sender=:1.20 -> dest=:1.19 reply_serial=571
       uint32 526562257
    signal sender=:1.19 -> dest=(null destination) serial=572 path=/org/gnome/PowerManager/Backlight; interface=org.gnome.PowerManager.Backlight; member=BrightnessChanged
       uint32 50
    method call sender=:1.19 -> dest=:1.20 serial=573 path=/; interface=org.gnome.ScreenSaver; member=UnThrottle
       uint32 526562257
    method return sender=:1.20 -> dest=:1.19 reply_serial=573
    signal sender=:1.19 -> dest=(null destination) serial=574 path=/org/gnome/PowerManager/Backlight; interface=org.gnome.PowerManager.Backlight; member=BrightnessChanged
       uint32 50
    org.gnome.ScreenSaver, does not have a valid path that i cant use in the above python dbus event listener.
    Last edited by airtonix; February 28th, 2010 at 02:36 AM.
    Fear is the mindkiller....
    The little death that obliterates...

  8. #38
    Join Date
    Sep 2008
    Location
    United Kingdom, Kent
    Beans
    129
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: [HOWTO] Run scripts for laptop lid open/close and dock/undock events

    Quote Originally Posted by airtonix View Post
    But i assume you do have a lid entry in :
    Code:
    /proc/acpi/button/lid
    This is what mine looks like :
    Code:
    $ tree /proc/acpi/button
    /proc/acpi/button
    |-- lid
    |   `-- LID0
    |       |-- info
    |       `-- state
    |-- power
    |   |-- PWRB
    |   |   `-- info
    |   `-- PWRF
    |       `-- info
    `-- sleep
        `-- SLPB
    Not sure that it matters, but i have my lid event in gnome-power-manager set to blank the screen.

    I assume you are running gnome ?
    Mine looks like this:
    Code:
    $ tree /proc/acpi/button
    /proc/acpi/button
    |-- lid
    |   `-- LID
    |       |-- info
    |       `-- state
    `-- power
        |-- PWRB
        |   `-- info
        `-- PWRF
            `-- info
    And yes, I am using gnome and I have the power manager set to blank screen

    I tried running this a thousand times and closing the lid quickly and it seems the lid state changes to closed when I close it
    Code:
    cat /proc/acpi/button/lid/LID/state
    So the event is detected but I have no idea where to go from here
    Last edited by SlickRick; February 28th, 2010 at 10:00 AM.
    Customize bash commands/Unknown Process/I love LoL/'Keep Aligned' Stuck
    Red/cyan* means top priority
    *only if you have compiz negative turned on

  9. #39
    Join Date
    Oct 2005
    Location
    Adelaide, South Australia
    Beans
    746
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: [HOWTO] Run scripts for laptop lid open/close and dock/undock events

    SlickRick, after trying to work out a better way to sniff the actual laptop lid dbus event, i found that by default dbus-monitor only listens to the session bus.

    you should try running :
    Code:
    dbus-monitor --system
    Also, i've since changed aspects of the python event listener to look at the actual dbus lid objects signals ... you can check your dbus hal exposes such an object with :

    Code:
    hal-find-by-property --key info.product --string "Lid Switch"
    My output from this looks like :
    Code:
    /org/freedesktop/Hal/devices/computer_logicaldev_input_4
    If you don't get any output, then try browsing through the output of :
    Code:
    lshal > ~/lshal.txt && gedit ~/lshal.txt
    See if you can find anything to do with a laptop lid switch/button.

    still cleaning up the code i'm making then i'll post it here.
    Fear is the mindkiller....
    The little death that obliterates...

  10. #40
    Join Date
    Oct 2005
    Location
    Adelaide, South Australia
    Beans
    746
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: [HOWTO] Run scripts for laptop lid open/close and dock/undock events

    Download


    Install/Setup
    1. download it somwhere to your homefolder (ie : ~/bin )
    2. make it exectuable :
      Code:
      chmod +x ~/bin/dbus-laptop-lid-listener.py
    3. add it to your session startup applications :
      1. system > preferences > startup applications
      2. click "add"
      3. Name : DBUS Laptop Lid Event Listener
      4. command :
        Code:
        ~/bin/dbus-laptop-lid-listener.py listen
      5. click close.
    4. Create a script to run when laptop lid OPENS :
      1. Code:
        gedit ~/bin/laptop-lid-opened.sh
      2. paste in the following :
        Code:
        #!/bin/sh
        notify-osd "lid open"
      3. make it executable :
        Code:
        chmod +x ~/bin/laptop-lid-opened.sh
    5. Create a script to run when laptop lid CLOSES :
      1. Code:
        gedit ~/bin/laptop-lid-closed.sh
      2. paste in the following :
        Code:
         
        #!/bin/sh
        # do stuff.
        # like change awway status on empathy or pidgin,
        # or pause rhythmbox, etc, etc.
      3. make it executable :
        Code:
         chmod +x ~/bin/laptop-lid-closed.sh


    Script won't take effect till you either :
    • run it manually, or
    • restart your current session by logging out and back in again.

    Syntax

    Code:
    dbus-laptop-lid-listener.py mode openScript closeScript
    mode
    • listen
      what you'd be interested in... sits in memory listening for dbus signals and runs your openScript when the acpi laptop lid state changes to "open" and your close script when it changes to "closed"
    • test
      prints out the hal address of your laptop lid switch.
    openScript, closeScript
    Any script, terminal commands, etc that can be executed under your users account.
    Attached Files Attached Files
    Last edited by airtonix; March 1st, 2010 at 07:24 AM.
    Fear is the mindkiller....
    The little death that obliterates...

Page 4 of 6 FirstFirst ... 23456 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •