summaryrefslogtreecommitdiff
path: root/src/input/lvgl_input_driver.cpp
blob: 61a85fa5ac277de0440bb40c216032605eff9697 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * Copyright 2023 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#include "lvgl_input_driver.hpp"
#include <stdint.h>

#include <cstdint>
#include <memory>
#include <variant>

#include "device_factory.hpp"
#include "feedback_haptics.hpp"
#include "input_touch_wheel.hpp"
#include "input_trigger.hpp"
#include "input_volume_buttons.hpp"
#include "lauxlib.h"
#include "lua.h"
#include "lvgl.h"
#include "nvs.hpp"
#include "property.hpp"

[[maybe_unused]] static constexpr char kTag[] = "input";

namespace input {

static void read_cb(lv_indev_drv_t* drv, lv_indev_data_t* data) {
  LvglInputDriver* instance =
      reinterpret_cast<LvglInputDriver*>(drv->user_data);
  instance->read(data);
}

static void feedback_cb(lv_indev_drv_t* drv, uint8_t event) {
  LvglInputDriver* instance =
      reinterpret_cast<LvglInputDriver*>(drv->user_data);
  instance->feedback(event);
}

auto intToMode(int raw) -> std::optional<drivers::NvsStorage::InputModes> {
  switch (raw) {
    case 0:
      return drivers::NvsStorage::InputModes::kButtonsOnly;
    case 1:
      return drivers::NvsStorage::InputModes::kButtonsWithWheel;
    case 2:
      return drivers::NvsStorage::InputModes::kDirectionalWheel;
    case 3:
      return drivers::NvsStorage::InputModes::kRotatingWheel;
    default:
      return {};
  }
}

LvglInputDriver::LvglInputDriver(drivers::NvsStorage& nvs,
                                 DeviceFactory& factory)
    : nvs_(nvs),
      factory_(factory),
      mode_(static_cast<int>(nvs.PrimaryInput()),
            [&](const lua::LuaValue& val) {
              if (!std::holds_alternative<int>(val)) {
                return false;
              }
              auto mode = intToMode(std::get<int>(val));
              if (!mode) {
                return false;
              }
              nvs.PrimaryInput(*mode);
              inputs_ = factory.createInputs(*mode);
              return true;
            }),
      driver_(),
      registration_(nullptr),
      inputs_(factory.createInputs(nvs.PrimaryInput())),
      feedbacks_(factory.createFeedbacks()),
      is_locked_(false) {
  lv_indev_drv_init(&driver_);
  driver_.type = LV_INDEV_TYPE_ENCODER;
  driver_.read_cb = read_cb;
  driver_.feedback_cb = feedback_cb;
  driver_.user_data = this;
  driver_.long_press_time = kLongPressDelayMs;
  driver_.long_press_repeat_time = kRepeatDelayMs;

  registration_ = lv_indev_drv_register(&driver_);
}

auto LvglInputDriver::read(lv_indev_data_t* data) -> void {
  // TODO: we should pass lock state on to the individual devices, since they
  // may wish to either ignore the lock state, or power down until unlock.
  if (is_locked_) {
    return;
  }
  for (auto&& device : inputs_) {
    device->read(data);
  }
}

auto LvglInputDriver::feedback(uint8_t event) -> void {
  if (is_locked_) {
    return;
  }
  for (auto&& device : feedbacks_) {
    device->feedback(event);
  }
}

auto LvglInputDriver::pushHooks(lua_State* L) -> int {
  lua_newtable(L);

  for (auto& dev : inputs_) {
    lua_pushlstring(L, dev->name().data(), dev->name().size());
    lua_newtable(L);

    for (auto& hook : dev->hooks()) {
      lua_pushlstring(L, hook.name().data(), hook.name().size());
      hook.pushHooks(L);
      lua_rawset(L, -3);
    }

    lua_rawset(L, -3);
  }
  return 1;
}

}  // namespace input