2017-09-17 17:45:03 +02:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2018-06-04 03:02:34 +02:00
|
|
|
#include <ctype.h>
|
2018-03-28 18:26:56 +02:00
|
|
|
#include <stdio.h>
|
2018-06-04 03:02:34 +02:00
|
|
|
#include <string.h>
|
2017-09-17 16:18:17 +02:00
|
|
|
#include <X11/Xlib.h>
|
|
|
|
|
2017-09-24 15:33:01 +02:00
|
|
|
#include "../util.h"
|
2022-10-26 22:14:53 +02:00
|
|
|
#include "../slstatus.h"
|
2017-09-17 16:18:17 +02:00
|
|
|
|
2018-06-04 03:02:34 +02: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 16:18:17 +02:00
|
|
|
const char *
|
2018-06-04 03:02:34 +02:00
|
|
|
keyboard_indicators(const char *fmt)
|
2017-09-17 16:18:17 +02:00
|
|
|
{
|
2018-05-02 08:26:23 +02:00
|
|
|
Display *dpy;
|
2017-09-17 16:18:17 +02:00
|
|
|
XKeyboardState state;
|
2018-06-04 03:02:34 +02:00
|
|
|
size_t fmtlen, i, n;
|
|
|
|
int togglecase, isset;
|
|
|
|
char key;
|
2017-09-17 16:18:17 +02:00
|
|
|
|
2018-05-02 08:26:23 +02:00
|
|
|
if (!(dpy = XOpenDisplay(NULL))) {
|
2018-05-18 10:59:05 +02:00
|
|
|
warn("XOpenDisplay: Failed to open display");
|
2017-09-17 16:18:17 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
XGetKeyboardControl(dpy, &state);
|
|
|
|
XCloseDisplay(dpy);
|
|
|
|
|
2018-06-04 03:02:34 +02: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 16:18:17 +02:00
|
|
|
}
|
2018-06-04 03:02:34 +02:00
|
|
|
buf[n] = 0;
|
|
|
|
return buf;
|
2017-09-17 16:18:17 +02:00
|
|
|
}
|