Use a modifier key to modify file association

2

1

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

  • If no external screen is plugged, don't disable.
  • If a modifier key is used to open the movie, don't disable.
  • If an external screen is plugged and no modifier 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 modifier key - the only restriction being that a single action must be used to open the movie.

pouzzler

Posted 2012-09-25T09:55:41.620

Reputation: 285

Answers

2

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

Rudolf Mühlbauer

Posted 2012-09-25T09:55:41.620

Reputation: 271

I'll look at it later today, but I wanted to thank you in the meantime. – pouzzler – 2012-09-26T08:21:46.577