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