I was in the computer lab, learning to code a simple concurrent echo server and I asked my professor if I can use C++,he nodded yes but he also said that C is elegant and much easier for stuffs like port programming. C is usually good for low-level stuff but C++ isn’t bad either. Later simple discussion between C and C++ lead to a challenge which my professor gave me. He recollected that he wrote a C program to control keyboard leds in his college days and challenged (more of a friendly challenge) to accomplish the same. While I managed to write one in C++ but it is more of a wrapper of API calls to Xlib which is a C library . My first thought was to use setleds or tweaking proc files but that would be cheating as calls would be wrapped inside system() parameters. So after searching I came across use of Xlib to accomplish the same, here. I modified my code upon the work of logixoul and found something difficult to comprehend (also found the documentation of XKBlib to be of little help, or maybe I was wrong somewhere). So I tried hacking around and got my result.
SCROLL_LOCK = 1
CAPSLOCK = 2
NUMLOCK = 16
are some predefined constants to be used by XkbLockModifiers. Most surprising was passing the bit manipulated result of these constants (can get how it changes, but it does play some part as changing them broke the result ).
I added some C++ 14 goodness, which was not needed and was more of an over-enginerring.
- auto instead of
- Use of thread for sleeping
- autofied parameter which was frankly not needed
- Trailing return type because why not
Anyways here goes the code, might not the best one but works. Toggles caps lock and num lock within an interval of 1s, infinetly. Compile with following Flag parameters -std=c++14 -lX11 -lpthread