forked from Meumeu/ZephyrusBling
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzephyrusbling.cpp
More file actions
227 lines (180 loc) · 6.34 KB
/
Copy pathzephyrusbling.cpp
File metadata and controls
227 lines (180 loc) · 6.34 KB
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
/*
* ZephyrusBling, a utility to display images on the LEDs of Zephyrus G14 laptop
* Copyright (C) 2020 Guillaume Meunier <guillaume.meunier@centraliens.net>
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "BlingDaemon.h"
#include <CLI/CLI.hpp>
#include <boost/lexical_cast.hpp>
#include <filesystem>
#include <fmt/format.h>
#include <gsl/span>
#include <gsl/span_ext>
#include <iostream>
namespace
{
void split_string(std::vector<std::string_view> & split, std::string_view s, char separator)
{
split.clear();
size_t n;
while (true)
{
n = s.find_first_of(separator);
split.emplace_back(s.substr(0, n));
if (n == std::string_view::npos)
break;
s = s.substr(n + 1);
}
}
std::vector<sdbus::Struct<double, double>> vector_to_vector_of_tuple_2(gsl::span<std::string_view> values)
{
std::vector<sdbus::Struct<double, double>> ret;
std::vector<std::string_view> split;
if (values.size() == 1)
{
split_string(split, values[0], ',');
if (split.size() != 1)
throw std::invalid_argument("Invalid argument");
ret.emplace_back(0, boost::lexical_cast<double>(split[0]));
return ret;
}
for (auto & i: values)
{
split_string(split, i, ',');
if (split.size() != 2)
throw std::invalid_argument("Invalid argument");
ret.emplace_back(boost::lexical_cast<double>(split[0]), boost::lexical_cast<double>(split[1]));
}
return ret;
}
std::vector<sdbus::Struct<double, double, double>> vector_to_vector_of_tuple_3(gsl::span<std::string_view> values)
{
std::vector<sdbus::Struct<double, double, double>> ret;
std::vector<std::string_view> split;
if (values.size() == 1)
{
split_string(split, values[0], ',');
if (split.size() != 2)
throw std::invalid_argument("Invalid argument");
ret.emplace_back(0, boost::lexical_cast<double>(split[0]), boost::lexical_cast<double>(split[1]));
return ret;
}
for (auto & i: values)
{
split_string(split, i, ',');
if (split.size() != 3)
throw std::invalid_argument("Invalid argument");
ret.emplace_back(boost::lexical_cast<double>(split[0]), boost::lexical_cast<double>(split[1]),
boost::lexical_cast<double>(split[2]));
}
return ret;
}
} // namespace
int main(int argc, char ** argv)
{
std::string image;
std::string text;
std::string font;
std::vector<std::string> fx;
double duration = 3;
int z_order = 0;
CLI::App app("description");
std::map<std::string, double> time_units{{"s", 1}, {"ms", 0.001}};
auto opt_image = app.add_option("--image", image, "Image to display")->check(CLI::ExistingFile);
auto opt_text = app.add_option("--text", text, "Text to display");
auto opt_font = app.add_option("--font", font, "Font to use");
auto opt_fx = app.add_option("--fx", fx, "Transform to apply, see below");
auto opt_duration =
app.add_option("--duration", duration, "How long the text or image is displayed")
->transform(CLI::AsNumberWithUnit{time_units, CLI::AsNumberWithUnit::CASE_SENSITIVE, "s"});
auto opt_zorder = app.add_option("--z-order", z_order, "Not implemented");
opt_image->excludes(opt_text);
opt_font->needs(opt_text);
app.footer(
R"(Exactly one of --text, --image is required.
By default, the text or image disappears after 3 seconds, use the --duration
option to override it.
Each --fx specifies a transform to apply, with the following format:
effect:params(:params)*
Each param is a sequence of comma separated floating point numbers whose meaning
depends on the effect.
If only one param is given for the effect, it stays constant while the image is
displayed. If there are several, the first number of each param is a timestamp
since the image is first displayed and the others are the arguments. In the latter
case, the arguments are linearly interpolated with respect to time.
The effect can be:
- translate: the 2 arguments are the displacement in cm on the X and Y axes
- rotate: the argument is the angle in radian
- scale: the 2 arguments are the scaling factor on the X and Y axes
- brightness: the argument is a multiplier applied on the grey channel of the image
- alpha: the argument is a multiplier applied on the alpha channel of the image)");
app.get_formatter()->column_width(40);
CLI11_PARSE(app, argc, argv);
if (app.count("--text") + app.count("--image") != 1)
{
std::cerr << app.help();
return 1;
}
sdbus::ObjectPath bling_path;
std::unique_ptr<sdbus::IConnection> session_bus;
std::unique_ptr<BlingDaemonProxy> daemon;
try
{
session_bus = sdbus::createSessionBusConnection();
daemon = std::make_unique<BlingDaemonProxy>(*session_bus);
if (app.count("--text"))
{
bling_path = daemon->CreateText(text, font);
}
else if (app.count("--image"))
{
bling_path = daemon->CreateImage(std::filesystem::absolute(image));
}
else
{
assert(false);
}
BlingProxy bling(*session_bus, bling_path);
std::vector<std::string_view> split;
for (std::string & i: fx)
{
split_string(split, i, ':');
assert(!split.empty());
std::string_view type = split.front();
gsl::span<std::string_view> data = gsl::make_span(split).subspan(1);
if (type == "translate")
bling.AddTranslate(vector_to_vector_of_tuple_3(data));
else if (type == "rotate")
bling.AddRotate(vector_to_vector_of_tuple_2(data));
else if (type == "scale")
bling.AddScale(vector_to_vector_of_tuple_3(data));
else if (type == "brightness")
bling.AddBrightness(vector_to_vector_of_tuple_2(data));
else if (type == "alpha")
bling.AddAlpha(vector_to_vector_of_tuple_2(data));
else
throw std::invalid_argument("Invalid effect: " + std::string(type));
}
daemon->Show(bling_path, duration, z_order);
fmt::print("Bling {} created\n", bling_path);
}
catch (std::exception & e)
{
fmt::print(stderr, "{}\n", e.what());
if (bling_path != "")
daemon->Destroy(bling_path);
return 1;
}
}