File: lv2_ui.cpp

package info (click to toggle)
easyeffects 8.0.8%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,752 kB
  • sloc: cpp: 22,118; sh: 691; python: 448; javascript: 64; makefile: 8
file content (291 lines) | stat: -rw-r--r-- 8,918 bytes parent folder | download
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
 * Copyright © 2017-2025 Wellington Wallace
 *
 * This file is part of Easy Effects.
 *
 * Easy Effects is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Easy Effects is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Easy Effects. If not, see <https://www.gnu.org/licenses/>.
 */

#include "lv2_ui.hpp"
#include <dlfcn.h>
#include <lv2/atom/atom.h>
#include <lv2/buf-size/buf-size.h>
#include <lv2/core/lv2.h>
#include <lv2/data-access/data-access.h>
#include <lv2/instance-access/instance-access.h>
#include <lv2/lv2plug.in/ns/ext/log/log.h>
#include <lv2/options/options.h>
#include <lv2/parameters/parameters.h>
#include <lv2/ui/ui.h>
#include <lv2/urid/urid.h>
#include <qtypes.h>
#include <array>
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <format>
#include <mutex>
#include <string>
#include "lilv/lilv.h"
#include "lv2_wrapper.hpp"
#include "util.hpp"

namespace lv2 {

NativeUi::NativeUi(Lv2Wrapper* wrapper) : wrapper(wrapper) {
  lv2_log = {.handle = wrapper,
             .printf = &Lv2Wrapper::lv2_printf,
             .vprintf = []([[maybe_unused]] LV2_Log_Handle handle, [[maybe_unused]] LV2_URID type, const char* fmt,
                           va_list ap) { return std::vprintf(fmt, ap); }};

  lv2_map = {.handle = wrapper, .map = [](LV2_URID_Map_Handle handle, const char* uri) {
               auto* lw = static_cast<Lv2Wrapper*>(handle);

               return lw->map_urid(uri);
             }};

  lv2_unmap = {.handle = wrapper, .unmap = [](LV2_URID_Unmap_Handle handle, LV2_URID urid) {
                 auto* lw = static_cast<Lv2Wrapper*>(handle);

                 return lw->map_urid_to_uri[urid].c_str();
               }};
}

NativeUi::~NativeUi() {
  close();
}

void NativeUi::load() {
  std::scoped_lock<std::mutex> lku(ui_mutex);

  if (!wrapper->has_instance()) {
    return;
  }

  LilvUIs* uis = lilv_plugin_get_uis(wrapper->get_lilv_plugin());

  if (!uis) {
    return;
  }

  LILV_FOREACH(uis, u, uis) {
    const LilvUI* ui = lilv_uis_get(uis, u);

    std::string ui_uri = lilv_node_as_uri(lilv_ui_get_uri(ui));

    util::debug(std::format("{} UI uri: {}", wrapper->get_plugin_uri(), ui_uri));

    const LilvNode* binary_node = lilv_ui_get_binary_uri(ui);
    const LilvNode* bundle_node = lilv_ui_get_bundle_uri(ui);

    auto path = lilv_file_uri_parse(lilv_node_as_uri(binary_node), nullptr);

    libhandle = dlopen(path, RTLD_NOW);

    lilv_free(path);

    if (!libhandle) {
      continue;
    }

    auto descfn = reinterpret_cast<LV2UI_DescriptorFunction>(dlsym(libhandle, "lv2ui_descriptor"));

    if (!descfn) {
      dlclose(libhandle);
      libhandle = nullptr;
      continue;
    }

    uint32_t index = 0U;

    while ((ui_descriptor = descfn(index++)) != nullptr) {
      if (ui_descriptor->URI != ui_uri) {
        continue;
      }

      idle_iface = static_cast<const LV2UI_Idle_Interface*>(ui_descriptor->extension_data(LV2_UI__idleInterface));
      show_iface = static_cast<const LV2UI_Show_Interface*>(ui_descriptor->extension_data(LV2_UI__showInterface));

      if (!idle_iface) {
        continue;
      }

      const LV2_Feature lv2_log_feature = {LV2_LOG__log, &lv2_log};

      const LV2_Feature lv2_map_feature = {LV2_URID__map, &lv2_map};

      const LV2_Feature lv2_unmap_feature = {LV2_URID__unmap, &lv2_unmap};

      const auto rate = wrapper->get_rate();
      const auto n_samples = wrapper->get_n_samples();

      auto options = std::to_array<LV2_Options_Option>(
          {{.context = LV2_OPTIONS_INSTANCE,
            .subject = 0,
            .key = wrapper->map_urid(LV2_PARAMETERS__sampleRate),
            .size = sizeof(float),
            .type = wrapper->map_urid(LV2_ATOM__Float),
            .value = &rate},
           {.context = LV2_OPTIONS_INSTANCE,
            .subject = 0,
            .key = wrapper->map_urid(LV2_BUF_SIZE__minBlockLength),
            .size = sizeof(int32_t),
            .type = wrapper->map_urid(LV2_ATOM__Int),
            .value = &lv2::Lv2Wrapper::min_quantum},
           {.context = LV2_OPTIONS_INSTANCE,
            .subject = 0,
            .key = wrapper->map_urid(LV2_BUF_SIZE__maxBlockLength),
            .size = sizeof(int32_t),
            .type = wrapper->map_urid(LV2_ATOM__Int),
            .value = &lv2::Lv2Wrapper::max_quantum},
           {.context = LV2_OPTIONS_INSTANCE,
            .subject = 0,
            .key = wrapper->map_urid(LV2_BUF_SIZE__nominalBlockLength),
            .size = sizeof(int32_t),
            .type = wrapper->map_urid(LV2_ATOM__Int),
            .value = &n_samples},
           {.context = LV2_OPTIONS_INSTANCE, .subject = 0, .key = 0, .size = 0, .type = 0, .value = nullptr}});

      LV2_Feature feature_options = {.URI = LV2_OPTIONS__options, .data = options.data()};

      LV2_Extension_Data_Feature extension_data = {wrapper->get_instance()->lv2_descriptor->extension_data};

      const LV2_Feature feature_dataAccess = {LV2_DATA_ACCESS_URI, &extension_data};

      const LV2_Feature feature_instAccess = {LV2_INSTANCE_ACCESS_URI, wrapper->get_instance()->lv2_handle};

      const LV2_Feature idle_feature = {LV2_UI__idleInterface, nullptr};

      const LV2_Feature parent_feature = {LV2_UI__parent, nullptr};

      const LV2_Feature fixed_size_feature = {LV2_UI__fixedSize, nullptr};

      const LV2_Feature no_user_resize_feature = {LV2_UI__noUserResize, nullptr};

      const LV2_Feature make_resident_feature = {LV2_UI_makeResident, nullptr};

      const LV2_Feature make_soname_resident_feature = {LV2_UI_makeSONameResident, nullptr};

      const auto features = std::to_array<const LV2_Feature*>(
          {&lv2_log_feature, &lv2_map_feature, &lv2_unmap_feature, &feature_options, wrapper->static_features.data(),
           &feature_dataAccess, &feature_instAccess, &idle_feature, &parent_feature, &fixed_size_feature,
           &no_user_resize_feature, &make_resident_feature, &make_soname_resident_feature, nullptr});

      LV2UI_Widget widget = nullptr;

      auto bundle_path = lilv_file_uri_parse(lilv_node_as_uri(bundle_node), nullptr);

      ui_handle = ui_descriptor->instantiate(
          ui_descriptor, wrapper->get_plugin_uri().c_str(), bundle_path,
          +[](LV2UI_Controller controller, uint32_t port_index, uint32_t, uint32_t port_protocol, const void* buffer) {
            auto wrapper = static_cast<Lv2Wrapper*>(controller);

            for (auto& p : wrapper->ports) {
              if (p.index == port_index && port_protocol == 0) {
                p.value = *static_cast<const float*>(buffer);
              }
            }
          },
          wrapper, &widget, features.data());

      lilv_free(bundle_path);

      if (ui_handle && show_iface && show_iface->show(ui_handle) != 0) {
        util::warning(std::format("Failed to show UI for {}", ui_uri));
      }

      break;
    }
  }

  lilv_uis_free(uis);

  // Initialize UI with current control values
  if (ui_descriptor && ui_handle) {
    for (const auto& p : wrapper->ports) {
      if (p.type == PortType::TYPE_CONTROL) {
        ui_descriptor->port_event(ui_handle, p.index, sizeof(float), 0, &p.value);
      }
    }
  }
}

void NativeUi::notify() {
  if (!ui_descriptor || !ui_handle || !ui_descriptor->port_event) {
    return;
  }

  std::scoped_lock<std::mutex> lk(ui_mutex);

  for (const auto& p : wrapper->ports) {
    if (p.type == PortType::TYPE_CONTROL && !p.is_input) {
      ui_descriptor->port_event(ui_handle, p.index, sizeof(float), 0, &p.value);
    }
  }
}

void NativeUi::update() {
  if (!idle_iface || !ui_handle) {
    return;
  }

  std::scoped_lock<std::mutex> lk(ui_mutex);

  idle_iface->idle(ui_handle);
}

void NativeUi::close() {
  std::scoped_lock<std::mutex> lk(ui_mutex);

  if (ui_descriptor && ui_handle) {
    if (show_iface) {
      show_iface->hide(ui_handle);
    }

    ui_descriptor->cleanup(ui_handle);
  }

  if (libhandle) {
    dlclose(libhandle);
  }

  ui_handle = nullptr;
  ui_descriptor = nullptr;
  libhandle = nullptr;
  show_iface = nullptr;
}

void NativeUi::port_event(uint port_index, float value) {
  if (!ui_descriptor || !ui_handle) {
    return;
  }

  ui_descriptor->port_event(ui_handle, port_index, sizeof(float), 0, &value);
}

void NativeUi::sync_to_database() {
  if (!ui_descriptor || !ui_handle) {
    return;
  }

  for (const auto& func : wrapper->sync_funcs) {
    func();
  }
}

bool NativeUi::has_ui() const {
  return ui_handle != nullptr;
}

}  // namespace lv2