Code

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