Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

When watching a movie on a laptop, it should cleverly disable its screen:

  • If no external screen is plugged, don't disable.
  • If a modifyer key is used to open the movie, don't disable.
  • If an external screen is plugged and no modifyer is used, disable the laptop screen.
  • When the media player exits, restore the laptop screen.

I've written the following script:

#!/bin/bash
if [ "$(cat /sys/class/drm/card0-VGA-1/status)" = connected ]
then
  xrandr --output eDP1 --off
  vlc "$1"
  while [ "$(pidof vlc)" > 0 ]
  do
    sleep 1
  done
  xrandr --output eDP1 --auto --below VGA1
else
  vlc "$1"
fi

eDP1 represents the laptop screen, VGA1 the external screen. The script is used by associating movie files in ~/.local/share/applications/mimeapps.list with the following desktop file:

[Desktop Entry]
Name=VLC
Comment=
Exec=path/to/the/above/script
Icon=vlc
Terminal=false
Type=Application
StartupNotify=true

Can you suggest a way to modify the script or the desktop file, or any other way to check for a modifyer key - the only restriction being that a single action must be used to open the movie.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

There seems to be no off-the-shelf solution. However, it is not difficult to use Xlib and Xkb directly.

Create a file getmodkey.c:

#include <X11/Xlib.h>
#include <stdio.h>
#include <X11/XKBlib.h>

int main() {
    XkbStateRec r;
    Display* d = XOpenDisplay(NULL);
    XkbGetState(d, XkbUseCoreKbd, &r);
    printf("mod: 0x%x\n", r.mods);
    XCloseDisplay(d);
    return !( r.mods & 1 );
}

and compile it with -lX11, for example

make LDLIBS="-lX11" getmodkey

The exit status code of the program would be 0 if the Shift-key was pressed, 1 otherwise. To test for a different modifier, adjust the (r.mods & keymask) condition (the mask for Shift is 1).

Then, this program should be easy to integrate,

if getmodkey; then 
  echo "shift!"
else
  echo "no shift"
fi
share|improve this answer
    
I'll look at it later today, but I wanted to thank you in the meantime. –  pouzzler Sep 26 '12 at 8:21
    
Thank you, just perfect. –  pouzzler Sep 26 '12 at 16:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.