From 9619251464a8cde9b831c3edb0ef69051c8f5f4a Mon Sep 17 00:00:00 2001 From: Cem Aksoylar Date: Sun, 27 Nov 2022 15:51:12 -0800 Subject: [PATCH 1/4] feat(display): Add modifiers widget --- app/include/zmk/display/widgets/mods_status.h | 18 +++++ app/src/display/status_screen.c | 14 ++++ app/src/display/widgets/CMakeLists.txt | 1 + app/src/display/widgets/Kconfig | 5 ++ app/src/display/widgets/mods_status.c | 65 +++++++++++++++++++ 5 files changed, 103 insertions(+) create mode 100644 app/include/zmk/display/widgets/mods_status.h create mode 100644 app/src/display/widgets/mods_status.c diff --git a/app/include/zmk/display/widgets/mods_status.h b/app/include/zmk/display/widgets/mods_status.h new file mode 100644 index 00000000..411b379a --- /dev/null +++ b/app/include/zmk/display/widgets/mods_status.h @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2022 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +#include +#include + +struct zmk_widget_mods_status { + sys_snode_t node; + lv_obj_t *obj; +}; + +int zmk_widget_mods_status_init(struct zmk_widget_mods_status *widget, lv_obj_t *parent); +lv_obj_t *zmk_widget_mods_status_obj(struct zmk_widget_mods_status *widget); diff --git a/app/src/display/status_screen.c b/app/src/display/status_screen.c index 58de09ae..e107bafd 100644 --- a/app/src/display/status_screen.c +++ b/app/src/display/status_screen.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -34,6 +35,10 @@ static struct zmk_widget_layer_status layer_status_widget; static struct zmk_widget_wpm_status wpm_status_widget; #endif +#if IS_ENABLED(CONFIG_ZMK_WIDGET_MODS_STATUS) +static struct zmk_widget_mods_status mods_status_widget; +#endif + lv_obj_t *zmk_display_status_screen() { lv_obj_t *screen; screen = lv_obj_create(NULL); @@ -65,5 +70,14 @@ lv_obj_t *zmk_display_status_screen() { zmk_widget_wpm_status_init(&wpm_status_widget, screen); lv_obj_align(zmk_widget_wpm_status_obj(&wpm_status_widget), LV_ALIGN_BOTTOM_RIGHT, 0, 0); #endif + +#if IS_ENABLED(CONFIG_ZMK_WIDGET_MODS_STATUS) + zmk_widget_mods_status_init(&mods_status_widget, screen); + lv_obj_set_style_local_text_font(zmk_widget_mods_status_obj(&mods_status_widget), + LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, + lv_theme_get_font_small()); + lv_obj_align(zmk_widget_mods_status_obj(&mods_status_widget), NULL, LV_ALIGN_IN_BOTTOM_RIGHT, 0, + 0); +#endif return screen; } diff --git a/app/src/display/widgets/CMakeLists.txt b/app/src/display/widgets/CMakeLists.txt index fbf07072..e0adc4c2 100644 --- a/app/src/display/widgets/CMakeLists.txt +++ b/app/src/display/widgets/CMakeLists.txt @@ -6,3 +6,4 @@ target_sources_ifdef(CONFIG_ZMK_WIDGET_OUTPUT_STATUS app PRIVATE output_status.c target_sources_ifdef(CONFIG_ZMK_WIDGET_PERIPHERAL_STATUS app PRIVATE peripheral_status.c) target_sources_ifdef(CONFIG_ZMK_WIDGET_LAYER_STATUS app PRIVATE layer_status.c) target_sources_ifdef(CONFIG_ZMK_WIDGET_WPM_STATUS app PRIVATE wpm_status.c) +target_sources_ifdef(CONFIG_ZMK_WIDGET_MODS_STATUS app PRIVATE mods_status.c) diff --git a/app/src/display/widgets/Kconfig b/app/src/display/widgets/Kconfig index 7ec20c1f..c009214e 100644 --- a/app/src/display/widgets/Kconfig +++ b/app/src/display/widgets/Kconfig @@ -36,4 +36,9 @@ config ZMK_WIDGET_WPM_STATUS select LV_USE_LABEL select ZMK_WPM +config ZMK_WIDGET_MODS_STATUS + bool "Widget for displaying active modifiers" + depends on !ZMK_SPLIT || ZMK_SPLIT_ROLE_CENTRAL + select LV_USE_LABEL + endmenu diff --git a/app/src/display/widgets/mods_status.c b/app/src/display/widgets/mods_status.c new file mode 100644 index 00000000..3086bc71 --- /dev/null +++ b/app/src/display/widgets/mods_status.c @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2022 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +#include +#include +#include +#include +#include +#include + +static sys_slist_t widgets = SYS_SLIST_STATIC_INIT(&widgets); + +struct mods_status_state { + uint8_t mods; +}; + +struct mods_status_state mods_status_get_state(const zmk_event_t *eh) { + return (struct mods_status_state){.mods = zmk_hid_get_explicit_mods()}; +}; + +void set_mods_symbol(lv_obj_t *label, struct mods_status_state state) { + char text[5] = {}; + + LOG_DBG("mods changed to %i", state.mods); + if (state.mods & (MOD_LCTL | MOD_RCTL)) + strcat(text, "C"); + if (state.mods & (MOD_LSFT | MOD_RSFT)) + strcat(text, "S"); + if (state.mods & (MOD_LALT | MOD_RALT)) + strcat(text, "A"); + if (state.mods & (MOD_LGUI | MOD_RGUI)) + strcat(text, "G"); + + lv_label_set_text(label, text); + lv_obj_align(label, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, -1, 0); +} + +void mods_status_update_cb(struct mods_status_state state) { + struct zmk_widget_mods_status *widget; + SYS_SLIST_FOR_EACH_CONTAINER(&widgets, widget, node) { set_mods_symbol(widget->obj, state); } +} + +ZMK_DISPLAY_WIDGET_LISTENER(widget_mods_status, struct mods_status_state, mods_status_update_cb, + mods_status_get_state) +ZMK_SUBSCRIPTION(widget_mods_status, zmk_keycode_state_changed); + +int zmk_widget_mods_status_init(struct zmk_widget_mods_status *widget, lv_obj_t *parent) { + widget->obj = lv_label_create(parent, NULL); + lv_label_set_align(widget->obj, LV_LABEL_ALIGN_RIGHT); + + lv_obj_set_size(widget->obj, 40, 15); + + sys_slist_append(&widgets, &widget->node); + + widget_mods_status_init(); + return 0; +} + +lv_obj_t *zmk_widget_mods_status_obj(struct zmk_widget_mods_status *widget) { return widget->obj; } From 4d83fc2cde2f021e1ca85bb2038ed989a6528f4d Mon Sep 17 00:00:00 2001 From: Cem Aksoylar Date: Thu, 1 Dec 2022 16:01:42 -0800 Subject: [PATCH 2/4] feat(docs): Document modifiers widget --- docs/docs/config/displays.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/docs/config/displays.md b/docs/docs/config/displays.md index e22f0da8..de82b284 100644 --- a/docs/docs/config/displays.md +++ b/docs/docs/config/displays.md @@ -23,6 +23,9 @@ Definition files: | `CONFIG_ZMK_WIDGET_BATTERY_STATUS_SHOW_PERCENTAGE` | bool | If battery widget is enabled, show percentage instead of icons | n | | `CONFIG_ZMK_WIDGET_OUTPUT_STATUS` | bool | Enable a widget to show the current output (USB/BLE) | y | | `CONFIG_ZMK_WIDGET_WPM_STATUS` | bool | Enable a widget to show words per minute | n | +| `CONFIG_ZMK_WIDGET_MODS_STATUS` | bool | Enable a widget to show active modifiers | n | + +Note that WPM and modifiers widgets are both shown on the bottom right of the display and hence can conflict with each other. Note that `CONFIG_ZMK_DISPLAY_INVERT` setting might not work as expected with custom status screens that utilize images. From 467ff20ea3ebe20cf6451d4957d4fae4b6e0415a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Aumu=CC=88ller?= Date: Fri, 21 Apr 2023 08:54:50 +0200 Subject: [PATCH 3/4] fix(display): Port modifiers widget to Zephyr 3.2/LVGL 8 --- app/include/zmk/display/widgets/mods_status.h | 2 +- app/src/display/status_screen.c | 8 +++----- app/src/display/widgets/mods_status.c | 10 ++++------ 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/app/include/zmk/display/widgets/mods_status.h b/app/include/zmk/display/widgets/mods_status.h index 411b379a..3850e324 100644 --- a/app/include/zmk/display/widgets/mods_status.h +++ b/app/include/zmk/display/widgets/mods_status.h @@ -7,7 +7,7 @@ #pragma once #include -#include +#include struct zmk_widget_mods_status { sys_snode_t node; diff --git a/app/src/display/status_screen.c b/app/src/display/status_screen.c index e107bafd..83d35df6 100644 --- a/app/src/display/status_screen.c +++ b/app/src/display/status_screen.c @@ -73,11 +73,9 @@ lv_obj_t *zmk_display_status_screen() { #if IS_ENABLED(CONFIG_ZMK_WIDGET_MODS_STATUS) zmk_widget_mods_status_init(&mods_status_widget, screen); - lv_obj_set_style_local_text_font(zmk_widget_mods_status_obj(&mods_status_widget), - LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, - lv_theme_get_font_small()); - lv_obj_align(zmk_widget_mods_status_obj(&mods_status_widget), NULL, LV_ALIGN_IN_BOTTOM_RIGHT, 0, - 0); + lv_obj_set_style_text_font(zmk_widget_mods_status_obj(&mods_status_widget), + lv_theme_get_font_small(screen), LV_PART_MAIN); + lv_obj_align(zmk_widget_mods_status_obj(&mods_status_widget), LV_ALIGN_BOTTOM_RIGHT, 0, 0); #endif return screen; } diff --git a/app/src/display/widgets/mods_status.c b/app/src/display/widgets/mods_status.c index 3086bc71..a963f7b4 100644 --- a/app/src/display/widgets/mods_status.c +++ b/app/src/display/widgets/mods_status.c @@ -4,7 +4,7 @@ * SPDX-License-Identifier: MIT */ -#include +#include LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include @@ -38,7 +38,7 @@ void set_mods_symbol(lv_obj_t *label, struct mods_status_state state) { strcat(text, "G"); lv_label_set_text(label, text); - lv_obj_align(label, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, -1, 0); + lv_obj_align(label, LV_ALIGN_BOTTOM_RIGHT, -1, 0); } void mods_status_update_cb(struct mods_status_state state) { @@ -51,10 +51,8 @@ ZMK_DISPLAY_WIDGET_LISTENER(widget_mods_status, struct mods_status_state, mods_s ZMK_SUBSCRIPTION(widget_mods_status, zmk_keycode_state_changed); int zmk_widget_mods_status_init(struct zmk_widget_mods_status *widget, lv_obj_t *parent) { - widget->obj = lv_label_create(parent, NULL); - lv_label_set_align(widget->obj, LV_LABEL_ALIGN_RIGHT); - - lv_obj_set_size(widget->obj, 40, 15); + widget->obj = lv_label_create(parent); + lv_obj_set_style_text_align(widget->obj, LV_TEXT_ALIGN_RIGHT, 0); sys_slist_append(&widgets, &widget->node); From 20131e4779c6273ba088b14a587a2ece2371ab06 Mon Sep 17 00:00:00 2001 From: Cem Aksoylar Date: Fri, 12 May 2023 14:28:01 -0700 Subject: [PATCH 4/4] feat(display): Add config for modifier chars --- app/src/display/widgets/Kconfig | 8 ++++++++ app/src/display/widgets/mods_status.c | 14 ++++++++++---- docs/docs/config/displays.md | 21 +++++++++++---------- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/app/src/display/widgets/Kconfig b/app/src/display/widgets/Kconfig index c009214e..d8a02c70 100644 --- a/app/src/display/widgets/Kconfig +++ b/app/src/display/widgets/Kconfig @@ -41,4 +41,12 @@ config ZMK_WIDGET_MODS_STATUS depends on !ZMK_SPLIT || ZMK_SPLIT_ROLE_CENTRAL select LV_USE_LABEL +if ZMK_WIDGET_MODS_STATUS + +config ZMK_WIDGET_MODS_STATUS_CHARACTERS + string "Characters to show for each modifier, corresponding to Control/Shift/Alt/GUI respectively" + default "CSAG" + +endif + endmenu diff --git a/app/src/display/widgets/mods_status.c b/app/src/display/widgets/mods_status.c index a963f7b4..f38109a7 100644 --- a/app/src/display/widgets/mods_status.c +++ b/app/src/display/widgets/mods_status.c @@ -14,6 +14,12 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include #include +#define MOD_CHARS CONFIG_ZMK_WIDGET_MODS_STATUS_CHARACTERS +#define MOD_CHARS_LEN (sizeof(MOD_CHARS) - 1) + +BUILD_ASSERT(MOD_CHARS_LEN == 4, + "ERROR: CONFIG_ZMK_WIDGET_MODS_STATUS_CHARACTERS should have exactly 4 characters"); + static sys_slist_t widgets = SYS_SLIST_STATIC_INIT(&widgets); struct mods_status_state { @@ -29,13 +35,13 @@ void set_mods_symbol(lv_obj_t *label, struct mods_status_state state) { LOG_DBG("mods changed to %i", state.mods); if (state.mods & (MOD_LCTL | MOD_RCTL)) - strcat(text, "C"); + strncat(text, &MOD_CHARS[0], 1); if (state.mods & (MOD_LSFT | MOD_RSFT)) - strcat(text, "S"); + strncat(text, &MOD_CHARS[1], 1); if (state.mods & (MOD_LALT | MOD_RALT)) - strcat(text, "A"); + strncat(text, &MOD_CHARS[2], 1); if (state.mods & (MOD_LGUI | MOD_RGUI)) - strcat(text, "G"); + strncat(text, &MOD_CHARS[3], 1); lv_label_set_text(label, text); lv_obj_align(label, LV_ALIGN_BOTTOM_RIGHT, -1, 0); diff --git a/docs/docs/config/displays.md b/docs/docs/config/displays.md index de82b284..d905fef3 100644 --- a/docs/docs/config/displays.md +++ b/docs/docs/config/displays.md @@ -14,16 +14,17 @@ Definition files: - [zmk/app/src/display/Kconfig](https://github.com/zmkfirmware/zmk/blob/main/app/src/display/Kconfig) - [zmk/app/src/display/widgets/Kconfig](https://github.com/zmkfirmware/zmk/blob/main/app/src/display/widgets/Kconfig) -| Config | Type | Description | Default | -| -------------------------------------------------- | ---- | -------------------------------------------------------------- | ------- | -| `CONFIG_ZMK_DISPLAY` | bool | Enable support for displays | n | -| `CONFIG_ZMK_DISPLAY_INVERT` | bool | Invert display colors from black-on-white to white-on-black | n | -| `CONFIG_ZMK_WIDGET_LAYER_STATUS` | bool | Enable a widget to show the highest, active layer | y | -| `CONFIG_ZMK_WIDGET_BATTERY_STATUS` | bool | Enable a widget to show battery charge information | y | -| `CONFIG_ZMK_WIDGET_BATTERY_STATUS_SHOW_PERCENTAGE` | bool | If battery widget is enabled, show percentage instead of icons | n | -| `CONFIG_ZMK_WIDGET_OUTPUT_STATUS` | bool | Enable a widget to show the current output (USB/BLE) | y | -| `CONFIG_ZMK_WIDGET_WPM_STATUS` | bool | Enable a widget to show words per minute | n | -| `CONFIG_ZMK_WIDGET_MODS_STATUS` | bool | Enable a widget to show active modifiers | n | +| Config | Type | Description | Default | +| -------------------------------------------------- | ------ | ----------------------------------------------------------------------------------------- | ------- | +| `CONFIG_ZMK_DISPLAY` | bool | Enable support for displays | n | +| `CONFIG_ZMK_DISPLAY_INVERT` | bool | Invert display colors from black-on-white to white-on-black | n | +| `CONFIG_ZMK_WIDGET_LAYER_STATUS` | bool | Enable a widget to show the highest, active layer | y | +| `CONFIG_ZMK_WIDGET_BATTERY_STATUS` | bool | Enable a widget to show battery charge information | y | +| `CONFIG_ZMK_WIDGET_BATTERY_STATUS_SHOW_PERCENTAGE` | bool | If battery widget is enabled, show percentage instead of icons | n | +| `CONFIG_ZMK_WIDGET_OUTPUT_STATUS` | bool | Enable a widget to show the current output (USB/BLE) | y | +| `CONFIG_ZMK_WIDGET_WPM_STATUS` | bool | Enable a widget to show words per minute | n | +| `CONFIG_ZMK_WIDGET_MODS_STATUS` | bool | Enable a widget to show active modifiers | n | +| `CONFIG_ZMK_WIDGET_MODS_STATUS_CHARACTERS` | string | Characters to show for each modifier, corresponding to Control/Shift/Alt/GUI respectively | "CSAG" | Note that WPM and modifiers widgets are both shown on the bottom right of the display and hence can conflict with each other.