1 /*
2 * (c) 2004 by Kalle Wallin <kaw@linux.se>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 */
19 #include "command.h"
20 #include "config.h"
21 #include "ncmpc.h"
22 #include "i18n.h"
23 #include "mpdclient.h"
24 #include "screen.h"
26 #include <ctype.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <glib.h>
31 #include <signal.h>
32 #include <unistd.h>
34 #undef DEBUG_KEYS
36 #ifdef DEBUG_KEYS
37 #define DK(x) x
38 #else
39 #define DK(x)
40 #endif
42 #define BS KEY_BACKSPACE
43 #define DEL KEY_DC
44 #define UP KEY_UP
45 #define DWN KEY_DOWN
46 #define LEFT KEY_LEFT
47 #define RGHT KEY_RIGHT
48 #define HOME KEY_HOME
49 #define END KEY_END
50 #define PGDN KEY_NPAGE
51 #define PGUP KEY_PPAGE
52 #define TAB 0x09
53 #define STAB 0x161
54 #define ESC 0x1B
55 #define F1 KEY_F(1)
56 #define F2 KEY_F(2)
57 #define F3 KEY_F(3)
58 #define F4 KEY_F(4)
59 #define F5 KEY_F(5)
60 #define F6 KEY_F(6)
61 #define F7 KEY_F(7)
64 static command_definition_t cmds[] = {
65 #ifdef ENABLE_KEYDEF_SCREEN
66 { {'K', 0, 0 }, 0, CMD_SCREEN_KEYDEF, "screen-keyedit",
67 N_("Key configuration screen") },
68 #endif
69 { { 'q', 'Q', 3 }, 0, CMD_QUIT, "quit",
70 N_("Quit") },
72 /* movment */
73 { { UP, 'k', 0 }, 0, CMD_LIST_PREVIOUS, "up",
74 N_("Move cursor up") },
75 { { DWN, 'j', 0 }, 0, CMD_LIST_NEXT, "down",
76 N_("Move cursor down") },
77 { { HOME, 0x01, 0 }, 0, CMD_LIST_FIRST, "home",
78 N_("Home ") },
79 { { END, 0x05, 0 }, 0, CMD_LIST_LAST, "end",
80 N_("End ") },
81 { { PGUP, 0, 0 }, 0, CMD_LIST_PREVIOUS_PAGE, "pgup",
82 N_("Page up") },
83 { { PGDN, 0, 0 }, 0, CMD_LIST_NEXT_PAGE, "pgdn",
84 N_("Page down") },
87 /* basic screens */
88 { { '1', F1, 'h' }, 0, CMD_SCREEN_HELP, "screen-help",
89 N_("Help screen") },
90 { { '2', F2, 0 }, 0, CMD_SCREEN_PLAY, "screen-playlist",
91 N_("Playlist screen") },
92 { { '3', F3, 0 }, 0, CMD_SCREEN_FILE, "screen-browse",
93 N_("Browse screen") },
96 /* player commands */
97 { { 13, 0, 0 }, 0, CMD_PLAY, "play",
98 N_("Play/Enter directory") },
99 { { 'P', 0, 0 }, 0, CMD_PAUSE,"pause",
100 N_("Pause") },
101 { { 's', BS, 0 }, 0, CMD_STOP, "stop",
102 N_("Stop") },
103 { { 'o', 0, 0 }, 0, CMD_CROP, "crop",
104 N_("Crop") },
105 { { '>', 0, 0 }, 0, CMD_TRACK_NEXT, "next",
106 N_("Next track") },
107 { { '<', 0, 0 }, 0, CMD_TRACK_PREVIOUS, "prev",
108 N_("Previous track") },
109 { { 'f', 0, 0 }, 0, CMD_SEEK_FORWARD, "seek-forward",
110 N_("Seek forward") },
111 { { 'b', 0, 0 }, 0, CMD_SEEK_BACKWARD, "seek-backward",
112 N_("Seek backward") },
113 { { '+', RGHT, 0 }, 0, CMD_VOLUME_UP, "volume-up",
114 N_("Increase volume") },
115 { { '-', LEFT, 0 }, 0, CMD_VOLUME_DOWN, "volume-down",
116 N_("Decrease volume") },
117 { { ' ', 0, 0 }, 0, CMD_SELECT, "select",
118 N_("Select/deselect song in playlist") },
119 { { 't', 0, 0 }, 0, CMD_SELECT_ALL, "select_all",
120 N_("Select all listed items") },
121 { { DEL, 'd', 0 }, 0, CMD_DELETE, "delete",
122 N_("Delete song from playlist") },
123 { { 'Z', 0, 0 }, 0, CMD_SHUFFLE, "shuffle",
124 N_("Shuffle playlist") },
125 { { 'c', 0, 0 }, 0, CMD_CLEAR, "clear",
126 N_("Clear playlist") },
127 { { 'r', 0, 0 }, 0, CMD_REPEAT, "repeat",
128 N_("Toggle repeat mode") },
129 { { 'z', 0, 0 }, 0, CMD_RANDOM, "random",
130 N_("Toggle random mode") },
131 { { 'x', 0, 0 }, 0, CMD_CROSSFADE, "crossfade",
132 N_("Toggle crossfade mode") },
133 { { 21, 0, 0 }, 0, CMD_DB_UPDATE, "db-update",
134 N_("Start a music database update") },
135 { { 'S', 0, 0 }, 0, CMD_SAVE_PLAYLIST, "save",
136 N_("Save playlist") },
137 { { 'a', 0, 0 }, 0, CMD_ADD, "add",
138 N_("Add url/file to playlist") },
140 { { '!', 0, 0 }, 0, CMD_GO_ROOT_DIRECTORY, "go-root-directory",
141 N_("Go to root directory") },
142 { { '"', 0, 0 }, 0, CMD_GO_PARENT_DIRECTORY, "go-parent-directory",
143 N_("Go to parent directory") },
145 /* lists */
146 { { 11, 0, 0 }, 0, CMD_LIST_MOVE_UP, "move-up",
147 N_("Move item up") },
148 { { 10, 0, 0 }, 0, CMD_LIST_MOVE_DOWN, "move-down",
149 N_("Move item down") },
150 { { 12, 0, 0 }, 0, CMD_SCREEN_UPDATE, "update",
151 N_("Update screen") },
154 /* ncmpc options */
155 { { 'w', 0, 0 }, 0, CMD_TOGGLE_FIND_WRAP, "wrap-mode",
156 N_("Toggle find mode") },
157 { { 'U', 0, 0 }, 0, CMD_TOGGLE_AUTOCENTER, "autocenter-mode",
158 N_("Toggle auto center mode") },
161 /* change screen */
162 { { TAB, 0, 0 }, 0, CMD_SCREEN_NEXT, "screen-next",
163 N_("Next screen") },
164 { { STAB, 0, 0 }, 0, CMD_SCREEN_PREVIOUS, "screen-prev",
165 N_("Previous screen") },
168 /* find */
169 { { '/', 0, 0 }, 0, CMD_LIST_FIND, "find",
170 N_("Forward find") },
171 { { 'n', 0, 0 }, 0, CMD_LIST_FIND_NEXT, "find-next",
172 N_("Forward find next") },
173 { { '?', 0, 0 }, 0, CMD_LIST_RFIND, "rfind",
174 N_("Backward find") },
175 { { 'p', 0, 0 }, 0, CMD_LIST_RFIND_NEXT, "rfind-next",
176 N_("Backward find previous") },
179 /* extra screens */
180 #ifdef ENABLE_ARTIST_SCREEN
181 { {'4', F4, 0 }, 0, CMD_SCREEN_ARTIST, "screen-artist",
182 N_("Artist screen") },
183 #endif
184 #ifdef ENABLE_SEARCH_SCREEN
185 { {'5', F5, 0 }, 0, CMD_SCREEN_SEARCH, "screen-search",
186 N_("Search screen") },
187 { {'m', 0, 0 }, 0, CMD_SEARCH_MODE, "search-mode",
188 N_("Change search mode") },
189 #endif
191 #ifdef ENABLE_LYRICS_SCREEN
192 { {'7', F7, 0 }, 0, CMD_SCREEN_LYRICS, "screen-lyrics",
193 N_("Lyrics screen") },
194 { {ESC, 0, 0 }, 0, CMD_INTERRUPT, "lyrics-interrupt",
195 N_("Interrupt action") },
196 { {'u', 0, 0 }, 0, CMD_LYRICS_UPDATE, "lyrics-update",
197 N_("Update Lyrics") },
198 #endif
200 { { -1, -1, -1 }, 0, CMD_NONE, NULL, NULL }
201 };
203 #ifdef ENABLE_KEYDEF_SCREEN
204 command_definition_t *
205 get_command_definitions(void)
206 {
207 return cmds;
208 }
209 #endif
211 const char *
212 key2str(int key)
213 {
214 static char buf[32];
215 int i;
217 buf[0] = 0;
218 switch(key) {
219 case 0:
220 return _("Undefined");
221 case ' ':
222 return _("Space");
223 case 13:
224 return _("Enter");
225 case BS:
226 return _("Backspace");
227 case DEL:
228 return _("Delete");
229 case UP:
230 return _("Up");
231 case DWN:
232 return _("Down");
233 case LEFT:
234 return _("Left");
235 case RGHT:
236 return _("Right");
237 case HOME:
238 return _("Home");
239 case END:
240 return _("End");
241 case PGDN:
242 return _("PageDown");
243 case PGUP:
244 return _("PageUp");
245 case TAB:
246 return _("Tab");
247 case STAB:
248 return _("Shift+Tab");
249 case ESC:
250 return _("Esc");
251 case KEY_IC:
252 return _("Insert");
253 default:
254 for (i = 0; i <= 63; i++)
255 if (key == KEY_F(i)) {
256 g_snprintf(buf, 32, "F%d", i );
257 return buf;
258 }
259 if (!(key & ~037))
260 g_snprintf(buf, 32, "Ctrl-%c", 'A'+(key & 037)-1 );
261 else if ((key & ~037) == 224)
262 g_snprintf(buf, 32, "Alt-%c", 'A'+(key & 037)-1 );
263 else if (key > 32 && key < 256)
264 g_snprintf(buf, 32, "%c", key);
265 else
266 g_snprintf(buf, 32, "0x%03X", key);
267 }
269 return buf;
270 }
272 void
273 command_dump_keys(void)
274 {
275 int i;
277 i = 0;
278 while (cmds[i].description) {
279 if (cmds[i].command != CMD_NONE)
280 printf(" %20s : %s\n", get_key_names(cmds[i].command,1),cmds[i].name);
281 i++;
282 }
283 }
285 static int
286 set_key_flags(command_definition_t *cp, command_t command, int flags)
287 {
288 int i;
290 i = 0;
291 while (cp[i].name) {
292 if (cp[i].command == command) {
293 cp[i].flags |= flags;
294 return 0;
295 }
296 i++;
297 }
299 return 1;
300 }
302 const char *
303 get_key_names(command_t command, int all)
304 {
305 int i;
307 i=0;
308 while (cmds[i].description) {
309 if (cmds[i].command == command) {
310 int j;
311 static char keystr[80];
313 g_strlcpy(keystr, key2str(cmds[i].keys[0]), sizeof(keystr));
314 if (!all)
315 return keystr;
316 j=1;
317 while (j < MAX_COMMAND_KEYS && cmds[i].keys[j] > 0) {
318 g_strlcat(keystr, " ", sizeof(keystr));
319 g_strlcat(keystr, key2str(cmds[i].keys[j]), sizeof(keystr));
320 j++;
321 }
322 return keystr;
323 }
324 i++;
325 }
326 return NULL;
327 }
329 const char *
330 get_key_description(command_t command)
331 {
332 int i;
334 i=0;
335 while (cmds[i].description) {
336 if (cmds[i].command == command)
337 return _(cmds[i].description);
338 i++;
339 }
341 return NULL;
342 }
344 const char *
345 get_key_command_name(command_t command)
346 {
347 int i;
349 i=0;
350 while (cmds[i].name) {
351 if (cmds[i].command == command)
352 return cmds[i].name;
353 i++;
354 }
355 return NULL;
356 }
358 command_t
359 get_key_command_from_name(char *name)
360 {
361 int i;
363 i=0;
364 while (cmds[i].name) {
365 if (strcmp(name, cmds[i].name) == 0)
366 return cmds[i].command;
367 i++;
368 }
370 return CMD_NONE;
371 }
373 command_t
374 find_key_command(int key, command_definition_t *c)
375 {
376 int i;
378 i=0;
379 while (key && c && c[i].name) {
380 if (c[i].keys[0] == key ||
381 c[i].keys[1] == key ||
382 c[i].keys[2] == key)
383 return c[i].command;
384 i++;
385 }
387 return CMD_NONE;
388 }
390 command_t
391 get_key_command(int key)
392 {
393 return find_key_command(key, cmds);
394 }
396 command_t
397 get_keyboard_command(void)
398 {
399 int key;
401 key = wgetch(stdscr);
402 if (key == ERR)
403 return CMD_NONE;
405 #ifdef HAVE_GETMOUSE
406 if (key == KEY_MOUSE)
407 return CMD_MOUSE_EVENT;
408 #endif
410 return get_key_command(key);
411 }
413 int
414 assign_keys(command_t command, int keys[MAX_COMMAND_KEYS])
415 {
416 int i;
418 i=0;
419 while (cmds[i].name) {
420 if (cmds[i].command == command) {
421 memcpy(cmds[i].keys, keys, sizeof(int)*MAX_COMMAND_KEYS);
422 cmds[i].flags |= COMMAND_KEY_MODIFIED;
423 return 0;
424 }
425 i++;
426 }
427 return -1;
428 }
430 int
431 check_key_bindings(command_definition_t *cp, char *buf, size_t bufsize)
432 {
433 int i;
434 int retval = 0;
436 if (cp == NULL)
437 cp = cmds;
439 i=0;
440 while (cp[i].name) {
441 cp[i].flags &= ~COMMAND_KEY_CONFLICT;
442 i++;
443 }
445 i=0;
446 while (cp[i].name) {
447 int j;
448 command_t cmd;
450 for(j=0; j<MAX_COMMAND_KEYS; j++)
451 if (cp[i].keys[j] &&
452 (cmd = find_key_command(cp[i].keys[j],cp)) != cp[i].command) {
453 if (buf) {
454 #ifdef ENABLE_KEYDEF_SCREEN
455 g_snprintf(buf, bufsize,
456 _("Key %s assigned to %s and %s (press %s for the key editor)"),
457 key2str(cp[i].keys[j]),
458 get_key_command_name(cp[i].command),
459 get_key_command_name(cmd),
460 get_key_names(CMD_SCREEN_KEYDEF,0));
461 #else
462 g_snprintf(buf, bufsize,
463 _("Error: Key %s assigned to %s and %s !!!\n"),
464 key2str(cp[i].keys[j]),
465 get_key_command_name(cp[i].command),
466 get_key_command_name(cmd));
467 #endif
468 } else
469 fprintf(stderr,
470 _("Error: Key %s assigned to %s and %s !!!\n"),
471 key2str(cp[i].keys[j]),
472 get_key_command_name(cp[i].command),
473 get_key_command_name(cmd));
474 cp[i].flags |= COMMAND_KEY_CONFLICT;
475 set_key_flags(cp, cmd, COMMAND_KEY_CONFLICT);
476 retval = -1;
477 }
478 i++;
479 }
480 return retval;
481 }
483 int
484 write_key_bindings(FILE *f, int flags)
485 {
486 int i,j;
488 if (flags & KEYDEF_WRITE_HEADER)
489 fprintf(f, "## Key bindings for ncmpc (generated by ncmpc)\n\n");
491 i = 0;
492 while (cmds[i].name && !ferror(f)) {
493 if (cmds[i].flags & COMMAND_KEY_MODIFIED ||
494 flags & KEYDEF_WRITE_ALL) {
495 fprintf(f, "## %s\n", cmds[i].description);
496 if (flags & KEYDEF_COMMENT_ALL)
497 fprintf(f, "#");
498 fprintf(f, "key %s = ", cmds[i].name);
499 for (j = 0; j < MAX_COMMAND_KEYS; j++) {
500 if (j && cmds[i].keys[j])
501 fprintf(f, ", ");
502 if (!j || cmds[i].keys[j]) {
503 if (cmds[i].keys[j]<256 && (isalpha(cmds[i].keys[j]) ||
504 isdigit(cmds[i].keys[j])))
505 fprintf(f, "\'%c\'", cmds[i].keys[j]);
506 else
507 fprintf(f, "%d", cmds[i].keys[j]);
508 }
509 }
510 fprintf(f,"\n\n");
511 }
512 i++;
513 }
515 return ferror(f);
516 }