File: bluetooth.hpp

package info (click to toggle)
waybar 0.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,364 kB
  • sloc: cpp: 24,698; xml: 742; python: 146; ansic: 77; makefile: 26
file content (83 lines) | stat: -rw-r--r-- 2,563 bytes parent folder | download | duplicates (2)
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
#pragma once

#include "ALabel.hpp"
#ifdef WANT_RFKILL
#include "util/rfkill.hpp"
#endif
#include <gio/gio.h>

#include <optional>
#include <string>
#include <vector>

namespace waybar::modules {

class Bluetooth : public ALabel {
  struct ControllerInfo {
    std::string path;
    std::string address;
    std::string address_type;
    // std::string name; // just use alias instead
    std::string alias;
    bool powered;
    bool discoverable;
    bool pairable;
    bool discovering;
  };

  // NOTE: there are some properties that not all devices provide
  struct DeviceInfo {
    std::string path;
    std::string paired_controller;
    std::string address;
    std::string address_type;
    // std::optional<std::string> name; // just use alias instead
    std::string alias;
    std::optional<std::string> icon;
    bool paired;
    bool trusted;
    bool blocked;
    bool connected;
    bool services_resolved;
    // NOTE: experimental feature in bluez
    std::optional<unsigned char> battery_percentage;
  };

 public:
  Bluetooth(const std::string&, const Json::Value&);
  virtual ~Bluetooth() = default;
  auto update() -> void override;

 private:
  static auto onObjectAdded(GDBusObjectManager*, GDBusObject*, gpointer) -> void;
  static auto onObjectRemoved(GDBusObjectManager*, GDBusObject*, gpointer) -> void;

  static auto onInterfaceAddedOrRemoved(GDBusObjectManager*, GDBusObject*, GDBusInterface*,
                                        gpointer) -> void;
  static auto onInterfaceProxyPropertiesChanged(GDBusObjectManagerClient*, GDBusObjectProxy*,
                                                GDBusProxy*, GVariant*, const gchar* const*,
                                                gpointer) -> void;

  auto getDeviceBatteryPercentage(GDBusObject*) -> std::optional<unsigned char>;
  auto getDeviceProperties(GDBusObject*, DeviceInfo&) -> bool;
  auto getControllerProperties(GDBusObject*, ControllerInfo&) -> bool;

  // Returns std::nullopt if no controller could be found
  auto findCurController() -> std::optional<ControllerInfo>;
  auto findConnectedDevices(const std::string&, std::vector<DeviceInfo>&) -> void;

#ifdef WANT_RFKILL
  util::Rfkill rfkill_;
#endif
  const std::unique_ptr<GDBusObjectManager, void (*)(GDBusObjectManager*)> manager_;

  std::string state_;
  std::optional<ControllerInfo> cur_controller_;
  std::vector<DeviceInfo> connected_devices_;
  DeviceInfo cur_focussed_device_;
  std::string device_enumerate_;

  std::vector<std::string> device_preference_;
};

}  // namespace waybar::modules