Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keyboard led indicator. #435

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ AM_CPPFLAGS = \
i3status_CFLAGS = \
$(AM_CFLAGS) \
$(CONFUSE_CFLAGS) \
$(X11_CFLAGS) \
$(XKBFILE_CFLAGS) \
$(YAJL_CFLAGS) \
$(PULSE_CFLAGS) \
$(NLGENL_CFLAGS) \
Expand All @@ -42,6 +44,8 @@ i3status_CPPFLAGS = \

i3status_LDADD = \
$(CONFUSE_LIBS) \
$(X11_LIBS) \
$(XKBFILE_CFLAGS) \
$(YAJL_LIBS) \
$(PULSE_LIBS) \
$(NLGENL_LIBS) \
Expand Down Expand Up @@ -73,6 +77,7 @@ i3status_SOURCES = \
src/print_volume.c \
src/print_wireless_info.c \
src/print_file_contents.c \
src/print_keyboard_status.c \
src/process_runs.c

if PULSE
Expand Down
2 changes: 2 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ AC_SEARCH_LIBS([shm_open], [rt])
AC_CANONICAL_HOST

PKG_CHECK_MODULES([CONFUSE], [libconfuse])
PKG_CHECK_MODULES([X11], [x11])
PKG_CHECK_MODULES([XKBFILE], [xkbfile])
PKG_CHECK_MODULES([YAJL], [yajl])

AC_ARG_ENABLE(pulseaudio,
Expand Down
16 changes: 16 additions & 0 deletions i3status.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,15 @@ int main(int argc, char *argv[]) {
CFG_STR("markup", "none", CFGF_NONE),
CFG_END()};

cfg_opt_t keyboard_opts[] = {
CFG_STR("format", "%C,%N", 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 run_watch_opts[] = {
CFG_STR("pidfile", NULL, CFGF_NONE),
CFG_STR("format", "%title: %status", CFGF_NONE),
Expand Down Expand Up @@ -473,6 +482,7 @@ int main(int argc, char *argv[]) {
CFG_SEC("memory", memory_opts, CFGF_NONE),
CFG_SEC("cpu_usage", usage_opts, CFGF_NONE),
CFG_SEC("read_file", read_opts, CFGF_TITLE | CFGF_MULTI),
CFG_SEC("keyboard_status", keyboard_opts, CFGF_NONE),
CFG_END()};

char *configfile = NULL;
Expand Down Expand Up @@ -673,6 +683,12 @@ int main(int argc, char *argv[]) {
SEC_CLOSE_MAP;
}

CASE_SEC("keyboard_status") {
SEC_OPEN_MAP("keyboard_status");
print_keyboard_status(json_gen, buffer, cfg_getstr(sec, "format"));
SEC_CLOSE_MAP;
}

CASE_SEC_TITLE("wireless") {
SEC_OPEN_MAP("wireless");
const char *interface = NULL;
Expand Down
5 changes: 5 additions & 0 deletions i3status.conf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ general {
}

order += "ipv6"
order += "keyboard_status"
order += "wireless _first_"
order += "ethernet _first_"
order += "battery all"
Expand Down Expand Up @@ -51,3 +52,7 @@ memory {
tztime local {
format = "%Y-%m-%d %H:%M:%S"
}

keyboard_status {
format = "CAPS:%C NUM:%N"
}
1 change: 1 addition & 0 deletions include/i3status.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ char *resolve_tilde(const char *path);
void *scalloc(size_t size);
char *sstrdup(const char *str);

void print_keyboard_status(yajl_gen json_gen, char *buffer, const char *format);
/* src/output.c */
void print_separator(const char *separator);
char *color(const char *colorstr);
Expand Down
41 changes: 41 additions & 0 deletions src/print_keyboard_status.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <X11/XKBlib.h>
#include <X11/extensions/XKBrules.h>

#include "i3status.h"

#define STRING_SIZE 20

void print_keyboard_status(yajl_gen json_gen, char *buffer, const char *format) {

char *outwalk = buffer;
char caps_lock = 0;
char num_lock = 0;
char string_caps_lock[STRING_SIZE];
char string_num_lock[STRING_SIZE];

Display *display = XOpenDisplay(getenv("DISPLAY"));
XKeyboardState keyboard_state;
XGetKeyboardControl(display, &keyboard_state);

if (display) {
caps_lock = keyboard_state.led_mask & 0x1;
num_lock = keyboard_state.led_mask & 0x2;
XCloseDisplay(display);
}

snprintf(string_caps_lock, STRING_SIZE, "%s", caps_lock ? "ON" : "OFF");
snprintf(string_num_lock, STRING_SIZE, "%s", num_lock ? "ON" : "OFF");

placeholder_t placeholders[] = {
{.name = "%C", .value = string_caps_lock},
{.name = "%N", .value = string_num_lock}};

const size_t num = sizeof(placeholders) / sizeof(placeholder_t);
buffer = format_placeholders(format, &placeholders[0], num);
OUTPUT_FULL_TEXT(buffer);
free(buffer);
}