Page 6 of 6 FirstFirst ... 456
Results 51 to 57 of 57

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

  1. #51
    Join Date
    Aug 2006
    Location
    Wiesbaden, Germany
    Beans
    35

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

    Hello out there,

    Can confirm - according to the origninal first post - still works for Ubuntu Natty / 11.04.
    I am on a T61 with a nvidia graphics card (should also run on intel).

    Only hal is removed, so restart of udev is required:

    Code:
     sudo service udev restart
    Furthermore, i changed the udev-script to /etc/udev/rules.d/80-thinkpad-T61.rules to

    Code:
    SUBSYSTEM=="platform", KERNEL=="dock.0", ACTION=="change", RUN+="/etc/thinkpad/dock.sh"
    Due to the fact, that /etc/thinkpad/dock.sh reads the /sys/.../docked file this is enough.

    A list of the actual logged in user(s) you will get with the last command. BUT, if you are logged into another tty, you might be added twice (or more to that list). uniq assures that only one appearance of a user will be in the list. On a multiuser system you "could" run into trouble, but grep -v ":0.0" assures you'll only get the ones of the actual display (no remote ones).

    That way a variable defined you can write in your /etc/thinkpad/dock.sh:

    Code:
    #!/bin/sh
    sleep 1
    THEUSER="$(last | head | grep -v ":0.0" | grep -v pts | grep -v system | grep still | cut -d" " -f1)"
    DOCKED=$(cat /sys/devices/platform/dock.0/docked)
    case "$DOCKED" in
            "0")
            #undocked event
            /home/${THEUSER}/bin/thinkpad_undocked
            ;;
            "1")
            #docked event
            /home/${THEUSER}/bin/thinkpad_docked
            ;;
    esac
    exit 0
    and in your ~/wherever/(docked|undocked):

    Code:
    #!/bin/bash
    # Get the actual logged in user (TESTED). If there is a chance that more 
    # than one user(name) is logged in, you have to sorround the 
    # rest of the code with 
    # for user in "${THEUSER}"; do [... the rest of code ...]; done
    # and change all accurrences of ${THEUSER} with ${user}
    # The latter is NOT TESTED
    THEUSER="$(last | head | grep -v ":0.0" | grep -v pts | grep -v system | grep -m1 still | cut -d" " -f1 | uniq)"
    
    # Rest of code
    #This runs so that root can run the following command under the user's environment
    source /home/${THEUSER}/.Xdbus
    DISPLAY=:0.0 su ${THEUSER} -c "aplay /whereever/docked.wav"
    # Next only for nvidia cards
    DISPLAY=:0.0 su ${THEUSER} -c "/usr/bin/disper -i < /home/${THEUSER}/.disper/mySettings.settings"
    DISPLAY=:0.0 su ${THEUSER} -c "notify-send  \"Thinkpad Docked\" -i /wherever/process-stop.png \"Thinkpad succesfully docked. Do not remove the device without pressing the undock button at the dock.\""
    logger "$0 - lenovo pc docked for user ${THEUSER} ..."
    Be aware, that in the latter i added the code for showing notify messages and using disper to add a second monitor (only works with nvidia cards). The settings-file for disper was simply exported on the commandline with disper -p > mySetting.settings and found the dot-disper directory a good place to store it (see disper --help). To revert to single monitor mode simply use disper -s. Logger logs everything to syslog. I found the process-stop.png (Faenza icon theme) and streamtuner.png files very convenient, because they match the led icons on the dock.

    Another tip: I had to reinstall tp-smapi due to a compilation error during a linux kernel install (this runs dkms again):

    Code:
    apt-get install --reinstall tp-smapi-dkms
    update-initramfs -k all -c
    Greets and have fun
    Axel
    Last edited by apos; August 16th, 2011 at 06:20 AM. Reason: Added notify event. Added logger. Added disper. Added tp-smapi-dkms reinstall.

  2. #52
    Join Date
    Oct 2011
    Beans
    3

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

    Thanks to lunatico for the clue:
    Quote Originally Posted by lunatico View Post
    I haven't tried this with 11.04 and I don't think I ever will because I just don't like it and find it very buggy. To debug this run the command below and undock to see what you get:
    Code:
    udevadm monitor --environment
    And see if the event you get when pressing the undock button matches your rule..
    I'm running a Dell Latitude E6400 with Ubuntu 11 and the earlier pieces helped but udev wasn't firing into my log file. Monitoring udevadm monitor --environment by running
    sudo udevadm monitor --environment >> device_event.log
    found I didn't have those events. So I followed the examples and rewrote the rules. The end result is a reconfiguration of the display when I dock or undock. It merges the technique from http://morgancollett.wordpress.com/2...ew-and-xrandr/ to setup the xorg.conf with multiple modes. Then I use the event to change the modes.
    ================================================== ==================
    Configuration of docking to change the Nvidia Twinview settings
    1. First configure the system for dual monitors (usually
    Code:
    sudo nvidia-settings
    ), then use sudo gedit /etc/X11/xorg.conf go to the Screen section it will appear like the example


    Code:
    Section "Screen"  
         Identifier     "Screen0"  
         Device         "Device0"  
         Monitor        "Monitor0"  
         DefaultDepth    24  
         Option         "TwinView" "1"  
         Option         "TwinViewXineramaInfoOrder" "DFP-1"  
         Option         "metamodes" "DFP-0: nvidia-auto-select +1920+0, DFP-1: nvidia-auto-select +0+0
         SubSection     "Display"  
             Depth       24  
         EndSubSection  
     EndSection
    2. Modify the line for the metamodes by adding a deliminator ; and duplicating the settings for the primary (laptop monitor). The secondary monitor will be set to null as below.
    Code:
         Option         "metamodes" "DFP-0: nvidia-auto-select +1920+0, DFP-1: nvidia-auto-select +0+0;DFP-0: nvidia-auto-select +0+0, DFP-1: null"
    3. Next open a terminal and test the alternate monitor settings.
    a) In a command prompt type

    Code:
       xrandr -s     1
    and press enter to change to single monitor.
    The system will change to a single monitor without restarting the X environment.
    b) Type
    Code:
    xrandr -s 0
    to change back to dual monitors

    With this configured we just need to capture the change of the docking status using the techniques in this thread.

    Capture Docking Event
    1) Hardware environments vary so you will need to determine the events to monitor
    a) type sudo
    udevadm monitor –environment >> ~/device_eventlog.log at a command prompt. I prefer the file becasue it's easier to parse.
    b) Change the hardware you wish to monitor (undock, unplug, depower..)
    c) In the terminal press CTRL+c to terminate the udevadm monitor –environment command.
    2) Examine the
    ~/device_eventlog.log it will display all the add, change and removes with details for each device.
    KERNEL[1318277278.266250] add /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4.2/1-4.2:1.0/0003:413C:2101.0017/hidraw/hidraw2 (hidraw)
    UDEV_LOG=3
    ACTION=add
    SUBSYSTEM=block
    DEVNAME= hidraw2
    DEVTYPE=disk
    SEQNUM=2585
    MAJOR=8
    MINOR=16
    3) Hook the docking event by writing a rule in the /lib/udev/rules.d folder as such as
    /lib/udev/rules.d/85-Latitude-E6400.rules with a content of:

    Code:
    KERNEL=="hidraw2",     DEVNAME=="hidraw2",ACTION=="add",     RUN+="/etc/dell/dock.sh 1" 
    KERNEL=="hidraw2",     DEVNAME=="hidraw2",ACTION=="remove",     RUN+="/etc/dell/dock.sh 0"
    4) Finally create the event to handle docking and undocking /etc/dell/dock.sh script
    Code:
    #!/bin/sh 
     # wait for the dock state to change 
     sleep 1 
     # Get the actual logged in user (TESTED). If there is a chance that more  
     # than one user(name) is logged in, you have to sorround the  
     # rest of the code with  
     # for user in "${THEUSER}"; do [... the rest of code ...]; done 
     # and change all accurrences of ${THEUSER} with ${user} 
     # The latter is NOT TESTED 
     THEUSER="$(last | head | grep -v ":0.0" | grep -v pts | grep -v system | grep -m1 still | cut -d" " -f1 | uniq)" 
      
     #This runs so that root can run the following command under the user's environment 
     source /home/${THEUSER}/.Xdbus 
      
     case "$1" in 
         "0") 
         #undocked event 
         DISPLAY=:0.0 su ${THEUSER} -c "xrandr -s 1" 
         # Removed  echo 'undocked event' >> /etc/dell/docking.log 
         logger 'undocked event'
         ;; 
         "1") 
         #docked event 
         DISPLAY=:0.0 su ${THEUSER} -c "xrandr -s 0" 
         # removed echo 'docked event' >> /etc/dell/docking.log 
         logger 'docked event'
         ;; 
     esac 
     exit 0
    Again thanks to lunatico and everyone for this information
    Last edited by Rob___; October 11th, 2011 at 02:39 PM.

  3. #53
    Join Date
    Aug 2006
    Location
    Wiesbaden, Germany
    Beans
    35

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

    @Rob____

    Thanks for your article, just for note there're some common rules for log files on *nix systems:

    1. log files should be placed in /var/log/wherever, not in /etc/. /etc/ should only be for configuration files.
    2. There is a very handy tool for creating logging entries called "logger" (see logger --help or man logger). Messages created with it go into /var/log/syslog by default. This file won't grew up until forever, but will be zipped from time to time. And, no hassle with rights about log files.


    So
    Code:
    logger test
    comes to
    Code:
    tail -f /var/log/syslog
    [...]
    # Oct 10 23:07:12 panama logger: test
    Cool, or? Saves a lot of echo "..." >>'s

  4. #54
    Join Date
    Oct 2011
    Beans
    3

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

    Quote Originally Posted by apos View Post
    @Rob____

    Thanks for your article, just for note there're some common rules for log files on *nix systems:

    1. log files should be placed in /var/log/wherever, not in /etc/. /etc/ should only be for configuration files.
    2. There is a very handy tool for creating logging entries called "logger" (see logger --help or man logger). Messages created with it go into /var/log/syslog by default. This file won't grew up until forever, but will be zipped from time to time. And, no hassle with rights about log files.


    So
    Code:
    logger test
    comes to
    Code:
    tail -f /var/log/syslog
    [...]
    # Oct 10 23:07:12 panama logger: test
    Cool, or? Saves a lot of echo "..." >>'s
    Sorry about that, bad form on my part. I'll give it a fix. The other item I found is the

    System Setting -> Log File Viewer includes UDEV so that can be used as a reference too.

  5. #55
    Join Date
    Oct 2011
    Beans
    3

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

    Change/Addition:

    I added ;DFP-0: null, DFP-1: nvidia-auto-select +0+0 to the options and set the lid close event when docked to run xrandr -s 2 to disable the laptop monitor and organize the desktop.


    Option "metamodes" "DFP-0: nvidia-auto-select +1920+0, DFP-1: nvidia-auto-select +0+0;DFP-0: nvidia-auto-select +0+0, DFP-1: null;DFP-0: null, DFP-1: nvidia-auto-select +0+0"

  6. #56
    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

    Quote Originally Posted by pete78 View Post
    The above described doch/undock events are not generated on my Lenovo W510 under Ubuntu 11.04. The content of /sys/devices/platform/dock.0/docked is also fixed to 0.
    Is this a bug of udev, Ubuntu, the w510 or should that not work when I press the undock button of the docking station? The docking station is a "Lenovo ThinkPad Mini Dock Series 3 - Mini-Dock".
    Hey! Did you ever got around this?

  7. #57
    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

    Quote Originally Posted by lunatico View Post
    Hey! Did you ever got around this?
    I'm using a ThinkPad W520 with a ThinkPad Mini Dock Plus Series 3 - 170W now and the lid open/close still works the same way but the dock/undock events has changed.
    I don't have time to write down specific steps but I have attached the necessary scripts. Just unzip the files and place them on same path as source.

    Open the files and change paths to match <user> with your username (for home directory).

    Make the following files executable:
    /etc/thinkpad/dock.sh
    /etc/thinkpad/undock.sh
    /home/<user>/scripts/dock/docked
    /home/<user>/scripts/dock/undocked

    Read the first post of this tutorial to understand user's environment so that 'source /home/<user>/.Xdbus' works.

    Note: On the .../undocked file the command to change the display to laptop monitor is commented out because I've chosen to run that when I do laptop lid open. Your choice.
    Attached Files Attached Files

Page 6 of 6 FirstFirst ... 456

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
  •