KeyMap.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. #include "core/KeyMap.h"
  2. #include "core/ModifierMap.h"
  3. #include <algorithm>
  4. #include <cctype>
  5. namespace lynx::keys
  6. {
  7. namespace
  8. {
  9. struct KeyName
  10. {
  11. int key;
  12. const char* name;
  13. };
  14. const KeyName keyNames[] = {
  15. { space, "Space" }, { enter, "Enter" }, { escape, "Esc" }, { tab, "Tab" }, { backspace, "Backspace" },
  16. { del, "Delete" }, { insert, "Insert" }, { up, "Up" }, { down, "Down" }, { left, "Left" },
  17. { right, "Right" }, { pageUp, "PageUp" }, { pageDown, "PageDown" }, { home, "Home" }, { end, "End" },
  18. { numpadPlus, "Numpad +" }, { numpadMinus, "Numpad -" }, { numpadMultiply, "Numpad *" }, { numpadDivide, "Numpad /" },
  19. { numpad0, "Numpad 0" }, { numpad1, "Numpad 1" }, { numpad2, "Numpad 2" }, { numpad3, "Numpad 3" }, { numpad4, "Numpad 4" },
  20. { numpad5, "Numpad 5" }, { numpad6, "Numpad 6" }, { numpad7, "Numpad 7" }, { numpad8, "Numpad 8" }, { numpad9, "Numpad 9" },
  21. };
  22. std::string lower (std::string s)
  23. {
  24. for (auto& c : s)
  25. c = (char) std::tolower ((unsigned char) c);
  26. return s;
  27. }
  28. std::string trim (const std::string& s)
  29. {
  30. const auto a = s.find_first_not_of (" \t\r\n");
  31. const auto b = s.find_last_not_of (" \t\r\n");
  32. return a == std::string::npos ? std::string() : s.substr (a, b - a + 1);
  33. }
  34. Chord chord (int key, std::uint8_t mods = none)
  35. {
  36. return { key, mods };
  37. }
  38. std::vector<Action> makeActions()
  39. {
  40. std::vector<Action> list {
  41. // transport and modes
  42. { "transport.playStop", TR_NOOP ("Play / stop (back to where playback started)"), Context::global, { chord (space) } },
  43. { "transport.pause", TR_NOOP ("Play / pause"), Context::global, { chord (space, ctrl) } },
  44. // FL 2026 (checked in FL, round 165s): Shift+Space stops and goes back to the start of the song / pattern, or
  45. // plays from there when stopped; the position clicked on a ruler is ignored (issue #11)
  46. { "transport.playFromStart", TR_NOOP ("Play, or stop at the start of the song or pattern"), Context::global, { chord (space, shift) } },
  47. { "transport.toggleMode", TR_NOOP ("Pattern / song mode"), Context::global, { chord ('L') }, true },
  48. { "transport.nextMarker", TR_NOOP ("Jump to the next song marker (Quantize song jumps while playing)"), Context::global, { chord (numpadMultiply, alt) } },
  49. { "transport.previousMarker", TR_NOOP ("Jump to the previous song marker"), Context::global, { chord (numpadDivide, alt) } },
  50. { "view.typingPiano", TR_NOOP ("Typing keyboard to piano on/off"), Context::global, { chord ('T', ctrl) } },
  51. { "view.toolbar", TR_NOOP ("Show or hide the toolbar panels"), Context::global, { chord (f11, ctrl) } },
  52. { "view.audioEditor", TR_NOOP ("Open the audio editor"), Context::global, {} },
  53. { "view.tempoTapper", TR_NOOP ("Tempo tapper (tap the tempo)"), Context::global, { chord (f5, ctrl) } },
  54. { "view.sampleBrowser", TR_NOOP ("Sample browser (separate window)"), Context::global, {} },
  55. // workspace panels
  56. { "panel.playlist", TR_NOOP ("Show, focus or hide the Playlist"), Context::global, { chord (f5) } },
  57. { "panel.channelRack", TR_NOOP ("Show, focus or hide the Channel Rack"), Context::global, { chord (f6) } },
  58. { "panel.pianoRoll", TR_NOOP ("Show, focus or hide the Piano Roll"), Context::global, { chord (f7) } },
  59. { "panel.browser", TR_NOOP ("Show, focus or hide the Browser"), Context::global, { chord (f8, alt) } },
  60. // FL's F8 (ShowPluginPickerAction, round 167o): opens the plugin picker, or closes it when it is open
  61. { "panel.quickAdd", TR_NOOP ("Quick-add palette: instruments and effects (press again to close)"), Context::global, { chord (f8) } },
  62. { "panel.mixer", TR_NOOP ("Show, focus or hide the Mixer"), Context::global, { chord (f9) } },
  63. // FL 2026's other window keys (its shortcut list, REA round 165a)
  64. { "view.pluginDatabase", TR_NOOP ("Plugin database (in the Browser)"), Context::global, { chord (f8, shift) } },
  65. // F10 / F11 toggle like FL's (round 167o, settingsKeyPress): Settings on another page switches to this one
  66. { "view.midiSettings", TR_NOOP ("MIDI settings (press again to close)"), Context::global, { chord (f10) } },
  67. { "view.projectInfo", TR_NOOP ("Project info (press again to close)"), Context::global, { chord (f11) } },
  68. { "view.closeWindows", TR_NOOP ("Close all windows (plugins, channel settings, tools)"), Context::global, { chord (f12) } },
  69. { "view.closePluginWindows", TR_NOOP ("Close all plugin windows"), Context::global, { chord (f12, alt) } },
  70. // FL's CloseAllUnfocusedWindowsAction (Ctrl+F12) and ShowTouchControllerAction (Alt+F7), round 167o
  71. { "view.closeUnfocusedWindows", TR_NOOP ("Close all windows except the focused one"), Context::global, { chord (f12, ctrl) } },
  72. { "view.touchKeyboard", TR_NOOP ("Show or hide the Touch keyboard"), Context::global, { chord (f7, alt) } },
  73. { "panel.maximisePlaylist", TR_NOOP ("Maximize or restore the Playlist"), Context::global, { chord (enter) } },
  74. // patterns
  75. { "pattern.next", TR_NOOP ("Next pattern"), Context::global, { chord (numpadPlus) } },
  76. { "pattern.previous", TR_NOOP ("Previous pattern"), Context::global, { chord (numpadMinus) } },
  77. { "pattern.nextEmpty", TR_NOOP ("Next empty pattern (and name it)"), Context::global, { chord (f4) } },
  78. { "pattern.firstEmpty", TR_NOOP ("First empty pattern (and name it)"), Context::global, { chord (f4, shift) } },
  79. { "pattern.nextEmptyNoName", TR_NOOP ("Next empty pattern (no naming)"), Context::global, { chord (f4, ctrl) } },
  80. { "pattern.rename", TR_NOOP ("Rename and color the pattern"), Context::global, { chord (f2) } },
  81. { "pattern.insert", TR_NOOP ("Insert an empty pattern before the current one"), Context::global, { chord (insert, ctrl | shift) } },
  82. { "pattern.clone", TR_NOOP ("Clone the current pattern"), Context::global, { chord ('C', ctrl | shift), chord ('C', alt) } },
  83. { "pattern.delete", TR_NOOP ("Delete the current pattern"), Context::global, { chord (del, ctrl | shift) } },
  84. { "pattern.moveUp", TR_NOOP ("Move the current pattern up"), Context::global, { chord (up, ctrl | shift) } },
  85. { "pattern.moveDown", TR_NOOP ("Move the current pattern down"), Context::global, { chord (down, ctrl | shift) } },
  86. // recording
  87. { "transport.record", TR_NOOP ("Recording on/off"), Context::global, { chord ('R') } },
  88. { "transport.metronome", TR_NOOP ("Metronome on/off"), Context::global, { chord ('M', ctrl) } },
  89. { "record.countIn", TR_NOOP ("Count-in before recording on/off"), Context::global, { chord ('P', ctrl) } },
  90. { "record.waitForInput", TR_NOOP ("Recording waits for the first note on/off"), Context::global, { chord ('I', ctrl) } },
  91. { "record.quickAudio", TR_NOOP ("Record audio now: arm the selected insert and record (press again to stop)"), Context::global, { chord ('R', shift) } },
  92. // files and history
  93. { "file.new", TR_NOOP ("New project"), Context::global, { chord ('N', ctrl) } },
  94. { "file.open", TR_NOOP ("Open project"), Context::global, { chord ('O', ctrl) } },
  95. { "file.save", TR_NOOP ("Save project"), Context::global, { chord ('S', ctrl) } },
  96. { "file.saveAs", TR_NOOP ("Save project as"), Context::global, { chord ('S', ctrl | shift) } },
  97. { "file.saveNewVersion", TR_NOOP ("Save new version (Song_2, Song_3...)"), Context::global, { chord ('S', ctrl | alt) } },
  98. { "file.export", TR_NOOP ("Export audio"), Context::global, { chord ('R', ctrl) } },
  99. { "file.exportMp3", TR_NOOP ("Export an MP3 file"), Context::global, { chord ('R', ctrl | shift) } },
  100. { "file.exportMidi", TR_NOOP ("Export a MIDI file"), Context::global, { chord ('M', ctrl | shift) } },
  101. // FL's File › Export › Master... (FileExportMasterAction, Ctrl+Alt+M; REA round 168e)
  102. { "file.exportMaster", TR_NOOP ("Export and master the project"), Context::global, { chord ('M', ctrl | alt) } },
  103. // FL's Tools › Consolidate playlist selection (EERenderPLSelectionLengthAction Ctrl+Alt+C, EEPLRenderSongLengthAction
  104. // Ctrl+Shift+Alt+C; REA round 168e)
  105. { "tools.consolidateSelection", TR_NOOP ("Consolidate playlist selection (from selection start)"), Context::global, { chord ('C', ctrl | alt) } },
  106. { "tools.consolidateSelectionSongStart", TR_NOOP ("Consolidate playlist selection (from song start)"), Context::global, { chord ('C', ctrl | shift | alt) } },
  107. { "edit.undo", TR_NOOP ("Undo"), Context::global, { chord ('Z', ctrl) } },
  108. // FL's Edit › "Undo backwards" (EditUndoBackwardsAction, Ctrl+Alt+Z; round 167k): one more step back
  109. { "edit.undoBackwards", TR_NOOP ("Undo backwards"), Context::global, { chord ('Z', ctrl | alt) } },
  110. { "edit.redo", TR_NOOP ("Redo"), Context::global, { chord ('Y', ctrl), chord ('Z', ctrl | shift) } },
  111. { "edit.history", TR_NOOP ("Undo history"), Context::global, { chord ('H', ctrl) } },
  112. { "help.shortcuts", TR_NOOP ("Shortcut sheet (press again to close)"), Context::global, { chord (f1) } },
  113. { "browser.search", TR_NOOP ("Search the Browser"), Context::global, { chord ('F', ctrl) } },
  114. // Channel Rack (focused)
  115. { "rack.selectPrevious", TR_NOOP ("Select the channel above"), Context::channelRack, { chord (up) } },
  116. { "rack.selectNext", TR_NOOP ("Select the channel below"), Context::channelRack, { chord (down) } },
  117. { "rack.extendUp", TR_NOOP ("Extend the selection up"), Context::channelRack, { chord (up, shift) } },
  118. { "rack.extendDown", TR_NOOP ("Extend the selection down"), Context::channelRack, { chord (down, shift) } },
  119. { "rack.moveUp", TR_NOOP ("Move the selected channels up"), Context::channelRack, { chord (up, alt) } },
  120. { "rack.moveDown", TR_NOOP ("Move the selected channels down"), Context::channelRack, { chord (down, alt) } },
  121. { "rack.selectAll", TR_NOOP ("Select all channels"), Context::channelRack, { chord ('A', ctrl) } },
  122. { "rack.clone", TR_NOOP ("Clone the selected channels"), Context::channelRack, { chord ('C', alt) } },
  123. { "rack.deleteSelected", TR_NOOP ("Delete the selected channels"), Context::channelRack, { chord (del, alt) } },
  124. { "rack.sendToInserts", TR_NOOP ("Send the selected channels to free mixer inserts"), Context::channelRack, { chord ('L', ctrl) } },
  125. { "rack.graphEditor", TR_NOOP ("Step graph lanes on/off"), Context::channelRack, { chord ('K', ctrl) } },
  126. { "rack.pianoRollView", TR_NOOP ("Piano-roll view of all channels on/off"), Context::channelRack, {} },
  127. { "rack.follow", TR_NOOP ("Follow the playhead (page along with the playing step) on/off"), Context::channelRack, {} },
  128. { "rack.groupSelected", TR_NOOP ("Put the selected channels in a new filter group"), Context::channelRack, { chord ('G', alt) } },
  129. { "rack.rotateLeft", TR_NOOP ("Rotate the selected channels' steps left"), Context::channelRack, { chord (left, ctrl | shift) } },
  130. { "rack.rotateRight", TR_NOOP ("Rotate the selected channels' steps right"), Context::channelRack, { chord (right, ctrl | shift) } },
  131. // Piano Roll (focused): tool dialogs and zoom
  132. { "pianoRoll.arpeggiate", TR_NOOP ("Arpeggiate..."), Context::pianoRoll, { chord ('A', alt) } },
  133. { "pianoRoll.strum", TR_NOOP ("Strum..."), Context::pianoRoll, { chord ('S', alt) } },
  134. { "pianoRoll.flip", TR_NOOP ("Flip..."), Context::pianoRoll, { chord ('Y', alt) } },
  135. { "pianoRoll.randomise", TR_NOOP ("Randomise..."), Context::pianoRoll, { chord ('R', alt) } },
  136. { "pianoRoll.scaleLevels", TR_NOOP ("Scale levels..."), Context::pianoRoll, { chord ('X', alt) } },
  137. { "pianoRoll.limit", TR_NOOP ("Limit to a range..."), Context::pianoRoll, { chord ('K', alt) } },
  138. // the other tools with FL's keys (REA round 143: the piano roll Tools menu)
  139. { "pianoRoll.quantize", TR_NOOP ("Quantize..."), Context::pianoRoll, { chord ('Q', alt) } },
  140. { "pianoRoll.chop", TR_NOOP ("Chop..."), Context::pianoRoll, { chord ('U', alt) } },
  141. { "pianoRoll.flam", TR_NOOP ("Flam..."), Context::pianoRoll, { chord ('F', alt) } },
  142. { "pianoRoll.claw", TR_NOOP ("Claw machine..."), Context::pianoRoll, { chord ('W', alt) } },
  143. { "pianoRoll.lfo", TR_NOOP ("LFO..."), Context::pianoRoll, { chord ('O', alt) } },
  144. { "pianoRoll.articulate", TR_NOOP ("Articulate..."), Context::pianoRoll, { chord ('L', alt) } },
  145. { "pianoRoll.riffMachine", TR_NOOP ("Riff machine..."), Context::pianoRoll, { chord ('E', alt) } },
  146. { "pianoRoll.chordProgression", TR_NOOP ("Generate chord progression..."), Context::pianoRoll, { chord ('P', alt) } },
  147. // note colours and the Select menu (REA round 153: FL's keys from its piano roll manual)
  148. { "pianoRoll.colourSelected", TR_NOOP ("Colour the selected notes with the note colour"), Context::pianoRoll, { chord ('C', alt) } },
  149. { "pianoRoll.selectByColour", TR_NOOP ("Select the notes of the same colour"), Context::pianoRoll, { chord ('C', shift) } },
  150. { "pianoRoll.invertSelection", TR_NOOP ("Invert the note selection"), Context::pianoRoll, { chord ('I', shift) } },
  151. { "pianoRoll.groupNotes", TR_NOOP ("Group the selected notes"), Context::pianoRoll, { chord ('G', shift) } },
  152. { "pianoRoll.ungroupNotes", TR_NOOP ("Ungroup the selected notes"), Context::pianoRoll, { chord ('G', alt) } },
  153. { "pianoRoll.zoomIn", TR_NOOP ("Zoom in"), Context::pianoRoll, { chord (pageUp) } },
  154. { "pianoRoll.zoomOut", TR_NOOP ("Zoom out"), Context::pianoRoll, { chord (pageDown) } },
  155. { "pianoRoll.zoom1", TR_NOOP ("Zoom: one bar"), Context::pianoRoll, { chord ('1', shift) }, true },
  156. { "pianoRoll.zoom2", TR_NOOP ("Zoom: four bars"), Context::pianoRoll, { chord ('2', shift) }, true },
  157. { "pianoRoll.zoom3", TR_NOOP ("Zoom: sixteen bars"), Context::pianoRoll, { chord ('3', shift) }, true },
  158. { "pianoRoll.zoomAll", TR_NOOP ("Zoom to show everything"), Context::pianoRoll, { chord ('4', shift) }, true },
  159. { "pianoRoll.zoomSelection", TR_NOOP ("Zoom to the selection"), Context::pianoRoll, { chord ('5', shift) }, true },
  160. { "pianoRoll.centrePlayhead", TR_NOOP ("Centre the view on the playhead"), Context::pianoRoll, { chord ('0', shift) }, true },
  161. // quick quantize (round 162, issue #9): FL's Ctrl+Q / Shift+Q, plus a 50% one to give a key of your own
  162. { "pianoRoll.quickQuantize", TR_NOOP ("Quick quantize (starts and lengths)"), Context::pianoRoll, { chord ('Q', ctrl) } },
  163. { "pianoRoll.quantizeStarts", TR_NOOP ("Quantize starts"), Context::pianoRoll, { chord ('Q', shift) }, true },
  164. { "pianoRoll.quantizeHalf", TR_NOOP ("Quantize starts 50% (halfway to the grid)"), Context::pianoRoll, {} },
  165. { "pianoRoll.follow", TR_NOOP ("Follow the playhead (auto scrolling) on/off"), Context::pianoRoll, {} },
  166. // FL's Piano roll menu (round 168g, REA_rodada168g.md: the ShortcutsModule / EEMenu keys): View, Select, Stamp,
  167. // Edit and File items
  168. { "pianoRoll.gridHighlights", TR_NOOP ("Note grid highlights on/off"), Context::pianoRoll, { chord ('B', alt) } },
  169. { "pianoRoll.backgroundWave", TR_NOOP ("Background waveform on/off"), Context::pianoRoll, { chord ('N', alt) } },
  170. { "pianoRoll.showChordName", TR_NOOP ("Show chord name for selection on/off"), Context::pianoRoll, {} },
  171. { "pianoRoll.selectRandomOne", TR_NOOP ("Select one at random"), Context::pianoRoll, { chord ('R', shift) }, true },
  172. { "pianoRoll.selectRandomMore", TR_NOOP ("Select more at random"), Context::pianoRoll, { chord ('M', shift) }, true },
  173. { "pianoRoll.selectOddSteps", TR_NOOP ("Select odd (swung) notes"), Context::pianoRoll, { chord ('O', shift) }, true },
  174. { "pianoRoll.stampNone", TR_NOOP ("Stamp: none (single notes)"), Context::pianoRoll, { chord ('N', shift) }, true },
  175. { "pianoRoll.insertSpace", TR_NOOP ("Insert space (the time selection)"), Context::pianoRoll, { chord (insert, ctrl) } },
  176. { "pianoRoll.sliceInsertSpace", TR_NOOP ("Slice & insert space"), Context::pianoRoll, { chord (insert, ctrl | alt) } },
  177. { "pianoRoll.deleteSpace", TR_NOOP ("Delete space (the time selection)"), Context::pianoRoll, { chord (del, ctrl) } },
  178. { "pianoRoll.trimSpace", TR_NOOP ("Trim to the time selection"), Context::pianoRoll, {} },
  179. { "pianoRoll.deleteAll", TR_NOOP ("Delete all the channel's notes"), Context::pianoRoll, {} },
  180. { "pianoRoll.insertControllerValue", TR_NOOP ("Insert current controller value"), Context::pianoRoll, { chord ('I', ctrl) } },
  181. { "pianoRoll.importMidi", TR_NOOP ("Import MIDI file..."), Context::pianoRoll, { chord ('M', ctrl) } },
  182. { "pianoRoll.exportMidi", TR_NOOP ("Export as MIDI file..."), Context::pianoRoll, { chord ('M', ctrl | shift) } },
  183. { "pianoRoll.pasteMidiClipboard", TR_NOOP ("Paste from MIDI clipboard..."), Context::pianoRoll, { chord ('V', ctrl | shift) } },
  184. { "pianoRoll.copyMidiClipboard", TR_NOOP ("Copy to MIDI clipboard"), Context::pianoRoll, {} },
  185. { "pianoRoll.openScore", TR_NOOP ("Open score..."), Context::pianoRoll, {} },
  186. { "pianoRoll.saveScore", TR_NOOP ("Save score as..."), Context::pianoRoll, {} },
  187. { "pianoRoll.browseScores", TR_NOOP ("Browse scores"), Context::pianoRoll, {} },
  188. // FL's Piano roll › Time markers: the same EEMarker* items as the Playlist's, on the pattern's markers (round 168g)
  189. { "pianoRoll.markerAdd", TR_NOOP ("Pattern time markers: Add one..."), Context::pianoRoll, { chord ('T', alt) } },
  190. { "pianoRoll.markerAuto", TR_NOOP ("Pattern time markers: Add auto"), Context::pianoRoll, { chord ('T', ctrl) } },
  191. { "pianoRoll.markerSignature", TR_NOOP ("Pattern time markers: Add time signature change..."), Context::pianoRoll, { chord ('T', shift | alt) } },
  192. // the tools and the other single letters (FL's keys; issue #12: they were fixed in the editor and won over a
  193. // global shortcut given the same key)
  194. { "pianoRoll.tool.pencil", TR_NOOP ("Tool: Pencil"), Context::pianoRoll, { chord ('P') } },
  195. { "pianoRoll.tool.brush", TR_NOOP ("Tool: Brush"), Context::pianoRoll, { chord ('B') } },
  196. { "pianoRoll.tool.drumPaint", TR_NOOP ("Tool: Drum brush"), Context::pianoRoll, { chord ('N') } },
  197. { "pianoRoll.tool.eraser", TR_NOOP ("Tool: Eraser"), Context::pianoRoll, { chord ('D') } },
  198. { "pianoRoll.tool.mute", TR_NOOP ("Tool: Mute"), Context::pianoRoll, { chord ('T') } },
  199. { "pianoRoll.tool.knife", TR_NOOP ("Tool: Knife"), Context::pianoRoll, { chord ('C') } },
  200. { "pianoRoll.tool.lasso", TR_NOOP ("Tool: Lasso (select)"), Context::pianoRoll, { chord ('E') } },
  201. { "pianoRoll.tool.zoom", TR_NOOP ("Tool: Zoom"), Context::pianoRoll, { chord ('Z') } },
  202. { "pianoRoll.tool.listen", TR_NOOP ("Tool: Listen"), Context::pianoRoll, { chord ('Y') } },
  203. { "pianoRoll.slide", TR_NOOP ("Slide notes on/off (new notes)"), Context::pianoRoll, { chord ('S') } },
  204. { "pianoRoll.previousChannel", TR_NOOP ("Previous channel"), Context::pianoRoll, { chord ('G') } },
  205. { "pianoRoll.nextChannel", TR_NOOP ("Next channel"), Context::pianoRoll, { chord ('K') } },
  206. { "pianoRoll.previousChannelWithNotes", TR_NOOP ("Previous channel with notes"), Context::pianoRoll, { chord ('H') } },
  207. { "pianoRoll.nextChannelWithNotes", TR_NOOP ("Next channel with notes"), Context::pianoRoll, { chord ('J') } },
  208. // Playlist (focused)
  209. { "playlist.zoomIn", TR_NOOP ("Zoom in"), Context::playlist, { chord (pageUp) } },
  210. { "playlist.zoomOut", TR_NOOP ("Zoom out"), Context::playlist, { chord (pageDown) } },
  211. { "playlist.zoom1", TR_NOOP ("Zoom: one bar"), Context::playlist, { chord ('1', shift) }, true },
  212. { "playlist.zoom2", TR_NOOP ("Zoom: four bars"), Context::playlist, { chord ('2', shift) }, true },
  213. { "playlist.zoom3", TR_NOOP ("Zoom: sixteen bars"), Context::playlist, { chord ('3', shift) }, true },
  214. { "playlist.zoomAll", TR_NOOP ("Zoom to show everything"), Context::playlist, { chord ('4', shift) }, true },
  215. { "playlist.zoomSelection", TR_NOOP ("Zoom to the selection"), Context::playlist, { chord ('5', shift) }, true },
  216. { "playlist.centrePlayhead", TR_NOOP ("Centre the view on the playhead"), Context::playlist, { chord ('0', shift) }, true },
  217. { "playlist.rippleDelete", TR_NOOP ("Delete the selected clips and close the gaps"), Context::playlist, { chord (del, shift) } },
  218. { "playlist.ripple", TR_NOOP ("Ripple editing on/off"), Context::playlist, { chord ('S', shift) } },
  219. // FL's Playlist › Edit › "Automatic crossfades (current project)" (round 166e)
  220. { "playlist.autoCrossfades", TR_NOOP ("Automatic crossfades (current project) on/off"), Context::playlist, {} },
  221. // FL's Select › time (round 167k: EESelAroundSelAction, EESelPrevAction / EESelNextAction, Playlist focused)
  222. { "playlist.selectTimeAround", TR_NOOP ("Select time around selection"), Context::playlist, { chord (enter, ctrl) } },
  223. { "playlist.timePrevious", TR_NOOP ("Select previous time"), Context::playlist, { chord (left, ctrl) } },
  224. { "playlist.timeNext", TR_NOOP ("Select next time"), Context::playlist, { chord (right, ctrl) } },
  225. // FL 2026's Playlist › Time markers (round 167k: EEMarkerAdd2Menu 32852, EEMarkerAddAutoMenu 16468,
  226. // EEMarkerAddTSChangeMenu 41044, EEMarkerAddSongLoopMenu 8276); Ctrl+T stays the typing piano elsewhere
  227. { "playlist.markerAdd", TR_NOOP ("Time markers: Add one..."), Context::playlist, { chord ('T', alt) } },
  228. { "playlist.markerAuto", TR_NOOP ("Time markers: Add auto"), Context::playlist, { chord ('T', ctrl) } },
  229. { "playlist.markerSignature", TR_NOOP ("Time markers: Add time signature change..."), Context::playlist, { chord ('T', shift | alt) } },
  230. { "playlist.markerLoop", TR_NOOP ("Time markers: Place loop"), Context::playlist, { chord ('T', shift) } },
  231. // FL 2026's Playlist › Edit (round 167k: EEPitchSelectedAudioClipsDown/UpAction, EEReverseSelectedAudioClipsAction)
  232. { "playlist.pitchClipsDown", TR_NOOP ("Pitch selected audio clips one semitone down"), Context::playlist, { chord ('7') } },
  233. { "playlist.pitchClipsUp", TR_NOOP ("Pitch selected audio clips one semitone up"), Context::playlist, { chord ('8') } },
  234. { "playlist.reverseClips", TR_NOOP ("Reverse selected audio clips"), Context::playlist, { chord ('9') } },
  235. { "playlist.quantizeStarts", TR_NOOP ("Quantize the clips' starts"), Context::playlist, { chord ('Q', ctrl) } },
  236. { "playlist.quantizeHalf", TR_NOOP ("Quantize the clips' starts 50% (halfway to the grid)"), Context::playlist, {} },
  237. { "playlist.follow", TR_NOOP ("Follow the playhead (auto scrolling) on/off"), Context::playlist, {} },
  238. { "playlist.tool.pencil", TR_NOOP ("Tool: Pencil"), Context::playlist, { chord ('P') } },
  239. { "playlist.tool.brush", TR_NOOP ("Tool: Brush"), Context::playlist, { chord ('B') } },
  240. { "playlist.tool.eraser", TR_NOOP ("Tool: Eraser"), Context::playlist, { chord ('D') } },
  241. { "playlist.tool.mute", TR_NOOP ("Tool: Mute"), Context::playlist, { chord ('T') } },
  242. { "playlist.tool.knife", TR_NOOP ("Tool: Knife"), Context::playlist, { chord ('C') } },
  243. { "playlist.tool.lasso", TR_NOOP ("Tool: Lasso (select)"), Context::playlist, { chord ('E') } },
  244. { "playlist.tool.zoom", TR_NOOP ("Tool: Zoom"), Context::playlist, { chord ('Z') } },
  245. { "playlist.tool.listen", TR_NOOP ("Tool: Listen"), Context::playlist, { chord ('Y') } },
  246. { "playlist.tool.slip", TR_NOOP ("Tool: Slip"), Context::playlist, { chord ('S') } },
  247. // FL's Performance mode (round 165m; Ctrl+P on FL's Playlist menu [DFM])
  248. { "playlist.performanceMode", TR_NOOP ("Performance mode on/off (launch the clips before the Start marker)"), Context::playlist, { chord ('P', ctrl) } },
  249. { "playlist.zoomPerformance", TR_NOOP ("Zoom on the performance clips"), Context::playlist, { chord ('6', shift) }, true },
  250. { "playlist.perfClipProgress", TR_NOOP ("Show the performance clips' progress"), Context::playlist, {} },
  251. { "playlist.perfTrackProgress", TR_NOOP ("Show the performance tracks' progress"), Context::playlist, {} },
  252. // round 168f: FL's Playlist keys and menu items ([DFM ShortcutsModule] EEMergeAction Ctrl+G, EEClonePLTracksAction
  253. // Ctrl+Shift+B, EEMakeUniqueAction Ctrl+Shift+V; the STRETCH button's hint "Shift+M")
  254. { "playlist.mergePatternClips", TR_NOOP ("Merge pattern clips"), Context::playlist, { chord ('G', ctrl) } },
  255. { "playlist.cloneTracks", TR_NOOP ("Clone selected playlist track(s)"), Context::playlist, { chord ('B', ctrl | shift) } },
  256. { "playlist.makeUnique", TR_NOOP ("Make the selected clips unique"), Context::playlist, { chord ('V', ctrl | shift) } },
  257. // FL's Alt+M / Shift+Alt+M (EEMuteAction / EEUnmuteAction, issue #53): the selected clips, or all of them
  258. { "playlist.muteClips", TR_NOOP ("Mute the selected clips (all when none is selected)"), Context::playlist, { chord ('M', alt) } },
  259. { "playlist.unmuteClips", TR_NOOP ("Unmute the selected clips (all when none is selected)"), Context::playlist, { chord ('M', shift | alt) } },
  260. { "playlist.stretchMode", TR_NOOP ("Stretch audio when resizing audio clips on/off"), Context::playlist, { chord ('M', shift) } },
  261. { "playlist.createVariants", TR_NOOP ("Create variants when stretching audio clips on/off"), Context::playlist, {} },
  262. { "playlist.lockAllToContent", TR_NOOP ("Lock all playlist tracks to content"), Context::playlist, {} },
  263. { "playlist.unlockAllFromContent", TR_NOOP ("Unlock all playlist tracks from content"), Context::playlist, {} },
  264. { "playlist.resetEmptyTracks", TR_NOOP ("Reset empty playlist tracks"), Context::playlist, {} },
  265. { "playlist.collapseAllGroups", TR_NOOP ("Collapse all grouped tracks"), Context::playlist, {} },
  266. { "playlist.expandAllGroups", TR_NOOP ("Expand all grouped tracks"), Context::playlist, {} },
  267. { "playlist.purgeUnusedAudio", TR_NOOP ("Purge unused audio clips"), Context::playlist, {} },
  268. // Browser (focused)
  269. { "browser.previous", TR_NOOP ("Previous file (preview)"), Context::browser, { chord (up) } },
  270. { "browser.next", TR_NOOP ("Next file (preview)"), Context::browser, { chord (down) } },
  271. { "browser.swapPrevious", TR_NOOP ("Previous file into the selected channel"), Context::browser, { chord (up, shift) } },
  272. { "browser.swapNext", TR_NOOP ("Next file into the selected channel"), Context::browser, { chord (down, shift) } },
  273. { "browser.load", TR_NOOP ("Load the file into the selected channel"), Context::browser, { chord (enter) } },
  274. { "browser.closeFolder", TR_NOOP ("Close the folder (or go to the parent folder)"), Context::browser, { chord (left) } },
  275. { "browser.openFolder", TR_NOOP ("Open the folder"), Context::browser, { chord (right) } },
  276. { "browser.rename", TR_NOOP ("Rename the selected plugin (Plugin database)"), Context::browser, { chord (f2) } },
  277. // Mixer (focused)
  278. { "mixer.moveLeft", TR_NOOP ("Move the selected inserts left"), Context::mixer, { chord (left, alt) } },
  279. { "mixer.moveRight", TR_NOOP ("Move the selected inserts right"), Context::mixer, { chord (right, alt) } },
  280. { "mixer.linkChannels", TR_NOOP ("Link the selected channels to the selected insert"), Context::mixer, { chord ('L', ctrl) } },
  281. { "mixer.linkChannelsFrom", TR_NOOP ("Link the selected channels to inserts starting from the selected one"), Context::mixer, { chord ('L', ctrl | shift) } },
  282. { "mixer.selectLinkedChannels", TR_NOOP ("Select the channels linked to the selected insert"), Context::mixer, { chord ('L', alt) } },
  283. // FL's mixer track menu keys (round 167k, the DFM's ShortCut values: F2, S, Alt+R, Shift+Alt+R)
  284. { "mixer.rename", TR_NOOP ("Rename and color the selected mixer track"), Context::mixer, { chord (f2) } },
  285. { "mixer.soloSelected", TR_NOOP ("Solo the selected mixer track(s)"), Context::mixer, { chord ('S') } },
  286. { "mixer.altSoloSelected", TR_NOOP ("Alt solo the selected mixer track(s) (with the tracks that feed them)"), Context::mixer, { chord ('S', alt) } },
  287. { "mixer.renderArmed", TR_NOOP ("Disk recording: render the armed tracks to wave file(s)"), Context::mixer, { chord ('R', alt) } },
  288. { "mixer.renderSelected", TR_NOOP ("Disk recording: render the selected tracks to wave file(s)"), Context::mixer, { chord ('R', shift | alt) } },
  289. { "mixer.deleteEffect", TR_NOOP ("Delete the selected effect slot's plugin"), Context::mixer, { chord (del) } },
  290. };
  291. // numeric keypad 1-9: select patterns 1-9
  292. static const char* patternIds[] = { "pattern.select1", "pattern.select2", "pattern.select3", "pattern.select4", "pattern.select5",
  293. "pattern.select6", "pattern.select7", "pattern.select8", "pattern.select9" };
  294. for (int i = 0; i < 9; ++i)
  295. list.push_back ({ patternIds[i], TR_NOOP ("Select pattern {0}"), Context::global, { chord (numpad1 + i) } });
  296. // number keys: mute (plain) / solo (Ctrl) channels 1-10 while the rack has focus
  297. static const char* muteIds[] = { "rack.mute1", "rack.mute2", "rack.mute3", "rack.mute4", "rack.mute5",
  298. "rack.mute6", "rack.mute7", "rack.mute8", "rack.mute9", "rack.mute10" };
  299. static const char* soloIds[] = { "rack.solo1", "rack.solo2", "rack.solo3", "rack.solo4", "rack.solo5",
  300. "rack.solo6", "rack.solo7", "rack.solo8", "rack.solo9", "rack.solo10" };
  301. for (int i = 0; i < 10; ++i)
  302. {
  303. const auto digit = i == 9 ? '0' : (char) ('1' + i);
  304. list.push_back ({ muteIds[i], TR_NOOP ("Mute or unmute channel {0}"), Context::channelRack, { chord (digit) } });
  305. list.push_back ({ soloIds[i], TR_NOOP ("Solo channel {0}"), Context::channelRack, { chord (digit, ctrl) } });
  306. }
  307. return list;
  308. }
  309. bool contextsOverlap (Context a, Context b)
  310. {
  311. return a == b;
  312. }
  313. // --- tiny JSON reader for {"id": "chord" | ["chord", ...], ...} ---------------------------------
  314. struct Json
  315. {
  316. const std::string& s;
  317. std::size_t i = 0;
  318. void skip()
  319. {
  320. while (i < s.size() && std::isspace ((unsigned char) s[i]))
  321. ++i;
  322. }
  323. bool eat (char c)
  324. {
  325. skip();
  326. if (i < s.size() && s[i] == c)
  327. {
  328. ++i;
  329. return true;
  330. }
  331. return false;
  332. }
  333. std::optional<std::string> string()
  334. {
  335. skip();
  336. if (i >= s.size() || s[i] != '"')
  337. return std::nullopt;
  338. std::string out;
  339. for (++i; i < s.size(); ++i)
  340. {
  341. const auto c = s[i];
  342. if (c == '"')
  343. {
  344. ++i;
  345. return out;
  346. }
  347. if (c == '\\' && i + 1 < s.size())
  348. {
  349. const auto n = s[++i];
  350. out += n == 'n' ? '\n' : n == 't' ? '\t' : n;
  351. }
  352. else
  353. out += c;
  354. }
  355. return std::nullopt;
  356. }
  357. };
  358. } // namespace
  359. bool Chord::isPrintable() const noexcept
  360. {
  361. return key > ' ' && key < 0x7f;
  362. }
  363. std::string displayString (const Chord& c)
  364. {
  365. return modmap::keyNames (toString (c), modmap::hostOs());
  366. }
  367. std::string toString (const Chord& c)
  368. {
  369. std::string out;
  370. if (c.mods & ctrl)
  371. out += "Ctrl+";
  372. if (c.mods & shift)
  373. out += "Shift+";
  374. if (c.mods & alt)
  375. out += "Alt+";
  376. if (c.key >= f1 && c.key <= f12)
  377. return out + "F" + std::to_string (c.key - f1 + 1);
  378. for (const auto& k : keyNames)
  379. if (k.key == c.key)
  380. return out + k.name;
  381. if (c.isPrintable())
  382. return out + std::string (1, (char) c.key);
  383. return out + "?";
  384. }
  385. std::optional<Chord> parseChord (const std::string& text)
  386. {
  387. auto rest = trim (text);
  388. Chord c;
  389. // modifiers first ("+" alone, or "Numpad +", is a key, so only split on "+" followed by more text)
  390. for (;;)
  391. {
  392. const auto l = lower (rest);
  393. auto take = [&] (const char* prefix, std::uint8_t mod) {
  394. const std::string p (prefix);
  395. if (l.size() > p.size() && l.compare (0, p.size(), p) == 0)
  396. {
  397. c.mods |= mod;
  398. rest = trim (rest.substr (p.size()));
  399. return true;
  400. }
  401. return false;
  402. };
  403. if (! (take ("ctrl+", ctrl) || take ("shift+", shift) || take ("alt+", alt)))
  404. break;
  405. }
  406. const auto name = lower (rest);
  407. if (name.empty())
  408. return std::nullopt;
  409. if (name.size() >= 2 && name[0] == 'f' && std::all_of (name.begin() + 1, name.end(), [] (char ch) { return std::isdigit ((unsigned char) ch); }))
  410. {
  411. const auto n = std::stoi (name.substr (1));
  412. if (n < 1 || n > 12)
  413. return std::nullopt;
  414. c.key = f1 + n - 1;
  415. return c;
  416. }
  417. for (const auto& k : keyNames)
  418. if (lower (k.name) == name)
  419. {
  420. c.key = k.key;
  421. return c;
  422. }
  423. if (name == "del")
  424. c.key = del;
  425. else if (name == "ins")
  426. c.key = insert;
  427. else if (name == "escape")
  428. c.key = escape;
  429. else if (name == "return")
  430. c.key = enter;
  431. else if (rest.size() == 1 && rest[0] > ' ' && rest[0] < 0x7f)
  432. c.key = std::toupper ((unsigned char) rest[0]);
  433. else
  434. return std::nullopt;
  435. return c;
  436. }
  437. const std::vector<Action>& actions()
  438. {
  439. static const auto list = makeActions();
  440. return list;
  441. }
  442. const Action* findAction (const std::string& id)
  443. {
  444. for (const auto& a : actions())
  445. if (id == a.id)
  446. return &a;
  447. return nullptr;
  448. }
  449. int typingPianoNote (int key, int octave, int root) noexcept
  450. {
  451. const auto base = 12 * typingpiano::clampOctave (octave) + typingpiano::clampRoot (root); // octave 4 -> 48
  452. const auto inRange = [] (int n) { return n >= 0 && n <= 127 ? n : -1; };
  453. static const char lowerRow[] = "ZSXDCVGBHNJM";
  454. static const char upperRow[] = "Q2W3ER5T6Y7UI9O0P";
  455. const auto c = key >= 'a' && key <= 'z' ? key - 'a' + 'A' : key;
  456. for (int i = 0; lowerRow[i] != 0; ++i)
  457. if (lowerRow[i] == c)
  458. return inRange (base + i);
  459. for (int i = 0; upperRow[i] != 0; ++i)
  460. if (upperRow[i] == c)
  461. return inRange (base + 12 + i);
  462. return -1;
  463. }
  464. KeyMap::KeyMap()
  465. {
  466. resetToDefaults();
  467. }
  468. void KeyMap::resetToDefaults()
  469. {
  470. chords.clear();
  471. for (const auto& a : actions())
  472. chords.push_back (a.defaults);
  473. }
  474. bool KeyMap::playsNote (const Chord& c, bool typingPiano) noexcept
  475. {
  476. return typingPiano && (c.mods & (ctrl | alt | shift)) == 0 && typingPianoNote (c.key) >= 0;
  477. }
  478. std::string KeyMap::findFromSecondaryWindow (const Chord& c, bool typingIntoText, bool typingPiano) const
  479. {
  480. if (! c.isValid() || c.key == escape) // (Esc closes the window)
  481. return {};
  482. const auto editingKey = c.isPrintable() || c.key == space || c.key == enter || c.key == tab || c.key == backspace || c.key == del
  483. || c.key == insert || (c.key >= up && c.key <= end);
  484. if (typingIntoText && editingKey && (c.mods & (ctrl | alt)) == 0)
  485. return {};
  486. if (auto own = findUserBound (c, Context::global, typingPiano); ! own.empty())
  487. return own;
  488. return find (c, Context::global, typingPiano);
  489. }
  490. std::string KeyMap::findUserBound (const Chord& c, Context focus, bool typingPiano) const
  491. {
  492. if (! c.isValid() || playsNote (c, typingPiano))
  493. return {};
  494. const auto plainPrintable = c.isPrintable() && (c.mods & (ctrl | alt)) == 0;
  495. const auto& list = actions();
  496. const Action* globalMatch = nullptr;
  497. for (std::size_t i = 0; i < list.size(); ++i)
  498. {
  499. const auto& a = list[i];
  500. if ((a.context != Context::global && a.context != focus) || chords[i] == a.defaults)
  501. continue;
  502. if (std::find (chords[i].begin(), chords[i].end(), c) == chords[i].end()
  503. || std::find (a.defaults.begin(), a.defaults.end(), c) != a.defaults.end())
  504. continue; // (a default key keeps its usual priority)
  505. if (typingPiano && plainPrintable && ! a.whilePiano)
  506. continue;
  507. if (a.context == focus && focus != Context::global)
  508. return a.id;
  509. if (globalMatch == nullptr)
  510. globalMatch = &a;
  511. }
  512. return globalMatch != nullptr ? globalMatch->id : std::string();
  513. }
  514. std::vector<std::string> KeyMap::shadowedBy (const Chord& c, const std::string& actionId) const
  515. {
  516. std::vector<std::string> out;
  517. const auto* owner = findAction (actionId);
  518. if (owner == nullptr || ! c.isValid())
  519. return out;
  520. const auto& list = actions();
  521. for (std::size_t i = 0; i < list.size(); ++i)
  522. {
  523. const auto& a = list[i];
  524. if (actionId == a.id || a.context == owner->context)
  525. continue;
  526. if (owner->context != Context::global && a.context != Context::global)
  527. continue; // two different panels never meet
  528. if (std::find (chords[i].begin(), chords[i].end(), c) != chords[i].end())
  529. out.push_back (a.id);
  530. }
  531. return out;
  532. }
  533. std::string KeyMap::find (const Chord& c, Context focus, bool typingPiano) const
  534. {
  535. if (! c.isValid() || playsNote (c, typingPiano))
  536. return {};
  537. const auto plainPrintable = c.isPrintable() && (c.mods & (ctrl | alt)) == 0;
  538. const auto& list = actions();
  539. const Action* globalMatch = nullptr;
  540. for (std::size_t i = 0; i < list.size(); ++i)
  541. {
  542. const auto& a = list[i];
  543. if (a.context != Context::global && a.context != focus)
  544. continue;
  545. if (std::find (chords[i].begin(), chords[i].end(), c) == chords[i].end())
  546. continue;
  547. if (typingPiano && plainPrintable && ! a.whilePiano)
  548. continue;
  549. if (a.context == focus && focus != Context::global)
  550. return a.id; // the focused panel wins
  551. if (globalMatch == nullptr)
  552. globalMatch = &a;
  553. }
  554. return globalMatch != nullptr ? globalMatch->id : std::string();
  555. }
  556. const std::vector<Chord>& KeyMap::chordsFor (const std::string& actionId) const
  557. {
  558. static const std::vector<Chord> empty;
  559. const auto& list = actions();
  560. for (std::size_t i = 0; i < list.size(); ++i)
  561. if (actionId == list[i].id)
  562. return chords[i];
  563. return empty;
  564. }
  565. void KeyMap::setChords (const std::string& actionId, std::vector<Chord> newChords)
  566. {
  567. const auto& list = actions();
  568. for (std::size_t i = 0; i < list.size(); ++i)
  569. if (actionId == list[i].id)
  570. chords[i] = std::move (newChords);
  571. }
  572. bool KeyMap::loadJson (const std::string& json, std::vector<std::string>* errors)
  573. {
  574. Json p { json };
  575. auto fail = [&] (const std::string& message) {
  576. if (errors != nullptr)
  577. errors->push_back (message);
  578. };
  579. if (! p.eat ('{'))
  580. return false;
  581. if (p.eat ('}'))
  582. return true;
  583. for (;;)
  584. {
  585. const auto id = p.string();
  586. if (! id || ! p.eat (':'))
  587. return false;
  588. std::vector<std::string> values;
  589. if (p.eat ('['))
  590. {
  591. if (! p.eat (']'))
  592. for (;;)
  593. {
  594. const auto v = p.string();
  595. if (! v)
  596. return false;
  597. values.push_back (*v);
  598. if (p.eat (']'))
  599. break;
  600. if (! p.eat (','))
  601. return false;
  602. }
  603. }
  604. else if (const auto v = p.string())
  605. {
  606. if (! trim (*v).empty())
  607. values.push_back (*v);
  608. }
  609. else
  610. return false;
  611. if (findAction (*id) == nullptr)
  612. fail ("unknown action: " + *id);
  613. else
  614. {
  615. std::vector<Chord> parsed;
  616. for (const auto& v : values)
  617. if (const auto c = parseChord (v))
  618. parsed.push_back (*c);
  619. else
  620. fail ("unreadable key for " + *id + ": " + v);
  621. setChords (*id, parsed);
  622. }
  623. if (p.eat ('}'))
  624. return true;
  625. if (! p.eat (','))
  626. return false;
  627. }
  628. }
  629. std::string KeyMap::toJson() const
  630. {
  631. std::string out = "{\n";
  632. const auto& list = actions();
  633. for (std::size_t i = 0; i < list.size(); ++i)
  634. {
  635. out += " \"" + std::string (list[i].id) + "\": [";
  636. for (std::size_t k = 0; k < chords[i].size(); ++k)
  637. out += (k > 0 ? ", \"" : "\"") + toString (chords[i][k]) + "\"";
  638. out += i + 1 < list.size() ? "],\n" : "]\n";
  639. }
  640. return out + "}\n";
  641. }
  642. std::vector<KeyMap::Binding> KeyMap::functionKeyBindings() const
  643. {
  644. std::vector<Binding> out;
  645. const auto& list = actions();
  646. for (std::size_t i = 0; i < list.size(); ++i)
  647. for (const auto& c : chords[i])
  648. if (c.key >= f1 && c.key <= f12)
  649. out.push_back ({ c, list[i].id, list[i].context });
  650. // plain, Shift, Ctrl, Alt, then the combinations; global before a panel's own
  651. auto modRank = [] (std::uint8_t m) {
  652. switch (m)
  653. {
  654. case none: return 0;
  655. case shift: return 1;
  656. case ctrl: return 2;
  657. case alt: return 3;
  658. default: return 4 + (int) m;
  659. }
  660. };
  661. std::stable_sort (out.begin(), out.end(), [&] (const Binding& a, const Binding& b) {
  662. if (a.chord.key != b.chord.key)
  663. return a.chord.key < b.chord.key;
  664. if (a.chord.mods != b.chord.mods)
  665. return modRank (a.chord.mods) < modRank (b.chord.mods);
  666. return (int) a.context < (int) b.context;
  667. });
  668. return out;
  669. }
  670. std::vector<std::pair<std::string, std::string>> KeyMap::conflicts() const
  671. {
  672. std::vector<std::pair<std::string, std::string>> out;
  673. const auto& list = actions();
  674. for (std::size_t a = 0; a < list.size(); ++a)
  675. for (std::size_t b = a + 1; b < list.size(); ++b)
  676. {
  677. if (! contextsOverlap (list[a].context, list[b].context))
  678. continue;
  679. for (const auto& c : chords[a])
  680. if (std::find (chords[b].begin(), chords[b].end(), c) != chords[b].end())
  681. {
  682. out.push_back ({ list[a].id, list[b].id });
  683. break;
  684. }
  685. }
  686. return out;
  687. }
  688. std::vector<std::string> KeyMap::actionsWith (const Chord& chord, Context context, const std::string& except) const
  689. {
  690. std::vector<std::string> out;
  691. const auto& list = actions();
  692. for (std::size_t i = 0; i < list.size(); ++i)
  693. if (list[i].context == context && except != list[i].id
  694. && std::find (chords[i].begin(), chords[i].end(), chord) != chords[i].end())
  695. out.push_back (list[i].id);
  696. return out;
  697. }
  698. bool KeyMap::isDefault (const std::string& actionId) const
  699. {
  700. const auto& list = actions();
  701. for (std::size_t i = 0; i < list.size(); ++i)
  702. if (actionId == list[i].id)
  703. return chords[i] == list[i].defaults;
  704. return true;
  705. }
  706. std::string KeyMap::overridesJson() const
  707. {
  708. std::string out = "{\n";
  709. const auto& list = actions();
  710. bool first = true;
  711. for (std::size_t i = 0; i < list.size(); ++i)
  712. {
  713. if (chords[i] == list[i].defaults)
  714. continue;
  715. out += first ? "" : ",\n";
  716. first = false;
  717. out += " \"" + std::string (list[i].id) + "\": [";
  718. for (std::size_t k = 0; k < chords[i].size(); ++k)
  719. out += (k > 0 ? ", \"" : "\"") + toString (chords[i][k]) + "\"";
  720. out += "]";
  721. }
  722. return out + (first ? "}\n" : "\n}\n");
  723. }
  724. } // namespace lynx::keys