1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2017 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "ncmpc_curses.h"
33 #define KEY_CTL(x) ((x) & 0x1f) /* KEY_CTL(A) == ^A == \1 */
35 #define BS KEY_BACKSPACE
40 #define RGHT KEY_RIGHT
43 #define PGDN KEY_NPAGE
44 #define PGUP KEY_PPAGE
58 #define C(x) KEY_CTL(x)
60 static command_definition_t cmds
[] = {
61 #ifdef ENABLE_KEYDEF_SCREEN
62 { {'K', 0, 0 }, 0, CMD_SCREEN_KEYDEF
, "screen-keyedit",
63 N_("Key configuration screen") },
65 { { 'q', 'Q', C('C') }, 0, CMD_QUIT
, "quit",
69 { { UP
, 'k', 0 }, 0, CMD_LIST_PREVIOUS
, "up",
70 N_("Move cursor up") },
71 { { DWN
, 'j', 0 }, 0, CMD_LIST_NEXT
, "down",
72 N_("Move cursor down") },
73 { { 'H', 0, 0 }, 0, CMD_LIST_TOP
, "top",
74 N_("Move cursor to the top of screen") },
75 { { 'M', 0, 0 }, 0, CMD_LIST_MIDDLE
, "middle",
76 N_("Move cursor to the middle of screen") },
77 { { 'L', 0, 0 }, 0, CMD_LIST_BOTTOM
, "bottom",
78 N_("Move cursor to the bottom of screen") },
79 { { HOME
, C('A'), 0 }, 0, CMD_LIST_FIRST
, "home",
80 N_("Move cursor to the top of the list") },
81 { { END
, C('E'), 0 }, 0, CMD_LIST_LAST
, "end",
82 N_("Move cursor to the bottom of the list") },
83 { { PGUP
, 0, 0 }, 0, CMD_LIST_PREVIOUS_PAGE
, "pgup",
85 { { PGDN
, 0, 0 }, 0, CMD_LIST_NEXT_PAGE
, "pgdn",
87 { { 'v', 0, 0 }, 0, CMD_LIST_RANGE_SELECT
, "range-select",
88 N_("Range selection") },
89 { { C('N'), 0, 0 }, 0, CMD_LIST_SCROLL_DOWN_LINE
, "scroll-down-line",
90 N_("Scroll down one line") },
91 { { C('B'), 0, 0 }, 0, CMD_LIST_SCROLL_UP_LINE
, "scroll-up-line",
92 N_("Scroll up one line") },
93 { { 'N', 0, 0 }, 0, CMD_LIST_SCROLL_DOWN_HALF
, "scroll-down-half",
94 N_("Scroll up half a screen") },
95 { { 'B', 0, 0 }, 0, CMD_LIST_SCROLL_UP_HALF
, "scroll-up-half",
96 N_("Scroll down half a screen") },
97 { { 'l', 0, 0 }, 0, CMD_SELECT_PLAYING
, "select-playing",
98 N_("Select currently playing song") },
102 { { '1', F1
, 'h' }, 0, CMD_SCREEN_HELP
, "screen-help",
104 { { '2', F2
, 0 }, 0, CMD_SCREEN_PLAY
, "screen-playlist",
105 N_("Queue screen") },
106 { { '3', F3
, 0 }, 0, CMD_SCREEN_FILE
, "screen-browse",
107 N_("Browse screen") },
110 /* player commands */
111 { { RET
, 0, 0 }, 0, CMD_PLAY
, "play",
112 N_("Play/Enter directory") },
113 { { 'P', 0, 0 }, 0, CMD_PAUSE
,"pause",
115 { { 's', BS
, 0 }, 0, CMD_STOP
, "stop",
117 { { 'o', 0, 0 }, 0, CMD_CROP
, "crop",
119 { { '>', 0, 0 }, 0, CMD_TRACK_NEXT
, "next",
121 { { '<', 0, 0 }, 0, CMD_TRACK_PREVIOUS
, "prev",
122 N_("Previous track") },
123 { { 'f', 0, 0 }, 0, CMD_SEEK_FORWARD
, "seek-forward",
124 N_("Seek forward") },
125 { { 'b', 0, 0 }, 0, CMD_SEEK_BACKWARD
, "seek-backward",
126 N_("Seek backward") },
127 { { '+', RGHT
, 0 }, 0, CMD_VOLUME_UP
, "volume-up",
128 N_("Increase volume") },
129 { { '-', LEFT
, 0 }, 0, CMD_VOLUME_DOWN
, "volume-down",
130 N_("Decrease volume") },
131 { { ' ', 0, 0 }, 0, CMD_SELECT
, "select",
132 N_("Select/deselect song in queue") },
133 { { 't', 0, 0 }, 0, CMD_SELECT_ALL
, "select_all",
134 N_("Select all listed items") },
135 { { DEL
, 'd', 0 }, 0, CMD_DELETE
, "delete",
136 N_("Delete song from queue") },
137 { { 'Z', 0, 0 }, 0, CMD_SHUFFLE
, "shuffle",
138 N_("Shuffle queue") },
139 { { 'c', 0, 0 }, 0, CMD_CLEAR
, "clear",
141 { { 'r', 0, 0 }, 0, CMD_REPEAT
, "repeat",
142 N_("Toggle repeat mode") },
143 { { 'z', 0, 0 }, 0, CMD_RANDOM
, "random",
144 N_("Toggle random mode") },
145 { { 'y', 0, 0 }, 0, CMD_SINGLE
, "single",
146 N_("Toggle single mode") },
147 { { 'C', 0, 0 }, 0, CMD_CONSUME
, "consume",
148 N_("Toggle consume mode") },
149 { { 'x', 0, 0 }, 0, CMD_CROSSFADE
, "crossfade",
150 N_("Toggle crossfade mode") },
151 { { C('U'), 0, 0 }, 0, CMD_DB_UPDATE
, "db-update",
152 N_("Start a music database update") },
153 { { 'S', 0, 0 }, 0, CMD_SAVE_PLAYLIST
, "save",
155 { { 'a', 0, 0 }, 0, CMD_ADD
, "add",
156 N_("Add url/file to queue") },
158 { { '!', 0, 0 }, 0, CMD_GO_ROOT_DIRECTORY
, "go-root-directory",
159 N_("Go to root directory") },
160 { { '"', 0, 0 }, 0, CMD_GO_PARENT_DIRECTORY
, "go-parent-directory",
161 N_("Go to parent directory") },
163 { { 'G', 0, 0 }, 0, CMD_LOCATE
, "locate",
164 N_("Locate song in browser") },
167 { { C('K'), 0, 0 }, 0, CMD_LIST_MOVE_UP
, "move-up",
168 N_("Move item up") },
169 { { C('J'), 0, 0 }, 0, CMD_LIST_MOVE_DOWN
, "move-down",
170 N_("Move item down") },
171 { { C('L'), 0, 0 }, 0, CMD_SCREEN_UPDATE
, "update",
172 N_("Refresh screen") },
176 { { 'w', 0, 0 }, 0, CMD_TOGGLE_FIND_WRAP
, "wrap-mode",
177 /* translators: toggle between wrapping and non-wrapping
179 N_("Toggle find mode") },
180 { { 'U', 0, 0 }, 0, CMD_TOGGLE_AUTOCENTER
, "autocenter-mode",
181 /* translators: the auto center mode always centers the song
182 currently being played */
183 N_("Toggle auto center mode") },
187 { { TAB
, 0, 0 }, 0, CMD_SCREEN_NEXT
, "screen-next",
189 { { STAB
, 0, 0 }, 0, CMD_SCREEN_PREVIOUS
, "screen-prev",
190 N_("Previous screen") },
191 { { '`', 0, 0 }, 0, CMD_SCREEN_SWAP
, "screen-swap",
192 N_("Swap to most recent screen") },
196 { { '/', 0, 0 }, 0, CMD_LIST_FIND
, "find",
197 N_("Forward find") },
198 { { 'n', 0, 0 }, 0, CMD_LIST_FIND_NEXT
, "find-next",
199 N_("Forward find next") },
200 { { '?', 0, 0 }, 0, CMD_LIST_RFIND
, "rfind",
201 N_("Backward find") },
202 { { 'p', 0, 0 }, 0, CMD_LIST_RFIND_NEXT
, "rfind-next",
203 N_("Backward find previous") },
204 { { '.', 0, 0 }, 0, CMD_LIST_JUMP
, "jump",
205 /* translators: this queries the user for a string
206 * and jumps directly (while the user is typing)
207 * to the entry which begins with this string */
212 #ifdef ENABLE_ARTIST_SCREEN
213 { {'4', F4
, 0 }, 0, CMD_SCREEN_ARTIST
, "screen-artist",
214 N_("Artist screen") },
216 #ifdef ENABLE_SEARCH_SCREEN
217 { {'5', F5
, 0 }, 0, CMD_SCREEN_SEARCH
, "screen-search",
218 N_("Search screen") },
219 { {'m', 0, 0 }, 0, CMD_SEARCH_MODE
, "search-mode",
220 N_("Change search mode") },
222 #ifdef ENABLE_SONG_SCREEN
223 { { 'i', 0, 0 }, 0, CMD_SCREEN_SONG
, "view",
224 N_("View the selected and the currently playing song") },
226 #ifdef ENABLE_LYRICS_SCREEN
227 { {'7', F7
, 0 }, 0, CMD_SCREEN_LYRICS
, "screen-lyrics",
228 N_("Lyrics screen") },
229 { {ESC
, 0, 0 }, 0, CMD_INTERRUPT
, "lyrics-interrupt",
230 /* translators: interrupt the current background action,
231 e.g. stop loading lyrics from the internet */
232 N_("Interrupt action") },
233 { {'u', 0, 0 }, 0, CMD_LYRICS_UPDATE
, "lyrics-update",
234 N_("Update Lyrics") },
235 /* this command may move out of #ifdef ENABLE_LYRICS_SCREEN
237 { {'e', 0, 0 }, 0, CMD_EDIT
, "edit",
238 N_("Edit the current item") },
241 #ifdef ENABLE_OUTPUTS_SCREEN
242 { {'8', F8
, 0 }, 0, CMD_SCREEN_OUTPUTS
, "screen-outputs",
243 N_("Outputs screen") },
246 #ifdef ENABLE_CHAT_SCREEN
247 { {'9', F9
, 0}, 0, CMD_SCREEN_CHAT
, "screen-chat",
251 { { -1, -1, -1 }, 0, CMD_NONE
, NULL
, NULL
}
254 #ifdef ENABLE_KEYDEF_SCREEN
255 command_definition_t
*
256 get_command_definitions(void)
262 get_cmds_max_name_width(command_definition_t
*c
)
264 static size_t max
= 0;
269 for (command_definition_t
*p
= c
; p
->name
!= NULL
; p
++) {
271 * width and length are considered the same here, as command
272 * names are not translated.
274 size_t len
= strlen(p
->name
);
290 return _("Undefined");
296 return _("Backspace");
312 return _("PageDown");
318 return _("Shift+Tab");
324 for (int i
= 0; i
<= 63; i
++)
325 if (key
== KEY_F(i
)) {
326 g_snprintf(buf
, 32, _("F%d"), i
);
330 g_snprintf(buf
, 32, _("Ctrl-%c"), 'A'+(key
& 037)-1 );
331 else if ((key
& ~037) == 224)
332 g_snprintf(buf
, 32, _("Alt-%c"), 'A'+(key
& 037)-1 );
333 else if (key
> 32 && key
< 256)
334 g_snprintf(buf
, 32, "%c", key
);
336 g_snprintf(buf
, 32, "0x%03X", key
);
342 command_dump_keys(void)
344 for (size_t i
= 0; cmds
[i
].description
; i
++)
345 if (cmds
[i
].command
!= CMD_NONE
)
346 printf(" %20s : %s\n",
347 get_key_names(cmds
[i
].command
, true),
354 set_key_flags(command_definition_t
*cp
, command_t command
, int flags
)
356 for (size_t i
= 0; cp
[i
].name
; i
++) {
357 if (cp
[i
].command
== command
) {
358 cp
[i
].flags
|= flags
;
367 get_key_names(command_t command
, bool all
)
369 for (int i
= 0; cmds
[i
].description
; i
++) {
370 if (cmds
[i
].command
== command
) {
371 static char keystr
[80];
373 g_strlcpy(keystr
, key2str(cmds
[i
].keys
[0]), sizeof(keystr
));
377 for (unsigned j
= 1; j
< MAX_COMMAND_KEYS
&&
378 cmds
[i
].keys
[j
] > 0; j
++) {
379 g_strlcat(keystr
, " ", sizeof(keystr
));
380 g_strlcat(keystr
, key2str(cmds
[i
].keys
[j
]), sizeof(keystr
));
389 get_key_description(command_t command
)
391 for (size_t i
= 0; cmds
[i
].description
; i
++)
392 if (cmds
[i
].command
== command
)
393 return _(cmds
[i
].description
);
399 get_key_command_name(command_t command
)
401 for (size_t i
= 0; cmds
[i
].name
; i
++)
402 if (cmds
[i
].command
== command
)
409 get_key_command_from_name(const char *name
)
411 for (size_t i
= 0; cmds
[i
].name
; i
++)
412 if (strcmp(name
, cmds
[i
].name
) == 0)
413 return cmds
[i
].command
;
419 find_key_command(int key
, const command_definition_t
*c
)
424 for (size_t i
= 0; c
[i
].name
; i
++) {
425 for (int j
= 0; j
< MAX_COMMAND_KEYS
; j
++)
426 if (c
[i
].keys
[j
] == key
)
434 get_key_command(int key
)
436 return find_key_command(key
, cmds
);
440 assign_keys(command_t command
, int keys
[MAX_COMMAND_KEYS
])
442 for (size_t i
= 0; cmds
[i
].name
; i
++) {
443 if (cmds
[i
].command
== command
) {
444 memcpy(cmds
[i
].keys
, keys
, sizeof(int)*MAX_COMMAND_KEYS
);
446 cmds
[i
].flags
|= COMMAND_KEY_MODIFIED
;
458 check_key_bindings(command_definition_t
*cp
, char *buf
, size_t bufsize
)
465 for (size_t i
= 0; cp
[i
].name
; i
++)
466 cp
[i
].flags
&= ~COMMAND_KEY_CONFLICT
;
468 for (size_t i
= 0; cp
[i
].name
; i
++) {
472 for(j
=0; j
<MAX_COMMAND_KEYS
; j
++) {
474 (cmd
= find_key_command(cp
[i
].keys
[j
],cp
)) != cp
[i
].command
) {
476 g_snprintf(buf
, bufsize
,
477 _("Key %s assigned to %s and %s"),
478 key2str(cp
[i
].keys
[j
]),
479 get_key_command_name(cp
[i
].command
),
480 get_key_command_name(cmd
));
483 _("Key %s assigned to %s and %s"),
484 key2str(cp
[i
].keys
[j
]),
485 get_key_command_name(cp
[i
].command
),
486 get_key_command_name(cmd
));
489 cp
[i
].flags
|= COMMAND_KEY_CONFLICT
;
490 set_key_flags(cp
, cmd
, COMMAND_KEY_CONFLICT
);
500 write_key_bindings(FILE *f
, int flags
)
502 if (flags
& KEYDEF_WRITE_HEADER
)
503 fprintf(f
, "## Key bindings for ncmpc (generated by ncmpc)\n\n");
505 for (size_t i
= 0; cmds
[i
].name
&& !ferror(f
); i
++) {
506 if (cmds
[i
].flags
& COMMAND_KEY_MODIFIED
||
507 flags
& KEYDEF_WRITE_ALL
) {
508 fprintf(f
, "## %s\n", cmds
[i
].description
);
509 if (flags
& KEYDEF_COMMENT_ALL
)
511 fprintf(f
, "key %s = ", cmds
[i
].name
);
512 for (int j
= 0; j
< MAX_COMMAND_KEYS
; j
++) {
513 if (j
&& cmds
[i
].keys
[j
])
515 if (!j
|| cmds
[i
].keys
[j
]) {
516 if (cmds
[i
].keys
[j
]<256 && (isalpha(cmds
[i
].keys
[j
]) ||
517 isdigit(cmds
[i
].keys
[j
])))
518 fprintf(f
, "\'%c\'", cmds
[i
].keys
[j
]);
520 fprintf(f
, "%d", cmds
[i
].keys
[j
]);
527 return ferror(f
) == 0;
530 #endif /* NCMPC_MINI */