inLimbo
TUI Music Player that keeps you in Limbo.
 
Loading...
Searching...
No Matches
keymaps.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <cstdlib>
5#include <iostream>
6#include <string_view>
7#include <unordered_map>
8
44
54{
55 Keybinds keybinds;
56
57 // Mapping of special key names to their ASCII values
58 const std::unordered_map<std::string_view, char> special_keys = {
59 {"Tab", '\t'},
60 {"Space", ' '},
61 {"Enter", '\n'},
62 {"Esc", 27},
63 };
64
74 auto reportError = [](const std::string& field, const std::string_view& key)
75 {
76 std::cerr << "** Error: ⚠ Unsupported or empty keybind '" << key << "' detected for field '"
77 << field << "'. Modify or remove that keybind field. **" << std::endl;
78 std::exit(EXIT_FAILURE);
79 };
80
92 auto handle_special_keys = [&](const std::string_view& key, const std::string& field) -> char
93 {
94 if (key.empty())
95 {
96 reportError(field, key);
97 }
98 if (special_keys.find(key) != special_keys.end())
99 {
100 return special_keys.at(key);
101 }
102 if (key.size() == 1)
103 {
104 return key[0]; // Single-character keys
105 }
106 reportError(field, key);
107 return -1; // Unreachable case
108 };
109
110 // Map each field name to the corresponding member in the Keybinds struct
111 const std::unordered_map<std::string, char*> field_map = {
112 {"scroll_up", &keybinds.scroll_up},
113 {"scroll_down", &keybinds.scroll_down},
114 {"toggle_focus", &keybinds.toggle_focus},
115 {"show_help", &keybinds.show_help},
116 {"toggle_play", &keybinds.toggle_play},
117 {"play_song", &keybinds.play_song},
118 {"play_song_next", &keybinds.play_song_next},
119 {"play_song_prev", &keybinds.play_song_prev},
120 {"vol_up", &keybinds.vol_up},
121 {"vol_down", &keybinds.vol_down},
122 {"toggle_mute", &keybinds.toggle_mute},
123 {"quit_app", &keybinds.quit_app},
124 {"seek_ahead_5", &keybinds.seek_ahead_5},
125 {"seek_behind_5", &keybinds.seek_behind_5},
126 {"view_lyrics", &keybinds.view_lyrics},
127 {"replay_song", &keybinds.replay_song},
128 {"goto_main_screen", &keybinds.goto_main_screen},
129 {"add_song_to_queue", &keybinds.add_song_to_queue},
130 {"add_artists_songs_to_queue", &keybinds.add_artists_songs_to_queue},
131 {"remove_song_from_queue", &keybinds.remove_song_from_queue},
132 {"play_this_song_next", &keybinds.play_this_song_next},
133 {"view_song_queue", &keybinds.view_song_queue},
134 {"view_current_song_info", &keybinds.view_current_song_info},
135 {"toggle_audio_devices", &keybinds.toggle_audio_devices},
136 {"search_menu", &keybinds.search_menu},
137 {"search_item_next", &keybinds.search_item_next},
138 {"search_item_prev", &keybinds.search_item_prev}};
139
141 if (verbose_logging)
142 {
143 std::cout << "[KEYBINDS] Parsing keybinds..." << std::endl;
144 }
145
146 // Populate the keybinds struct by reading the fields from the TOML configuration
147 for (const auto& [field, member_ptr] : field_map)
148 {
149 const std::string_view key = parseTOMLField(PARENT_KEYBINDS, field);
150
151 if (verbose_logging)
152 {
153 std::cout << "[KEYBINDS] Parsing keybind for " << field << ": " << key << std::endl;
154 }
155
156 *member_ptr = handle_special_keys(key, field);
157
158 if (verbose_logging)
159 {
160 std::cout << "[KEYBINDS] Mapped keybind for " << field
161 << " to ASCII value: " << static_cast<int>(*member_ptr) << std::endl;
162 }
163 }
164
165 if (verbose_logging)
166 {
167 std::cout << "[KEYBINDS] Keybind parsing complete! Moving on..." << std::endl;
168 }
169
170 return keybinds;
171}
auto parseKeybinds() -> Keybinds
Parses the keybinds from the TOML configuration.
Definition keymaps.hpp:53
Struct to hold keybinding mappings.
Definition keymaps.hpp:15
char goto_main_screen
Definition keymaps.hpp:31
char toggle_audio_devices
Definition keymaps.hpp:39
char show_help
Definition keymaps.hpp:19
char search_item_next
Definition keymaps.hpp:41
char add_artists_songs_to_queue
Definition keymaps.hpp:34
char scroll_up
Definition keymaps.hpp:16
char seek_ahead_5
Definition keymaps.hpp:28
char toggle_mute
Definition keymaps.hpp:26
char search_menu
Definition keymaps.hpp:40
char replay_song
Definition keymaps.hpp:32
char vol_down
Definition keymaps.hpp:25
char view_song_queue
Definition keymaps.hpp:37
char view_lyrics
Definition keymaps.hpp:30
char play_song_prev
Definition keymaps.hpp:23
char search_item_prev
Definition keymaps.hpp:42
char seek_behind_5
Definition keymaps.hpp:29
char scroll_down
Definition keymaps.hpp:17
char play_song_next
Definition keymaps.hpp:22
char toggle_play
Definition keymaps.hpp:20
char remove_song_from_queue
Definition keymaps.hpp:35
char play_this_song_next
Definition keymaps.hpp:36
char toggle_focus
Definition keymaps.hpp:18
char vol_up
Definition keymaps.hpp:24
char add_song_to_queue
Definition keymaps.hpp:33
char quit_app
Definition keymaps.hpp:27
char view_current_song_info
Definition keymaps.hpp:38
char play_song
Definition keymaps.hpp:21
#define PARENT_DBG
Definition toml_parser.hpp:25
#define PARENT_DBG_FIELD_KEYBINDS_PARSER_LOG
Definition toml_parser.hpp:29
#define PARENT_KEYBINDS
Definition toml_parser.hpp:32
auto parseTOMLFieldBool(const string &parent, const string &field) -> bool
Definition toml_parser.hpp:202
auto parseTOMLField(string parent, string field) -> string_view
Parses a string field from the TOML configuration.
Definition toml_parser.hpp:144