1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "mpdclient.h"
35 #define KEY_CTL(x) ((x) & 0x1f) /* KEY_CTL(A) == ^A == \1 */
37 #define BS KEY_BACKSPACE
42 #define RGHT KEY_RIGHT
45 #define PGDN KEY_NPAGE
46 #define PGUP KEY_PPAGE
60 #define C(x) KEY_CTL(x)
62 static command_definition_t cmds
[] = {
63 #ifdef ENABLE_KEYDEF_SCREEN
64 { {'K', 0, 0 }, 0, CMD_SCREEN_KEYDEF
, "screen-keyedit",
65 N_("Key configuration screen") },
67 { { 'q', 'Q', C('C') }, 0, CMD_QUIT
, "quit",
71 { { UP
, 'k', 0 }, 0, CMD_LIST_PREVIOUS
, "up",
72 N_("Move cursor up") },
73 { { DWN
, 'j', 0 }, 0, CMD_LIST_NEXT
, "down",
74 N_("Move cursor down") },
75 { { 'H', 0, 0 }, 0, CMD_LIST_TOP
, "top",
76 N_("Move cursor to the top of screen") },
77 { { 'M', 0, 0 }, 0, CMD_LIST_MIDDLE
, "middle",
78 N_("Move cursor to the middle of screen") },
79 { { 'L', 0, 0 }, 0, CMD_LIST_BOTTOM
, "bottom",
80 N_("Move cursor to the bottom of screen") },
81 { { HOME
, C('A'), 0 }, 0, CMD_LIST_FIRST
, "home",
82 N_("Move cursor to the top of the list") },
83 { { END
, C('E'), 0 }, 0, CMD_LIST_LAST
, "end",
84 N_("Move cursor to the bottom of the list") },
85 { { PGUP
, 0, 0 }, 0, CMD_LIST_PREVIOUS_PAGE
, "pgup",
87 { { PGDN
, 0, 0 }, 0, CMD_LIST_NEXT_PAGE
, "pgdn",
89 { { 'v', 0, 0 }, 0, CMD_LIST_RANGE_SELECT
, "range-select",
90 N_("Range selection") },
91 { { C('N'), 0, 0 }, 0, CMD_LIST_SCROLL_DOWN_LINE
, "scroll-down-line",
92 N_("Scroll up one line") },
93 { { C('B'), 0, 0 }, 0, CMD_LIST_SCROLL_UP_LINE
, "scroll-up-line",
94 N_("Scroll down one line") },
95 { { 'N', 0, 0 }, 0, CMD_LIST_SCROLL_DOWN_HALF
, "scroll-down-half",
96 N_("Scroll up half a screen") },
97 { { 'B', 0, 0 }, 0, CMD_LIST_SCROLL_UP_HALF
, "scroll-up-half",
98 N_("Scroll down half a screen") },
99 { { 'l', 0, 0 }, 0, CMD_SELECT_PLAYING
, "select-playing",
100 N_("Select currently playing song") },
104 { { '1', F1
, 'h' }, 0, CMD_SCREEN_HELP
, "screen-help",
106 { { '2', F2
, 0 }, 0, CMD_SCREEN_PLAY
, "screen-playlist",
107 N_("Queue screen") },
108 { { '3', F3
, 0 }, 0, CMD_SCREEN_FILE
, "screen-browse",
109 N_("Browse screen") },
112 /* player commands */
113 { { RET
, 0, 0 }, 0, CMD_PLAY
, "play",
114 N_("Play/Enter directory") },
115 { { 'P', 0, 0 }, 0, CMD_PAUSE
,"pause",
117 { { 's', BS
, 0 }, 0, CMD_STOP
, "stop",
119 { { 'o', 0, 0 }, 0, CMD_CROP
, "crop",
121 { { '>', 0, 0 }, 0, CMD_TRACK_NEXT
, "next",
123 { { '<', 0, 0 }, 0, CMD_TRACK_PREVIOUS
, "prev",
124 N_("Previous track") },
125 { { 'f', 0, 0 }, 0, CMD_SEEK_FORWARD
, "seek-forward",
126 N_("Seek forward") },
127 { { 'b', 0, 0 }, 0, CMD_SEEK_BACKWARD
, "seek-backward",
128 N_("Seek backward") },
129 { { '+', RGHT
, 0 }, 0, CMD_VOLUME_UP
, "volume-up",
130 N_("Increase volume") },
131 { { '-', LEFT
, 0 }, 0, CMD_VOLUME_DOWN
, "volume-down",
132 N_("Decrease volume") },
133 { { ' ', 0, 0 }, 0, CMD_SELECT
, "select",
134 N_("Select/deselect song in queue") },
135 { { 't', 0, 0 }, 0, CMD_SELECT_ALL
, "select_all",
136 N_("Select all listed items") },
137 { { DEL
, 'd', 0 }, 0, CMD_DELETE
, "delete",
138 N_("Delete song from queue") },
139 { { 'Z', 0, 0 }, 0, CMD_SHUFFLE
, "shuffle",
140 N_("Shuffle queue") },
141 { { 'c', 0, 0 }, 0, CMD_CLEAR
, "clear",
143 { { 'r', 0, 0 }, 0, CMD_REPEAT
, "repeat",
144 N_("Toggle repeat mode") },
145 { { 'z', 0, 0 }, 0, CMD_RANDOM
, "random",
146 N_("Toggle random mode") },
147 { { 'y', 0, 0 }, 0, CMD_SINGLE
, "single",
148 N_("Toggle single mode") },
149 { { 'C', 0, 0 }, 0, CMD_CONSUME
, "consume",
150 N_("Toggle consume mode") },
151 { { 'x', 0, 0 }, 0, CMD_CROSSFADE
, "crossfade",
152 N_("Toggle crossfade mode") },
153 { { C('U'), 0, 0 }, 0, CMD_DB_UPDATE
, "db-update",
154 N_("Start a music database update") },
155 { { 'S', 0, 0 }, 0, CMD_SAVE_PLAYLIST
, "save",
157 { { 'a', 0, 0 }, 0, CMD_ADD
, "add",
158 N_("Add url/file to queue") },
160 { { '!', 0, 0 }, 0, CMD_GO_ROOT_DIRECTORY
, "go-root-directory",
161 N_("Go to root directory") },
162 { { '"', 0, 0 }, 0, CMD_GO_PARENT_DIRECTORY
, "go-parent-directory",
163 N_("Go to parent directory") },
165 { { 'G', 0, 0 }, 0, CMD_LOCATE
, "locate",
166 N_("Locate song in browser") },
169 { { C('K'), 0, 0 }, 0, CMD_LIST_MOVE_UP
, "move-up",
170 N_("Move item up") },
171 { { C('J'), 0, 0 }, 0, CMD_LIST_MOVE_DOWN
, "move-down",
172 N_("Move item down") },
173 { { C('L'), 0, 0 }, 0, CMD_SCREEN_UPDATE
, "update",
174 N_("Refresh screen") },
178 { { 'w', 0, 0 }, 0, CMD_TOGGLE_FIND_WRAP
, "wrap-mode",
179 /* translators: toggle between wrapping and non-wrapping
181 N_("Toggle find mode") },
182 { { 'U', 0, 0 }, 0, CMD_TOGGLE_AUTOCENTER
, "autocenter-mode",
183 /* translators: the auto center mode always centers the song
184 currently being played */
185 N_("Toggle auto center mode") },
189 { { TAB
, 0, 0 }, 0, CMD_SCREEN_NEXT
, "screen-next",
191 { { STAB
, 0, 0 }, 0, CMD_SCREEN_PREVIOUS
, "screen-prev",
192 N_("Previous screen") },
193 { { '`', 0, 0 }, 0, CMD_SCREEN_SWAP
, "screen-swap",
194 N_("Swap to most recent screen") },
198 { { '/', 0, 0 }, 0, CMD_LIST_FIND
, "find",
199 N_("Forward find") },
200 { { 'n', 0, 0 }, 0, CMD_LIST_FIND_NEXT
, "find-next",
201 N_("Forward find next") },
202 { { '?', 0, 0 }, 0, CMD_LIST_RFIND
, "rfind",
203 N_("Backward find") },
204 { { 'p', 0, 0 }, 0, CMD_LIST_RFIND_NEXT
, "rfind-next",
205 N_("Backward find previous") },
206 { { '.', 0, 0 }, 0, CMD_LIST_JUMP
, "jump",
207 /* translators: this queries the user for a string
208 * and jumps directly (while the user is typing)
209 * to the entry which begins with this string */
214 #ifdef ENABLE_ARTIST_SCREEN
215 { {'4', F4
, 0 }, 0, CMD_SCREEN_ARTIST
, "screen-artist",
216 N_("Artist screen") },
218 #ifdef ENABLE_SEARCH_SCREEN
219 { {'5', F5
, 0 }, 0, CMD_SCREEN_SEARCH
, "screen-search",
220 N_("Search screen") },
221 { {'m', 0, 0 }, 0, CMD_SEARCH_MODE
, "search-mode",
222 N_("Change search mode") },
224 #ifdef ENABLE_SONG_SCREEN
225 { { 'i', 0, 0 }, 0, CMD_SCREEN_SONG
, "view",
226 N_("View the selected and the currently playing song") },
228 #ifdef ENABLE_LYRICS_SCREEN
229 { {'7', F7
, 0 }, 0, CMD_SCREEN_LYRICS
, "screen-lyrics",
230 N_("Lyrics screen") },
231 { {ESC
, 0, 0 }, 0, CMD_INTERRUPT
, "lyrics-interrupt",
232 /* translators: interrupt the current background action,
233 e.g. stop loading lyrics from the internet */
234 N_("Interrupt action") },
235 { {'u', 0, 0 }, 0, CMD_LYRICS_UPDATE
, "lyrics-update",
236 N_("Update Lyrics") },
237 /* this command may move out of #ifdef ENABLE_LYRICS_SCREEN
239 { {'e', 0, 0 }, 0, CMD_EDIT
, "edit",
240 N_("Edit the current item") },
243 #ifdef ENABLE_OUTPUTS_SCREEN
244 { {'8', F8
, 0 }, 0, CMD_SCREEN_OUTPUTS
, "screen-outputs",
245 N_("Outputs screen") },
248 #ifdef ENABLE_CHAT_SCREEN
249 { {'9', F9
, 0}, 0, CMD_SCREEN_CHAT
, "screen-chat",
253 { { -1, -1, -1 }, 0, CMD_NONE
, NULL
, NULL
}
256 #ifdef ENABLE_KEYDEF_SCREEN
257 command_definition_t
*
258 get_command_definitions(void)
264 get_cmds_max_name_width(command_definition_t
*c
)
266 static size_t max
= 0;
272 command_definition_t
*p
;
274 for (p
= c
; p
->name
!= NULL
; p
++) {
276 * width and length are considered the same here, as command
277 * names are not translated.
279 len
= (size_t) strlen(p
->name
);
297 return _("Undefined");
303 return _("Backspace");
319 return _("PageDown");
325 return _("Shift+Tab");
331 for (i
= 0; i
<= 63; i
++)
332 if (key
== KEY_F(i
)) {
333 g_snprintf(buf
, 32, _("F%d"), i
);
337 g_snprintf(buf
, 32, _("Ctrl-%c"), 'A'+(key
& 037)-1 );
338 else if ((key
& ~037) == 224)
339 g_snprintf(buf
, 32, _("Alt-%c"), 'A'+(key
& 037)-1 );
340 else if (key
> 32 && key
< 256)
341 g_snprintf(buf
, 32, "%c", key
);
343 g_snprintf(buf
, 32, "0x%03X", key
);
350 command_dump_keys(void)
352 for (int i
= 0; cmds
[i
].description
; i
++)
353 if (cmds
[i
].command
!= CMD_NONE
)
354 printf(" %20s : %s\n",
355 get_key_names(cmds
[i
].command
, true),
362 set_key_flags(command_definition_t
*cp
, command_t command
, int flags
)
364 for (int i
= 0; cp
[i
].name
; i
++) {
365 if (cp
[i
].command
== command
) {
366 cp
[i
].flags
|= flags
;
375 get_key_names(command_t command
, bool all
)
377 for (int i
= 0; cmds
[i
].description
; i
++) {
378 if (cmds
[i
].command
== command
) {
379 static char keystr
[80];
381 g_strlcpy(keystr
, key2str(cmds
[i
].keys
[0]), sizeof(keystr
));
385 for (int j
= 1; j
< MAX_COMMAND_KEYS
&&
386 cmds
[i
].keys
[j
] > 0; j
++) {
387 g_strlcat(keystr
, " ", sizeof(keystr
));
388 g_strlcat(keystr
, key2str(cmds
[i
].keys
[j
]), sizeof(keystr
));
397 get_key_description(command_t command
)
399 for (int i
= 0; cmds
[i
].description
; i
++)
400 if (cmds
[i
].command
== command
)
401 return _(cmds
[i
].description
);
407 get_key_command_name(command_t command
)
409 for (int i
= 0; cmds
[i
].name
; i
++)
410 if (cmds
[i
].command
== command
)
417 get_key_command_from_name(char *name
)
419 for (int i
= 0; cmds
[i
].name
; i
++)
420 if (strcmp(name
, cmds
[i
].name
) == 0)
421 return cmds
[i
].command
;
427 find_key_command(int key
, command_definition_t
*c
)
432 for (int i
= 0; c
[i
].name
; i
++) {
433 for (int j
= 0; j
< MAX_COMMAND_KEYS
; j
++)
434 if (c
[i
].keys
[j
] == key
)
442 get_key_command(int key
)
444 return find_key_command(key
, cmds
);
448 get_keyboard_command(void)
452 key
= wgetch(stdscr
);
453 if (key
== ERR
|| key
== '\0')
457 if (key
== KEY_MOUSE
)
458 return CMD_MOUSE_EVENT
;
461 return get_key_command(key
);
465 assign_keys(command_t command
, int keys
[MAX_COMMAND_KEYS
])
467 for (int i
= 0; cmds
[i
].name
; i
++) {
468 if (cmds
[i
].command
== command
) {
469 memcpy(cmds
[i
].keys
, keys
, sizeof(int)*MAX_COMMAND_KEYS
);
471 cmds
[i
].flags
|= COMMAND_KEY_MODIFIED
;
483 check_key_bindings(command_definition_t
*cp
, char *buf
, size_t bufsize
)
491 for (i
= 0; cp
[i
].name
; i
++)
492 cp
[i
].flags
&= ~COMMAND_KEY_CONFLICT
;
494 for (i
= 0; cp
[i
].name
; i
++) {
498 for(j
=0; j
<MAX_COMMAND_KEYS
; j
++) {
500 (cmd
= find_key_command(cp
[i
].keys
[j
],cp
)) != cp
[i
].command
) {
502 g_snprintf(buf
, bufsize
,
503 _("Key %s assigned to %s and %s"),
504 key2str(cp
[i
].keys
[j
]),
505 get_key_command_name(cp
[i
].command
),
506 get_key_command_name(cmd
));
509 _("Key %s assigned to %s and %s"),
510 key2str(cp
[i
].keys
[j
]),
511 get_key_command_name(cp
[i
].command
),
512 get_key_command_name(cmd
));
515 cp
[i
].flags
|= COMMAND_KEY_CONFLICT
;
516 set_key_flags(cp
, cmd
, COMMAND_KEY_CONFLICT
);
526 write_key_bindings(FILE *f
, int flags
)
528 if (flags
& KEYDEF_WRITE_HEADER
)
529 fprintf(f
, "## Key bindings for ncmpc (generated by ncmpc)\n\n");
531 for (int i
= 0; cmds
[i
].name
&& !ferror(f
); i
++) {
532 if (cmds
[i
].flags
& COMMAND_KEY_MODIFIED
||
533 flags
& KEYDEF_WRITE_ALL
) {
534 fprintf(f
, "## %s\n", cmds
[i
].description
);
535 if (flags
& KEYDEF_COMMENT_ALL
)
537 fprintf(f
, "key %s = ", cmds
[i
].name
);
538 for (int j
= 0; j
< MAX_COMMAND_KEYS
; j
++) {
539 if (j
&& cmds
[i
].keys
[j
])
541 if (!j
|| cmds
[i
].keys
[j
]) {
542 if (cmds
[i
].keys
[j
]<256 && (isalpha(cmds
[i
].keys
[j
]) ||
543 isdigit(cmds
[i
].keys
[j
])))
544 fprintf(f
, "\'%c\'", cmds
[i
].keys
[j
]);
546 fprintf(f
, "%d", cmds
[i
].keys
[j
]);
556 #endif /* NCMPC_MINI */