Code

moved i18n macros to i18n.h
[ncmpc.git] / src / command.c
1 /* 
2  * $Id$
3  *
4  * (c) 2004 by Kalle Wallin <kaw@linux.se>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
21 #include "command.h"
22 #include "config.h"
23 #include "ncmpc.h"
24 #include "i18n.h"
25 #include "mpdclient.h"
26 #include "screen.h"
28 #include <ctype.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include <glib.h>
33 #include <signal.h>
34 #include <unistd.h>
36 #undef DEBUG_KEYS
38 #ifdef DEBUG_KEYS
39 #define DK(x) x
40 #else
41 #define DK(x)
42 #endif
44 #define BS   KEY_BACKSPACE
45 #define DEL  KEY_DC
46 #define UP   KEY_UP
47 #define DWN  KEY_DOWN
48 #define LEFT KEY_LEFT
49 #define RGHT KEY_RIGHT
50 #define HOME KEY_HOME
51 #define END  KEY_END
52 #define PGDN KEY_NPAGE
53 #define PGUP KEY_PPAGE
54 #define TAB  0x09
55 #define STAB 0x161
56 #define ESC  0x1B
57 #define F1   KEY_F(1)
58 #define F2   KEY_F(2)
59 #define F3   KEY_F(3)
60 #define F4   KEY_F(4)
61 #define F5   KEY_F(5)
62 #define F6   KEY_F(6)
63 #define F7   KEY_F(7)
66 static command_definition_t cmds[] =
67 {
68 #ifdef ENABLE_KEYDEF_SCREEN
69   { {'K',   0,   0 }, 0, CMD_SCREEN_KEYDEF,    "screen-keyedit",
70     N_("Key configuration screen") },
71 #endif
72   { { 'q', 'Q',  3 }, 0, CMD_QUIT,   "quit",
73     N_("Quit") },  
75   /* movment */
76   { {  UP,  'k',   0 }, 0, CMD_LIST_PREVIOUS,      "up",
77     N_("Move cursor up") },
78   { { DWN,  'j',   0 }, 0, CMD_LIST_NEXT,          "down",
79     N_("Move cursor down") },
80   { { HOME, 0x01, 0 }, 0, CMD_LIST_FIRST,          "home",
81     N_("Home ") },
82   { { END,  0x05, 0 }, 0, CMD_LIST_LAST,           "end",
83     N_("End ") },
84   { { PGUP,   0,   0 }, 0, CMD_LIST_PREVIOUS_PAGE, "pgup",
85     N_("Page up") },
86   { { PGDN,   0,   0 }, 0, CMD_LIST_NEXT_PAGE,     "pgdn", 
87     N_("Page down") },
90   /* basic screens */
91   { { '1', F1, 'h' }, 0, CMD_SCREEN_HELP,      "screen-help",
92     N_("Help screen") },
93   { { '2', F2,  0 }, 0, CMD_SCREEN_PLAY,      "screen-playlist",
94     N_("Playlist screen") },
95   { { '3', F3,  0 }, 0, CMD_SCREEN_FILE,      "screen-browse",
96     N_("Browse screen") },
99   /* player commands */
100   { {  13,   0,   0 }, 0, CMD_PLAY, "play",  
101     N_("Play/Enter directory") },
102   { { 'P',   0,   0 }, 0, CMD_PAUSE,"pause", 
103     N_("Pause") },
104   { { 's',  BS,   0 }, 0, CMD_STOP, "stop",   
105     N_("Stop") },
106   { { 'o',  0,   0 }, 0, CMD_CROP, "crop",
107     N_("Crop") },
108   { { '>',   0,   0 }, 0, CMD_TRACK_NEXT, "next", 
109     N_("Next track") },
110   { { '<',   0,   0 }, 0, CMD_TRACK_PREVIOUS, "prev", 
111     N_("Previous track") },
112   { { 'f',   0,   0 }, 0, CMD_SEEK_FORWARD, "seek-forward", 
113     N_("Seek forward") },
114   { { 'b',   0,   0 }, 0, CMD_SEEK_BACKWARD, "seek-backward", 
115     N_("Seek backward") },
116   { { '+', RGHT,  0 }, 0, CMD_VOLUME_UP, "volume-up", 
117     N_("Increase volume") },
118   { { '-', LEFT,  0 }, 0, CMD_VOLUME_DOWN, "volume-down", 
119     N_("Decrease volume") },
120   { { ' ',   0,   0 }, 0, CMD_SELECT, "select", 
121     N_("Select/deselect song in playlist") },
122   { { 't',   0,   0 }, 0, CMD_SELECT_ALL, "select_all",
123     N_("Select all listed items") },
124   { { DEL,  'd',  0 }, 0, CMD_DELETE, "delete",
125     N_("Delete song from playlist") },
126   { { 'Z',   0,   0 }, 0, CMD_SHUFFLE, "shuffle",
127     N_("Shuffle playlist") },
128   { { 'c',   0,   0 }, 0, CMD_CLEAR, "clear",
129     N_("Clear playlist") },
130   { { 'r',   0,   0 }, 0, CMD_REPEAT, "repeat",
131     N_("Toggle repeat mode") },
132   { { 'z',   0,   0 }, 0, CMD_RANDOM, "random",
133     N_("Toggle random mode") },
134   { { 'x',   0,   0 }, 0, CMD_CROSSFADE, "crossfade",
135     N_("Toggle crossfade mode") },
136   { { 21,    0,   0 }, 0, CMD_DB_UPDATE,  "db-update",
137     N_("Start a music database update") },
138   { { 'S',   0,   0 }, 0, CMD_SAVE_PLAYLIST, "save",
139     N_("Save playlist") },
140   { { 'a',   0,   0 }, 0, CMD_ADD, "add",
141     N_("Add url/file to playlist") },
143   { { '!',   0,   0 }, 0, CMD_GO_ROOT_DIRECTORY, "go-root-directory",
144     N_("Go to root directory") },
145   { { '"',   0,   0 }, 0, CMD_GO_PARENT_DIRECTORY, "go-parent-directory",
146     N_("Go to parent directory") },
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") },
170  
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
204   { { -1,  -1,  -1 }, 0, CMD_NONE, NULL, NULL }
205 };
207 command_definition_t *
208 get_command_definitions(void)
210         return cmds;
213 const char *
214 key2str(int key)
216         static char buf[32];
217         int i;
219         buf[0] = 0;
220         switch(key) {
221         case 0:
222                 return _("Undefined");
223         case ' ':
224                 return _("Space");
225         case 13:
226                 return _("Enter");
227         case BS:
228                 return _("Backspace");
229         case DEL:
230                 return _("Delete");
231         case UP:
232                 return _("Up");
233         case DWN:
234                 return _("Down");
235         case LEFT:
236                 return _("Left");
237         case RGHT:
238                 return _("Right");
239         case HOME:
240                 return _("Home");
241         case END:
242                 return _("End");
243         case PGDN:
244                 return _("PageDown");
245         case PGUP:
246                 return _("PageUp");
247         case TAB:
248                 return _("Tab");
249         case STAB:
250                 return _("Shift+Tab");
251         case ESC:
252                 return _("Esc");
253         case KEY_IC:
254                 return _("Insert");
255         default:
256                 for (i = 0; i <= 63; i++)
257                         if (key == KEY_F(i)) {
258                                 g_snprintf(buf, 32, "F%d", i );
259                                 return buf;
260                         }
261                 if (!(key & ~037))
262                         g_snprintf(buf, 32, "Ctrl-%c", 'A'+(key & 037)-1 );
263                 else if ((key & ~037) == 224)
264                         g_snprintf(buf, 32, "Alt-%c", 'A'+(key & 037)-1 );
265                 else if (key > 32 && key < 256)
266                         g_snprintf(buf, 32, "%c", key);
267                 else
268                         g_snprintf(buf, 32, "0x%03X", key);
269         }
271         return buf;
274 void
275 command_dump_keys(void)
277         int i;
279         i = 0;
280         while (cmds[i].description) {
281                 if (cmds[i].command != CMD_NONE)
282                         printf(" %20s : %s\n", get_key_names(cmds[i].command,1),cmds[i].name);
283                 i++;
284         }
287 static int
288 set_key_flags(command_definition_t *cp, command_t command, int flags)
290         int i;
292         i = 0;
293         while (cp[i].name) {
294                 if (cp[i].command == command) {
295                         cp[i].flags |= flags;
296                         return 0;
297                 }
298                 i++;
299         }
301         return 1;
304 const char *
305 get_key_names(command_t command, int all)
307   int i;
309   i=0;
310   while (cmds[i].description) {
311           if (cmds[i].command == command) {
312                   int j;
313                   static char keystr[80];
315                   g_strlcpy(keystr, key2str(cmds[i].keys[0]), sizeof(keystr));
316                   if (!all)
317                           return keystr;
318                   j=1;
319                   while (j < MAX_COMMAND_KEYS && cmds[i].keys[j] > 0) {
320                           g_strlcat(keystr, " ", sizeof(keystr));
321                           g_strlcat(keystr, key2str(cmds[i].keys[j]), sizeof(keystr));
322                           j++;
323                   }
324                   return keystr;
325           }
326           i++;
327   }
328   return NULL;
331 const char *
332 get_key_description(command_t command)
334         int i;
336         i=0;
337         while (cmds[i].description) {
338                 if (cmds[i].command == command)
339                         return _(cmds[i].description);
340                 i++;
341         }
343         return NULL;
346 const char *
347 get_key_command_name(command_t command)
349         int i;
351         i=0;
352         while (cmds[i].name) {
353                 if (cmds[i].command == command)
354                         return cmds[i].name;
355                 i++;
356         }
357         return NULL;
360 command_t
361 get_key_command_from_name(char *name)
363         int i;
365         i=0;
366         while (cmds[i].name) {
367                 if (strcmp(name, cmds[i].name) == 0)
368                         return cmds[i].command;
369                 i++;
370         }
372         return CMD_NONE;
375 command_t
376 find_key_command(int key, command_definition_t *c)
378         int i;
380         i=0;
381         while (key && c && c[i].name) {
382                 if (c[i].keys[0] == key ||
383                     c[i].keys[1] == key ||
384                     c[i].keys[2] == key)
385                         return c[i].command;
386                 i++;
387         }
389         return CMD_NONE;
392 command_t
393 get_key_command(int key)
395         return find_key_command(key, cmds);
398 int
399 my_wgetch(WINDOW *w)
401         int c;
403         c = wgetch(w);
405         /* handle resize event */
406         if (c == KEY_RESIZE)
407                 screen_resize();
409 #ifdef ENABLE_RAW_MODE
410         /* handle SIGSTOP (Ctrl-Z) */
411         if (c == 26 || c == 407)
412                 sigstop();
413         /* handle SIGINT (Ctrl-C) */
414         if (c == 3)
415                 kill(getpid(), SIGTERM);
416 #endif
418         return c;
421 command_t
422 get_keyboard_command(void)
424         int key;
426         key = my_wgetch(stdscr);
427         if (key == ERR)
428                 return CMD_NONE;
430 #ifdef HAVE_GETMOUSE
431         if (key == KEY_MOUSE)
432                 return CMD_MOUSE_EVENT;
433 #endif
435         return get_key_command(key);
438 int
439 assign_keys(command_t command, int keys[MAX_COMMAND_KEYS])
441         int i;
443         i=0;
444         while (cmds[i].name) {
445                 if (cmds[i].command == command) {
446                         memcpy(cmds[i].keys, keys, sizeof(int)*MAX_COMMAND_KEYS);
447                         cmds[i].flags |= COMMAND_KEY_MODIFIED;
448                         return 0;
449                 }
450                 i++;
451         }
452         return -1;
455 int
456 check_key_bindings(command_definition_t *cp, char *buf, size_t bufsize)
458         int i;
459         int retval = 0;
461         if (cp == NULL)
462                 cp = cmds;
464         i=0;
465         while (cp[i].name) {
466                 cp[i].flags &= ~COMMAND_KEY_CONFLICT;
467                 i++;
468         }
470         i=0;
471         while (cp[i].name) {
472                 int j;
473                 command_t cmd;
475                 for(j=0; j<MAX_COMMAND_KEYS; j++)
476                         if (cp[i].keys[j] &&
477                             (cmd = find_key_command(cp[i].keys[j],cp)) != cp[i].command) {
478                                 if (buf) {
479 #ifdef ENABLE_KEYDEF_SCREEN
480                                         g_snprintf(buf, bufsize,
481                                                    _("Key %s assigned to %s and %s (press %s for the key editor)"),
482                                                    key2str(cp[i].keys[j]),
483                                                    get_key_command_name(cp[i].command),
484                                                    get_key_command_name(cmd),
485                                                    get_key_names(CMD_SCREEN_KEYDEF,0));
486 #else
487                                         g_snprintf(buf, bufsize,
488                                                    _("Error: Key %s assigned to %s and %s !!!\n"),
489                                                    key2str(cp[i].keys[j]),
490                                                    get_key_command_name(cp[i].command),
491                                                    get_key_command_name(cmd));
492 #endif
493                                 } else
494                                         fprintf(stderr,
495                                                 _("Error: Key %s assigned to %s and %s !!!\n"),
496                                                 key2str(cp[i].keys[j]),
497                                                 get_key_command_name(cp[i].command),
498                                                 get_key_command_name(cmd));
499                                 cp[i].flags |= COMMAND_KEY_CONFLICT;
500                                 set_key_flags(cp, cmd, COMMAND_KEY_CONFLICT);
501                                 retval = -1;
502                         }
503                 i++;
504         }
505         return retval;
508 int
509 write_key_bindings(FILE *f, int flags)
511         int i,j;
513         if( flags & KEYDEF_WRITE_HEADER )
514                 fprintf(f, "## Key bindings for ncmpc (generated by ncmpc)\n\n");
516         i=0;
517         while (cmds[i].name && !ferror(f)) {
518                 if (cmds[i].flags & COMMAND_KEY_MODIFIED ||
519                     flags & KEYDEF_WRITE_ALL) {
520                         fprintf(f, "## %s\n", cmds[i].description);
521                         if (flags & KEYDEF_COMMENT_ALL)
522                                 fprintf(f, "#");
523                         fprintf(f, "key %s = ", cmds[i].name);
524                         for (j = 0; j < MAX_COMMAND_KEYS; j++) {
525                                 if (j && cmds[i].keys[j])
526                                         fprintf(f, ",  ");
527                                 if (!j || cmds[i].keys[j]) {
528                                         if (cmds[i].keys[j]<256 && (isalpha(cmds[i].keys[j]) ||
529                                                                     isdigit(cmds[i].keys[j])))
530                                                 fprintf(f, "\'%c\'", cmds[i].keys[j]);
531                                         else
532                                                 fprintf(f, "%d", cmds[i].keys[j]);
533                                 }
534                         }
535                         fprintf(f,"\n\n");
536                 }
537                 i++;
538         }
540         return ferror(f);