configure.ac: require GLib 2.14
[ncmpc.git] / src / command.c
blob9495b79aa5ea62301478d9ee0e433c8315824194
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 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 "config.h"
22 #include "ncmpc.h"
23 #include "i18n.h"
24 #include "mpdclient.h"
25 #include "screen.h"
27 #include <ctype.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <glib.h>
32 #include <signal.h>
33 #include <unistd.h>
35 #define KEY_CTL(x) ((x) & 0x1f) /* KEY_CTL(A) == ^A == \1 */
37 #define BS KEY_BACKSPACE
38 #define DEL KEY_DC
39 #define UP KEY_UP
40 #define DWN KEY_DOWN
41 #define LEFT KEY_LEFT
42 #define RGHT KEY_RIGHT
43 #define HOME KEY_HOME
44 #define END KEY_END
45 #define PGDN KEY_NPAGE
46 #define PGUP KEY_PPAGE
47 #define TAB 0x09
48 #define STAB 0x161
49 #define ESC 0x1B
50 #define RET '\r'
51 #define F1 KEY_F(1)
52 #define F2 KEY_F(2)
53 #define F3 KEY_F(3)
54 #define F4 KEY_F(4)
55 #define F5 KEY_F(5)
56 #define F6 KEY_F(6)
57 #define F7 KEY_F(7)
58 #define F8 KEY_F(8)
59 #define F9 KEY_F(9)
60 #define C(x) KEY_CTL(x)
62 static command_definition_t cmds[] = {
63 #ifdef ENABLE_KEYDEF_SCREEN
64 { {'K', 0, 0 }, 0, CMD_SCREEN_KEYDEF, "screen-keyedit",
65 N_("Key configuration screen") },
66 #endif
67 { { 'q', 'Q', C('C') }, 0, CMD_QUIT, "quit",
68 N_("Quit") },
70 /* movement */
71 { { UP, 'k', 0 }, 0, CMD_LIST_PREVIOUS, "up",
72 N_("Move cursor up") },
73 { { DWN, 'j', 0 }, 0, CMD_LIST_NEXT, "down",
74 N_("Move cursor down") },
75 { { 'H', 0, 0 }, 0, CMD_LIST_TOP, "top",
76 N_("Move cursor to the top of screen") },
77 { { 'M', 0, 0 }, 0, CMD_LIST_MIDDLE, "middle",
78 N_("Move cursor to the middle of screen") },
79 { { 'L', 0, 0 }, 0, CMD_LIST_BOTTOM, "bottom",
80 N_("Move cursor to the bottom of screen") },
81 { { HOME, C('A'), 0 }, 0, CMD_LIST_FIRST, "home",
82 N_("Move cursor to the top of the list") },
83 { { END, C('E'), 0 }, 0, CMD_LIST_LAST, "end",
84 N_("Move cursor to the bottom of the list") },
85 { { PGUP, 0, 0 }, 0, CMD_LIST_PREVIOUS_PAGE, "pgup",
86 N_("Page up") },
87 { { PGDN, 0, 0 }, 0, CMD_LIST_NEXT_PAGE, "pgdn",
88 N_("Page down") },
89 { { 'v', 0, 0 }, 0, CMD_LIST_RANGE_SELECT, "range-select",
90 N_("Range selection") },
91 { { C('N'), 0, 0 }, 0, CMD_LIST_SCROLL_DOWN_LINE, "scroll-down-line",
92 N_("Scroll up one line") },
93 { { C('B'), 0, 0 }, 0, CMD_LIST_SCROLL_UP_LINE, "scroll-up-line",
94 N_("Scroll down one line") },
95 { { 'N', 0, 0 }, 0, CMD_LIST_SCROLL_DOWN_HALF, "scroll-down-half",
96 N_("Scroll up half a screen") },
97 { { 'B', 0, 0 }, 0, CMD_LIST_SCROLL_UP_HALF, "scroll-up-half",
98 N_("Scroll down half a screen") },
99 { { 'l', 0, 0 }, 0, CMD_SELECT_PLAYING, "select-playing",
100 N_("Select currently playing song") },
103 /* basic screens */
104 { { '1', F1, 'h' }, 0, CMD_SCREEN_HELP, "screen-help",
105 N_("Help screen") },
106 { { '2', F2, 0 }, 0, CMD_SCREEN_PLAY, "screen-playlist",
107 N_("Playlist screen") },
108 { { '3', F3, 0 }, 0, CMD_SCREEN_FILE, "screen-browse",
109 N_("Browse screen") },
112 /* player commands */
113 { { RET, 0, 0 }, 0, CMD_PLAY, "play",
114 N_("Play/Enter directory") },
115 { { 'P', 0, 0 }, 0, CMD_PAUSE,"pause",
116 N_("Pause") },
117 { { 's', BS, 0 }, 0, CMD_STOP, "stop",
118 N_("Stop") },
119 { { 'o', 0, 0 }, 0, CMD_CROP, "crop",
120 N_("Crop") },
121 { { '>', 0, 0 }, 0, CMD_TRACK_NEXT, "next",
122 N_("Next track") },
123 { { '<', 0, 0 }, 0, CMD_TRACK_PREVIOUS, "prev",
124 N_("Previous track") },
125 { { 'f', 0, 0 }, 0, CMD_SEEK_FORWARD, "seek-forward",
126 N_("Seek forward") },
127 { { 'b', 0, 0 }, 0, CMD_SEEK_BACKWARD, "seek-backward",
128 N_("Seek backward") },
129 { { '+', RGHT, 0 }, 0, CMD_VOLUME_UP, "volume-up",
130 N_("Increase volume") },
131 { { '-', LEFT, 0 }, 0, CMD_VOLUME_DOWN, "volume-down",
132 N_("Decrease volume") },
133 { { ' ', 0, 0 }, 0, CMD_SELECT, "select",
134 N_("Select/deselect song in playlist") },
135 { { 't', 0, 0 }, 0, CMD_SELECT_ALL, "select_all",
136 N_("Select all listed items") },
137 { { DEL, 'd', 0 }, 0, CMD_DELETE, "delete",
138 N_("Delete song from playlist") },
139 { { 'Z', 0, 0 }, 0, CMD_SHUFFLE, "shuffle",
140 N_("Shuffle playlist") },
141 { { 'c', 0, 0 }, 0, CMD_CLEAR, "clear",
142 N_("Clear playlist") },
143 { { 'r', 0, 0 }, 0, CMD_REPEAT, "repeat",
144 N_("Toggle repeat mode") },
145 { { 'z', 0, 0 }, 0, CMD_RANDOM, "random",
146 N_("Toggle random mode") },
147 { { 'y', 0, 0 }, 0, CMD_SINGLE, "single",
148 N_("Toggle single mode") },
149 { { 'C', 0, 0 }, 0, CMD_CONSUME, "consume",
150 N_("Toggle consume mode") },
151 { { 'x', 0, 0 }, 0, CMD_CROSSFADE, "crossfade",
152 N_("Toggle crossfade mode") },
153 { { C('U'), 0, 0 }, 0, CMD_DB_UPDATE, "db-update",
154 N_("Start a music database update") },
155 { { 'S', 0, 0 }, 0, CMD_SAVE_PLAYLIST, "save",
156 N_("Save playlist") },
157 { { 'a', 0, 0 }, 0, CMD_ADD, "add",
158 N_("Add url/file to playlist") },
160 { { '!', 0, 0 }, 0, CMD_GO_ROOT_DIRECTORY, "go-root-directory",
161 N_("Go to root directory") },
162 { { '"', 0, 0 }, 0, CMD_GO_PARENT_DIRECTORY, "go-parent-directory",
163 N_("Go to parent directory") },
165 { { 'G', 0, 0 }, 0, CMD_LOCATE, "locate",
166 N_("Locate song in browser") },
168 /* lists */
169 { { C('K'), 0, 0 }, 0, CMD_LIST_MOVE_UP, "move-up",
170 N_("Move item up") },
171 { { C('J'), 0, 0 }, 0, CMD_LIST_MOVE_DOWN, "move-down",
172 N_("Move item down") },
173 { { C('L'), 0, 0 }, 0, CMD_SCREEN_UPDATE, "update",
174 N_("Refresh screen") },
177 /* ncmpc options */
178 { { 'w', 0, 0 }, 0, CMD_TOGGLE_FIND_WRAP, "wrap-mode",
179 /* translators: toggle between wrapping and non-wrapping
180 search */
181 N_("Toggle find mode") },
182 { { 'U', 0, 0 }, 0, CMD_TOGGLE_AUTOCENTER, "autocenter-mode",
183 /* translators: the auto center mode always centers the song
184 currently being played */
185 N_("Toggle auto center mode") },
188 /* change screen */
189 { { TAB, 0, 0 }, 0, CMD_SCREEN_NEXT, "screen-next",
190 N_("Next screen") },
191 { { STAB, 0, 0 }, 0, CMD_SCREEN_PREVIOUS, "screen-prev",
192 N_("Previous screen") },
193 { { '`', 0, 0 }, 0, CMD_SCREEN_SWAP, "screen-swap",
194 N_("Swap to most recent screen") },
197 /* find */
198 { { '/', 0, 0 }, 0, CMD_LIST_FIND, "find",
199 N_("Forward find") },
200 { { 'n', 0, 0 }, 0, CMD_LIST_FIND_NEXT, "find-next",
201 N_("Forward find next") },
202 { { '?', 0, 0 }, 0, CMD_LIST_RFIND, "rfind",
203 N_("Backward find") },
204 { { 'p', 0, 0 }, 0, CMD_LIST_RFIND_NEXT, "rfind-next",
205 N_("Backward find previous") },
206 { { '.', 0, 0 }, 0, CMD_LIST_JUMP, "jump",
207 /* translators: this queries the user for a string
208 * and jumps directly (while the user is typing)
209 * to the entry which begins with this string */
210 N_("Jump to") },
213 /* extra screens */
214 #ifdef ENABLE_ARTIST_SCREEN
215 { {'4', F4, 0 }, 0, CMD_SCREEN_ARTIST, "screen-artist",
216 N_("Artist screen") },
217 #endif
218 #ifdef ENABLE_SEARCH_SCREEN
219 { {'5', F5, 0 }, 0, CMD_SCREEN_SEARCH, "screen-search",
220 N_("Search screen") },
221 { {'m', 0, 0 }, 0, CMD_SEARCH_MODE, "search-mode",
222 N_("Change search mode") },
223 #endif
224 #ifdef ENABLE_SONG_SCREEN
225 { { 'i', 0, 0 }, 0, CMD_SCREEN_SONG, "view",
226 N_("View the selected and the currently playing song") },
227 #endif
228 #ifdef ENABLE_LYRICS_SCREEN
229 { {'7', F7, 0 }, 0, CMD_SCREEN_LYRICS, "screen-lyrics",
230 N_("Lyrics screen") },
231 { {ESC, 0, 0 }, 0, CMD_INTERRUPT, "lyrics-interrupt",
232 /* translators: interrupt the current background action,
233 e.g. stop loading lyrics from the internet */
234 N_("Interrupt action") },
235 { {'u', 0, 0 }, 0, CMD_LYRICS_UPDATE, "lyrics-update",
236 N_("Update Lyrics") },
237 /* this command may move out of #ifdef ENABLE_LYRICS_SCREEN
238 at some point */
239 { {'e', 0, 0 }, 0, CMD_EDIT, "edit",
240 N_("Edit the current item") },
241 #endif
243 #ifdef ENABLE_OUTPUTS_SCREEN
244 { {'8', F8, 0 }, 0, CMD_SCREEN_OUTPUTS, "screen-outputs",
245 N_("Outputs screen") },
246 #endif
248 #ifdef ENABLE_CHAT_SCREEN
249 { {'9', F9, 0}, 0, CMD_SCREEN_CHAT, "screen-chat",
250 N_("Chat screen") },
251 #endif
253 { { -1, -1, -1 }, 0, CMD_NONE, NULL, NULL }
256 #ifdef ENABLE_KEYDEF_SCREEN
257 command_definition_t *
258 get_command_definitions(void)
260 return cmds;
263 size_t
264 get_cmds_max_name_width(command_definition_t *c)
266 static size_t max = 0;
268 if (max != 0)
269 return max;
271 size_t len;
272 command_definition_t *p;
274 for (p = c; p->name != NULL; p++) {
276 * width and length are considered the same here, as command
277 * names are not translated.
279 len = (size_t) strlen(p->name);
280 if (len > max)
281 max = len;
284 return max;
286 #endif
288 const char *
289 key2str(int key)
291 static char buf[32];
292 int i;
294 buf[0] = 0;
295 switch(key) {
296 case 0:
297 return _("Undefined");
298 case ' ':
299 return _("Space");
300 case RET:
301 return _("Enter");
302 case BS:
303 return _("Backspace");
304 case DEL:
305 return _("Delete");
306 case UP:
307 return _("Up");
308 case DWN:
309 return _("Down");
310 case LEFT:
311 return _("Left");
312 case RGHT:
313 return _("Right");
314 case HOME:
315 return _("Home");
316 case END:
317 return _("End");
318 case PGDN:
319 return _("PageDown");
320 case PGUP:
321 return _("PageUp");
322 case TAB:
323 return _("Tab");
324 case STAB:
325 return _("Shift+Tab");
326 case ESC:
327 return _("Esc");
328 case KEY_IC:
329 return _("Insert");
330 default:
331 for (i = 0; i <= 63; i++)
332 if (key == KEY_F(i)) {
333 g_snprintf(buf, 32, _("F%d"), i );
334 return buf;
336 if (!(key & ~037))
337 g_snprintf(buf, 32, _("Ctrl-%c"), 'A'+(key & 037)-1 );
338 else if ((key & ~037) == 224)
339 g_snprintf(buf, 32, _("Alt-%c"), 'A'+(key & 037)-1 );
340 else if (key > 32 && key < 256)
341 g_snprintf(buf, 32, "%c", key);
342 else
343 g_snprintf(buf, 32, "0x%03X", key);
346 return buf;
349 void
350 command_dump_keys(void)
352 for (int i = 0; cmds[i].description; i++)
353 if (cmds[i].command != CMD_NONE)
354 printf(" %20s : %s\n",
355 get_key_names(cmds[i].command, true),
356 cmds[i].name);
359 #ifndef NCMPC_MINI
361 static void
362 set_key_flags(command_definition_t *cp, command_t command, int flags)
364 for (int i = 0; cp[i].name; i++) {
365 if (cp[i].command == command) {
366 cp[i].flags |= flags;
367 break;
372 #endif
374 const char *
375 get_key_names(command_t command, bool all)
377 for (int i = 0; cmds[i].description; i++) {
378 if (cmds[i].command == command) {
379 static char keystr[80];
381 g_strlcpy(keystr, key2str(cmds[i].keys[0]), sizeof(keystr));
382 if (!all)
383 return keystr;
385 for (int j = 1; j < MAX_COMMAND_KEYS &&
386 cmds[i].keys[j] > 0; j++) {
387 g_strlcat(keystr, " ", sizeof(keystr));
388 g_strlcat(keystr, key2str(cmds[i].keys[j]), sizeof(keystr));
390 return keystr;
393 return NULL;
396 const char *
397 get_key_description(command_t command)
399 for (int i = 0; cmds[i].description; i++)
400 if (cmds[i].command == command)
401 return _(cmds[i].description);
403 return NULL;
406 const char *
407 get_key_command_name(command_t command)
409 for (int i = 0; cmds[i].name; i++)
410 if (cmds[i].command == command)
411 return cmds[i].name;
413 return NULL;
416 command_t
417 get_key_command_from_name(char *name)
419 for (int i = 0; cmds[i].name; i++)
420 if (strcmp(name, cmds[i].name) == 0)
421 return cmds[i].command;
423 return CMD_NONE;
426 command_t
427 find_key_command(int key, command_definition_t *c)
429 assert(key != 0);
430 assert(c != NULL);
432 for (int i = 0; c[i].name; i++) {
433 for (int j = 0; j < MAX_COMMAND_KEYS; j++)
434 if (c[i].keys[j] == key)
435 return c[i].command;
438 return CMD_NONE;
441 command_t
442 get_key_command(int key)
444 return find_key_command(key, cmds);
447 command_t
448 get_keyboard_command(void)
450 int key;
452 key = wgetch(stdscr);
453 if (key == ERR || key == '\0')
454 return CMD_NONE;
456 #ifdef HAVE_GETMOUSE
457 if (key == KEY_MOUSE)
458 return CMD_MOUSE_EVENT;
459 #endif
461 return get_key_command(key);
465 assign_keys(command_t command, int keys[MAX_COMMAND_KEYS])
467 for (int i = 0; cmds[i].name; i++) {
468 if (cmds[i].command == command) {
469 memcpy(cmds[i].keys, keys, sizeof(int)*MAX_COMMAND_KEYS);
470 #ifndef NCMPC_MINI
471 cmds[i].flags |= COMMAND_KEY_MODIFIED;
472 #endif
473 return 0;
477 return -1;
480 #ifndef NCMPC_MINI
483 check_key_bindings(command_definition_t *cp, char *buf, size_t bufsize)
485 int i;
486 int retval = 0;
488 if (cp == NULL)
489 cp = cmds;
491 for (i = 0; cp[i].name; i++)
492 cp[i].flags &= ~COMMAND_KEY_CONFLICT;
494 for (i = 0; cp[i].name; i++) {
495 int j;
496 command_t cmd;
498 for(j=0; j<MAX_COMMAND_KEYS; j++) {
499 if (cp[i].keys[j] &&
500 (cmd = find_key_command(cp[i].keys[j],cp)) != cp[i].command) {
501 if (buf) {
502 g_snprintf(buf, bufsize,
503 _("Key %s assigned to %s and %s"),
504 key2str(cp[i].keys[j]),
505 get_key_command_name(cp[i].command),
506 get_key_command_name(cmd));
507 } else {
508 fprintf(stderr,
509 _("Key %s assigned to %s and %s"),
510 key2str(cp[i].keys[j]),
511 get_key_command_name(cp[i].command),
512 get_key_command_name(cmd));
513 fputc('\n', stderr);
515 cp[i].flags |= COMMAND_KEY_CONFLICT;
516 set_key_flags(cp, cmd, COMMAND_KEY_CONFLICT);
517 retval = -1;
522 return retval;
526 write_key_bindings(FILE *f, int flags)
528 if (flags & KEYDEF_WRITE_HEADER)
529 fprintf(f, "## Key bindings for ncmpc (generated by ncmpc)\n\n");
531 for (int i = 0; cmds[i].name && !ferror(f); i++) {
532 if (cmds[i].flags & COMMAND_KEY_MODIFIED ||
533 flags & KEYDEF_WRITE_ALL) {
534 fprintf(f, "## %s\n", cmds[i].description);
535 if (flags & KEYDEF_COMMENT_ALL)
536 fprintf(f, "#");
537 fprintf(f, "key %s = ", cmds[i].name);
538 for (int j = 0; j < MAX_COMMAND_KEYS; j++) {
539 if (j && cmds[i].keys[j])
540 fprintf(f, ", ");
541 if (!j || cmds[i].keys[j]) {
542 if (cmds[i].keys[j]<256 && (isalpha(cmds[i].keys[j]) ||
543 isdigit(cmds[i].keys[j])))
544 fprintf(f, "\'%c\'", cmds[i].keys[j]);
545 else
546 fprintf(f, "%d", cmds[i].keys[j]);
549 fprintf(f,"\n\n");
553 return ferror(f);
556 #endif /* NCMPC_MINI */