Skip to content

Commit 3af183b

Browse files
committed
chore: simplify config options with set_option
1 parent c746c18 commit 3af183b

1 file changed

Lines changed: 96 additions & 139 deletions

File tree

src/Config.cpp

Lines changed: 96 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,37 @@ template <typename T> void connect(const std::pair<bool, T> &pair, T *target) {
7979
*target = pair.second;
8080
}
8181

82+
template <typename T>
83+
void set_option(std::string name, const std::vector<std::string> &options_src,
84+
const std::vector<T> &options_dst,
85+
const std::pair<bool, std::string> &source, T *target) {
86+
// sizes should be equal
87+
assert(options_src.size() == options_dst.size());
88+
89+
// source needs to be set
90+
if (!source.first)
91+
return;
92+
93+
// find option in src and set dst
94+
for (size_t i = 0; i != options_src.size(); ++i)
95+
if (options_src[i] == source.second) {
96+
*target = options_dst[i];
97+
return;
98+
}
99+
100+
// option was not found
101+
std::string options = "";
102+
for (const std::string &option : options_src)
103+
options += option + "', '";
104+
105+
// remove trailing "', '"
106+
options = options.substr(0, options.size() - 4);
107+
108+
// send notification
109+
notify_send("Config", "No such option in %s ['%s']: %s", name.c_str(),
110+
options.c_str(), source.second.c_str());
111+
}
112+
82113
Config::Config() {
83114
path = "";
84115
last_write_time = std::filesystem::file_time_type::min();
@@ -222,25 +253,6 @@ bool Config::load() {
222253
connect(xcursor_table->getInt("size"), &cursor.xcursor.size);
223254
}
224255

225-
// used for both mouse and touchpad
226-
auto pointer_profile = [&](toml::Table *table,
227-
libinput_config_accel_profile *dest,
228-
std::string name) {
229-
if (auto [fst, snd] = table->getString("profile"); fst) {
230-
if (snd == "none")
231-
*dest = LIBINPUT_CONFIG_ACCEL_PROFILE_NONE;
232-
else if (snd == "flat")
233-
*dest = LIBINPUT_CONFIG_ACCEL_PROFILE_FLAT;
234-
else if (snd == "adaptive")
235-
*dest = LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE;
236-
else
237-
notify_send("Config",
238-
"No such option in pointer.%s.profile ['none', "
239-
"'flat', 'adaptive']: %s",
240-
name.c_str(), snd.c_str());
241-
}
242-
};
243-
244256
// mouse
245257
std::unique_ptr<toml::Table> mouse = pointer->getTable("mouse");
246258
if (mouse) {
@@ -252,7 +264,11 @@ bool Config::load() {
252264
connect(mouse->getBool("left_handed"), &cursor.mouse.left_handed);
253265

254266
// profile
255-
pointer_profile(mouse.get(), &cursor.mouse.profile, "mouse");
267+
set_option("pointer.mouse.profile", {"none", "flat", "adaptive"},
268+
{LIBINPUT_CONFIG_ACCEL_PROFILE_NONE,
269+
LIBINPUT_CONFIG_ACCEL_PROFILE_FLAT,
270+
LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE},
271+
mouse->getString("profile"), &cursor.mouse.profile);
256272

257273
// accel speed
258274
connect(mouse->getDouble("accel_speed"), &cursor.mouse.accel_speed);
@@ -262,66 +278,41 @@ bool Config::load() {
262278
std::unique_ptr<toml::Table> touchpad = pointer->getTable("touchpad");
263279
if (touchpad) {
264280
// tap to click
265-
auto tap_to_click = touchpad->getBool("tap_to_click");
266-
if (tap_to_click.first)
281+
if (auto tap_to_click = touchpad->getBool("tap_to_click");
282+
tap_to_click.first)
267283
cursor.touchpad.tap_to_click =
268284
static_cast<libinput_config_tap_state>(tap_to_click.second);
269285

270286
// tap and drag
271-
auto tap_and_drag = touchpad->getBool("tap_and_drag");
272-
if (tap_and_drag.first)
287+
if (auto tap_and_drag = touchpad->getBool("tap_and_drag");
288+
tap_and_drag.first)
273289
cursor.touchpad.tap_and_drag =
274290
static_cast<libinput_config_drag_state>(
275291
tap_and_drag.second);
276292

277293
// drag lock
278-
auto drag_lock = touchpad->getString("drag_lock");
279-
if (drag_lock.first) {
280-
if (drag_lock.second == "none")
281-
cursor.touchpad.drag_lock =
282-
LIBINPUT_CONFIG_DRAG_LOCK_DISABLED;
283-
else if (drag_lock.second == "timeout")
284-
cursor.touchpad.drag_lock =
285-
LIBINPUT_CONFIG_DRAG_LOCK_ENABLED_TIMEOUT;
286-
else if (drag_lock.second == "enabled")
287-
cursor.touchpad.drag_lock =
288-
LIBINPUT_CONFIG_DRAG_LOCK_ENABLED;
289-
else if (drag_lock.second == "sticky")
290-
cursor.touchpad.drag_lock =
291-
LIBINPUT_CONFIG_DRAG_LOCK_ENABLED_STICKY;
292-
else
293-
notify_send(
294-
"Config",
295-
"No such option in pointer.touchpad.drag_lock ['none', "
296-
"'timeout', 'enabled', 'sticky']: %s",
297-
drag_lock.second.c_str());
298-
}
294+
set_option(
295+
"pointer.touchpad.drag_lock", {"none", "timeout", "sticky"},
296+
{LIBINPUT_CONFIG_DRAG_LOCK_DISABLED,
297+
LIBINPUT_CONFIG_DRAG_LOCK_ENABLED_TIMEOUT,
298+
LIBINPUT_CONFIG_DRAG_LOCK_ENABLED_STICKY},
299+
touchpad->getString("drag_lock"), &cursor.touchpad.drag_lock);
299300

300301
// tap button map
301-
auto tap_button_map = touchpad->getString("tap_button_map");
302-
if (tap_button_map.first) {
303-
if (tap_button_map.second == "lrm")
304-
cursor.touchpad.tap_button_map =
305-
LIBINPUT_CONFIG_TAP_MAP_LRM;
306-
else if (tap_button_map.second == "lmr")
307-
cursor.touchpad.tap_button_map =
308-
LIBINPUT_CONFIG_TAP_MAP_LMR;
309-
else
310-
notify_send(
311-
"Config",
312-
"No such option in pointer.tap_button_map ['lrm', "
313-
"'lmr']: %s",
314-
tap_button_map.second.c_str());
315-
}
302+
set_option(
303+
"pointer.touchpad.tap_button_map", {"lrm", "lmr"},
304+
{LIBINPUT_CONFIG_TAP_MAP_LRM, LIBINPUT_CONFIG_TAP_MAP_LMR},
305+
touchpad->getString("tap_button_map"),
306+
&cursor.touchpad.tap_button_map);
316307

317308
// natural scroll
318309
connect(touchpad->getBool("natural_scroll"),
319310
&cursor.touchpad.natural_scroll);
320311

321312
// disable while typing
322-
auto disable_while_typing =
323-
touchpad->getBool("disable_while_typing");
324-
if (disable_while_typing.first)
313+
if (auto disable_while_typing =
314+
touchpad->getBool("disable_while_typing");
315+
disable_while_typing.first)
325316
cursor.touchpad.disable_while_typing =
326317
static_cast<libinput_config_dwt_state>(
327318
disable_while_typing.second);
@@ -331,75 +322,46 @@ bool Config::load() {
331322
&cursor.touchpad.left_handed);
332323

333324
// middle emulation
334-
auto middle_emulation = touchpad->getBool("middle_emulation");
335-
if (middle_emulation.first)
325+
if (auto middle_emulation = touchpad->getBool("middle_emulation");
326+
middle_emulation.first)
336327
cursor.touchpad.middle_emulation =
337328
static_cast<libinput_config_middle_emulation_state>(
338329
middle_emulation.second);
339330

340331
// scroll method
341-
auto scroll_method = touchpad->getString("scroll_method");
342-
if (scroll_method.first) {
343-
if (scroll_method.second == "none")
344-
cursor.touchpad.scroll_method =
345-
LIBINPUT_CONFIG_SCROLL_NO_SCROLL;
346-
else if (scroll_method.second == "2fg")
347-
cursor.touchpad.scroll_method = LIBINPUT_CONFIG_SCROLL_2FG;
348-
else if (scroll_method.second == "edge")
349-
cursor.touchpad.scroll_method = LIBINPUT_CONFIG_SCROLL_EDGE;
350-
else if (scroll_method.second == "button")
351-
cursor.touchpad.scroll_method =
352-
LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
353-
else
354-
notify_send("Config",
355-
"No such option in "
356-
"pointer.touchpad.scroll_method ['none', "
357-
"'2fg', 'edge', 'button']: %s",
358-
scroll_method.second.c_str());
359-
}
332+
set_option("pointer.touchpad.scroll_method",
333+
{"none", "2fg", "edge", "button"},
334+
{LIBINPUT_CONFIG_SCROLL_NO_SCROLL,
335+
LIBINPUT_CONFIG_SCROLL_2FG, LIBINPUT_CONFIG_SCROLL_EDGE,
336+
LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN},
337+
touchpad->getString("scroll_method"),
338+
&cursor.touchpad.scroll_method);
360339

361340
// click method
362-
auto click_method = touchpad->getString("click_method");
363-
if (click_method.first) {
364-
if (click_method.second == "none")
365-
cursor.touchpad.click_method =
366-
LIBINPUT_CONFIG_CLICK_METHOD_NONE;
367-
else if (click_method.second == "buttonareas")
368-
cursor.touchpad.click_method =
369-
LIBINPUT_CONFIG_CLICK_METHOD_BUTTON_AREAS;
370-
else if (click_method.second == "clickfinger")
371-
cursor.touchpad.click_method =
372-
LIBINPUT_CONFIG_CLICK_METHOD_CLICKFINGER;
373-
else
374-
notify_send("Config",
375-
"No such option in "
376-
"pointer.touchpad.click_method ['none', "
377-
"'buttonareas', 'clickfinger']: %s",
378-
click_method.second.c_str());
379-
}
341+
set_option("pointer.touchpad.click_method",
342+
{"none", "buttonareas", "clickfinger"},
343+
{LIBINPUT_CONFIG_CLICK_METHOD_NONE,
344+
LIBINPUT_CONFIG_CLICK_METHOD_BUTTON_AREAS,
345+
LIBINPUT_CONFIG_CLICK_METHOD_CLICKFINGER},
346+
touchpad->getString("click_method"),
347+
&cursor.touchpad.click_method);
380348

381349
// event mode
382-
if (auto [fst, snd] = touchpad->getString("event_mode"); fst) {
383-
if (snd == "enabled")
384-
cursor.touchpad.event_mode =
385-
LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
386-
else if (snd == "disabled")
387-
cursor.touchpad.event_mode =
388-
LIBINPUT_CONFIG_SEND_EVENTS_DISABLED;
389-
else if (snd == "mousedisabled")
390-
cursor.touchpad.event_mode =
391-
LIBINPUT_CONFIG_SEND_EVENTS_DISABLED_ON_EXTERNAL_MOUSE;
392-
else
393-
notify_send("Config",
394-
"No such option in pointer.touchpad.event_mode "
395-
"['enabled', "
396-
"'disabled', 'mousedisabled']: %s",
397-
snd.c_str());
398-
}
350+
set_option("pointer.touchpad.event_mode",
351+
{"enabled", "disabled", "mousedisabled"},
352+
{LIBINPUT_CONFIG_SEND_EVENTS_ENABLED,
353+
LIBINPUT_CONFIG_SEND_EVENTS_DISABLED,
354+
LIBINPUT_CONFIG_SEND_EVENTS_DISABLED_ON_EXTERNAL_MOUSE},
355+
touchpad->getString("event_mode"),
356+
&cursor.touchpad.event_mode);
399357

400358
// profile
401-
pointer_profile(touchpad.get(), &cursor.touchpad.profile,
402-
"touchpad");
359+
set_option("pointer.touchpad.profile", {"none", "flat", "adaptive"},
360+
{LIBINPUT_CONFIG_ACCEL_PROFILE_NONE,
361+
LIBINPUT_CONFIG_ACCEL_PROFILE_FLAT,
362+
LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE},
363+
touchpad->getString("profile"),
364+
&cursor.touchpad.profile);
403365

404366
// accel speed
405367
connect(touchpad->getDouble("accel_speed"),
@@ -424,15 +386,9 @@ bool Config::load() {
424386
config_file.table->getTable("tiling");
425387
if (tiling_table) {
426388
// method
427-
const std::string method_values[] = {"none", "grid", "master",
428-
"dwindle"};
429-
auto method = tiling_table->getString("method");
430-
if (method.first)
431-
for (int i = 0; i != TILE_DWINDLE + 1; ++i)
432-
if (method_values[i] == method.second) {
433-
tiling.method = static_cast<TileMethod>(i);
434-
break;
435-
}
389+
set_option("tiling.method", {"none", "grid", "master", "dwindle"},
390+
{TILE_NONE, TILE_GRID, TILE_MASTER, TILE_DWINDLE},
391+
tiling_table->getString("method"), &tiling.method);
436392
}
437393

438394
// get awm binds
@@ -571,15 +527,16 @@ bool Config::load() {
571527

572528
// transform
573529
auto transform = table.getString("transform");
574-
std::string transform_values[] = {
575-
"none", "90", "180", "270", "f", "f90", "f180", "f270"};
576-
if (transform.first)
577-
for (int i = 0; i != WL_OUTPUT_TRANSFORM_FLIPPED_270 + 1;
578-
++i)
579-
if (transform.second == transform_values[i]) {
580-
oc->transform = static_cast<wl_output_transform>(i);
581-
break;
582-
}
530+
set_option(
531+
"monitors.transform",
532+
{"none", "90", "180", "270", "f", "f90", "f180", "f270"},
533+
{WL_OUTPUT_TRANSFORM_NORMAL, WL_OUTPUT_TRANSFORM_90,
534+
WL_OUTPUT_TRANSFORM_180, WL_OUTPUT_TRANSFORM_270,
535+
WL_OUTPUT_TRANSFORM_FLIPPED,
536+
WL_OUTPUT_TRANSFORM_FLIPPED_90,
537+
WL_OUTPUT_TRANSFORM_FLIPPED_180,
538+
WL_OUTPUT_TRANSFORM_FLIPPED_270},
539+
table.getString("transform"), &oc->transform);
583540

584541
// scale
585542
connect<float>(table.getDouble("scale"), &oc->scale);

0 commit comments

Comments
 (0)