2017-09-17 15:45:03 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2018-06-04 01:02:34 +00:00
|
|
|
#include <ctype.h>
|
2018-03-28 16:26:56 +00:00
|
|
|
#include <stdio.h>
|
2018-06-04 01:02:34 +00:00
|
|
|
#include <string.h>
|
2017-09-17 14:18:17 +00:00
|
|
|
#include <X11/Xlib.h>
|
|
|
|
|
2017-09-24 13:33:01 +00:00
|
|
|
#include "../util.h"
|
2017-09-17 14:18:17 +00:00
|
|
|
|
2018-06-04 01:02:34 +00:00
|
|
|
/*
|
|
|
|
* fmt consists of uppercase or lowercase 'c' for caps lock and/or 'n' for num
|
|
|
|
* lock, each optionally followed by '?', in the order of indicators desired.
|
|
|
|
* If followed by '?', the letter with case preserved is included in the output
|
|
|
|
* if the corresponding indicator is on. Otherwise, the letter is always
|
|
|
|
* included, lowercase when off and uppercase when on.
|
|
|
|
*/
|
2017-09-17 14:18:17 +00:00
|
|
|
const char *
|
2018-06-04 01:02:34 +00:00
|
|
|
keyboard_indicators(const char *fmt)
|
2017-09-17 14:18:17 +00:00
|
|
|
{
|
2018-05-02 06:26:23 +00:00
|
|
|
Display *dpy;
|
2017-09-17 14:18:17 +00:00
|
|
|
XKeyboardState state;
|
2018-06-04 01:02:34 +00:00
|
|
|
size_t fmtlen, i, n;
|
|
|
|
int togglecase, isset;
|
|
|
|
char key;
|
2017-09-17 14:18:17 +00:00
|
|
|
|
2018-05-02 06:26:23 +00:00
|
|
|
if (!(dpy = XOpenDisplay(NULL))) {
|
2018-05-18 08:59:05 +00:00
|
|
|
warn("XOpenDisplay: Failed to open display");
|
2017-09-17 14:18:17 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
XGetKeyboardControl(dpy, &state);
|
|
|
|
XCloseDisplay(dpy);
|
|
|
|
|
2018-06-04 01:02:34 +00:00
|
|
|
fmtlen = strnlen(fmt, 4);
|
|
|
|
for (i = n = 0; i < fmtlen; i++) {
|
|
|
|
key = tolower(fmt[i]);
|
|
|
|
if (key != 'c' && key != 'n') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
togglecase = (i + 1 >= fmtlen || fmt[i + 1] != '?');
|
|
|
|
isset = (state.led_mask & (1 << (key == 'n')));
|
|
|
|
if (togglecase) {
|
|
|
|
buf[n++] = isset ? toupper(key) : key;
|
|
|
|
} else if (isset) {
|
|
|
|
buf[n++] = fmt[i];
|
|
|
|
}
|
2017-09-17 14:18:17 +00:00
|
|
|
}
|
2018-06-04 01:02:34 +00:00
|
|
|
buf[n] = 0;
|
|
|
|
return buf;
|
2017-09-17 14:18:17 +00:00
|
|
|
}
|