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