Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into mouse
Browse files Browse the repository at this point in the history
  • Loading branch information
peppapighs committed Sep 24, 2024
2 parents ac31d98 + 33e3b02 commit 591482d
Show file tree
Hide file tree
Showing 40 changed files with 473 additions and 218 deletions.
5 changes: 3 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ repos:
hooks:
- id: remove-tabs
exclude: "vendor-prefixes\\.txt$"
- repo: https://github.com/pocc/pre-commit-hooks
rev: v1.3.5
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v18.1.8
hooks:
- id: clang-format
types_or: [c++, c]
args:
- -i
- repo: https://github.com/pre-commit/mirrors-prettier
Expand Down
2 changes: 1 addition & 1 deletion app/boards/arm/corneish_zen/widgets/battery_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void battery_status_update_cb(struct battery_status_state state) {
static struct battery_status_state battery_status_get_state(const zmk_event_t *eh) {
const struct zmk_battery_state_changed *ev = as_zmk_battery_state_changed(eh);

return (struct battery_status_state) {
return (struct battery_status_state){
.level = (ev != NULL) ? ev->state_of_charge : zmk_battery_state_of_charge(),
#if IS_ENABLED(CONFIG_USB_DEVICE_STACK)
.usb_present = zmk_usb_is_powered(),
Expand Down
11 changes: 6 additions & 5 deletions app/boards/arm/corneish_zen/widgets/layer_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
static sys_slist_t widgets = SYS_SLIST_STATIC_INIT(&widgets);

struct layer_status_state {
uint8_t index;
zmk_keymap_layer_index_t index;
const char *label;
};

static void set_layer_symbol(lv_obj_t *label, struct layer_status_state state) {
const char *layer_label = state.label;
uint8_t active_layer_index = state.index;
zmk_keymap_layer_index_t active_layer_index = state.index;

if (layer_label == NULL) {
if (layer_label == NULL || strlen(layer_label) == 0) {
char text[6] = {};

sprintf(text, " %i", active_layer_index);
Expand All @@ -44,8 +44,9 @@ static void layer_status_update_cb(struct layer_status_state state) {
}

static struct layer_status_state layer_status_get_state(const zmk_event_t *eh) {
uint8_t index = zmk_keymap_highest_layer_active();
return (struct layer_status_state){.index = index, .label = zmk_keymap_layer_name(index)};
zmk_keymap_layer_index_t index = zmk_keymap_highest_layer_active();
return (struct layer_status_state){
.index = index, .label = zmk_keymap_layer_name(zmk_keymap_layer_index_to_id(index))};
}

ZMK_DISPLAY_WIDGET_LISTENER(widget_layer_status, struct layer_status_state, layer_status_update_cb,
Expand Down
2 changes: 1 addition & 1 deletion app/boards/shields/nice_view/widgets/peripheral_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static void battery_status_update_cb(struct battery_status_state state) {
}

static struct battery_status_state battery_status_get_state(const zmk_event_t *eh) {
return (struct battery_status_state) {
return (struct battery_status_state){
.level = zmk_battery_state_of_charge(),
#if IS_ENABLED(CONFIG_USB_DEVICE_STACK)
.usb_present = zmk_usb_is_powered(),
Expand Down
9 changes: 5 additions & 4 deletions app/boards/shields/nice_view/widgets/status.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct output_status_state {
};

struct layer_status_state {
uint8_t index;
zmk_keymap_layer_index_t index;
const char *label;
};

Expand Down Expand Up @@ -212,7 +212,7 @@ static void battery_status_update_cb(struct battery_status_state state) {
static struct battery_status_state battery_status_get_state(const zmk_event_t *eh) {
const struct zmk_battery_state_changed *ev = as_zmk_battery_state_changed(eh);

return (struct battery_status_state) {
return (struct battery_status_state){
.level = (ev != NULL) ? ev->state_of_charge : zmk_battery_state_of_charge(),
#if IS_ENABLED(CONFIG_USB_DEVICE_STACK)
.usb_present = zmk_usb_is_powered(),
Expand Down Expand Up @@ -277,8 +277,9 @@ static void layer_status_update_cb(struct layer_status_state state) {
}

static struct layer_status_state layer_status_get_state(const zmk_event_t *eh) {
uint8_t index = zmk_keymap_highest_layer_active();
return (struct layer_status_state){.index = index, .label = zmk_keymap_layer_name(index)};
zmk_keymap_layer_index_t index = zmk_keymap_highest_layer_active();
return (struct layer_status_state){
.index = index, .label = zmk_keymap_layer_name(zmk_keymap_layer_index_to_id(index))};
}

ZMK_DISPLAY_WIDGET_LISTENER(widget_layer_status, struct layer_status_state, layer_status_update_cb,
Expand Down
13 changes: 10 additions & 3 deletions app/include/drivers/behavior.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ struct zmk_behavior_local_id_map {
#if IS_ENABLED(CONFIG_ZMK_BEHAVIOR_METADATA)

#define ZMK_BEHAVIOR_METADATA_INITIALIZER(node_id) \
{ .display_name = DT_PROP_OR(node_id, display_name, DEVICE_DT_NAME(node_id)), }
{ \
.display_name = DT_PROP_OR(node_id, display_name, DEVICE_DT_NAME(node_id)), \
}

#else

Expand All @@ -132,10 +134,15 @@ struct zmk_behavior_local_id_map {
#endif // IS_ENABLED(CONFIG_ZMK_BEHAVIOR_METADATA)

#define ZMK_BEHAVIOR_REF_INITIALIZER(node_id, _dev) \
{ .device = _dev, .metadata = ZMK_BEHAVIOR_METADATA_INITIALIZER(node_id), }
{ \
.device = _dev, \
.metadata = ZMK_BEHAVIOR_METADATA_INITIALIZER(node_id), \
}

#define ZMK_BEHAVIOR_LOCAL_ID_MAP_INITIALIZER(node_id, _dev) \
{ .device = _dev, }
{ \
.device = _dev, \
}

#define ZMK_BEHAVIOR_REF_DEFINE(name, node_id, _dev) \
static const STRUCT_SECTION_ITERABLE(zmk_behavior_ref, name) = \
Expand Down
16 changes: 16 additions & 0 deletions app/include/zmk/behavior.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ struct zmk_behavior_binding_event {
int layer;
uint32_t position;
int64_t timestamp;
#if IS_ENABLED(CONFIG_ZMK_SPLIT)
uint8_t source;
#endif
};

/**
Expand All @@ -42,6 +45,19 @@ struct zmk_behavior_binding_event {
*/
const struct device *zmk_behavior_get_binding(const char *name);

/**
* @brief Invoke a behavior given its binding and invoking event details.
*
* @param src_binding Behavior binding to invoke.
* @param event The binding event struct containing details of the event that invoked it.
* @param pressed Whether the binding is pressed or released.
*
* @retval 0 If successful.
* @retval Negative errno code if failure.
*/
int zmk_behavior_invoke_binding(const struct zmk_behavior_binding *src_binding,
struct zmk_behavior_binding_event event, bool pressed);

/**
* @brief Get a local ID for a behavior from its @p name field.
*
Expand Down
4 changes: 2 additions & 2 deletions app/include/zmk/behavior_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
#include <stdint.h>
#include <zmk/behavior.h>

int zmk_behavior_queue_add(uint32_t position, const struct zmk_behavior_binding behavior,
bool press, uint32_t wait);
int zmk_behavior_queue_add(const struct zmk_behavior_binding_event *event,
const struct zmk_behavior_binding behavior, bool press, uint32_t wait);
2 changes: 1 addition & 1 deletion app/include/zmk/keymap.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ zmk_keymap_layer_id_t zmk_keymap_layer_index_to_id(zmk_keymap_layer_index_t laye
zmk_keymap_layer_id_t zmk_keymap_layer_default(void);
zmk_keymap_layers_state_t zmk_keymap_layer_state(void);
bool zmk_keymap_layer_active(zmk_keymap_layer_id_t layer);
zmk_keymap_layer_id_t zmk_keymap_highest_layer_active(void);
zmk_keymap_layer_index_t zmk_keymap_highest_layer_active(void);
int zmk_keymap_layer_activate(zmk_keymap_layer_id_t layer);
int zmk_keymap_layer_deactivate(zmk_keymap_layer_id_t layer);
int zmk_keymap_layer_toggle(zmk_keymap_layer_id_t layer);
Expand Down
4 changes: 0 additions & 4 deletions app/include/zmk/matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@

#if DT_HAS_COMPAT_STATUS_OKAY(zmk_physical_layout)

#if ZMK_MATRIX_HAS_TRANSFORM
#error "To use physical layouts, remove the chosen `zmk,matrix-transform` value."
#endif

#define ZMK_PHYSICAL_LAYOUT_BYTE_ARRAY(node_id) \
uint8_t _CONCAT(prop_, node_id)[DT_PROP_LEN(DT_PHANDLE(node_id, transform), map)];

Expand Down
7 changes: 7 additions & 0 deletions app/include/zmk/physical_layouts.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

#include <zephyr/kernel.h>
#include <zmk/matrix_transform.h>
#include <zmk/event_manager.h>

struct zmk_physical_layout_selection_changed {
uint8_t selection;
};

ZMK_EVENT_DECLARE(zmk_physical_layout_selection_changed);

struct zmk_key_physical_attrs {
int16_t width;
Expand Down
1 change: 1 addition & 0 deletions app/include/zmk/split/bluetooth/service.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct sensor_event {

struct zmk_split_run_behavior_data {
uint8_t position;
uint8_t source;
uint8_t state;
uint32_t param1;
uint32_t param2;
Expand Down
1 change: 1 addition & 0 deletions app/include/zmk/split/bluetooth/uuid.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
#define ZMK_SPLIT_BT_CHAR_RUN_BEHAVIOR_UUID ZMK_BT_SPLIT_UUID(0x00000002)
#define ZMK_SPLIT_BT_CHAR_SENSOR_STATE_UUID ZMK_BT_SPLIT_UUID(0x00000003)
#define ZMK_SPLIT_BT_UPDATE_HID_INDICATORS_UUID ZMK_BT_SPLIT_UUID(0x00000004)
#define ZMK_SPLIT_BT_SELECT_PHYS_LAYOUT_UUID ZMK_BT_SPLIT_UUID(0x00000005)
2 changes: 1 addition & 1 deletion app/include/zmk/virtual_key_position.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Gets the sensor number from the virtual key position.
*/
#define ZMK_SENSOR_POSITION_FROM_VIRTUAL_KEY_POSITION(vkp) ((vkp)-ZMK_KEYMAP_LEN)
#define ZMK_SENSOR_POSITION_FROM_VIRTUAL_KEY_POSITION(vkp) ((vkp) - ZMK_KEYMAP_LEN)

/**
* Gets the virtual key position to use for the combo with the given index.
Expand Down
67 changes: 67 additions & 0 deletions app/src/behavior.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@

#endif

#include <zmk/ble.h>
#if ZMK_BLE_IS_CENTRAL
#include <zmk/split/bluetooth/central.h>
#endif

#include <drivers/behavior.h>
#include <zmk/behavior.h>
#include <zmk/hid.h>
#include <zmk/matrix.h>

#include <zmk/events/position_state_changed.h>

#include <zephyr/logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

Expand Down Expand Up @@ -49,6 +56,66 @@ const struct device *z_impl_behavior_get_binding(const char *name) {
return NULL;
}

static int invoke_locally(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event, bool pressed) {
if (pressed) {
return behavior_keymap_binding_pressed(binding, event);
} else {
return behavior_keymap_binding_released(binding, event);
}
}

int zmk_behavior_invoke_binding(const struct zmk_behavior_binding *src_binding,
struct zmk_behavior_binding_event event, bool pressed) {
// We want to make a copy of this, since it may be converted from
// relative to absolute before being invoked
struct zmk_behavior_binding binding = *src_binding;

const struct device *behavior = zmk_behavior_get_binding(binding.behavior_dev);

if (!behavior) {
LOG_WRN("No behavior assigned to %d on layer %d", event.position, event.layer);
return 1;
}

int err = behavior_keymap_binding_convert_central_state_dependent_params(&binding, event);
if (err) {
LOG_ERR("Failed to convert relative to absolute behavior binding (err %d)", err);
return err;
}

enum behavior_locality locality = BEHAVIOR_LOCALITY_CENTRAL;
err = behavior_get_locality(behavior, &locality);
if (err) {
LOG_ERR("Failed to get behavior locality %d", err);
return err;
}

switch (locality) {
case BEHAVIOR_LOCALITY_CENTRAL:
return invoke_locally(&binding, event, pressed);
case BEHAVIOR_LOCALITY_EVENT_SOURCE:
#if ZMK_BLE_IS_CENTRAL // source is a member of event because CONFIG_ZMK_SPLIT is enabled
if (event.source == ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL) {
return invoke_locally(&binding, event, pressed);
} else {
return zmk_split_bt_invoke_behavior(event.source, &binding, event, pressed);
}
#else
return invoke_locally(&binding, event, pressed);
#endif
case BEHAVIOR_LOCALITY_GLOBAL:
#if ZMK_BLE_IS_CENTRAL
for (int i = 0; i < ZMK_SPLIT_BLE_PERIPHERAL_COUNT; i++) {
zmk_split_bt_invoke_behavior(i, &binding, event, pressed);
}
#endif
return invoke_locally(&binding, event, pressed);
}

return -ENOTSUP;
}

#if IS_ENABLED(CONFIG_ZMK_BEHAVIOR_METADATA)

int zmk_behavior_get_empty_param_metadata(const struct device *dev,
Expand Down
28 changes: 22 additions & 6 deletions app/src/behavior_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include <zmk/behavior_queue.h>
#include <zmk/behavior.h>

#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
Expand All @@ -14,6 +15,9 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

struct q_item {
uint32_t position;
#if IS_ENABLED(CONFIG_ZMK_SPLIT)
uint8_t source;
#endif
struct zmk_behavior_binding binding;
bool press : 1;
uint32_t wait : 31;
Expand All @@ -32,12 +36,16 @@ static void behavior_queue_process_next(struct k_work *work) {
item.binding.param2);

struct zmk_behavior_binding_event event = {.position = item.position,
.timestamp = k_uptime_get()};
.timestamp = k_uptime_get(),
#if IS_ENABLED(CONFIG_ZMK_SPLIT)
.source = item.source
#endif
};

if (item.press) {
behavior_keymap_binding_pressed(&item.binding, event);
zmk_behavior_invoke_binding(&item.binding, event, true);
} else {
behavior_keymap_binding_released(&item.binding, event);
zmk_behavior_invoke_binding(&item.binding, event, false);
}

LOG_DBG("Processing next queued behavior in %dms", item.wait);
Expand All @@ -49,9 +57,17 @@ static void behavior_queue_process_next(struct k_work *work) {
}
}

int zmk_behavior_queue_add(uint32_t position, const struct zmk_behavior_binding binding, bool press,
uint32_t wait) {
struct q_item item = {.press = press, .binding = binding, .wait = wait};
int zmk_behavior_queue_add(const struct zmk_behavior_binding_event *event,
const struct zmk_behavior_binding binding, bool press, uint32_t wait) {
struct q_item item = {
.press = press,
.binding = binding,
.wait = wait,
.position = event->position,
#if IS_ENABLED(CONFIG_ZMK_SPLIT)
.source = event->source,
#endif
};

const int ret = k_msgq_put(&zmk_behavior_queue_msgq, &item, K_NO_WAIT);
if (ret < 0) {
Expand Down
5 changes: 1 addition & 4 deletions app/src/behaviors/behavior_caps_word.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,7 @@ static int behavior_caps_word_init(const struct device *dev) {
#define CAPS_WORD_LABEL(i, _n) DT_INST_LABEL(i)

#define PARSE_BREAK(i) \
{ \
.page = ZMK_HID_USAGE_PAGE(i), .id = ZMK_HID_USAGE_ID(i), \
.implicit_modifiers = SELECT_MODS(i) \
}
{.page = ZMK_HID_USAGE_PAGE(i), .id = ZMK_HID_USAGE_ID(i), .implicit_modifiers = SELECT_MODS(i)}

#define BREAK_ITEM(i, n) PARSE_BREAK(DT_INST_PROP_BY_IDX(n, continue_list, i))

Expand Down
Loading

0 comments on commit 591482d

Please sign in to comment.