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 { { 'l', 0, 0 }, 0, CMD_LOCATE, "locate",
146 N_("Locate song in browser") },
148 /* lists */
149 { { 11, 0, 0 }, 0, CMD_LIST_MOVE_UP, "move-up",
150 N_("Move item up") },
151 { { 10, 0, 0 }, 0, CMD_LIST_MOVE_DOWN, "move-down",
152 N_("Move item down") },
153 { { 12, 0, 0 }, 0, CMD_SCREEN_UPDATE, "update",
154 N_("Update screen") },
157 /* ncmpc options */
158 { { 'w', 0, 0 }, 0, CMD_TOGGLE_FIND_WRAP, "wrap-mode",
159 N_("Toggle find mode") },
160 { { 'U', 0, 0 }, 0, CMD_TOGGLE_AUTOCENTER, "autocenter-mode",
161 N_("Toggle auto center mode") },
164 /* change screen */
165 { { TAB, 0, 0 }, 0, CMD_SCREEN_NEXT, "screen-next",
166 N_("Next screen") },
167 { { STAB, 0, 0 }, 0, CMD_SCREEN_PREVIOUS, "screen-prev",
168 N_("Previous screen") },
171 /* find */
172 { { '/', 0, 0 }, 0, CMD_LIST_FIND, "find",
173 N_("Forward find") },
174 { { 'n', 0, 0 }, 0, CMD_LIST_FIND_NEXT, "find-next",
175 N_("Forward find next") },
176 { { '?', 0, 0 }, 0, CMD_LIST_RFIND, "rfind",
177 N_("Backward find") },
178 { { 'p', 0, 0 }, 0, CMD_LIST_RFIND_NEXT, "rfind-next",
179 N_("Backward find previous") },
182 /* extra screens */
183 #ifdef ENABLE_ARTIST_SCREEN
184 { {'4', F4, 0 }, 0, CMD_SCREEN_ARTIST, "screen-artist",
185 N_("Artist screen") },
186 #endif
187 #ifdef ENABLE_SEARCH_SCREEN
188 { {'5', F5, 0 }, 0, CMD_SCREEN_SEARCH, "screen-search",
189 N_("Search screen") },
190 { {'m', 0, 0 }, 0, CMD_SEARCH_MODE, "search-mode",
191 N_("Change search mode") },
192 #endif
194 #ifdef ENABLE_LYRICS_SCREEN
195 { {'7', F7, 0 }, 0, CMD_SCREEN_LYRICS, "screen-lyrics",
196 N_("Lyrics screen") },
197 { {ESC, 0, 0 }, 0, CMD_INTERRUPT, "lyrics-interrupt",
198 N_("Interrupt action") },
199 { {'u', 0, 0 }, 0, CMD_LYRICS_UPDATE, "lyrics-update",
200 N_("Update Lyrics") },
201 #endif
203 { { -1, -1, -1 }, 0, CMD_NONE, NULL, NULL }
204 };
206 #ifdef ENABLE_KEYDEF_SCREEN
207 command_definition_t *
208 get_command_definitions(void)
209 {
210 return cmds;
211 }
212 #endif
214 const char *
215 key2str(int key)
216 {
217 static char buf[32];
218 int i;
220 buf[0] = 0;
221 switch(key) {
222 case 0:
223 return _("Undefined");
224 case ' ':
225 return _("Space");
226 case 13:
227 return _("Enter");
228 case BS:
229 return _("Backspace");
230 case DEL:
231 return _("Delete");
232 case UP:
233 return _("Up");
234 case DWN:
235 return _("Down");
236 case LEFT:
237 return _("Left");
238 case RGHT:
239 return _("Right");
240 case HOME:
241 return _("Home");
242 case END:
243 return _("End");
244 case PGDN:
245 return _("PageDown");
246 case PGUP:
247 return _("PageUp");
248 case TAB:
249 return _("Tab");
250 case STAB:
251 return _("Shift+Tab");
252 case ESC:
253 return _("Esc");
254 case KEY_IC:
255 return _("Insert");
256 default:
257 for (i = 0; i <= 63; i++)
258 if (key == KEY_F(i)) {
259 g_snprintf(buf, 32, "F%d", i );
260 return buf;
261 }
262 if (!(key & ~037))
263 g_snprintf(buf, 32, "Ctrl-%c", 'A'+(key & 037)-1 );
264 else if ((key & ~037) == 224)
265 g_snprintf(buf, 32, "Alt-%c", 'A'+(key & 037)-1 );
266 else if (key > 32 && key < 256)
267 g_snprintf(buf, 32, "%c", key);
268 else
269 g_snprintf(buf, 32, "0x%03X", key);
270 }
272 return buf;
273 }
275 void
276 command_dump_keys(void)
277 {
278 int i;
280 i = 0;
281 while (cmds[i].description) {
282 if (cmds[i].command != CMD_NONE)
283 printf(" %20s : %s\n", get_key_names(cmds[i].command,1),cmds[i].name);
284 i++;
285 }
286 }
288 static int
289 set_key_flags(command_definition_t *cp, command_t command, int flags)
290 {
291 int i;
293 i = 0;
294 while (cp[i].name) {
295 if (cp[i].command == command) {
296 cp[i].flags |= flags;
297 return 0;
298 }
299 i++;
300 }
302 return 1;
303 }
305 const char *
306 get_key_names(command_t command, int all)
307 {
308 int i;
310 i=0;
311 while (cmds[i].description) {
312 if (cmds[i].command == command) {
313 int j;
314 static char keystr[80];
316 g_strlcpy(keystr, key2str(cmds[i].keys[0]), sizeof(keystr));
317 if (!all)
318 return keystr;
319 j=1;
320 while (j < MAX_COMMAND_KEYS && cmds[i].keys[j] > 0) {
321 g_strlcat(keystr, " ", sizeof(keystr));
322 g_strlcat(keystr, key2str(cmds[i].keys[j]), sizeof(keystr));
323 j++;
324 }
325 return keystr;
326 }
327 i++;
328 }
329 return NULL;
330 }
332 const char *
333 get_key_description(command_t command)
334 {
335 int i;
337 i=0;
338 while (cmds[i].description) {
339 if (cmds[i].command == command)
340 return _(cmds[i].description);
341 i++;
342 }
344 return NULL;
345 }
347 const char *
348 get_key_command_name(command_t command)
349 {
350 int i;
352 i=0;
353 while (cmds[i].name) {
354 if (cmds[i].command == command)
355 return cmds[i].name;
356 i++;
357 }
358 return NULL;
359 }
361 command_t
362 get_key_command_from_name(char *name)
363 {
364 int i;
366 i=0;
367 while (cmds[i].name) {
368 if (strcmp(name, cmds[i].name) == 0)
369 return cmds[i].command;
370 i++;
371 }
373 return CMD_NONE;
374 }
376 command_t
377 find_key_command(int key, command_definition_t *c)
378 {
379 int i;
381 i=0;
382 while (key && c && c[i].name) {
383 if (c[i].keys[0] == key ||
384 c[i].keys[1] == key ||
385 c[i].keys[2] == key)
386 return c[i].command;
387 i++;
388 }
390 return CMD_NONE;
391 }
393 command_t
394 get_key_command(int key)
395 {
396 return find_key_command(key, cmds);
397 }
399 command_t
400 get_keyboard_command(void)
401 {
402 int key;
404 key = wgetch(stdscr);
405 if (key == ERR)
406 return CMD_NONE;
408 #ifdef HAVE_GETMOUSE
409 if (key == KEY_MOUSE)
410 return CMD_MOUSE_EVENT;
411 #endif
413 return get_key_command(key);
414 }
416 int
417 assign_keys(command_t command, int keys[MAX_COMMAND_KEYS])
418 {
419 int i;
421 i=0;
422 while (cmds[i].name) {
423 if (cmds[i].command == command) {
424 memcpy(cmds[i].keys, keys, sizeof(int)*MAX_COMMAND_KEYS);
425 cmds[i].flags |= COMMAND_KEY_MODIFIED;
426 return 0;
427 }
428 i++;
429 }
430 return -1;
431 }
433 int
434 check_key_bindings(command_definition_t *cp, char *buf, size_t bufsize)
435 {
436 int i;
437 int retval = 0;
439 if (cp == NULL)
440 cp = cmds;
442 i=0;
443 while (cp[i].name) {
444 cp[i].flags &= ~COMMAND_KEY_CONFLICT;
445 i++;
446 }
448 i=0;
449 while (cp[i].name) {
450 int j;
451 command_t cmd;
453 for(j=0; j<MAX_COMMAND_KEYS; j++)
454 if (cp[i].keys[j] &&
455 (cmd = find_key_command(cp[i].keys[j],cp)) != cp[i].command) {
456 if (buf) {
457 #ifdef ENABLE_KEYDEF_SCREEN
458 g_snprintf(buf, bufsize,
459 _("Key %s assigned to %s and %s (press %s for the key editor)"),
460 key2str(cp[i].keys[j]),
461 get_key_command_name(cp[i].command),
462 get_key_command_name(cmd),
463 get_key_names(CMD_SCREEN_KEYDEF,0));
464 #else
465 g_snprintf(buf, bufsize,
466 _("Error: Key %s assigned to %s and %s !!!\n"),
467 key2str(cp[i].keys[j]),
468 get_key_command_name(cp[i].command),
469 get_key_command_name(cmd));
470 #endif
471 } else
472 fprintf(stderr,
473 _("Error: Key %s assigned to %s and %s !!!\n"),
474 key2str(cp[i].keys[j]),
475 get_key_command_name(cp[i].command),
476 get_key_command_name(cmd));
477 cp[i].flags |= COMMAND_KEY_CONFLICT;
478 set_key_flags(cp, cmd, COMMAND_KEY_CONFLICT);
479 retval = -1;
480 }
481 i++;
482 }
483 return retval;
484 }
486 int
487 write_key_bindings(FILE *f, int flags)
488 {
489 int i,j;
491 if (flags & KEYDEF_WRITE_HEADER)
492 fprintf(f, "## Key bindings for ncmpc (generated by ncmpc)\n\n");
494 i = 0;
495 while (cmds[i].name && !ferror(f)) {
496 if (cmds[i].flags & COMMAND_KEY_MODIFIED ||
497 flags & KEYDEF_WRITE_ALL) {
498 fprintf(f, "## %s\n", cmds[i].description);
499 if (flags & KEYDEF_COMMENT_ALL)
500 fprintf(f, "#");
501 fprintf(f, "key %s = ", cmds[i].name);
502 for (j = 0; j < MAX_COMMAND_KEYS; j++) {
503 if (j && cmds[i].keys[j])
504 fprintf(f, ", ");
505 if (!j || cmds[i].keys[j]) {
506 if (cmds[i].keys[j]<256 && (isalpha(cmds[i].keys[j]) ||
507 isdigit(cmds[i].keys[j])))
508 fprintf(f, "\'%c\'", cmds[i].keys[j]);
509 else
510 fprintf(f, "%d", cmds[i].keys[j]);
511 }
512 }
513 fprintf(f,"\n\n");
514 }
515 i++;
516 }
518 return ferror(f);
519 }