KeyMap.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. #include "core/KeyMap.h"
  2. #include <algorithm>
  3. #include <cctype>
  4. namespace lynx::keys
  5. {
  6. namespace
  7. {
  8. struct KeyName
  9. {
  10. int key;
  11. const char* name;
  12. };
  13. const KeyName keyNames[] = {
  14. { space, "Space" }, { enter, "Enter" }, { escape, "Esc" }, { tab, "Tab" }, { backspace, "Backspace" },
  15. { del, "Delete" }, { insert, "Insert" }, { up, "Up" }, { down, "Down" }, { left, "Left" },
  16. { right, "Right" }, { pageUp, "PageUp" }, { pageDown, "PageDown" }, { home, "Home" }, { end, "End" },
  17. { numpadPlus, "Numpad +" }, { numpadMinus, "Numpad -" },
  18. { numpad0, "Numpad 0" }, { numpad1, "Numpad 1" }, { numpad2, "Numpad 2" }, { numpad3, "Numpad 3" }, { numpad4, "Numpad 4" },
  19. { numpad5, "Numpad 5" }, { numpad6, "Numpad 6" }, { numpad7, "Numpad 7" }, { numpad8, "Numpad 8" }, { numpad9, "Numpad 9" },
  20. };
  21. std::string lower (std::string s)
  22. {
  23. for (auto& c : s)
  24. c = (char) std::tolower ((unsigned char) c);
  25. return s;
  26. }
  27. std::string trim (const std::string& s)
  28. {
  29. const auto a = s.find_first_not_of (" \t\r\n");
  30. const auto b = s.find_last_not_of (" \t\r\n");
  31. return a == std::string::npos ? std::string() : s.substr (a, b - a + 1);
  32. }
  33. Chord chord (int key, std::uint8_t mods = none)
  34. {
  35. return { key, mods };
  36. }
  37. std::vector<Action> makeActions()
  38. {
  39. std::vector<Action> list {
  40. // transport and modes
  41. { "transport.playStop", TR_NOOP ("Play / stop (back to where playback started)"), Context::global, { chord (space) } },
  42. { "transport.pause", TR_NOOP ("Play / pause"), Context::global, { chord (space, ctrl) } },
  43. { "transport.toggleMode", TR_NOOP ("Pattern / song mode"), Context::global, { chord ('L') }, true },
  44. { "view.typingPiano", TR_NOOP ("Typing keyboard as a piano on/off"), Context::global, { chord ('T', ctrl) } },
  45. { "view.toolbar", TR_NOOP ("Show or hide the toolbar panels"), Context::global, { chord (f11, ctrl) } },
  46. { "view.audioEditor", TR_NOOP ("Open the audio editor"), Context::global, {} },
  47. { "view.tempoTapper", TR_NOOP ("Tempo tapper (tap the tempo)"), Context::global, { chord (f5, ctrl) } },
  48. { "view.sampleBrowser", TR_NOOP ("Sample browser (separate window)"), Context::global, {} },
  49. // workspace panels
  50. { "panel.playlist", TR_NOOP ("Show, focus or hide the Playlist"), Context::global, { chord (f5) } },
  51. { "panel.channelRack", TR_NOOP ("Show, focus or hide the Channel Rack"), Context::global, { chord (f6) } },
  52. { "panel.pianoRoll", TR_NOOP ("Show, focus or hide the Piano Roll"), Context::global, { chord (f7) } },
  53. { "panel.browser", TR_NOOP ("Show, focus or hide the Browser"), Context::global, { chord (f8, alt) } },
  54. { "panel.quickAdd", TR_NOOP ("Quick-add palette: instruments and effects"), Context::global, { chord (f8) } },
  55. { "panel.mixer", TR_NOOP ("Show, focus or hide the Mixer"), Context::global, { chord (f9) } },
  56. // FL 2026's other window keys (its shortcut list, REA round 165a)
  57. { "view.pluginDatabase", TR_NOOP ("Plugin database (in the Browser)"), Context::global, { chord (f8, shift) } },
  58. { "view.midiSettings", TR_NOOP ("MIDI settings"), Context::global, { chord (f10) } },
  59. { "view.projectInfo", TR_NOOP ("Project info"), Context::global, { chord (f11) } },
  60. { "view.closeWindows", TR_NOOP ("Close all windows (plugins, channel settings, tools)"), Context::global, { chord (f12) } },
  61. { "view.closePluginWindows", TR_NOOP ("Close all plugin windows"), Context::global, { chord (f12, alt) } },
  62. { "panel.maximisePlaylist", TR_NOOP ("Maximize or restore the Playlist"), Context::global, { chord (enter) } },
  63. // patterns
  64. { "pattern.next", TR_NOOP ("Next pattern"), Context::global, { chord (numpadPlus) } },
  65. { "pattern.previous", TR_NOOP ("Previous pattern"), Context::global, { chord (numpadMinus) } },
  66. { "pattern.nextEmpty", TR_NOOP ("Next empty pattern (and name it)"), Context::global, { chord (f4) } },
  67. { "pattern.rename", TR_NOOP ("Rename and color the pattern"), Context::global, { chord (f2) } },
  68. { "pattern.insert", TR_NOOP ("Insert an empty pattern before the current one"), Context::global, { chord (insert, ctrl | shift) } },
  69. { "pattern.clone", TR_NOOP ("Clone the current pattern"), Context::global, { chord ('C', alt) } },
  70. { "pattern.moveUp", TR_NOOP ("Move the current pattern up"), Context::global, { chord (up, ctrl | shift) } },
  71. { "pattern.moveDown", TR_NOOP ("Move the current pattern down"), Context::global, { chord (down, ctrl | shift) } },
  72. // recording
  73. { "transport.record", TR_NOOP ("Recording on/off"), Context::global, { chord ('R') } },
  74. { "transport.metronome", TR_NOOP ("Metronome on/off"), Context::global, { chord ('M', ctrl) } },
  75. { "record.countIn", TR_NOOP ("Count-in before recording on/off"), Context::global, { chord ('P', ctrl) } },
  76. { "record.waitForInput", TR_NOOP ("Recording waits for the first note on/off"), Context::global, { chord ('I', ctrl) } },
  77. { "record.quickAudio", TR_NOOP ("Record audio now: arm the selected insert and record (press again to stop)"), Context::global, { chord ('R', shift) } },
  78. // files and history
  79. { "file.new", TR_NOOP ("New project"), Context::global, { chord ('N', ctrl) } },
  80. { "file.open", TR_NOOP ("Open project"), Context::global, { chord ('O', ctrl) } },
  81. { "file.save", TR_NOOP ("Save project"), Context::global, { chord ('S', ctrl) } },
  82. { "file.saveAs", TR_NOOP ("Save project as"), Context::global, { chord ('S', ctrl | shift) } },
  83. { "file.saveNewVersion", TR_NOOP ("Save new version (Song_2, Song_3...)"), Context::global, { chord ('S', ctrl | alt) } },
  84. { "file.export", TR_NOOP ("Export audio"), Context::global, { chord ('R', ctrl) } },
  85. { "edit.undo", TR_NOOP ("Undo"), Context::global, { chord ('Z', ctrl) } },
  86. { "edit.redo", TR_NOOP ("Redo"), Context::global, { chord ('Y', ctrl), chord ('Z', ctrl | shift) } },
  87. { "edit.history", TR_NOOP ("Undo history"), Context::global, { chord ('H', ctrl) } },
  88. { "help.shortcuts", TR_NOOP ("Shortcut sheet"), Context::global, { chord (f1) } },
  89. { "browser.search", TR_NOOP ("Search the Browser"), Context::global, { chord ('F', ctrl) } },
  90. // Channel Rack (focused)
  91. { "rack.selectPrevious", TR_NOOP ("Select the channel above"), Context::channelRack, { chord (up) } },
  92. { "rack.selectNext", TR_NOOP ("Select the channel below"), Context::channelRack, { chord (down) } },
  93. { "rack.extendUp", TR_NOOP ("Extend the selection up"), Context::channelRack, { chord (up, shift) } },
  94. { "rack.extendDown", TR_NOOP ("Extend the selection down"), Context::channelRack, { chord (down, shift) } },
  95. { "rack.moveUp", TR_NOOP ("Move the selected channels up"), Context::channelRack, { chord (up, alt) } },
  96. { "rack.moveDown", TR_NOOP ("Move the selected channels down"), Context::channelRack, { chord (down, alt) } },
  97. { "rack.selectAll", TR_NOOP ("Select all channels"), Context::channelRack, { chord ('A', ctrl) } },
  98. { "rack.clone", TR_NOOP ("Clone the selected channels"), Context::channelRack, { chord ('C', alt) } },
  99. { "rack.deleteSelected", TR_NOOP ("Delete the selected channels"), Context::channelRack, { chord (del, alt) } },
  100. { "rack.sendToInserts", TR_NOOP ("Send the selected channels to free mixer inserts"), Context::channelRack, { chord ('L', ctrl) } },
  101. { "rack.graphEditor", TR_NOOP ("Step graph lanes on/off"), Context::channelRack, { chord ('K', ctrl) } },
  102. { "rack.pianoRollView", TR_NOOP ("Piano-roll view of all channels on/off"), Context::channelRack, {} },
  103. { "rack.groupSelected", TR_NOOP ("Put the selected channels in a new filter group"), Context::channelRack, { chord ('G', alt) } },
  104. { "rack.rotateLeft", TR_NOOP ("Rotate the selected channels' steps left"), Context::channelRack, { chord (left, ctrl | shift) } },
  105. { "rack.rotateRight", TR_NOOP ("Rotate the selected channels' steps right"), Context::channelRack, { chord (right, ctrl | shift) } },
  106. // Piano Roll (focused): tool dialogs and zoom
  107. { "pianoRoll.arpeggiate", TR_NOOP ("Arpeggiate..."), Context::pianoRoll, { chord ('A', alt) } },
  108. { "pianoRoll.strum", TR_NOOP ("Strum..."), Context::pianoRoll, { chord ('S', alt) } },
  109. { "pianoRoll.flip", TR_NOOP ("Flip..."), Context::pianoRoll, { chord ('Y', alt) } },
  110. { "pianoRoll.randomise", TR_NOOP ("Randomise..."), Context::pianoRoll, { chord ('R', alt) } },
  111. { "pianoRoll.scaleLevels", TR_NOOP ("Scale levels..."), Context::pianoRoll, { chord ('X', alt) } },
  112. { "pianoRoll.limit", TR_NOOP ("Limit to a range..."), Context::pianoRoll, { chord ('K', alt) } },
  113. // the other tools with FL's keys (REA round 143: the piano roll Tools menu)
  114. { "pianoRoll.quantize", TR_NOOP ("Quantize..."), Context::pianoRoll, { chord ('Q', alt) } },
  115. { "pianoRoll.chop", TR_NOOP ("Chop..."), Context::pianoRoll, { chord ('U', alt) } },
  116. { "pianoRoll.flam", TR_NOOP ("Flam..."), Context::pianoRoll, { chord ('F', alt) } },
  117. { "pianoRoll.claw", TR_NOOP ("Claw machine..."), Context::pianoRoll, { chord ('W', alt) } },
  118. { "pianoRoll.lfo", TR_NOOP ("LFO..."), Context::pianoRoll, { chord ('O', alt) } },
  119. { "pianoRoll.articulate", TR_NOOP ("Articulate..."), Context::pianoRoll, { chord ('L', alt) } },
  120. { "pianoRoll.riffMachine", TR_NOOP ("Riff machine..."), Context::pianoRoll, { chord ('E', alt) } },
  121. { "pianoRoll.chordProgression", TR_NOOP ("Generate chord progression..."), Context::pianoRoll, { chord ('P', alt) } },
  122. // note colours and the Select menu (REA round 153: FL's keys from its piano roll manual)
  123. { "pianoRoll.colourSelected", TR_NOOP ("Colour the selected notes with the note colour"), Context::pianoRoll, { chord ('C', alt) } },
  124. { "pianoRoll.selectByColour", TR_NOOP ("Select the notes of the same colour"), Context::pianoRoll, { chord ('C', shift) } },
  125. { "pianoRoll.invertSelection", TR_NOOP ("Invert the note selection"), Context::pianoRoll, { chord ('I', shift) } },
  126. { "pianoRoll.groupNotes", TR_NOOP ("Group the selected notes"), Context::pianoRoll, { chord ('G', shift) } },
  127. { "pianoRoll.ungroupNotes", TR_NOOP ("Ungroup the selected notes"), Context::pianoRoll, { chord ('G', alt) } },
  128. { "pianoRoll.zoomIn", TR_NOOP ("Zoom in"), Context::pianoRoll, { chord (pageUp) } },
  129. { "pianoRoll.zoomOut", TR_NOOP ("Zoom out"), Context::pianoRoll, { chord (pageDown) } },
  130. { "pianoRoll.zoom1", TR_NOOP ("Zoom: one bar"), Context::pianoRoll, { chord ('1', shift) }, true },
  131. { "pianoRoll.zoom2", TR_NOOP ("Zoom: four bars"), Context::pianoRoll, { chord ('2', shift) }, true },
  132. { "pianoRoll.zoom3", TR_NOOP ("Zoom: sixteen bars"), Context::pianoRoll, { chord ('3', shift) }, true },
  133. { "pianoRoll.zoomAll", TR_NOOP ("Zoom to show everything"), Context::pianoRoll, { chord ('4', shift) }, true },
  134. { "pianoRoll.zoomSelection", TR_NOOP ("Zoom to the selection"), Context::pianoRoll, { chord ('5', shift) }, true },
  135. { "pianoRoll.centrePlayhead", TR_NOOP ("Centre the view on the playhead"), Context::pianoRoll, { chord ('0', shift) }, true },
  136. // quick quantize (round 162, issue #9): FL's Ctrl+Q / Shift+Q, plus a 50% one to give a key of your own
  137. { "pianoRoll.quickQuantize", TR_NOOP ("Quick quantize (starts and lengths)"), Context::pianoRoll, { chord ('Q', ctrl) } },
  138. { "pianoRoll.quantizeStarts", TR_NOOP ("Quantize starts"), Context::pianoRoll, { chord ('Q', shift) }, true },
  139. { "pianoRoll.quantizeHalf", TR_NOOP ("Quantize starts 50% (halfway to the grid)"), Context::pianoRoll, {} },
  140. { "pianoRoll.follow", TR_NOOP ("Follow the playhead (auto scrolling) on/off"), Context::pianoRoll, {} },
  141. // Playlist (focused)
  142. { "playlist.zoomIn", TR_NOOP ("Zoom in"), Context::playlist, { chord (pageUp) } },
  143. { "playlist.zoomOut", TR_NOOP ("Zoom out"), Context::playlist, { chord (pageDown) } },
  144. { "playlist.zoom1", TR_NOOP ("Zoom: one bar"), Context::playlist, { chord ('1', shift) }, true },
  145. { "playlist.zoom2", TR_NOOP ("Zoom: four bars"), Context::playlist, { chord ('2', shift) }, true },
  146. { "playlist.zoom3", TR_NOOP ("Zoom: sixteen bars"), Context::playlist, { chord ('3', shift) }, true },
  147. { "playlist.zoomAll", TR_NOOP ("Zoom to show everything"), Context::playlist, { chord ('4', shift) }, true },
  148. { "playlist.zoomSelection", TR_NOOP ("Zoom to the selection"), Context::playlist, { chord ('5', shift) }, true },
  149. { "playlist.centrePlayhead", TR_NOOP ("Centre the view on the playhead"), Context::playlist, { chord ('0', shift) }, true },
  150. { "playlist.rippleDelete", TR_NOOP ("Delete the selected clips and close the gaps"), Context::playlist, { chord (del, shift) } },
  151. { "playlist.ripple", TR_NOOP ("Ripple editing on/off"), Context::playlist, { chord ('S', shift) } },
  152. { "playlist.quantizeStarts", TR_NOOP ("Quantize the clips' starts"), Context::playlist, { chord ('Q', ctrl) } },
  153. { "playlist.quantizeHalf", TR_NOOP ("Quantize the clips' starts 50% (halfway to the grid)"), Context::playlist, {} },
  154. { "playlist.follow", TR_NOOP ("Follow the playhead (auto scrolling) on/off"), Context::playlist, {} },
  155. // Browser (focused)
  156. { "browser.previous", TR_NOOP ("Previous file (preview)"), Context::browser, { chord (up) } },
  157. { "browser.next", TR_NOOP ("Next file (preview)"), Context::browser, { chord (down) } },
  158. { "browser.swapPrevious", TR_NOOP ("Previous file into the selected channel"), Context::browser, { chord (up, shift) } },
  159. { "browser.swapNext", TR_NOOP ("Next file into the selected channel"), Context::browser, { chord (down, shift) } },
  160. { "browser.load", TR_NOOP ("Load the file into the selected channel"), Context::browser, { chord (enter) } },
  161. { "browser.closeFolder", TR_NOOP ("Close the folder (or go to the parent folder)"), Context::browser, { chord (left) } },
  162. { "browser.openFolder", TR_NOOP ("Open the folder"), Context::browser, { chord (right) } },
  163. { "browser.rename", TR_NOOP ("Rename the selected plugin (Plugin database)"), Context::browser, { chord (f2) } },
  164. // Mixer (focused)
  165. { "mixer.moveLeft", TR_NOOP ("Move the selected inserts left"), Context::mixer, { chord (left, alt) } },
  166. { "mixer.moveRight", TR_NOOP ("Move the selected inserts right"), Context::mixer, { chord (right, alt) } },
  167. { "mixer.linkChannels", TR_NOOP ("Link the selected channels to the selected insert"), Context::mixer, { chord ('L', ctrl) } },
  168. { "mixer.linkChannelsFrom", TR_NOOP ("Link the selected channels to inserts starting from the selected one"), Context::mixer, { chord ('L', ctrl | shift) } },
  169. { "mixer.selectLinkedChannels", TR_NOOP ("Select the channels linked to the selected insert"), Context::mixer, { chord ('L', alt) } },
  170. { "mixer.deleteEffect", TR_NOOP ("Delete the selected effect slot's plugin"), Context::mixer, { chord (del) } },
  171. };
  172. // numeric keypad 1-9: select patterns 1-9
  173. static const char* patternIds[] = { "pattern.select1", "pattern.select2", "pattern.select3", "pattern.select4", "pattern.select5",
  174. "pattern.select6", "pattern.select7", "pattern.select8", "pattern.select9" };
  175. for (int i = 0; i < 9; ++i)
  176. list.push_back ({ patternIds[i], TR_NOOP ("Select pattern {0}"), Context::global, { chord (numpad1 + i) } });
  177. // number keys: mute (plain) / solo (Ctrl) channels 1-10 while the rack has focus
  178. static const char* muteIds[] = { "rack.mute1", "rack.mute2", "rack.mute3", "rack.mute4", "rack.mute5",
  179. "rack.mute6", "rack.mute7", "rack.mute8", "rack.mute9", "rack.mute10" };
  180. static const char* soloIds[] = { "rack.solo1", "rack.solo2", "rack.solo3", "rack.solo4", "rack.solo5",
  181. "rack.solo6", "rack.solo7", "rack.solo8", "rack.solo9", "rack.solo10" };
  182. for (int i = 0; i < 10; ++i)
  183. {
  184. const auto digit = i == 9 ? '0' : (char) ('1' + i);
  185. list.push_back ({ muteIds[i], TR_NOOP ("Mute or unmute channel {0}"), Context::channelRack, { chord (digit) } });
  186. list.push_back ({ soloIds[i], TR_NOOP ("Solo channel {0}"), Context::channelRack, { chord (digit, ctrl) } });
  187. }
  188. return list;
  189. }
  190. bool contextsOverlap (Context a, Context b)
  191. {
  192. return a == b;
  193. }
  194. // --- tiny JSON reader for {"id": "chord" | ["chord", ...], ...} ---------------------------------
  195. struct Json
  196. {
  197. const std::string& s;
  198. std::size_t i = 0;
  199. void skip()
  200. {
  201. while (i < s.size() && std::isspace ((unsigned char) s[i]))
  202. ++i;
  203. }
  204. bool eat (char c)
  205. {
  206. skip();
  207. if (i < s.size() && s[i] == c)
  208. {
  209. ++i;
  210. return true;
  211. }
  212. return false;
  213. }
  214. std::optional<std::string> string()
  215. {
  216. skip();
  217. if (i >= s.size() || s[i] != '"')
  218. return std::nullopt;
  219. std::string out;
  220. for (++i; i < s.size(); ++i)
  221. {
  222. const auto c = s[i];
  223. if (c == '"')
  224. {
  225. ++i;
  226. return out;
  227. }
  228. if (c == '\\' && i + 1 < s.size())
  229. {
  230. const auto n = s[++i];
  231. out += n == 'n' ? '\n' : n == 't' ? '\t' : n;
  232. }
  233. else
  234. out += c;
  235. }
  236. return std::nullopt;
  237. }
  238. };
  239. } // namespace
  240. bool Chord::isPrintable() const noexcept
  241. {
  242. return key > ' ' && key < 0x7f;
  243. }
  244. std::string toString (const Chord& c)
  245. {
  246. std::string out;
  247. if (c.mods & ctrl)
  248. out += "Ctrl+";
  249. if (c.mods & shift)
  250. out += "Shift+";
  251. if (c.mods & alt)
  252. out += "Alt+";
  253. if (c.key >= f1 && c.key <= f12)
  254. return out + "F" + std::to_string (c.key - f1 + 1);
  255. for (const auto& k : keyNames)
  256. if (k.key == c.key)
  257. return out + k.name;
  258. if (c.isPrintable())
  259. return out + std::string (1, (char) c.key);
  260. return out + "?";
  261. }
  262. std::optional<Chord> parseChord (const std::string& text)
  263. {
  264. auto rest = trim (text);
  265. Chord c;
  266. // modifiers first ("+" alone, or "Numpad +", is a key, so only split on "+" followed by more text)
  267. for (;;)
  268. {
  269. const auto l = lower (rest);
  270. auto take = [&] (const char* prefix, std::uint8_t mod) {
  271. const std::string p (prefix);
  272. if (l.size() > p.size() && l.compare (0, p.size(), p) == 0)
  273. {
  274. c.mods |= mod;
  275. rest = trim (rest.substr (p.size()));
  276. return true;
  277. }
  278. return false;
  279. };
  280. if (! (take ("ctrl+", ctrl) || take ("shift+", shift) || take ("alt+", alt)))
  281. break;
  282. }
  283. const auto name = lower (rest);
  284. if (name.empty())
  285. return std::nullopt;
  286. if (name.size() >= 2 && name[0] == 'f' && std::all_of (name.begin() + 1, name.end(), [] (char ch) { return std::isdigit ((unsigned char) ch); }))
  287. {
  288. const auto n = std::stoi (name.substr (1));
  289. if (n < 1 || n > 12)
  290. return std::nullopt;
  291. c.key = f1 + n - 1;
  292. return c;
  293. }
  294. for (const auto& k : keyNames)
  295. if (lower (k.name) == name)
  296. {
  297. c.key = k.key;
  298. return c;
  299. }
  300. if (name == "del")
  301. c.key = del;
  302. else if (name == "ins")
  303. c.key = insert;
  304. else if (name == "escape")
  305. c.key = escape;
  306. else if (name == "return")
  307. c.key = enter;
  308. else if (rest.size() == 1 && rest[0] > ' ' && rest[0] < 0x7f)
  309. c.key = std::toupper ((unsigned char) rest[0]);
  310. else
  311. return std::nullopt;
  312. return c;
  313. }
  314. const std::vector<Action>& actions()
  315. {
  316. static const auto list = makeActions();
  317. return list;
  318. }
  319. const Action* findAction (const std::string& id)
  320. {
  321. for (const auto& a : actions())
  322. if (id == a.id)
  323. return &a;
  324. return nullptr;
  325. }
  326. int typingPianoNote (int key) noexcept
  327. {
  328. static const char lowerRow[] = "ZSXDCVGBHNJM";
  329. static const char upperRow[] = "Q2W3ER5T6Y7UI9O0P";
  330. const auto c = key >= 'a' && key <= 'z' ? key - 'a' + 'A' : key;
  331. for (int i = 0; lowerRow[i] != 0; ++i)
  332. if (lowerRow[i] == c)
  333. return 60 + i;
  334. for (int i = 0; upperRow[i] != 0; ++i)
  335. if (upperRow[i] == c)
  336. return 72 + i;
  337. return -1;
  338. }
  339. KeyMap::KeyMap()
  340. {
  341. resetToDefaults();
  342. }
  343. void KeyMap::resetToDefaults()
  344. {
  345. chords.clear();
  346. for (const auto& a : actions())
  347. chords.push_back (a.defaults);
  348. }
  349. bool KeyMap::playsNote (const Chord& c, bool typingPiano) noexcept
  350. {
  351. return typingPiano && (c.mods & (ctrl | alt | shift)) == 0 && typingPianoNote (c.key) >= 0;
  352. }
  353. std::string KeyMap::find (const Chord& c, Context focus, bool typingPiano) const
  354. {
  355. if (! c.isValid() || playsNote (c, typingPiano))
  356. return {};
  357. const auto plainPrintable = c.isPrintable() && (c.mods & (ctrl | alt)) == 0;
  358. const auto& list = actions();
  359. const Action* globalMatch = nullptr;
  360. for (std::size_t i = 0; i < list.size(); ++i)
  361. {
  362. const auto& a = list[i];
  363. if (a.context != Context::global && a.context != focus)
  364. continue;
  365. if (std::find (chords[i].begin(), chords[i].end(), c) == chords[i].end())
  366. continue;
  367. if (typingPiano && plainPrintable && ! a.whilePiano)
  368. continue;
  369. if (a.context == focus && focus != Context::global)
  370. return a.id; // the focused panel wins
  371. if (globalMatch == nullptr)
  372. globalMatch = &a;
  373. }
  374. return globalMatch != nullptr ? globalMatch->id : std::string();
  375. }
  376. const std::vector<Chord>& KeyMap::chordsFor (const std::string& actionId) const
  377. {
  378. static const std::vector<Chord> empty;
  379. const auto& list = actions();
  380. for (std::size_t i = 0; i < list.size(); ++i)
  381. if (actionId == list[i].id)
  382. return chords[i];
  383. return empty;
  384. }
  385. void KeyMap::setChords (const std::string& actionId, std::vector<Chord> newChords)
  386. {
  387. const auto& list = actions();
  388. for (std::size_t i = 0; i < list.size(); ++i)
  389. if (actionId == list[i].id)
  390. chords[i] = std::move (newChords);
  391. }
  392. bool KeyMap::loadJson (const std::string& json, std::vector<std::string>* errors)
  393. {
  394. Json p { json };
  395. auto fail = [&] (const std::string& message) {
  396. if (errors != nullptr)
  397. errors->push_back (message);
  398. };
  399. if (! p.eat ('{'))
  400. return false;
  401. if (p.eat ('}'))
  402. return true;
  403. for (;;)
  404. {
  405. const auto id = p.string();
  406. if (! id || ! p.eat (':'))
  407. return false;
  408. std::vector<std::string> values;
  409. if (p.eat ('['))
  410. {
  411. if (! p.eat (']'))
  412. for (;;)
  413. {
  414. const auto v = p.string();
  415. if (! v)
  416. return false;
  417. values.push_back (*v);
  418. if (p.eat (']'))
  419. break;
  420. if (! p.eat (','))
  421. return false;
  422. }
  423. }
  424. else if (const auto v = p.string())
  425. {
  426. if (! trim (*v).empty())
  427. values.push_back (*v);
  428. }
  429. else
  430. return false;
  431. if (findAction (*id) == nullptr)
  432. fail ("unknown action: " + *id);
  433. else
  434. {
  435. std::vector<Chord> parsed;
  436. for (const auto& v : values)
  437. if (const auto c = parseChord (v))
  438. parsed.push_back (*c);
  439. else
  440. fail ("unreadable key for " + *id + ": " + v);
  441. setChords (*id, parsed);
  442. }
  443. if (p.eat ('}'))
  444. return true;
  445. if (! p.eat (','))
  446. return false;
  447. }
  448. }
  449. std::string KeyMap::toJson() const
  450. {
  451. std::string out = "{\n";
  452. const auto& list = actions();
  453. for (std::size_t i = 0; i < list.size(); ++i)
  454. {
  455. out += " \"" + std::string (list[i].id) + "\": [";
  456. for (std::size_t k = 0; k < chords[i].size(); ++k)
  457. out += (k > 0 ? ", \"" : "\"") + toString (chords[i][k]) + "\"";
  458. out += i + 1 < list.size() ? "],\n" : "]\n";
  459. }
  460. return out + "}\n";
  461. }
  462. std::vector<std::pair<std::string, std::string>> KeyMap::conflicts() const
  463. {
  464. std::vector<std::pair<std::string, std::string>> out;
  465. const auto& list = actions();
  466. for (std::size_t a = 0; a < list.size(); ++a)
  467. for (std::size_t b = a + 1; b < list.size(); ++b)
  468. {
  469. if (! contextsOverlap (list[a].context, list[b].context))
  470. continue;
  471. for (const auto& c : chords[a])
  472. if (std::find (chords[b].begin(), chords[b].end(), c) != chords[b].end())
  473. {
  474. out.push_back ({ list[a].id, list[b].id });
  475. break;
  476. }
  477. }
  478. return out;
  479. }
  480. std::vector<std::string> KeyMap::actionsWith (const Chord& chord, Context context, const std::string& except) const
  481. {
  482. std::vector<std::string> out;
  483. const auto& list = actions();
  484. for (std::size_t i = 0; i < list.size(); ++i)
  485. if (list[i].context == context && except != list[i].id
  486. && std::find (chords[i].begin(), chords[i].end(), chord) != chords[i].end())
  487. out.push_back (list[i].id);
  488. return out;
  489. }
  490. bool KeyMap::isDefault (const std::string& actionId) const
  491. {
  492. const auto& list = actions();
  493. for (std::size_t i = 0; i < list.size(); ++i)
  494. if (actionId == list[i].id)
  495. return chords[i] == list[i].defaults;
  496. return true;
  497. }
  498. std::string KeyMap::overridesJson() const
  499. {
  500. std::string out = "{\n";
  501. const auto& list = actions();
  502. bool first = true;
  503. for (std::size_t i = 0; i < list.size(); ++i)
  504. {
  505. if (chords[i] == list[i].defaults)
  506. continue;
  507. out += first ? "" : ",\n";
  508. first = false;
  509. out += " \"" + std::string (list[i].id) + "\": [";
  510. for (std::size_t k = 0; k < chords[i].size(); ++k)
  511. out += (k > 0 ? ", \"" : "\"") + toString (chords[i][k]) + "\"";
  512. out += "]";
  513. }
  514. return out + (first ? "}\n" : "\n}\n");
  515. }
  516. } // namespace lynx::keys