Software keyboard refactored (#594)
Removed software keyboard in Tactility subproject (GuiService, Lvgl.cpp) and re-implemented it in TactilityKernel.
This commit is contained in:
committed by
GitHub
parent
58a529cc44
commit
e6e1dcd0ca
@@ -10,7 +10,6 @@
|
||||
|
||||
#include <Tactility/Assets.h>
|
||||
#include <Tactility/Tactility.h>
|
||||
#include <Tactility/lvgl/Keyboard.h>
|
||||
|
||||
#include <tactility/log.h>
|
||||
|
||||
@@ -22,6 +21,7 @@
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/queue.h>
|
||||
|
||||
#include <lvgl/devices/keyboard.h>
|
||||
#include <lvgl/lvgl.h>
|
||||
|
||||
#include <algorithm>
|
||||
@@ -470,11 +470,12 @@ static void hidHostSubscribeNext(HidHostCtx& ctx) {
|
||||
getMainDispatcher().dispatch([] {
|
||||
if (!hid_host_ctx || hid_host_ctx->kbIndev != nullptr) return;
|
||||
if (!lvgl_try_lock(1000)) { LOG_W(TAG, "LVGL lock failed for kb indev"); return; }
|
||||
|
||||
auto* kb = lv_indev_create();
|
||||
lv_indev_set_type(kb, LV_INDEV_TYPE_KEYPAD);
|
||||
lv_indev_set_read_cb(kb, hidHostKeyboardReadCb);
|
||||
hid_host_ctx->kbIndev = kb;
|
||||
lvgl::hardware_keyboard_set_indev(kb);
|
||||
lvgl_hardware_keyboard_add_custom(kb);
|
||||
lvgl_unlock();
|
||||
LOG_I(TAG, "Keyboard indev registered");
|
||||
});
|
||||
@@ -707,7 +708,7 @@ static int hidHostGapCb(struct ble_gap_event* event, void* /*arg*/) {
|
||||
return;
|
||||
}
|
||||
if (saved_kb) {
|
||||
lvgl::hardware_keyboard_set_indev(nullptr);
|
||||
lvgl_hardware_keyboard_remove_custom(saved_kb);
|
||||
lv_indev_delete(saved_kb);
|
||||
}
|
||||
if (saved_mouse) lv_indev_delete(saved_mouse);
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
#include "Tactility/lvgl/Keyboard.h"
|
||||
#include "Tactility/service/gui/GuiService.h"
|
||||
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/drivers/keyboard.h>
|
||||
|
||||
namespace tt::lvgl {
|
||||
|
||||
static lv_indev_t* keyboard_device = nullptr;
|
||||
static lv_group_t* pending_keyboard_group = nullptr;
|
||||
|
||||
void software_keyboard_show(lv_obj_t* textarea) {
|
||||
auto gui_service = service::gui::findService();
|
||||
if (gui_service != nullptr) {
|
||||
gui_service->softwareKeyboardShow(textarea);
|
||||
}
|
||||
}
|
||||
|
||||
void software_keyboard_hide() {
|
||||
auto gui_service = service::gui::findService();
|
||||
if (gui_service != nullptr) {
|
||||
gui_service->softwareKeyboardHide();
|
||||
}
|
||||
}
|
||||
|
||||
bool software_keyboard_is_enabled() {
|
||||
auto gui_service = service::gui::findService();
|
||||
if (gui_service != nullptr) {
|
||||
return gui_service->softwareKeyboardIsEnabled();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void software_keyboard_activate(lv_group_t* group) {
|
||||
pending_keyboard_group = group;
|
||||
if (keyboard_device != nullptr) {
|
||||
lv_indev_set_group(keyboard_device, group);
|
||||
}
|
||||
}
|
||||
|
||||
void software_keyboard_deactivate() {
|
||||
pending_keyboard_group = nullptr;
|
||||
if (keyboard_device != nullptr) {
|
||||
lv_indev_set_group(keyboard_device, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
bool hardware_keyboard_is_available() {
|
||||
if (keyboard_device != nullptr) {
|
||||
return true;
|
||||
}
|
||||
bool has_kernel_keyboard = false;
|
||||
device_for_each_of_type(&KEYBOARD_TYPE, &has_kernel_keyboard, [](Device* device, void* context) {
|
||||
if (device_is_ready(device)) {
|
||||
*static_cast<bool*>(context) = true;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
return has_kernel_keyboard;
|
||||
}
|
||||
|
||||
void hardware_keyboard_set_indev(lv_indev_t* device) {
|
||||
keyboard_device = device;
|
||||
// If an app already activated a keyboard group while no hardware keyboard was
|
||||
// connected, apply the pending group now that the device is available.
|
||||
if (device != nullptr && pending_keyboard_group != nullptr) {
|
||||
lv_indev_set_group(device, pending_keyboard_group);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
#ifdef ESP_PLATFORM
|
||||
|
||||
#include <Tactility/Assets.h>
|
||||
#include <Tactility/lvgl/Keyboard.h>
|
||||
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/drivers/usb_host_hid.h>
|
||||
@@ -15,6 +14,7 @@
|
||||
#include <freertos/semphr.h>
|
||||
|
||||
#include <lvgl/lvgl.h>
|
||||
#include <lvgl/devices/keyboard.h>
|
||||
|
||||
#include <atomic>
|
||||
|
||||
@@ -171,6 +171,7 @@ static void usbHidInputTask(void* arg) {
|
||||
lv_indev_set_read_cb(ctx->kb_indev, keyboard_read_cb);
|
||||
lv_indev_set_user_data(ctx->kb_indev, ctx);
|
||||
lv_indev_set_group(ctx->kb_indev, lv_group_get_default());
|
||||
lvgl_hardware_keyboard_add_custom(ctx->kb_indev);
|
||||
|
||||
lvgl_unlock();
|
||||
|
||||
@@ -230,13 +231,15 @@ static void usbHidInputTask(void* arg) {
|
||||
}
|
||||
case USB_HID_EVENT_KEYBOARD_CONNECTED:
|
||||
if (ctx->kb_indev && lvgl_try_lock(pdMS_TO_TICKS(200))) {
|
||||
hardware_keyboard_set_indev(ctx->kb_indev);
|
||||
lvgl_keyboard_enable(ctx->kb_indev);
|
||||
lvgl_unlock();
|
||||
}
|
||||
break;
|
||||
case USB_HID_EVENT_KEYBOARD_DISCONNECTED:
|
||||
if (lvgl_try_lock(pdMS_TO_TICKS(200))) {
|
||||
hardware_keyboard_set_indev(nullptr);
|
||||
if (ctx->kb_indev) {
|
||||
lvgl_keyboard_disable(ctx->kb_indev);
|
||||
}
|
||||
lvgl_unlock();
|
||||
}
|
||||
break;
|
||||
@@ -263,7 +266,7 @@ static void usbHidInputTask(void* arg) {
|
||||
if (ctx->mouse_indev) { lv_indev_delete(ctx->mouse_indev); ctx->mouse_indev = nullptr; }
|
||||
if (ctx->mouse_cursor) { lv_obj_delete(ctx->mouse_cursor); ctx->mouse_cursor = nullptr; }
|
||||
if (ctx->kb_indev) {
|
||||
hardware_keyboard_set_indev(nullptr);
|
||||
lvgl_hardware_keyboard_remove_custom(ctx->kb_indev);
|
||||
lv_indev_delete(ctx->kb_indev);
|
||||
ctx->kb_indev = nullptr;
|
||||
}
|
||||
@@ -347,7 +350,7 @@ void stopUsbHidInput() {
|
||||
if (ctx->mouse_indev) { lv_indev_delete(ctx->mouse_indev); ctx->mouse_indev = nullptr; }
|
||||
if (ctx->mouse_cursor) { lv_obj_delete(ctx->mouse_cursor); ctx->mouse_cursor = nullptr; }
|
||||
if (ctx->kb_indev) {
|
||||
hardware_keyboard_set_indev(nullptr);
|
||||
lvgl_hardware_keyboard_remove_custom(ctx->kb_indev);
|
||||
lv_indev_delete(ctx->kb_indev);
|
||||
ctx->kb_indev = nullptr;
|
||||
}
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
#ifdef ESP_PLATFORM
|
||||
|
||||
#include <lvgl.h>
|
||||
|
||||
#include <lvgl/lvgl.h>
|
||||
|
||||
#include <Tactility/service/gui/GuiService.h>
|
||||
#include <lvgl/devices/keyboard.h>
|
||||
|
||||
extern "C" {
|
||||
|
||||
@@ -17,9 +14,9 @@ lv_obj_t* __wrap_lv_textarea_create(lv_obj_t* parent) {
|
||||
lv_obj_set_style_pad_all(textarea, 2, LV_STATE_DEFAULT);
|
||||
}
|
||||
|
||||
auto gui_service = tt::service::gui::findService();
|
||||
if (gui_service != nullptr) {
|
||||
gui_service->keyboardAddTextArea(textarea);
|
||||
auto* software_keyboard = lvgl_software_keyboard_get_last();
|
||||
if (software_keyboard != nullptr) {
|
||||
lvgl_keyboard_add_textarea(software_keyboard, textarea);
|
||||
}
|
||||
|
||||
if (lv_display_get_color_format(lv_obj_get_display(parent)) == LV_COLOR_FORMAT_L8) {
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
#include <Tactility/service/gui/GuiService.h>
|
||||
|
||||
#include "lvgl/devices/keyboard.h"
|
||||
|
||||
#include <Tactility/LogMessages.h>
|
||||
#include <Tactility/Tactility.h>
|
||||
#include <Tactility/app/AppInstance.h>
|
||||
@@ -113,7 +116,6 @@ int32_t GuiService::guiMain() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
service->keyboardGroup = lv_group_create();
|
||||
lv_obj_set_style_border_width(screen_root, 0, LV_STATE_DEFAULT);
|
||||
lv_obj_set_style_pad_all(screen_root, 0, LV_STATE_DEFAULT);
|
||||
|
||||
@@ -157,11 +159,12 @@ lv_obj_t* GuiService::createAppViews(lv_obj_t* parent) {
|
||||
lv_obj_set_style_border_width(child_container, 0, LV_STATE_DEFAULT);
|
||||
lv_obj_set_flex_grow(child_container, 1);
|
||||
|
||||
if (softwareKeyboardIsEnabled()) {
|
||||
keyboard = lv_keyboard_create(parent);
|
||||
lv_obj_add_flag(keyboard, LV_OBJ_FLAG_HIDDEN);
|
||||
if (lvgl_software_keyboard_is_enabled()) {
|
||||
lvgl_software_keyboard_construct(&software_keyboard, parent);
|
||||
} else {
|
||||
keyboard = nullptr;
|
||||
software_keyboard = {
|
||||
nullptr
|
||||
};
|
||||
}
|
||||
|
||||
return child_container;
|
||||
@@ -281,9 +284,8 @@ void GuiService::onStop(ServiceContext& service) {
|
||||
thread->join();
|
||||
|
||||
lvgl_lock();
|
||||
if (keyboardGroup != nullptr) {
|
||||
lv_group_delete(keyboardGroup);
|
||||
keyboardGroup = nullptr;
|
||||
if (software_keyboard.object != nullptr) {
|
||||
lvgl_software_keyboard_destruct(&software_keyboard);
|
||||
}
|
||||
|
||||
auto* default_group = lv_group_get_default();
|
||||
|
||||
@@ -1,76 +0,0 @@
|
||||
#include <Tactility/lvgl/Keyboard.h>
|
||||
#include <Tactility/service/gui/GuiService.h>
|
||||
#include <Tactility/TactilityConfig.h>
|
||||
#include <Tactility/service/espnow/EspNowService.h>
|
||||
|
||||
#include <tactility/check.h>
|
||||
|
||||
#include <lvgl/lvgl.h>
|
||||
|
||||
namespace tt::service::gui {
|
||||
|
||||
static void show_keyboard(lv_event_t* event) {
|
||||
auto service = findService();
|
||||
if (service != nullptr) {
|
||||
lv_obj_t* target = lv_event_get_current_target_obj(event);
|
||||
service->softwareKeyboardShow(target);
|
||||
lv_obj_scroll_to_view(target, LV_ANIM_ON);
|
||||
}
|
||||
}
|
||||
|
||||
static void hide_keyboard(lv_event_t* event) {
|
||||
auto service = findService();
|
||||
if (service != nullptr) {
|
||||
service->softwareKeyboardHide();
|
||||
}
|
||||
}
|
||||
|
||||
bool GuiService::softwareKeyboardIsEnabled() {
|
||||
return !lvgl::hardware_keyboard_is_available() || TT_CONFIG_FORCE_ONSCREEN_KEYBOARD;
|
||||
}
|
||||
|
||||
void GuiService::softwareKeyboardShow(lv_obj_t* textarea) {
|
||||
lock();
|
||||
|
||||
if (isStarted && keyboard != nullptr) {
|
||||
lv_obj_clear_flag(keyboard, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_keyboard_set_textarea(keyboard, textarea);
|
||||
}
|
||||
|
||||
unlock();
|
||||
}
|
||||
|
||||
void GuiService::softwareKeyboardHide() {
|
||||
lock();
|
||||
|
||||
if (isStarted && keyboard != nullptr) {
|
||||
lv_obj_add_flag(keyboard, LV_OBJ_FLAG_HIDDEN);
|
||||
}
|
||||
|
||||
unlock();
|
||||
}
|
||||
|
||||
void GuiService::keyboardAddTextArea(lv_obj_t* textarea) {
|
||||
lock();
|
||||
|
||||
if (isStarted) {
|
||||
check(lvgl_try_lock(0), "lvgl should already be locked before calling this method");
|
||||
|
||||
if (softwareKeyboardIsEnabled()) {
|
||||
lv_obj_add_event_cb(textarea, show_keyboard, LV_EVENT_FOCUSED, nullptr);
|
||||
lv_obj_add_event_cb(textarea, hide_keyboard, LV_EVENT_DEFOCUSED, nullptr);
|
||||
lv_obj_add_event_cb(textarea, hide_keyboard, LV_EVENT_READY, nullptr);
|
||||
|
||||
// lv_obj_t auto-remove themselves from the group when they are destroyed (last checked in LVGL 8.3)
|
||||
lv_group_add_obj(keyboardGroup, textarea);
|
||||
|
||||
lvgl::software_keyboard_activate(keyboardGroup);
|
||||
}
|
||||
|
||||
lvgl_unlock();
|
||||
}
|
||||
|
||||
unlock();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -9,10 +9,10 @@
|
||||
#include <Tactility/service/ServiceRegistration.h>
|
||||
#include <Tactility/settings/KeyboardSettings.h>
|
||||
|
||||
#include <lvgl/lvgl.h>
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/drivers/backlight.h>
|
||||
#include <tactility/drivers/keyboard.h>
|
||||
#include <lvgl/lvgl.h>
|
||||
|
||||
namespace tt::service::keyboardidle {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user