summaryrefslogtreecommitdiff
path: root/src/lua/include/property.hpp
blob: 03229bc17bbc226844b2925e0964c02c8075ea8a (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
/*
 * Copyright 2023 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#pragma once

#include <stdint.h>
#include <memory>
#include <string>

#include "audio_events.hpp"
#include "bluetooth_types.hpp"
#include "lua.hpp"
#include "lvgl.h"
#include "service_locator.hpp"

namespace lua {

// FIXME: We should use some kind of interface for this instead.
using LuaValue = std::variant<std::monostate,
                              int,
                              bool,
                              std::string,
                              audio::Track,
                              drivers::bluetooth::Device,
                              std::vector<drivers::bluetooth::Device>>;

using LuaFunction = std::function<int(lua_State*)>;

class Property {
 public:
  Property() : Property(std::monostate{}) {}
  Property(const LuaValue&);
  Property(const LuaValue&, std::function<bool(const LuaValue&)>);

  auto Get() -> const LuaValue& { return *value_; }

  auto IsTwoWay() -> bool { return cb_.has_value(); }

  auto PushValue(lua_State& s) -> int;
  auto PopValue(lua_State& s) -> bool;
  auto Update(const LuaValue& new_val) -> void;

  auto AddLuaBinding(lua_State*, int ref) -> void;

 private:
  std::unique_ptr<LuaValue> value_;
  std::optional<std::function<bool(const LuaValue&)>> cb_;
  std::pmr::vector<std::pair<lua_State*, int>> bindings_;
};

class PropertyBindings {
 public:
  PropertyBindings(lua_State&);

  auto Register(lua_State*, Property*) -> void;
  auto Register(lua_State*, LuaFunction) -> void;

  auto GetFunction(size_t i) -> const LuaFunction&;

 private:
  std::pmr::vector<LuaFunction> functions_;
};

}  // namespace lua