Adding key_indicator that shows the status of 'Caps Lock', 'Num Lock'

and 'Scroll Lock' in i3status output.
This commit is contained in:
Marco 2022-07-18 22:17:37 +02:00
parent d53da19a79
commit dd3db16c10
5 changed files with 85 additions and 1 deletions

2
.gitignore vendored
View File

@ -8,3 +8,5 @@ man/i3status.1
# it is up to you to arrange for it to be ignored by git,
# e.g. by listing your directory in .git/info/exclude.
/build
.vscode

View File

@ -443,6 +443,18 @@ int main(int argc, char *argv[]) {
CFG_CUSTOM_SEP_BLOCK_WIDTH_OPT,
CFG_END()};
cfg_opt_t key_indicator_opts[] = {
CFG_STR("format", "%content", CFGF_NONE),
CFG_STR("format_bad", "%title - %errno: %error", CFGF_NONE),
CFG_STR("path", NULL, CFGF_NONE),
CFG_INT("max_characters", 255, CFGF_NONE),
CFG_CUSTOM_ALIGN_OPT,
CFG_CUSTOM_COLOR_OPTS,
CFG_CUSTOM_MIN_WIDTH_OPT,
CFG_CUSTOM_SEPARATOR_OPT,
CFG_CUSTOM_SEP_BLOCK_WIDTH_OPT,
CFG_END()};
cfg_opt_t read_opts[] = {
CFG_STR("format", "%content", CFGF_NONE),
CFG_STR("format_bad", "%title - %errno: %error", CFGF_NONE),
@ -473,6 +485,7 @@ int main(int argc, char *argv[]) {
CFG_SEC("load", load_opts, CFGF_NONE),
CFG_SEC("memory", memory_opts, CFGF_NONE),
CFG_SEC("cpu_usage", usage_opts, CFGF_NONE),
CFG_SEC("key_indicator", key_indicator_opts, CFGF_NONE),
CFG_SEC("read_file", read_opts, CFGF_TITLE | CFGF_MULTI),
CFG_END()};
@ -932,6 +945,17 @@ int main(int argc, char *argv[]) {
SEC_CLOSE_MAP;
}
CASE_SEC("key_indicator") {
SEC_OPEN_MAP("key_indicator");
key_indicator_ctx_t ctx = {
.json_gen = json_gen,
.buf = buffer,
.buflen = sizeof(buffer),
};
print_key_indicator(&ctx);
SEC_CLOSE_MAP;
}
CASE_SEC_TITLE("read_file") {
SEC_OPEN_MAP("read_file");
file_contents_ctx_t ctx = {

View File

@ -424,6 +424,14 @@ typedef struct {
void print_volume(volume_ctx_t *ctx);
typedef struct {
yajl_gen json_gen;
char *buf;
const size_t buflen;
} key_indicator_ctx_t;
void print_key_indicator(key_indicator_ctx_t *ctx);
bool process_runs(const char *path);
int volume_pulseaudio(uint32_t sink_idx, const char *sink_name);
bool description_pulseaudio(uint32_t sink_idx, const char *sink_name, char buffer[MAX_SINK_DESCRIPTION_LEN]);

View File

@ -144,6 +144,7 @@ add_project_arguments('-D_GNU_SOURCE', language: 'c')
# https://mesonbuild.com/howtox.html#add-math-library-lm-portably
m_dep = cc.find_library('m', required: false)
rt_dep = cc.find_library('rt', required: false)
x_dep = cc.find_library('X11', required: true)
confuse_dep = dependency('libconfuse', method: 'pkg-config')
yajl_dep = dependency('yajl', method: 'pkg-config')
@ -170,8 +171,10 @@ i3status_srcs = [
'src/print_time.c',
'src/print_volume.c',
'src/print_wireless_info.c',
'src/print_key_indicator.c',
'src/print_file_contents.c',
'src/process_runs.c',
'src/print_key_indicator.c',
]
thread_dep = dependency('threads')
@ -180,6 +183,7 @@ i3status_deps = [
thread_dep,
m_dep,
rt_dep,
x_dep,
confuse_dep,
yajl_dep,
config_h,
@ -202,7 +206,7 @@ if host_os == 'netbsd'
i3status_deps += [prop_dep]
endif
inc = include_directories('include')
inc = include_directories('include', '/usr/include/')
executable(
'i3status',

46
src/print_key_indicator.c Normal file
View File

@ -0,0 +1,46 @@
#include <stdlib.h>
#include <yajl/yajl_gen.h>
#include <yajl/yajl_version.h>
#include <X11/extensions/XKB.h>
#include <X11/XKBlib.h>
#include "i3status.h"
#define NUM_INDS 3
#define STRING_SIZE 6
void print_key_indicator(key_indicator_ctx_t *ctx) {
char *outwalk = ctx->buf;
char *ind_names[NUM_INDS] = {"Num Lock", "Caps Lock", "Scroll Lock"};
char indicator_string[STRING_SIZE];
char indicator_chars[3];
int i;
int ndx = 0;
Atom iatoms[NUM_INDS] = {0};
Bool state[NUM_INDS] = {0};
Display *dpy = XOpenDisplay(NULL);
for (i = 0; i < NUM_INDS; i++) {
iatoms[i] = XInternAtom(dpy, ind_names[i], 0);
}
for (i = 0; i < NUM_INDS; i++) {
XkbGetNamedIndicator(dpy, iatoms[i], &ndx, &state[i], NULL, NULL);
}
indicator_chars[0] = state[0] ? 'N' : 'n';
indicator_chars[1] = state[1] ? 'C' : 'c';
indicator_chars[2] = state[2] ? 'S' : 's';
snprintf(indicator_string, STRING_SIZE, "%c %c %c", indicator_chars[0], indicator_chars[1], indicator_chars[2]);
OUTPUT_FULL_TEXT(indicator_string);
XCloseDisplay(dpy);
}