Code

removed KEY_RESIZE
[ncmpc.git] / src / command.c
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 command_definition_t *
204 get_command_definitions(void)
206         return cmds;
209 const char *
210 key2str(int key)
212         static char buf[32];
213         int i;
215         buf[0] = 0;
216         switch(key) {
217         case 0:
218                 return _("Undefined");
219         case ' ':
220                 return _("Space");
221         case 13:
222                 return _("Enter");
223         case BS:
224                 return _("Backspace");
225         case DEL:
226                 return _("Delete");
227         case UP:
228                 return _("Up");
229         case DWN:
230                 return _("Down");
231         case LEFT:
232                 return _("Left");
233         case RGHT:
234                 return _("Right");
235         case HOME:
236                 return _("Home");
237         case END:
238                 return _("End");
239         case PGDN:
240                 return _("PageDown");
241         case PGUP:
242                 return _("PageUp");
243         case TAB:
244                 return _("Tab");
245         case STAB:
246                 return _("Shift+Tab");
247         case ESC:
248                 return _("Esc");
249         case KEY_IC:
250                 return _("Insert");
251         default:
252                 for (i = 0; i <= 63; i++)
253                         if (key == KEY_F(i)) {
254                                 g_snprintf(buf, 32, "F%d", i );
255                                 return buf;
256                         }
257                 if (!(key & ~037))
258                         g_snprintf(buf, 32, "Ctrl-%c", 'A'+(key & 037)-1 );
259                 else if ((key & ~037) == 224)
260                         g_snprintf(buf, 32, "Alt-%c", 'A'+(key & 037)-1 );
261                 else if (key > 32 && key < 256)
262                         g_snprintf(buf, 32, "%c", key);
263                 else
264                         g_snprintf(buf, 32, "0x%03X", key);
265         }
267         return buf;
270 void
271 command_dump_keys(void)
273         int i;
275         i = 0;
276         while (cmds[i].description) {
277                 if (cmds[i].command != CMD_NONE)
278                         printf(" %20s : %s\n", get_key_names(cmds[i].command,1),cmds[i].name);
279                 i++;
280         }
283 static int
284 set_key_flags(command_definition_t *cp, command_t command, int flags)
286         int i;
288         i = 0;
289         while (cp[i].name) {
290                 if (cp[i].command == command) {
291                         cp[i].flags |= flags;
292                         return 0;
293                 }
294                 i++;
295         }
297         return 1;
300 const char *
301 get_key_names(command_t command, int all)
303   int i;
305   i=0;
306   while (cmds[i].description) {
307           if (cmds[i].command == command) {
308                   int j;
309                   static char keystr[80];
311                   g_strlcpy(keystr, key2str(cmds[i].keys[0]), sizeof(keystr));
312                   if (!all)
313                           return keystr;
314                   j=1;
315                   while (j < MAX_COMMAND_KEYS && cmds[i].keys[j] > 0) {
316                           g_strlcat(keystr, " ", sizeof(keystr));
317                           g_strlcat(keystr, key2str(cmds[i].keys[j]), sizeof(keystr));
318                           j++;
319                   }
320                   return keystr;
321           }
322           i++;
323   }
324   return NULL;
327 const char *
328 get_key_description(command_t command)
330         int i;
332         i=0;
333         while (cmds[i].description) {
334                 if (cmds[i].command == command)
335                         return _(cmds[i].description);
336                 i++;
337         }
339         return NULL;
342 const char *
343 get_key_command_name(command_t command)
345         int i;
347         i=0;
348         while (cmds[i].name) {
349                 if (cmds[i].command == command)
350                         return cmds[i].name;
351                 i++;
352         }
353         return NULL;
356 command_t
357 get_key_command_from_name(char *name)
359         int i;
361         i=0;
362         while (cmds[i].name) {
363                 if (strcmp(name, cmds[i].name) == 0)
364                         return cmds[i].command;
365                 i++;
366         }
368         return CMD_NONE;
371 command_t
372 find_key_command(int key, command_definition_t *c)
374         int i;
376         i=0;
377         while (key && c && c[i].name) {
378                 if (c[i].keys[0] == key ||
379                     c[i].keys[1] == key ||
380                     c[i].keys[2] == key)
381                         return c[i].command;
382                 i++;
383         }
385         return CMD_NONE;
388 command_t
389 get_key_command(int key)
391         return find_key_command(key, cmds);
394 int
395 my_wgetch(WINDOW *w)
397         int c;
399         c = wgetch(w);
401 #ifdef ENABLE_RAW_MODE
402         /* handle SIGSTOP (Ctrl-Z) */
403         if (c == 26 || c == 407)
404                 sigstop();
405         /* handle SIGINT (Ctrl-C) */
406         if (c == 3)
407                 kill(getpid(), SIGTERM);
408 #endif
410         return c;
413 command_t
414 get_keyboard_command(void)
416         int key;
418         key = my_wgetch(stdscr);
419         if (key == ERR)
420                 return CMD_NONE;
422 #ifdef HAVE_GETMOUSE
423         if (key == KEY_MOUSE)
424                 return CMD_MOUSE_EVENT;
425 #endif
427         return get_key_command(key);
430 int
431 assign_keys(command_t command, int keys[MAX_COMMAND_KEYS])
433         int i;
435         i=0;
436         while (cmds[i].name) {
437                 if (cmds[i].command == command) {
438                         memcpy(cmds[i].keys, keys, sizeof(int)*MAX_COMMAND_KEYS);
439                         cmds[i].flags |= COMMAND_KEY_MODIFIED;
440                         return 0;
441                 }
442                 i++;
443         }
444         return -1;
447 int
448 check_key_bindings(command_definition_t *cp, char *buf, size_t bufsize)
450         int i;
451         int retval = 0;
453         if (cp == NULL)
454                 cp = cmds;
456         i=0;
457         while (cp[i].name) {
458                 cp[i].flags &= ~COMMAND_KEY_CONFLICT;
459                 i++;
460         }
462         i=0;
463         while (cp[i].name) {
464                 int j;
465                 command_t cmd;
467                 for(j=0; j<MAX_COMMAND_KEYS; j++)
468                         if (cp[i].keys[j] &&
469                             (cmd = find_key_command(cp[i].keys[j],cp)) != cp[i].command) {
470                                 if (buf) {
471 #ifdef ENABLE_KEYDEF_SCREEN
472                                         g_snprintf(buf, bufsize,
473                                                    _("Key %s assigned to %s and %s (press %s for the key editor)"),
474                                                    key2str(cp[i].keys[j]),
475                                                    get_key_command_name(cp[i].command),
476                                                    get_key_command_name(cmd),
477                                                    get_key_names(CMD_SCREEN_KEYDEF,0));
478 #else
479                                         g_snprintf(buf, bufsize,
480                                                    _("Error: Key %s assigned to %s and %s !!!\n"),
481                                                    key2str(cp[i].keys[j]),
482                                                    get_key_command_name(cp[i].command),
483                                                    get_key_command_name(cmd));
484 #endif
485                                 } else
486                                         fprintf(stderr,
487                                                 _("Error: Key %s assigned to %s and %s !!!\n"),
488                                                 key2str(cp[i].keys[j]),
489                                                 get_key_command_name(cp[i].command),
490                                                 get_key_command_name(cmd));
491                                 cp[i].flags |= COMMAND_KEY_CONFLICT;
492                                 set_key_flags(cp, cmd, COMMAND_KEY_CONFLICT);
493                                 retval = -1;
494                         }
495                 i++;
496         }
497         return retval;
500 int
501 write_key_bindings(FILE *f, int flags)
503         int i,j;
505         if (flags & KEYDEF_WRITE_HEADER)
506                 fprintf(f, "## Key bindings for ncmpc (generated by ncmpc)\n\n");
508         i = 0;
509         while (cmds[i].name && !ferror(f)) {
510                 if (cmds[i].flags & COMMAND_KEY_MODIFIED ||
511                     flags & KEYDEF_WRITE_ALL) {
512                         fprintf(f, "## %s\n", cmds[i].description);
513                         if (flags & KEYDEF_COMMENT_ALL)
514                                 fprintf(f, "#");
515                         fprintf(f, "key %s = ", cmds[i].name);
516                         for (j = 0; j < MAX_COMMAND_KEYS; j++) {
517                                 if (j && cmds[i].keys[j])
518                                         fprintf(f, ",  ");
519                                 if (!j || cmds[i].keys[j]) {
520                                         if (cmds[i].keys[j]<256 && (isalpha(cmds[i].keys[j]) ||
521                                                                     isdigit(cmds[i].keys[j])))
522                                                 fprintf(f, "\'%c\'", cmds[i].keys[j]);
523                                         else
524                                                 fprintf(f, "%d", cmds[i].keys[j]);
525                                 }
526                         }
527                         fprintf(f,"\n\n");
528                 }
529                 i++;
530         }
532         return ferror(f);