release v0.29
[ncmpc.git] / src / command.c
blobf903165f57a113034df735373395a4a5a3d43a7f
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.
20 #include "command.h"
21 #include "i18n.h"
22 #include "ncmpc_curses.h"
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <glib.h>
30 #include <signal.h>
31 #include <unistd.h>
33 #define KEY_CTL(x) ((x) & 0x1f) /* KEY_CTL(A) == ^A == \1 */
35 #define BS KEY_BACKSPACE
36 #define DEL KEY_DC
37 #define UP KEY_UP
38 #define DWN KEY_DOWN
39 #define LEFT KEY_LEFT
40 #define RGHT KEY_RIGHT
41 #define HOME KEY_HOME
42 #define END KEY_END
43 #define PGDN KEY_NPAGE
44 #define PGUP KEY_PPAGE
45 #define TAB 0x09
46 #define STAB 0x161
47 #define ESC 0x1B
48 #define RET '\r'
49 #define F1 KEY_F(1)
50 #define F2 KEY_F(2)
51 #define F3 KEY_F(3)
52 #define F4 KEY_F(4)
53 #define F5 KEY_F(5)
54 #define F6 KEY_F(6)
55 #define F7 KEY_F(7)
56 #define F8 KEY_F(8)
57 #define F9 KEY_F(9)
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") },
64 #endif
65 { { 'q', 'Q', C('C') }, 0, CMD_QUIT, "quit",
66 N_("Quit") },
68 /* movement */
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",
84 N_("Page up") },
85 { { PGDN, 0, 0 }, 0, CMD_LIST_NEXT_PAGE, "pgdn",
86 N_("Page down") },
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") },
101 /* basic screens */
102 { { '1', F1, 'h' }, 0, CMD_SCREEN_HELP, "screen-help",
103 N_("Help screen") },
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",
114 N_("Pause") },
115 { { 's', BS, 0 }, 0, CMD_STOP, "stop",
116 N_("Stop") },
117 { { 'o', 0, 0 }, 0, CMD_CROP, "crop",
118 N_("Crop") },
119 { { '>', 0, 0 }, 0, CMD_TRACK_NEXT, "next",
120 N_("Next track") },
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",
140 N_("Clear queue") },
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",
154 N_("Save queue") },
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") },
166 /* lists */
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") },
175 /* ncmpc options */
176 { { 'w', 0, 0 }, 0, CMD_TOGGLE_FIND_WRAP, "wrap-mode",
177 /* translators: toggle between wrapping and non-wrapping
178 search */
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") },
186 /* change screen */
187 { { TAB, 0, 0 }, 0, CMD_SCREEN_NEXT, "screen-next",
188 N_("Next screen") },
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") },
195 /* find */
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 */
208 N_("Jump to") },
211 /* extra screens */
212 #ifdef ENABLE_ARTIST_SCREEN
213 { {'4', F4, 0 }, 0, CMD_SCREEN_ARTIST, "screen-artist",
214 N_("Artist screen") },
215 #endif
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") },
221 #endif
222 #ifdef ENABLE_SONG_SCREEN
223 { { 'i', 0, 0 }, 0, CMD_SCREEN_SONG, "view",
224 N_("View the selected and the currently playing song") },
225 #endif
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
236 at some point */
237 { {'e', 0, 0 }, 0, CMD_EDIT, "edit",
238 N_("Edit the current item") },
239 #endif
241 #ifdef ENABLE_OUTPUTS_SCREEN
242 { {'8', F8, 0 }, 0, CMD_SCREEN_OUTPUTS, "screen-outputs",
243 N_("Outputs screen") },
244 #endif
246 #ifdef ENABLE_CHAT_SCREEN
247 { {'9', F9, 0}, 0, CMD_SCREEN_CHAT, "screen-chat",
248 N_("Chat screen") },
249 #endif
251 { { -1, -1, -1 }, 0, CMD_NONE, NULL, NULL }
254 #ifdef ENABLE_KEYDEF_SCREEN
255 command_definition_t *
256 get_command_definitions(void)
258 return cmds;
261 size_t
262 get_cmds_max_name_width(command_definition_t *c)
264 static size_t max = 0;
266 if (max != 0)
267 return max;
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);
275 if (len > max)
276 max = len;
279 return max;
281 #endif
283 const char *
284 key2str(int key)
286 switch(key) {
287 static char buf[32];
289 case 0:
290 return _("Undefined");
291 case ' ':
292 return _("Space");
293 case RET:
294 return _("Enter");
295 case BS:
296 return _("Backspace");
297 case DEL:
298 return _("Delete");
299 case UP:
300 return _("Up");
301 case DWN:
302 return _("Down");
303 case LEFT:
304 return _("Left");
305 case RGHT:
306 return _("Right");
307 case HOME:
308 return _("Home");
309 case END:
310 return _("End");
311 case PGDN:
312 return _("PageDown");
313 case PGUP:
314 return _("PageUp");
315 case TAB:
316 return _("Tab");
317 case STAB:
318 return _("Shift+Tab");
319 case ESC:
320 return _("Esc");
321 case KEY_IC:
322 return _("Insert");
323 default:
324 for (int i = 0; i <= 63; i++)
325 if (key == KEY_F(i)) {
326 g_snprintf(buf, 32, _("F%d"), i );
327 return buf;
329 if (!(key & ~037))
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);
335 else
336 g_snprintf(buf, 32, "0x%03X", key);
337 return buf;
341 void
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),
348 cmds[i].name);
351 #ifndef NCMPC_MINI
353 static void
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;
359 break;
364 #endif
366 const char *
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));
374 if (!all)
375 return 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));
382 return keystr;
385 return NULL;
388 const char *
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);
395 return NULL;
398 const char *
399 get_key_command_name(command_t command)
401 for (size_t i = 0; cmds[i].name; i++)
402 if (cmds[i].command == command)
403 return cmds[i].name;
405 return NULL;
408 command_t
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;
415 return CMD_NONE;
418 command_t
419 find_key_command(int key, const command_definition_t *c)
421 assert(key != 0);
422 assert(c != NULL);
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)
427 return c[i].command;
430 return CMD_NONE;
433 command_t
434 get_key_command(int key)
436 return find_key_command(key, cmds);
439 bool
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);
445 #ifndef NCMPC_MINI
446 cmds[i].flags |= COMMAND_KEY_MODIFIED;
447 #endif
448 return true;
452 return false;
455 #ifndef NCMPC_MINI
457 bool
458 check_key_bindings(command_definition_t *cp, char *buf, size_t bufsize)
460 bool success = true;
462 if (cp == NULL)
463 cp = cmds;
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++) {
469 int j;
470 command_t cmd;
472 for(j=0; j<MAX_COMMAND_KEYS; j++) {
473 if (cp[i].keys[j] &&
474 (cmd = find_key_command(cp[i].keys[j],cp)) != cp[i].command) {
475 if (buf) {
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));
481 } else {
482 fprintf(stderr,
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));
487 fputc('\n', stderr);
489 cp[i].flags |= COMMAND_KEY_CONFLICT;
490 set_key_flags(cp, cmd, COMMAND_KEY_CONFLICT);
491 success = false;
496 return success;
499 bool
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)
510 fprintf(f, "#");
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])
514 fprintf(f, ", ");
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]);
519 else
520 fprintf(f, "%d", cmds[i].keys[j]);
523 fprintf(f,"\n\n");
527 return ferror(f) == 0;
530 #endif /* NCMPC_MINI */