summaryrefslogtreecommitdiffhomepage
path: root/config.cpp
blob: d51f6f6c91869fbbd0554d0cf299d9f25e3a8b69 (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
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
#include "config.h"

#include "log.h"

#include <libreichwein/file.h>
#include <libreichwein/stringhelper.h>

#include <fmt/format.h>

#include <string>

using namespace std::string_literals;

namespace fs = std::filesystem;

const char *device = "default";    // playback device

std::string config_filename = "click.cfg";

fs::path click_data_filename = "media/click.s16le";

fs::path libdir = "/usr/lib/click";

const std::string programname = "click";

Config::Config(int argc, char** argv):
  m_argv(argc)
{
  for (int i = 0; i < argc; ++i) {
    m_argv[i] = argv[i];
  }

  recover();
}

Config::~Config()
{
  persist();
}

void Config::recover()
{
  try {
    // presets / defaults
    m_midi_channel = CLICK_CHANNEL;
    m_midi_note = CLICK_NOTE;
    m_bpm = default_bpm;
    m_mode = default_mode;
    m_output = default_output;

    std::string config = Reichwein::File::getFile(get_config_filename());

    std::vector<std::string> lines = Reichwein::Stringhelper::split(config, "\n");

    for (const auto& i: lines) {
      if (i.starts_with("midi_channel=")) {
        m_midi_channel = stoul(i.substr(13));
        signal_channel(m_midi_channel);
      }
      if (i.starts_with("midi_note=")) {
        m_midi_note = stoul(i.substr(10));
        signal_note(m_midi_note);
      }
      if (i.starts_with("bpm=")) {
        m_bpm = stoul(i.substr(4));
        signal_bpm(m_bpm);
      }
      if (i.starts_with("mode=")) {
        m_mode = stoul(i.substr(5));
        signal_mode(m_mode);
      }
      if (i.starts_with("output=")) {
        m_output = stoul(i.substr(7));
        signal_output(m_output);
      }
    }
  } catch (const std::exception& ex) {
    log_cout << "Config not found. Setting config to defaults." << std::endl;
  }
}

void Config::persist()
{
  std::string config =
    fmt::format("midi_channel={}\n", m_midi_channel) +
    fmt::format("midi_note={}\n", m_midi_note) +
    fmt::format("bpm={}\n", m_bpm) +
    fmt::format("mode={}\n", m_mode) +
    fmt::format("output={}\n", m_output);

  Reichwein::File::setFile(get_config_filename(), config);
}

int Config::get_midi_channel() const
{
  return m_midi_channel;
}

void Config::set_midi_channel(int channel)
{
  m_midi_channel = channel;
  signal_channel(channel);
}

int Config::get_midi_note() const
{
  return m_midi_note;
}

void Config::set_midi_note(int note)
{
  m_midi_note = note;
  signal_note(note);
}

int Config::get_bpm() const
{
  return m_bpm;
}

void Config::set_bpm(int bpm)
{
  m_bpm = bpm;
  signal_bpm(bpm);
}

int Config::get_mode() const
{
  return m_mode;
}

void Config::set_mode(int mode)
{
  m_mode = mode;
  signal_mode(mode);
}

int Config::get_output() const
{
  return m_output;
}

void Config::set_output(int output)
{
  m_output = output;
  signal_output(output);
}

std::string Config::get_config_filename() const
{
  char* envvar = getenv("HOME");
  if (envvar == nullptr || !std::filesystem::exists(envvar)) {
    return config_filename;
  } else {
    return std::filesystem::path(getenv("HOME")) / ("."s + config_filename);
  }
}

std::string Config::get_argv(int i) const
{
  return m_argv[i];
}

std::filesystem::path Config::get_click_data_filename() const
{
  // 1st try: executable-local media/click.s16le
  fs::path result{fs::path{m_argv[0]}.parent_path() / click_data_filename};
  if (fs::exists(result)) {
    return result;
  }

  // 2nd try:
  result = libdir / click_data_filename;
  if (fs::exists(result)) {
    return result;
  }

  throw std::runtime_error("Click file not found");
}

std::string Config::get_programname() const
{
  return programname;
}