File: mpd.hpp

package info (click to toggle)
waybar 0.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,464 kB
  • sloc: cpp: 25,331; xml: 742; python: 146; ansic: 77; makefile: 29
file content (68 lines) | stat: -rw-r--r-- 1,666 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
#pragma once

#include <fmt/format.h>
#include <mpd/client.h>
#include <spdlog/spdlog.h>

#include <condition_variable>
#include <thread>

#include "ALabel.hpp"
#include "modules/mpd/state.hpp"

namespace waybar::modules {

class MPD : public ALabel {
  friend class detail::Context;

  // State machine
  detail::Context context_{this};

  const std::string module_name_;

  // Not using unique_ptr since we don't manage the pointer
  // (It's either nullptr, or from the config)
  const char* server_;
  const unsigned port_;
  const std::string password_;

  unsigned timeout_;

  detail::unique_connection connection_;

  detail::unique_status status_;
  mpd_state state_;
  detail::unique_song song_;

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

 private:
  std::string getTag(mpd_tag_type type, unsigned idx = 0) const;
  std::string getFilename() const;
  void setLabel();
  std::string getStateIcon() const;
  std::string getOptionIcon(std::string optionName, bool activated) const;

  // GUI-side methods
  bool handlePlayPause(GdkEventButton* const&);
  void emit() { dp.emit(); }

  // MPD-side, Non-GUI methods.
  void tryConnect();
  void checkErrors(mpd_connection* conn);
  void fetchState();
  void queryMPD();

  inline bool stopped() const { return connection_ && state_ == MPD_STATE_STOP; }
  inline bool playing() const { return connection_ && state_ == MPD_STATE_PLAY; }
  inline bool paused() const { return connection_ && state_ == MPD_STATE_PAUSE; }
};

#if !defined(MPD_NOINLINE)
#include "modules/mpd/state.inl.hpp"
#endif

}  // namespace waybar::modules