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