summaryrefslogtreecommitdiff
path: root/src/tangara/input/lvgl_input_driver.hpp
blob: ecb3a36d43ec8b829cf8d7a56e043ad742b92d2f (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
/*
 * Copyright 2023 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#pragma once

#include <cstdint>
#include <deque>
#include <memory>
#include <set>

#include "core/lv_group.h"
#include "indev/lv_indev.h"

#include "drivers/gpios.hpp"
#include "drivers/nvs.hpp"
#include "drivers/touchwheel.hpp"
#include "input/device_factory.hpp"
#include "input/feedback_device.hpp"
#include "input/input_device.hpp"
#include "input/input_hook.hpp"
#include "lua/lua_thread.hpp"
#include "lua/property.hpp"

namespace input {

/*
 * Implementation of an LVGL input device. This class composes multiple
 * IInputDevice and IFeedbackDevice instances together into a single LVGL
 * device.
 */
class LvglInputDriver {
 public:
  LvglInputDriver(drivers::NvsStorage& nvs, DeviceFactory&);

  auto wheelMode() -> lua::Property& { return wheel_mode_; }
  auto buttonMode() -> lua::Property& { return button_mode_; }
  auto lockedMode() -> lua::Property& { return locked_mode_; }
  auto hapticsMode() -> lua::Property& { return haptics_mode_; }

  auto setGroup(lv_group_t*) -> void;
  auto read(lv_indev_data_t* data) -> void;
  auto feedback(uint8_t) -> void;
  auto lock(bool l) -> void;

  auto pushHooks(lua_State* L) -> int;

 private:
  drivers::NvsStorage& nvs_;
  DeviceFactory& factory_;

  lua::Property wheel_mode_;
  lua::Property button_mode_;
  lua::Property locked_mode_;
  lua::Property haptics_mode_;
  lv_indev_t* device_;

  std::vector<std::shared_ptr<IInputDevice>> inputs_;
  std::vector<std::shared_ptr<IFeedbackDevice>> feedbacks_;

  /*
   * Key for identifying which device, trigger, and specific hook are being
   * overriden by Lua.
   */
  struct OverrideSelector {
    std::string device_name;
    std::string trigger_name;
    std::string hook_name;

    friend bool operator<(const OverrideSelector& l,
                          const OverrideSelector& r) {
      return std::tie(l.device_name, l.trigger_name, l.hook_name) <
             std::tie(r.device_name, r.trigger_name, r.hook_name);
    }
  };
  class LuaTrigger {
   public:
    LuaTrigger(LvglInputDriver&, IInputDevice&, TriggerHooks&);

    static auto get(lua_State*, int idx) -> LuaTrigger&;
    static auto luaGc(lua_State*) -> int;
    static auto luaToString(lua_State*) -> int;
    static auto luaNewIndex(lua_State*) -> int;

    static constexpr struct luaL_Reg kFuncs[] = {{"__gc", luaGc},
                                                 {"__tostring", luaToString},
                                                 {"__newindex", luaNewIndex},
                                                 {NULL, NULL}};

   private:
    LvglInputDriver* driver_;

    std::string device_;
    std::string trigger_;
    std::map<std::string, std::string> hooks_;
  };
  struct LuaOverride {
    lua_State* L;
    int ref;
  };

  /* Userdata object for tracking the Lua mirror of a TriggerHooks object. */

  /* A hook override implemented as a lua callback */

  std::map<OverrideSelector, LuaOverride> overrides_;

  auto setOverride(lua_State* L, const OverrideSelector&) -> void;
  auto applyOverride(const OverrideSelector&, LuaOverride&) -> void;

  bool is_locked_;
};

}  // namespace input