feat(display): Add modifiers widget
This commit is contained in:
parent
0a4b1a6533
commit
9619251464
5 changed files with 103 additions and 0 deletions
18
app/include/zmk/display/widgets/mods_status.h
Normal file
18
app/include/zmk/display/widgets/mods_status.h
Normal file
|
@ -0,0 +1,18 @@
|
|||
/*
|
||||
* Copyright (c) 2022 The ZMK Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <lvgl.h>
|
||||
#include <kernel.h>
|
||||
|
||||
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);
|
|
@ -9,6 +9,7 @@
|
|||
#include <zmk/display/widgets/battery_status.h>
|
||||
#include <zmk/display/widgets/layer_status.h>
|
||||
#include <zmk/display/widgets/wpm_status.h>
|
||||
#include <zmk/display/widgets/mods_status.h>
|
||||
#include <zmk/display/status_screen.h>
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
65
app/src/display/widgets/mods_status.c
Normal file
65
app/src/display/widgets/mods_status.c
Normal file
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
* Copyright (c) 2022 The ZMK Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#include <logging/log.h>
|
||||
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||
|
||||
#include <zmk/display.h>
|
||||
#include <zmk/display/widgets/mods_status.h>
|
||||
#include <zmk/events/keycode_state_changed.h>
|
||||
#include <zmk/event_manager.h>
|
||||
#include <zmk/endpoints.h>
|
||||
#include <zmk/hid.h>
|
||||
|
||||
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; }
|
Loading…
Add table
Reference in a new issue