• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ossia / score / 33834678478

04 Sep 2026 03:50AM UTC coverage: 24.333% (+0.3%) from 23.997%
33834678478

push

github

jcelerier
tests: the editor-look cases assumed a Windows desktop

Three of them failed under the coverage job's Xvfb display.

Redo is Ctrl+Y only on Windows -- qplatformtheme.cpp binds that one under
KB_Win, and X11 and macOS use Ctrl+Shift+Z -- so the redo half of the hex
undo case tested nothing there. Ask QKeySequence instead.

The other two turn on an editor committing when the focus leaves it, and
QWidget::setFocus only routes through setFocusWidget in an active window.
There is no window manager on that display, so the view was shown but never
activated and no focus event was ever sent: the value simply stayed put.
The fixture now activates the window and waits for it, and the cases that
depend on it say so -- one of them was passing on an empty check.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014L61CWsHUpxm7opHyZbsKu

55424 of 227776 relevant lines covered (24.33%)

63160.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.2
/src/plugins/score-lib-device/Device/Protocol/DeviceInterface.cpp
1
// This is an open source non-commercial project. Dear PVS-Studio, please check
2
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
3
#include "DeviceInterface.hpp"
4

5
#include <Device/Address/AddressSettings.hpp>
6
#include <Device/Node/DeviceNode.hpp>
7

8
#include <score/application/ApplicationContext.hpp>
9
#include <score/tools/std/String.hpp>
10

11
#include <core/application/ApplicationSettings.hpp>
12

13
#include <ossia/detail/logger.hpp>
14
#include <ossia/network/base/device.hpp>
15
#include <ossia/network/base/parameter.hpp>
16
#include <ossia/network/base/protocol.hpp>
17
#include <ossia/network/common/network_logger.hpp>
18
#include <ossia/network/context.hpp>
19
#include <ossia/network/domain/domain.hpp>
20

21
#include <ossia-qt/invoke.hpp>
22
#include <ossia-qt/name_utils.hpp>
23

24
#include <boost/asio/io_context.hpp>
25
#include <boost/asio/post.hpp>
26
#include <boost/asio/strand.hpp>
27
#include <boost/asio/use_future.hpp>
28

29
#include <QCoreApplication>
30
#include <QMenu>
31

32
#include <spdlog/sinks/sink.h>
33

34
#include <wobjectimpl.h>
35
W_OBJECT_IMPL(Device::DeviceInterface)
3,269 ✔
36
namespace Device
37
{
38
struct DeviceSettings;
39

40
static void ToAddress_rec(State::Address& addr, const ossia::net::node_base* cur, int N)
175,654 ✔
41
{
42
  if(auto padre = cur->get_parent())
175,654 ✔
43
  {
44
    ToAddress_rec(addr, padre, N + 1);
136,856 ✔
45
    addr.path.push_back(QString::fromStdString(cur->get_name()));
136,856 ✔
46
  }
136,856 ✔
47
  else
48
  {
49
    // The last node is the root node "/", which by convention
50
    // has the same name than the device
51
    addr.path.reserve(N);
38,798 ✔
52
    addr.device = QString::fromStdString(cur->get_name());
38,798 ✔
53
  }
54
}
175,654 ✔
55

56
static State::Address ToAddress(const ossia::net::node_base& node)
38,798 ✔
57
{
58
  State::Address addr;
38,798 ✔
59
  const ossia::net::node_base* cur = &node;
38,798 ✔
60

61
  ToAddress_rec(addr, cur, 1);
38,798 ✔
62
  return addr;
38,798 ✔
63
}
38,798 ✔
64

65
ossia::net::node_base*
66
findNodeFromPath(const QStringList& path, ossia::net::device_base& dev)
5,540 ✔
67
{
68
  using namespace ossia;
69
  // Find the relevant node to add in the device
70
  ossia::net::node_base* node = &dev.get_root_node();
5,540 ✔
71
  for(int i = 0; i < path.size(); i++)
21,569 ✔
72
  {
73
    auto cld = node->find_child(path[i]);
16,391 ✔
74
    if(cld)
16,391 ✔
75
      node = cld;
16,029 ✔
76
    else
77
    {
78
      qDebug() << "looking for" << path << " -- last found: " << node->get_name().c_str()
724 ✔
79
               << "\n";
362 ✔
80
      return {};
362 ✔
81
    }
82
  }
16,029 ✔
83

84
  return node;
5,178 ✔
85
}
5,540 ✔
86

87
using small_node_vec = ossia::small_vector<const Device::Node*, 16>;
88
static void getPath(small_node_vec& v, const Device::Node* cur)
9 ✔
89
{
90
  auto p = cur->parent();
9 ✔
91
  if(p && !p->is<Device::DeviceSettings>())
9 ✔
92
  {
93
    getPath(v, p);
×
94
  }
×
95
  v.push_back(cur);
9 ✔
96
}
9 ✔
97

98
ossia::net::node_base*
99
findNodeFromPath(const Device::Node& path, ossia::net::device_base& dev)
9 ✔
100
{
101
  using namespace ossia;
102
  // Find the relevant node to add in the device
103
  ossia::net::node_base* node = &dev.get_root_node();
9 ✔
104
  if(!path.is<Device::DeviceSettings>())
9 ✔
105
  {
106
    // First fill the vector of nodes
107
    ossia::small_vector<const Device::Node*, 16> vec;
9 ✔
108
    getPath(vec, &path);
9 ✔
109

110
    for(std::size_t i = 0; i < vec.size(); i++)
18 ✔
111
    {
112
      auto cld = node->find_child(vec[i]->displayName());
9 ✔
113
      if(cld)
9 ✔
114
        node = cld;
9 ✔
115
      else
116
      {
117
        qDebug() << "looking for" << Device::address(path).address << " " << i << " "
×
118
                 << vec.size() << " " << vec[i]->displayName()
×
119
                 << " -- last found: " << node->get_name().c_str() << "\n";
×
120
        return {};
×
121
      }
122
    }
9 ✔
123
    return node;
9 ✔
124
  }
9 ✔
125
  else
126
  {
127
    return &dev.get_root_node();
×
128
  }
129
}
9 ✔
130

131
static Device::AddressSettings ToAddressSettings(const ossia::net::node_base& node)
15,155 ✔
132
{
133
  Device::AddressSettings s;
15,155 ✔
134
  const auto& addr = node.get_parameter();
15,155 ✔
135

136
  if(addr)
15,155 ✔
137
  {
138
    //addr->request_value();
139

140
    s.name = QString::fromStdString(node.get_name());
15,033 ✔
141

142
    if(hasEditableValue(addr->get_type()))
15,033 ✔
143
    {
144
      s.ioType = ossia::access_mode::BI; // addr->get_access();
15,032 ✔
145
      s.clipMode = addr->get_bounding();
15,032 ✔
146
      s.repetitionFilter = addr->get_repetition_filter();
15,032 ✔
147
      s.unit = addr->get_unit();
15,032 ✔
148
      s.extendedAttributes = node.get_extended_attributes();
15,032 ✔
149
      s.domain = addr->get_domain();
15,032 ✔
150

151
      try
152
      {
153
        s.value = addr->value();
15,032 ✔
154
      }
15,032 ✔
155
      catch(...)
156
      {
157
        s.value = ossia::init_value(addr->get_value_type());
×
158
      }
×
159

160
      if(addr->get_type() == ossia::parameter_type::AUDIO && !s.domain.get())
15,032 ✔
161
        s.domain = ossia::make_domain(0.f, 1.f);
2 ✔
162
    }
15,032 ✔
163
  }
15,033 ✔
164
  else
165
  {
166
    s.name = QString::fromStdString(node.get_name());
122 ✔
167
  }
168
  return s;
15,155 ✔
169
}
15,155 ✔
170

171
static void updateOSSIAAddress(
65 ✔
172
    const Device::FullAddressSettings& settings, ossia::net::parameter_base& addr)
173
{
174
  SCORE_ASSERT(settings.ioType);
65 ✔
175
  addr.set_access(*settings.ioType);
65 ✔
176

177
  addr.set_bounding(settings.clipMode);
65 ✔
178

179
  addr.set_repetition_filter(ossia::repetition_filter(settings.repetitionFilter));
65 ✔
180

181
  addr.set_value_type(settings.value.get_type());
65 ✔
182

183
  addr.set_value(settings.value);
65 ✔
184

185
  addr.set_domain(settings.domain);
65 ✔
186

187
  addr.set_unit(settings.unit);
65 ✔
188

189
  addr.get_node().set_extended_attributes(settings.extendedAttributes);
65 ✔
190
}
65 ✔
191

192
static void createOSSIAAddress(
65 ✔
193
    const Device::FullAddressSettings& settings, ossia::net::node_base& node)
194
{
195
  if(!settings.value.v)
65 ✔
196
    return;
×
197

198
  auto addr = node.create_parameter(settings.value.get_type());
65 ✔
199
  if(addr)
65 ✔
200
    updateOSSIAAddress(settings, *addr);
65 ✔
201
}
65 ✔
202

203
Device::Node ToDeviceExplorer(const ossia::net::node_base& ossia_node)
129 ✔
204
{
205
  Device::Node score_node{ToAddressSettings(ossia_node), nullptr};
129 ✔
206
  {
207
    const auto& cld = ossia_node.children();
129 ✔
208
    score_node.reserve(cld.size());
129 ✔
209

210
    // 2. Recurse on the children
211
    for(const auto& ossia_child : cld)
135 ✔
212
    {
213
      if(!ossia::net::get_hidden(*ossia_child) && !ossia::net::get_zombie(*ossia_child))
6 ✔
214
      {
215
        auto child_n = ToDeviceExplorer(*ossia_child);
6 ✔
216
        child_n.setParent(&score_node);
6 ✔
217
        score_node.push_back(std::move(child_n));
6 ✔
218
      }
6 ✔
219
    }
220
  }
129 ✔
221
  return score_node;
129 ✔
222
}
129 ✔
223

224
ossia::net::node_base*
225
createNodeFromPath(const QStringList& path, ossia::net::device_base& dev)
65 ✔
226
{
227
  using namespace ossia;
228
  // Find the relevant node to add in the device
229
  ossia::net::node_base* node = &dev.get_root_node();
65 ✔
230
  for(int i = 0; i < path.size(); i++)
94 ✔
231
  {
232
    auto cld = node->find_child(path[i]);
65 ✔
233
    if(!cld)
65 ✔
234
    {
235
      // We have to start adding sub-nodes from here.
236
      ossia::net::node_base* parentnode = node;
36 ✔
237
      for(int k = i; k < path.size(); k++)
72 ✔
238
      {
239
        auto path_k = path[k].toStdString();
36 ✔
240
        auto newNode = parentnode->create_child(path_k);
36 ✔
241
        SCORE_ASSERT(newNode);
36 ✔
242
        if(path_k != newNode->get_name())
36 ✔
243
        {
244
          qDebug() << "Warning! " << path[k]
×
245
                   << " was requested as node name but backend converted to  "
×
246
                   << newNode->get_name().c_str()
×
247
                   << "was obtained. Check for special characters.";
×
248
          parentnode->remove_child(*newNode);
×
249
          return nullptr;
×
250
          /*
251
          for (const auto& node : parentnode->children())
252
          {
253
            qDebug() << node->get_name().c_str();
254
          }*/
255
        }
256
        if(k == path.size() - 1)
36 ✔
257
        {
258
          node = newNode;
36 ✔
259
        }
36 ✔
260
        else
261
        {
262
          parentnode = newNode;
×
263
        }
264
      }
36 ✔
265

266
      break;
36 ✔
267
    }
268
    else
269
    {
270
      node = cld;
29 ✔
271
    }
272
  }
29 ✔
273

274
  return node;
65 ✔
275
}
65 ✔
276

277
bool DeviceInterface::connected() const
3,166 ✔
278
{
279
  return bool(getDevice());
3,166 ✔
280
}
281

282
void DeviceInterface::addAddress(const FullAddressSettings& settings)
313 ✔
283
{
284
  using namespace ossia;
285
  if(!m_capas.canAddNode)
313 ✔
286
  {
287
    return; // TODO return bool instead, and check in the node update proxy ?
×
288
  }
289

290
  if(auto dev = getDevice())
313 ✔
291
  {
292
    // Create the node. It is added into the device.
293
    ossia::net::node_base* node = createNodeFromPath(settings.address.path, *dev);
65 ✔
294
    if(node)
65 ✔
295
    {
296
      // Populate the node with a parameter (if it isn't a no_value_t).
297
      createOSSIAAddress(settings, *node);
65 ✔
298
    }
65 ✔
299
  }
65 ✔
300
}
313 ✔
301

302
bool DeviceInterface::isLearning() const
×
303
{
304
  return false;
×
305
}
306

307
void DeviceInterface::setLearning(bool) { }
×
308

309
QMimeData* DeviceInterface::mimeData() const
×
310
{
311
  return nullptr;
×
312
}
313

314
void DeviceInterface::setupContextMenu(QMenu&) const { }
×
315

316
DeviceInterface::DeviceInterface(Device::DeviceSettings s)
1,812 ✔
317
    : m_settings(std::move(s))
453 ✔
318
{
906 ✔
319
}
×
320

321
DeviceInterface::~DeviceInterface() { }
453 ✔
322

323
const Device::DeviceSettings& DeviceInterface::settings() const noexcept
21,326 ✔
324
{
325
  return m_settings;
21,326 ✔
326
}
327

328
const QString& DeviceInterface::name() const noexcept
14,013 ✔
329
{
330
  return settings().name;
14,013 ✔
331
}
332

333
void DeviceInterface::addNode(const Device::Node& n)
313 ✔
334
{
335
  auto full = Device::FullAddressSettings::make<Device::FullAddressSettings::as_parent>(
313 ✔
336
      n.get<Device::AddressSettings>(), Device::address(*n.parent()));
313 ✔
337

338
  // Add in the device implementation
339
  addAddress(full);
313 ✔
340

341
  for(const auto& child : n)
313 ✔
342
  {
343
    addNode(child);
×
344
  }
345
}
313 ✔
346

347
DeviceCapas DeviceInterface::capabilities() const noexcept
4,977 ✔
348
{
349
  return m_capas;
4,977 ✔
350
}
351

352
DeviceResources DeviceInterface::usedResources() const noexcept
×
353
{
354
  return {};
×
355
}
356

357
void DeviceInterface::disconnect()
286 ✔
358
{
359
  // Whatever the device advertises, the callbacks enableCallbacks() installed
360
  // must go before the nodes do: clear_children() below would otherwise report
361
  // every node as removed and the explorer would drop the device's tree. This
362
  // also resets the flag, so that the device rebuilt by reconnect() gets its
363
  // callbacks back.
364
  disableCallbacks();
286 ✔
365

366
  m_callbacks.clear();
286 ✔
367
  if(auto dev = getDevice())
286 ✔
368
  {
369
    auto& root = dev->get_root_node();
46 ✔
370
    root.clear_children();
46 ✔
371
  }
46 ✔
372
}
286 ✔
373

374
void DeviceInterface::recreate(const Device::Node&) { }
×
375

376
void DeviceInterface::updateSettings(const Device::DeviceSettings& newsettings)
26 ✔
377
{
378
  // TODO we have to maintain the prior connection state
379
  // if we were disconnected, we stay disconnected
380
  // else we reconnect. See in Minuit / MIDI also.
381
  if(auto dev = getDevice())
26 ✔
382
  {
383
    // First we save the existing nodes.
384
    Device::Node score_device{settings(), nullptr};
26 ✔
385

386
    // Recurse on the children
387
    {
388
      const auto& ossia_children = dev->get_root_node().children();
26 ✔
389
      score_device.reserve(ossia_children.size());
26 ✔
390
      for(const auto& node : ossia_children)
85 ✔
391
      {
392
        score_device.push_back(ToDeviceExplorer(*node.get()));
59 ✔
393
      }
394
    }
26 ✔
395

396
    // We change the settings safely
397
    disconnect();
26 ✔
398

399
    m_settings = newsettings;
26 ✔
400

401
    auto con_handle = std::make_shared<QMetaObject::Connection>();
26 ✔
402
    *con_handle = connect(
26 ✔
403
        this, &Device::DeviceInterface::deviceChanged, this,
404
        [this, con_handle, data = std::move(score_device)](auto oldd, auto newd) {
77 ✔
405
      if(newd)
51 ✔
406
      {
407
        recreate(std::move(data));
25 ✔
408
        // The new ossia nodes carry no callbacks: listen again to what the
409
        // explorer shows.
410
        restoreListening();
25 ✔
411
        QObject::disconnect(*con_handle);
25 ✔
412
      }
25 ✔
413
        });
51 ✔
414

415
    reconnect();
26 ✔
416
  }
26 ✔
417
  else
418
  {
419
    // We're already disconnected
420
    m_settings = newsettings;
×
421
  }
422
}
26 ✔
423

424
void DeviceInterface::updateAddress(
10 ✔
425
    const State::Address& currentAddr, const Device::FullAddressSettings& settings)
426
{
427
  if(auto dev = getDevice())
10 ✔
428
  {
429
    if(auto node = findNodeFromPath(currentAddr.path, *dev))
×
430
    {
431
      bool is_listening = m_callbacks.find(currentAddr) != m_callbacks.end();
×
432

433
      if(!settings.value.valid())
×
434
      {
435
        if(is_listening)
×
436
        {
437
          // Remove callbacks
438
          auto it = m_callbacks.find(currentAddr);
×
439
          if(it != m_callbacks.end())
×
440
          {
441
            it->second.first->remove_callback(it->second.second);
×
442
            m_callbacks.erase(it);
×
443
          }
×
444

445
          is_listening = false;
×
446
        }
×
447

448
        // Remove param
449
        node->remove_parameter();
×
450
      }
×
451
      else
452
      {
453
        if(auto p = node->get_parameter())
×
454
          updateOSSIAAddress(settings, *p);
×
455
        else
456
          createOSSIAAddress(settings, *node);
×
457
      }
458

459
      auto newName = settings.address.path.last();
×
460
      if(!latin_compare(newName, node->get_name()))
×
461
      {
462
        renameListening_impl(currentAddr, newName);
×
463
        node->set_name(newName.toStdString());
×
464
      }
×
465
    }
×
466
    else
467
    {
468
      qDebug() << "updateAddress: Warning ! did not find" << currentAddr;
×
469
    }
470
  }
×
471
}
10 ✔
472

473
void DeviceInterface::removeListening_impl(
10 ✔
474
    ossia::net::node_base& node, State::Address addr)
475
{
476
  // Find & remove our callback
477
  auto it = m_callbacks.find(addr);
10 ✔
478
  if(it != m_callbacks.end())
10 ✔
479
  {
480
    it->second.first->remove_callback(it->second.second);
2 ✔
481
    m_callbacks.erase(it);
2 ✔
482
  }
2 ✔
483

484
  // Recurse
485
  for(const auto& child : node.children())
16 ✔
486
  {
487
    State::Address sub_addr = addr;
6 ✔
488
    sub_addr.path += QString::fromStdString(child->get_name());
6 ✔
489
    removeListening_impl(*child.get(), std::move(sub_addr));
6 ✔
490
  }
6 ✔
491
}
10 ✔
492

493
void DeviceInterface::removeListening_impl(
×
494
    ossia::net::node_base& node, State::Address addr, std::vector<State::Address>& vec)
495
{
496
  // Find & remove our callback
497
  auto it = m_callbacks.find(addr);
×
498
  if(it != m_callbacks.end())
×
499
  {
500
    it->second.first->remove_callback(it->second.second);
×
501
    m_callbacks.erase(it);
×
502
    vec.push_back(addr);
×
503
  }
×
504

505
  // Recurse
506
  for(const auto& child : node.children())
×
507
  {
508
    State::Address sub_addr = addr;
×
509
    sub_addr.path += QString::fromStdString(child->get_name());
×
510
    removeListening_impl(*child.get(), std::move(sub_addr));
×
511
  }
×
512
}
×
513
bool is_parent(const State::Address& parent, const State::Address& child)
2,004 ✔
514
{
515
  const auto p_size = parent.path.size();
2,004 ✔
516
  if(child.path.size() < p_size)
2,004 ✔
517
    return false;
638 ✔
518
  for(int i = 0; i < p_size; i++)
2,859 ✔
519
  {
520
    if(child.path[i] != parent.path[i])
2,232 ✔
521
      return false;
739 ✔
522
  }
1,493 ✔
523
  return true;
627 ✔
524
}
2,004 ✔
525

526
void DeviceInterface::renameListening_impl(
116 ✔
527
    const State::Address& parent, const QString& newName)
528
{
529
  // Store the elements that are renamed
530
  std::vector<std::pair<State::Address, callback_pair>> saved_elts;
116 ✔
531
  for(auto it = m_callbacks.begin(); it != m_callbacks.end();)
2,120 ✔
532
  {
533
    if(is_parent(parent, it->first))
2,004 ✔
534
    {
535
      State::Address addr = it->first;
627 ✔
536
      addr.path[parent.path.size() - 1] = newName;
627 ✔
537
      saved_elts.push_back({std::move(addr), it->second});
627 ✔
538
      it = m_callbacks.erase(it);
627 ✔
539
    }
627 ✔
540
    else
541
    {
542
      ++it;
1,377 ✔
543
    }
544
  }
545

546
  // Put things back after renaming
547
  for(auto&& p : std::move(saved_elts))
743 ✔
548
  {
549
    p.second.first->replace_callback(
627 ✔
550
        p.second.second,
627 ✔
551
        [this, addr = p.first](const ossia::value& val) { valueUpdated(addr, val); });
627 ✔
552
    m_callbacks.insert(std::move(p));
627 ✔
553
  }
554
}
116 ✔
555

556
namespace
557
{
558
struct in_sink final : public spdlog::sinks::sink
559
{
560
  const DeviceInterface& m_dev;
561
  in_sink(const DeviceInterface& dev)
×
562
      : m_dev{dev}
×
563
  {
×
564
  }
×
565
  void log(const spdlog::details::log_msg& msg) override
×
566
  {
567
    m_dev.logInbound(QString::fromLatin1(msg.payload.data(), msg.payload.size()));
×
568
  }
×
569

570
  void flush() override { }
×
571
  void set_pattern(const std::string& pattern) override { }
×
572
  void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override { }
×
573
};
574
struct out_sink final : public spdlog::sinks::sink
575
{
576
  const DeviceInterface& m_dev;
577
  out_sink(const DeviceInterface& dev)
×
578
      : m_dev{dev}
×
579
  {
×
580
  }
×
581
  void log(const spdlog::details::log_msg& msg) override
×
582
  {
583
    m_dev.logOutbound(QString::fromLatin1(msg.payload.data(), msg.payload.size()));
×
584
  }
×
585

586
  void flush() override { }
×
587
  void set_pattern(const std::string& pattern) override { }
×
588
  void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override { }
×
589
};
590
}
591
void DeviceInterface::setLogging_impl(DeviceLogging b) const
956 ✔
592
{
593
  if(auto dev = getDevice())
956 ✔
594
  {
595
    switch(b)
951 ✔
596
    {
597
      case DeviceLogging::LogNothing: {
598
        dev->get_protocol().set_logger({});
951 ✔
599
        return;
951 ✔
600
      }
601
      case DeviceLogging::LogEverything: {
602
        ossia::net::network_logger logger;
×
603
        logger.inbound_logger = std::make_shared<ossia::logger_type>(
×
604
            "in_logger", std::make_shared<in_sink>(*this));
×
605
        logger.outbound_logger = std::make_shared<ossia::logger_type>(
×
606
            "out_logger", std::make_shared<out_sink>(*this));
×
607

608
        logger.inbound_logger->set_pattern("%v");
×
609
        logger.inbound_logger->set_level(spdlog::level::info);
×
610
        logger.outbound_logger->set_pattern("%v");
×
611
        logger.outbound_logger->set_level(spdlog::level::info);
×
612
        dev->get_protocol().set_logger(std::move(logger));
×
613
        break;
614
      }
×
615
      case DeviceLogging::LogUnfolded: {
616
        ossia::net::network_logger logger;
×
617
        logger.inbound_listened_logger = std::make_shared<ossia::logger_type>(
×
618
            "in_logger", std::make_shared<in_sink>(*this));
×
619
        logger.outbound_listened_logger = std::make_shared<ossia::logger_type>(
×
620
            "out_logger", std::make_shared<out_sink>(*this));
×
621

622
        logger.inbound_listened_logger->set_pattern("%v");
×
623
        logger.inbound_listened_logger->set_level(spdlog::level::info);
×
624
        logger.outbound_listened_logger->set_pattern("%v");
×
625
        logger.outbound_listened_logger->set_level(spdlog::level::info);
×
626
        dev->get_protocol().set_logger(std::move(logger));
×
627
        break;
628
      }
×
629
    }
630
  }
×
631
}
956 ✔
632

633
void DeviceInterface::enableCallbacks()
199 ✔
634
{
635
  if(!m_callbacksEnabled)
199 ✔
636
  {
637
    auto dev = getDevice();
199 ✔
638
    if(dev)
199 ✔
639
    {
640
      dev->on_node_created.connect<&DeviceInterface::nodeCreated>(this);
199 ✔
641
      dev->on_node_removing.connect<&DeviceInterface::nodeRemoving>(this);
199 ✔
642
      dev->on_node_renamed.connect<&DeviceInterface::nodeRenamed>(this);
199 ✔
643
      dev->on_parameter_created.connect<&DeviceInterface::addressCreated>(this);
199 ✔
644
      dev->on_parameter_removing.connect<&DeviceInterface::addressRemoved>(this);
199 ✔
645
      dev->on_attribute_modified.connect<&DeviceInterface::addressUpdated>(this);
199 ✔
646
    }
199 ✔
647
    m_callbacksEnabled = true;
199 ✔
648
  }
199 ✔
649
}
199 ✔
650

651
void DeviceInterface::disableCallbacks()
288 ✔
652
{
653
  if(m_callbacksEnabled)
288 ✔
654
  {
655
    auto dev = getDevice();
6 ✔
656
    if(dev)
6 ✔
657
    {
658
      dev->on_node_created.disconnect<&DeviceInterface::nodeCreated>(this);
6 ✔
659
      dev->on_node_removing.disconnect<&DeviceInterface::nodeRemoving>(this);
6 ✔
660
      dev->on_node_renamed.disconnect<&DeviceInterface::nodeRenamed>(this);
6 ✔
661
      dev->on_parameter_created.disconnect<&DeviceInterface::addressCreated>(this);
6 ✔
662
      dev->on_parameter_removing.disconnect<&DeviceInterface::addressRemoved>(this);
6 ✔
663
      dev->on_attribute_modified.disconnect<&DeviceInterface::addressUpdated>(this);
6 ✔
664
    }
6 ✔
665
    m_callbacksEnabled = false;
6 ✔
666
  }
6 ✔
667
}
288 ✔
668

669
Device::Node DeviceInterface::simple_refresh()
30 ✔
670
{
671
  Device::Node score_device{settings(), nullptr};
30 ✔
672

673
  // Recurse on the children
674
  const auto& ossia_children = getDevice()->get_root_node().children();
30 ✔
675
  score_device.reserve(ossia_children.size());
30 ✔
676
  for(const auto& node : ossia_children)
90 ✔
677
  {
678
    score_device.push_back(ToDeviceExplorer(*node.get()));
60 ✔
679
  }
680

681
  score_device.get<Device::DeviceSettings>().name
30 ✔
682
      = QString::fromStdString(getDevice()->get_name());
60 ✔
683

684
  return score_device;
30 ✔
685
}
30 ✔
686

687
void DeviceInterface::removeNode(const State::Address& address)
×
688
{
689
  using namespace ossia;
690
  if(!m_capas.canRemoveNode)
×
691
    return;
×
692
  if(auto dev = getDevice())
×
693
  {
694
    if(auto node = findNodeFromPath(address.path, *dev))
×
695
    {
696
      auto parent = node->get_parent();
×
697
      if(parent->has_child(*node))
×
698
      {
699
        /* If we are listening to this node, we recursively
700
         * remove listening to all the children. */
701
        removeListening_impl(*node, address);
×
702

703
        // TODO !! if we remove nodes while recording
704
        // (or anything involving a registered listening state), there will be
705
        // crashes.
706
        // The Device Explorer should be locked for edition during recording /
707
        // playing.
708
        parent->remove_child(node->get_name());
×
709
      }
×
710
    }
×
711
    else
712
    {
713
      qDebug() << "Warning ! removeNode: did not find" << address;
×
714
    }
715
  }
×
716
}
×
717

718
Device::Node DeviceInterface::refresh()
2 ✔
719
{
720
  Device::Node device_node{settings(), nullptr};
2 ✔
721

722
  if(auto dev = getDevice())
2 ✔
723
  {
724
    auto& root = dev->get_root_node();
2 ✔
725
    // Clear the listening
726
    removeListening_impl(root, State::Address{m_settings.name, {}});
2 ✔
727

728
    disableCallbacks();
2 ✔
729
    m_callbacks.clear();
2 ✔
730

731
    auto fut = dev->get_protocol().update_async(root);
2 ✔
732
    auto res = fut.wait_for(std::chrono::seconds(1));
2 ✔
733
    if(res == std::future_status::ready)
2 ✔
734
    {
735
      // Make a device explorer node from the current state of the device.
736
      // First make the node corresponding to the root node.
737

738
      // Recurse on the children
739
      const auto& children = root.children();
2 ✔
740
      device_node.reserve(children.size());
2 ✔
741
      for(const auto& node : children)
2 ✔
742
      {
743
        device_node.push_back(ToDeviceExplorer(*node.get()));
×
744
      }
745
    }
2 ✔
746
    enableCallbacks();
2 ✔
747

748
    device_node.get<Device::DeviceSettings>().name = settings().name;
2 ✔
749
  }
2 ✔
750

751
  return device_node;
2 ✔
752
}
2 ✔
753

754
std::optional<ossia::value> DeviceInterface::refresh(const State::Address& address)
×
755
{
756
  if(auto dev = getDevice())
×
757
  {
758
    auto node = findNodeFromPath(address.path, *dev);
×
759
    if(node)
×
760
    {
761
      if(auto addr = node->get_parameter())
×
762
      {
763
        return addr->fetch_value();
×
764
      }
765
    }
×
766
  }
×
767

768
  return {};
×
769
}
×
770

771
void DeviceInterface::request(const Device::Node& address)
9 ✔
772
{
773
  if(auto dev = getDevice())
9 ✔
774
  {
775
    auto node = findNodeFromPath(address, *dev);
9 ✔
776
    if(node)
9 ✔
777
    {
778
      if(auto addr = node->get_parameter())
9 ✔
779
      {
780
        addr->request_value();
9 ✔
781
      }
9 ✔
782
    }
9 ✔
783
  }
9 ✔
784
}
9 ✔
785

786
Device::Node DeviceInterface::getNode(const State::Address& address) const
×
787
{
788
  if(auto dev = getDevice())
×
789
  {
790
    auto ossia_node = findNodeFromPath(address.path, *dev);
×
791
    if(ossia_node)
×
792
      return ToDeviceExplorer(*ossia_node);
×
793
  }
×
794

795
  return {};
×
796
}
×
797

798
Device::Node DeviceInterface::getNodeWithoutChildren(const State::Address& address) const
2 ✔
799
{
800
  if(auto dev = getDevice())
2 ✔
801
  {
802
    auto ossia_node = findNodeFromPath(address.path, *dev);
2 ✔
803
    if(ossia_node)
2 ✔
804
    {
805
      return Device::Node{ToAddressSettings(*ossia_node), nullptr};
2 ✔
806
    }
807
  }
×
808

809
  return {};
×
810
}
2 ✔
811

812
void DeviceInterface::setListening(const State::Address& addr, bool b)
6,342 ✔
813
{
814
  static const bool gui = score::AppContext().applicationSettings.gui;
6,342 ✔
815
  if(!gui)
6,342 ✔
816
    return;
551 ✔
817

818
  // Remember the request even when the device is not connected, or the
819
  // address does not exist yet: restoreListening() applies it once it does.
820
  if(b)
5,791 ✔
821
    m_listeningRequests.insert(addr);
5,533 ✔
822
  else
823
    m_listeningRequests.erase(addr);
258 ✔
824

825
  listen_impl(addr, b);
5,791 ✔
826
}
6,342 ✔
827

828
std::vector<State::Address> DeviceInterface::listeningRequests() const
4 ✔
829
{
830
  return std::vector<State::Address>(
4 ✔
831
      m_listeningRequests.begin(), m_listeningRequests.end());
4 ✔
832
}
×
833

834
void DeviceInterface::restoreListening()
434 ✔
835
{
836
  static const bool gui = score::AppContext().applicationSettings.gui;
434 ✔
837
  if(!gui)
434 ✔
838
    return;
40 ✔
839
  if(!getDevice())
394 ✔
840
    return;
×
841

842
  for(const auto& addr : m_listeningRequests)
550 ✔
843
    listen_impl(addr, true);
156 ✔
844
}
434 ✔
845

846
bool DeviceInterface::listen_impl(const State::Address& addr, bool b)
5,947 ✔
847
{
848
  if(auto dev = getDevice())
5,947 ✔
849
  {
850
    // First check if the address is already listening
851
    // so that we don't have to go through the tree.
852
    auto cb_it = m_callbacks.find(addr);
5,699 ✔
853

854
    // Nothing to stop: do not even look the node up (it may well be gone)
855
    if(!b && cb_it == m_callbacks.end())
5,699 ✔
856
      return false;
4 ✔
857

858
    ossia::net::parameter_base* ossia_addr{};
5,695 ✔
859
    if(cb_it == m_callbacks.end())
5,695 ✔
860
    {
861
      auto n = findNodeFromPath(addr.path, *dev);
5,538 ✔
862
      if(!n)
5,538 ✔
863
      {
864
        return false;
362 ✔
865
      }
866

867
      ossia_addr = n->get_parameter();
5,176 ✔
868
      if(!ossia_addr)
5,176 ✔
869
        return false;
×
870
    }
5,176 ✔
871
    else
872
    {
873
      ossia_addr = cb_it->second.first;
157 ✔
874
      if(!ossia_addr)
157 ✔
875
      {
876
        m_callbacks.erase(cb_it);
×
877
        return false;
×
878
      }
879
    }
880

881
    SCORE_ASSERT(bool(ossia_addr));
5,333 ✔
882

883
    // If we want to enable listening
884
    // and the address wasn't already listening
885
    if(b)
5,333 ✔
886
    {
887
      if(cb_it == m_callbacks.end())
5,326 ✔
888
      {
889
        m_callbacks.insert(
10,352 ✔
890
            {addr,
10,352 ✔
891
             {ossia_addr,
5,176 ✔
892
              ossia_addr->add_callback(
5,176 ✔
893
                  [this, addr](const ossia::value& val) { valueUpdated(addr, val); })}});
5,178 ✔
894
      }
5,176 ✔
895

896
      // FIXME crash here when refresh audio device
897
      // An impulse is an event, not a state: the value an address holds when
898
      // we subscribe is worth reporting for every other type, but replaying an
899
      // impulse nobody sent makes listeners believe one just arrived.
900
      //
901
      // Both the declared type and what it happens to hold: a parameter of
902
      // another type carrying an impulse is still an event nobody sent.
903
      const auto v = ossia_addr->value();
5,326 ✔
904
      if(ossia_addr->get_value_type() != ossia::val_type::IMPULSE
5,326 ✔
905
         && v.get_type() != ossia::val_type::IMPULSE)
5,326 ✔
906
      {
907
        valueUpdated(addr, v);
4,618 ✔
908
      }
4,618 ✔
909
    }
5,326 ✔
910
    else
911
    {
912
      // If we can disable listening
913
      if(cb_it != m_callbacks.end())
7 ✔
914
      {
915
        ossia_addr->remove_callback(cb_it->second.second);
7 ✔
916
        m_callbacks.erase(cb_it);
7 ✔
917
      }
7 ✔
918
    }
919
    return true;
5,333 ✔
920
  }
921
  return false;
248 ✔
922
}
5,947 ✔
923

924
std::vector<State::Address> DeviceInterface::listening() const
18 ✔
925
{
926
  if(!connected())
18 ✔
927
    return {};
1 ✔
928

929
  static const bool gui = score::AppContext().applicationSettings.gui;
17 ✔
930
  if(!gui)
17 ✔
931
    return {};
×
932

933
  std::vector<State::Address> addrs;
17 ✔
934
  addrs.reserve(m_callbacks.size());
17 ✔
935

936
  for(const auto& elt : m_callbacks)
31 ✔
937
  {
938
    addrs.push_back(elt.first);
14 ✔
939
  }
940

941
  return addrs;
17 ✔
942
}
35 ✔
943

944
void DeviceInterface::addToListening(const std::vector<State::Address>& addresses)
×
945
{
946
  if(!connected())
×
947
    return;
×
948

949
  static const bool gui = score::AppContext().applicationSettings.gui;
×
950
  if(!gui)
×
951
    return;
×
952

953
  for(const auto& addr : addresses)
×
954
  {
955
    SCORE_ASSERT(addr.device == this->settings().name);
×
956
    setListening(addr, true);
×
957
  }
958
}
×
959

960
void DeviceInterface::sendMessage(const State::Address& addr, const ossia::value& v)
27 ✔
961
{
962
  if(auto dev = getDevice())
27 ✔
963
  {
964
    if(auto node = findNodeFromPath(addr.path, *dev))
×
965
    {
966
      auto addr = node->get_parameter();
×
967
      if(addr)
×
968
      {
969
        addr->push_value(v);
×
970
      }
×
971
    }
×
972
    else
973
    {
974
      qDebug() << "Warning! sendMessage: did not find" << addr;
×
975
    }
976
  }
×
977
}
27 ✔
978

979
bool DeviceInterface::isLogging() const
777 ✔
980
{
981
  return m_logging;
777 ✔
982
}
983

984
void DeviceInterface::setLogging(DeviceLogging b)
60 ✔
985
{
986
  if(!connected())
60 ✔
987
  {
988
    m_logging = b;
40 ✔
989
    return;
40 ✔
990
  }
991

992
  if(b == m_logging)
20 ✔
993
    return;
20 ✔
994

995
  m_logging = b;
×
996
  setLogging_impl(m_logging);
×
997
}
60 ✔
998

999
OwningDeviceInterface::~OwningDeviceInterface() { }
60 ✔
1000

1001
void OwningDeviceInterface::replaceDevice(ossia::net::device_base* d)
×
1002
{
1003
  disconnect();
×
1004
  m_dev.reset(d);
×
1005
  deviceChanged(nullptr, d);
×
1006
  m_owned = false;
×
1007
}
×
1008

1009
void OwningDeviceInterface::releaseDevice()
×
1010
{
1011
  DeviceInterface::disconnect();
×
1012
  deviceChanged(m_dev.get(), nullptr);
×
1013
  // FIXME instead it should just be shared_ptrs in player m_dev.release();
1014
}
×
1015

1016
void OwningDeviceInterface::disconnect()
281 ✔
1017
{
1018
  if(m_owned)
281 ✔
1019
  {
1020
    DeviceInterface::disconnect();
281 ✔
1021
    // TODO why not auto dev = m_dev; ... like in MIDIDevice ?
1022
    deviceChanged(m_dev.get(), nullptr);
281 ✔
1023
    m_dev.reset();
281 ✔
1024
  }
281 ✔
1025
}
281 ✔
1026

1027
void DeviceInterface::nodeCreated(const ossia::net::node_base& n)
8,438 ✔
1028
{
1029
  pathAdded(ToAddress(n));
8,438 ✔
1030
}
8,438 ✔
1031

1032
void DeviceInterface::nodeRemoving(const ossia::net::node_base& n)
6,516 ✔
1033
{
1034
  pathRemoved(ToAddress(n));
6,516 ✔
1035
}
6,516 ✔
1036

1037
void DeviceInterface::nodeRenamed(
308 ✔
1038
    const ossia::net::node_base& node, std::string old_name)
1039
{
1040
  if(!node.get_parent())
308 ✔
1041
    return;
192 ✔
1042

1043
  State::Address currentAddress = ToAddress(*node.get_parent());
116 ✔
1044
  currentAddress.path.push_back(QString::fromStdString(old_name));
116 ✔
1045

1046
  Device::AddressSettings as = ToAddressSettings(node);
116 ✔
1047
  as.name = QString::fromStdString(node.get_name());
116 ✔
1048

1049
  renameListening_impl(currentAddress, as.name);
116 ✔
1050

1051
  pathUpdated(currentAddress, as);
116 ✔
1052
}
308 ✔
1053

1054
void DeviceInterface::addressCreated(const ossia::net::parameter_base& addr)
6,330 ✔
1055
{
1056
  State::Address currentAddress = ToAddress(addr.get_node());
6,330 ✔
1057
  Device::AddressSettings as = ToAddressSettings(addr.get_node());
6,330 ✔
1058
  pathUpdated(currentAddress, as);
6,330 ✔
1059
  ossia::qt::run_async(
6,330 ✔
1060
      QCoreApplication::instance(), [self = QPointer{this}, addr = currentAddress] {
12,660 ✔
1061
    if(self)
6,330 ✔
1062
      self->setListening(addr, true);
6,071 ✔
1063
  });
6,330 ✔
1064
}
6,330 ✔
1065

1066
void DeviceInterface::addressUpdated(
8,578 ✔
1067
    const ossia::net::node_base& node, ossia::string_view key)
1068
{
1069
  const bool hidden = (ossia::net::get_zombie(node) || ossia::net::get_hidden(node));
8,578 ✔
1070

1071
  State::Address currentAddress = ToAddress(node);
8,578 ✔
1072
  if(hidden)
8,578 ✔
1073
  {
1074
    pathRemoved(currentAddress);
×
1075
    return;
×
1076
  }
1077

1078
  Device::AddressSettings as = ToAddressSettings(node);
8,578 ✔
1079

1080
  pathUpdated(currentAddress, as);
8,578 ✔
1081
}
8,578 ✔
1082

1083
void DeviceInterface::addressRemoved(const ossia::net::parameter_base& addr)
4,410 ✔
1084
{
1085
  auto address = ToAddress(addr.get_node());
4,410 ✔
1086

1087
  auto cb_it = m_callbacks.find(address);
4,410 ✔
1088
  if(cb_it != m_callbacks.end())
4,410 ✔
1089
  {
1090
    m_callbacks.erase(cb_it);
3,438 ✔
1091
  }
3,438 ✔
1092
  auto& node = addr.get_node();
4,410 ✔
1093
  State::Address currentAddress = ToAddress(node);
4,410 ✔
1094
  Device::AddressSettings as;
4,410 ✔
1095
  as.name = QString::fromStdString(node.get_name());
4,410 ✔
1096
  pathUpdated(currentAddress, as);
4,410 ✔
1097
}
4,410 ✔
1098

1099
void releaseDevice(
×
1100
    ossia::net::network_context& ctx, std::unique_ptr<ossia::net::device_base> dd)
1101
{
1102
  releaseDevice(ctx, std::shared_ptr(std::move(dd)));
×
1103
}
×
1104

1105
void releaseDevice(
152 ✔
1106
    ossia::net::network_context& ctx, std::shared_ptr<ossia::net::device_base> shared_dd)
1107
{
1108
  if(shared_dd)
152 ✔
1109
  {
1110
    auto strand = boost::asio::make_strand(ctx.context);
×
1111
    boost::asio::dispatch(strand, [dev = shared_dd] { dev->get_protocol().stop(); });
×
1112

1113
    std::future<void> wait1 = boost::asio::dispatch(strand, boost::asio::use_future);
×
1114
    if(auto res = wait1.wait_for(std::chrono::seconds(1));
×
1115
       res != std::future_status::ready)
×
1116
    {
1117
      qDebug() << "Device deletion: asio thread seems stuck (1)";
×
1118
    }
×
1119

1120
    boost::asio::dispatch(strand, [d = std::move(shared_dd)]() mutable { d.reset(); });
×
1121

1122
    std::future<void> wait2 = boost::asio::dispatch(strand, boost::asio::use_future);
×
1123
    if(auto res = wait2.wait_for(std::chrono::seconds(1));
×
1124
       res != std::future_status::ready)
×
1125
    {
1126
      qDebug() << "Device deletion: asio thread seems stuck (2)";
×
1127
    }
×
1128
  }
×
1129
}
152 ✔
1130
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc