New Punto Switcher for linux: xswitcher

The termination of xneur support caused me some suffering over the past six months (with the advent of OpenSUSE 15.1 on my desktops: when xneur is turned on, windows lose focus and flicker amusingly to the keyboard input beat) .

“Ah, damn it, I started typing again in the wrong layout” - it is obscene in my work often. And does not add positive.


At the same time, I (as a design engineer) can articulate quite clearly what I want. And I wanted (first from Punto Switcher, and then, thanks to Windows Vista, finally replanting on Linux, from xneur) exactly one. Realizing that the rubbish on the screen is not in that layout (this usually happens at the end of a new word set), stomp on “Pause / Break”. And get what I printed.

At the moment, the product has an optimal (from the point of view of me) functionality / complexity ratio. It's time to share.

TL.DR


All sorts of technical details will go on, so first - a “touch” link for the impatient.

The following behavior is currently hardcoded:

  • “Pause / Break”: backspace the last word, switches the layout in the active window (between 0 and 1) and types again.
  • Left Ctrl Without Nothing: Toggles the layout in the active window (between 0 and 1).
  • “Left Shift without anything”: turns on layout 0 in the active window.
  • “Right Shift without Nothing”: turns on layout No. 1 in the active window.

From now on, I plan to customize the behavior. Without feedback, it’s not interesting (I’m happy with that anyway). I believe that on Habré there will be a sufficient percentage of the audience with similar problems.

NB Since in the current version, the keylogger is screwed to "/ dev / input /", xswitcher should run with root rights:

chown root:root xswitcher
chmod +xs xswitcher

Please note: the owner of the file with suid must be root, as who the owner is in is suid and will turn on startup.

Paranoids (I'm no exception) can clone from GIT and assemble on the spot. Like that:

go get "github.com/micmonay/keybd_event"
go get "github.com/gvalkov/golang-evdev"

### X11 headers for OpenSUSE/deb-based
zypper install libX11-devel libXmu-devel
apt-get install libx11-dev libxmu-dev

cd "x switcher/src/"
go build -o xswitcher -ldflags "-s -w" --tags static_all src/*.go

Autostart add to taste (depending on DE).

It works, "don’t ask for porridge" (≈30 seconds CPU per day, ≈12 MB in RSS).

Details


Now for the details.

The entire repository was originally dedicated to my pet project, and starting another one is too lazy. So, everything is piled up (just in folders) and covered with AGPL ("patent vice versa").

The xswitcher code is written in golang, with a minimal splash of C. It is assumed that this approach will give the least effort (so far it is). While maintaining the ability to connect the missing via cgo.

The text of the commentary is laid out, from where it was borrowed and why. Because the xneur code “did not inspire me”, loloswitcher took as a starting point .

Using "/ dev / input /" has its advantages (everything can be seen, including the pressed key with auto-repeat), and the minuses. Cons are:

  • Auto-repeat (events with code “2”) does not correlate with repeating with X.
  • You can’t see the input through the X11 interfaces (for example, VNC works).
  • Need a root.

Alternatively, you can subscribe to X events through "XSelectExtensionEvent ()". You can peek in the xinput code . For go, I didn’t find anything like it, and the draft implementation gave a hundred lines of C-code in a go. So far set aside.

The conclusion "back" so far has been made by screwing the virtual keyboard. Thanks to the author keybd_event, but there is too high-level abstraction and will have to be redone further. For example, I have the right Win-key, the 3rd row selects. And only the left Win is broadcast back.

Known bugs


  • We don’t know anything about the “composite” input (example: ½). It is not needed right now.
  • We incorrectly reproduce the right Win. In my case, it breaks the arrangement of accents.
  • There is no clear parsing of input. Instead, there are several functions: Compare (), CtrlSequence (), RepeatSequence (), SpaceSequence (). thanknsmcanfor attentiveness: fixed in the code here. With a certain probability, you can catch bugs when replacing.
    In this place I do not know “how to” and I will be glad to any suggestions.
  • (Oh, horror) competitive use of channels (keyboardEvents, miceEvents).

Conclusion


The code is the simplest procedural. And stupid like me. So, I am comforting myself with the hope that almost any techie will be able to finish what he wants. And this product due to this will not disappear without support like most just-for-fun.

Good luck

All Articles