-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(behaviors): Create a list to lookup behaviors
Added BEHAVIOR_DT_DEFINE() and BEHAVIOR_DT_INST_DEFINE(), which work exactly like the DEVICE_*_DEFINE() macros, except they also register the device as a behavior by adding a pointer to it to a memory section. Added zmk_behavior_get_binding(), which works like device_get_binding() except that it only searches the devices that have been registered as behaviors. This ensures that behaviors cannot have name collisions with other devices defined by the SoC, which will be important when we remove the label property from behaviors so they are given their node names. As an added benefit, this is faster since it searches a smaller list. Some basic benchmark code I wrote indicates it takes 30-70% as long, depending on where the behavior is in the list and whether the name string is an exact pointer match. From now on, behaviors should use BEHAVIOR_*_DEFINe() instead of DEVICE_*_DEFINE(), and any code that looks up a behavior by name should use zmk_behavior_get_binding() instead of device_get_binding().
- Loading branch information
1 parent
5ecd352
commit 36eda57
Showing
31 changed files
with
231 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright (c) 2023 The ZMK Contributors | ||
* | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
#include <zephyr/linker/linker-defs.h> | ||
|
||
ITERABLE_SECTION_ROM(zmk_behavior_ref, 4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright (c) 2023 The ZMK Contributors | ||
* | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
#include <zephyr/device.h> | ||
#include <zephyr/init.h> | ||
#include <zephyr/sys/util_macro.h> | ||
#include <string.h> | ||
|
||
#include <drivers/behavior.h> | ||
#include <zmk/behavior.h> | ||
|
||
#include <zephyr/logging/log.h> | ||
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); | ||
|
||
const struct device *zmk_behavior_get_binding(const char *name) { | ||
return behavior_get_binding(name); | ||
} | ||
|
||
const struct device *z_impl_behavior_get_binding(const char *name) { | ||
if (name == NULL || name[0] == '\0') { | ||
return NULL; | ||
} | ||
|
||
STRUCT_SECTION_FOREACH(zmk_behavior_ref, item) { | ||
if (z_device_is_ready(item->device) && item->device->name == name) { | ||
return item->device; | ||
} | ||
} | ||
|
||
STRUCT_SECTION_FOREACH(zmk_behavior_ref, item) { | ||
if (z_device_is_ready(item->device) && strcmp(item->device->name, name) == 0) { | ||
return item->device; | ||
} | ||
} | ||
|
||
return NULL; | ||
} | ||
|
||
#if IS_ENABLED(CONFIG_LOG) | ||
static int check_behavior_names(const struct device *dev) { | ||
ARG_UNUSED(dev); | ||
|
||
// Behavior names must be unique, but we don't have a good way to enforce this | ||
// at compile time, so log an error at runtime if they aren't unique. | ||
ptrdiff_t count; | ||
STRUCT_SECTION_COUNT(zmk_behavior_ref, &count); | ||
|
||
for (ptrdiff_t i = 0; i < count; i++) { | ||
const struct zmk_behavior_ref *current; | ||
STRUCT_SECTION_GET(zmk_behavior_ref, i, ¤t); | ||
|
||
for (ptrdiff_t j = i + 1; j < count; j++) { | ||
const struct zmk_behavior_ref *other; | ||
STRUCT_SECTION_GET(zmk_behavior_ref, j, &other); | ||
|
||
if (strcmp(current->device->name, other->device->name) == 0) { | ||
LOG_ERR("Multiple behaviors have the same name '%s'", current->device->name); | ||
} | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
SYS_INIT(check_behavior_names, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); | ||
#endif // IS_ENABLED(CONFIG_LOG) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.