04 October 2009

Toggle Skype main window with keyboard

Here is another script I often use. It toggles visibility of Skype's window. The script is written in Python (I have just found most clear example of using D-Bus in Python) and relies on Skype supporting D-Bus remote API (OSS build of Skype does not support it). Other dependencies are python-dbus and wmctrl. Wmctrl is used to really give focus to Skype main window and to switch to the desktop where it is (BTW the script should work even without wmctrl installed, but may not bring up the main window to the top sometimes) and python-dbus is needed to send messages to Skype to show and hide it's main window.
Using is again simple: save the source to some file (lets say skype-toggle.py), make it executable (chmod 755 skype-toggle.py) and add a hotkey to your window manager to run this script. That's all! :)
#!/usr/bin/env python
import dbus
import os

remote_bus = dbus.SessionBus()

out_connection = remote_bus.get_object('com.Skype.API', '/com/Skype')

out_connection.Invoke('NAME SkypeToggler')
out_connection.Invoke('PROTOCOL 5')
wnd_state = out_connection.Invoke('GET WINDOWSTATE')

if 'WINDOWSTATE NORMAL' == wnd_state:
    print 'Hide'
elif 'WINDOWSTATE HIDDEN' == wnd_state:
    print 'Show'
    wId = os.popen('wmctrl -lp | grep Skype | sort -n | head -n 1 | awk "{print $1}"').read()
    print wId
    os.system('wmctrl -ia ' + wId)

03 October 2009

xkbswitch - simple keyboard layout switcher for X11

I've been thinking of what to post here for a long time and couldn't find anything interesting, useful and at the same time that is not copy-pasted from somewhere else in Internet. Today I was just searching for a source of one my tiny utility and realized that firstly it is useful (as I'm searching for it after a year passed when I wrote it) and secondly it cannot be a duplicate of anything from the Internet because I have written it by myself :)

So here is the prehistory: I am trying to build a handy and lightweight desktop environment based on Linux (or FreeBSD) at home long since. So I am not the one using Gnome, KDE and even XFCE. Now I am pretty satisfied with Arch Linux and Openbox with a few utilities (I can write separate post about my environment if someone would be interested. Just for info my desktop uses 68 Mb of RAM when it starts). Another aspect of my non-standard requirements is that I'm used to switch keyboard layouts using separate hotkeys for each layout. (For example I am using Ctrl+Shift+1 for English, Ctrl+Shift+2 for Russian and Ctrl+Shift+3 for Ukrainian.) Xxkb cannot handle such things, so the only way is some kind of thirdparty tool. I know two programs that can behave the way I need (well... the first one I am actively using and the second one should also handle different hotkeys, but I haven't checked it personally), they are xneur and kkbswitch. I like xneur very much but it stucks in Arch and further keyboard input is impossible until xneur is restarted. I am using it at work on Ubuntu 9.10 and everything is ok, so I don't know whether it is a bug of xneur or Arch's one. Kkbswitch is designed for KDE and depends on it's libraries, that is why I haven't even tried it.

Been stuck with all this for some time I opened sources off xxkb and taken the "engine" of it. (I just needed xxkb because I have never dealt with Xlib and wanted to get something working ASAP.) So the idea was to make a command line tool which would enable the layout given as it's argument. What for? To use it with hotkeys handled by your window manager :). Almost any WM allows you to define custom hotkeys. Given the above it is very easy to achieve the behavior I wanted. Here is the source of layout switching utility:

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

void PrintUsage();

main(int argc, char **argv)
    int xkbGroup;
    if (argc < 2 || (xkbGroup = atoi(argv[1])) < 0 || xkbGroup > 3)

    int xkbEventType, xkbError, xkbReason;
    int mjr = XkbMajorVersion, mnr = XkbMinorVersion;
    Display *display = NULL;

    display = XkbOpenDisplay(NULL, &xkbEventType, &xkbError,
   &mjr, &mnr, &xkbReason);
    if (NULL == display)
        warnx("Cannot open X display %s", XDisplayName(NULL));
        switch (xkbReason)
            case XkbOD_BadServerVersion:
            case XkbOD_BadLibraryVersion:
                warnx("Incomatible versions of client and server xkb libraries");
            case XkbOD_ConnectionRefused:
                warnx("Connection to X server refused");
            case XkbOD_NonXkbServer:
                warnx("XKB extension is not present");
                warnx("Unknown error from XkbOpenDisplay: %d", xkbReason);

    Bool status = XkbLockGroup(display, XkbUseCoreKbd, xkbGroup);


    return status ? 0 : 1;

    printf("Usage: xkbswitch [0-3] sets keyboard layout\n");

Use cc -I/usr/include -L/usr/lib -lX11 -o xkbswitch xkbswitch.c to compile it. Using it is simple and straightforward too: xxkbswitch 0 enables layout with index 0 (in my case Ctrl+Shift+1 executes xkbswitch 0 and sets English layout, xkbswitch 1 and sets Russian).