1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
4 *
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.
9 *
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.
14 *
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.
18 */
20 #include "screen_keydef.h"
21 #include "screen_interface.h"
22 #include "screen_status.h"
23 #include "screen_find.h"
24 #include "i18n.h"
25 #include "conf.h"
26 #include "screen.h"
27 #include "screen_utils.h"
28 #include "Compiler.h"
30 #include <assert.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <glib.h>
35 static struct list_window *lw;
37 static command_definition_t *cmds = NULL;
39 /** the number of commands */
40 static unsigned command_n_commands = 0;
42 /**
43 * the position of the "apply" item. It's the same as command_n_commands,
44 * because array subscripts start at 0, while numbers of items start at 1.
45 */
46 gcc_pure
47 static inline unsigned
48 command_item_apply(void)
49 {
50 return command_n_commands;
51 }
53 /** the position of the "apply and save" item */
54 gcc_pure
55 static inline unsigned
56 command_item_save(void)
57 {
58 return command_item_apply() + 1;
59 }
61 /** the number of items in the "command" view */
62 gcc_pure
63 static inline unsigned
64 command_length(void)
65 {
66 return command_item_save() + 1;
67 }
70 /**
71 * The command being edited, represented by a array subscript to @cmds, or -1,
72 * if no command is being edited
73 */
74 static int subcmd = -1;
76 /** The number of keys assigned to the current command */
77 static unsigned subcmd_n_keys = 0;
79 /** The position of the up ("[..]") item */
80 gcc_const
81 static inline unsigned
82 subcmd_item_up(void)
83 {
84 return 0;
85 }
87 /** The position of the "add a key" item */
88 gcc_pure
89 static inline unsigned
90 subcmd_item_add(void)
91 {
92 return subcmd_n_keys + 1;
93 }
95 /** The number of items in the list_window, if there's a command being edited */
96 gcc_pure
97 static inline unsigned
98 subcmd_length(void)
99 {
100 return subcmd_item_add() + 1;
101 }
103 /** Check whether a given item is a key */
104 gcc_pure
105 static inline bool
106 subcmd_item_is_key(unsigned i)
107 {
108 return (i > subcmd_item_up() && i < subcmd_item_add());
109 }
111 /**
112 * Convert an item id (as in lw->selected) into a "key id", which is an array
113 * subscript to cmds[subcmd].keys.
114 */
115 gcc_const
116 static inline unsigned
117 subcmd_item_to_key_id(unsigned i)
118 {
119 return i - 1;
120 }
123 static int
124 keybindings_changed(void)
125 {
126 command_definition_t *orginal_cmds = get_command_definitions();
127 size_t size = command_n_commands * sizeof(command_definition_t);
129 return memcmp(orginal_cmds, cmds, size);
130 }
132 static void
133 apply_keys(void)
134 {
135 if (keybindings_changed()) {
136 command_definition_t *orginal_cmds = get_command_definitions();
137 size_t size = command_n_commands * sizeof(command_definition_t);
139 memcpy(orginal_cmds, cmds, size);
140 screen_status_printf(_("You have new key bindings"));
141 } else
142 screen_status_printf(_("Keybindings unchanged."));
143 }
145 static int
146 save_keys(void)
147 {
148 if (check_user_conf_dir()) {
149 screen_status_printf(_("Error: Unable to create directory ~/.ncmpc - %s"),
150 strerror(errno));
151 screen_bell();
152 return -1;
153 }
155 char *filename = build_user_key_binding_filename();
156 FILE *f = fopen(filename, "w");
157 if (f == NULL) {
158 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
159 screen_bell();
160 g_free(filename);
161 return -1;
162 }
164 if (write_key_bindings(f, KEYDEF_WRITE_HEADER))
165 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
166 else
167 screen_status_printf(_("Wrote %s"), filename);
169 g_free(filename);
170 return fclose(f);
171 }
173 /* TODO: rename to check_n_keys / subcmd_count_keys? */
174 static void
175 check_subcmd_length(void)
176 {
177 unsigned i;
179 /* this loops counts the continous valid keys at the start of the the keys
180 array, so make sure you don't have gaps */
181 for (i = 0; i < MAX_COMMAND_KEYS; i++)
182 if (cmds[subcmd].keys[i] == 0)
183 break;
184 subcmd_n_keys = i;
186 list_window_set_length(lw, subcmd_length());
187 }
189 static void
190 keydef_paint(void);
192 static void
193 keydef_repaint(void)
194 {
195 keydef_paint();
196 wrefresh(lw->w);
197 }
199 /** lw->start the last time switch_to_subcmd_mode() was called */
200 static unsigned saved_start = 0;
202 static void
203 switch_to_subcmd_mode(int cmd)
204 {
205 assert(subcmd == -1);
207 saved_start = lw->start;
209 subcmd = cmd;
210 list_window_reset(lw);
211 check_subcmd_length();
213 keydef_repaint();
214 }
216 static void
217 switch_to_command_mode(void)
218 {
219 assert(subcmd != -1);
221 list_window_set_length(lw, command_length());
222 list_window_set_cursor(lw, subcmd);
223 subcmd = -1;
225 lw->start = saved_start;
227 keydef_repaint();
228 }
230 /**
231 * Delete a key from a given command's definition
232 * @param cmd_index the command
233 * @param key_index the key (see below)
234 */
235 static void
236 delete_key(int cmd_index, int key_index)
237 {
238 /* shift the keys to close the gap that appeared */
239 int i = key_index+1;
240 while (i < MAX_COMMAND_KEYS && cmds[cmd_index].keys[i])
241 cmds[cmd_index].keys[key_index++] = cmds[cmd_index].keys[i++];
243 /* As key_index now holds the index of the last key slot that contained
244 a key, we use it to empty this slot, because this key has been copied
245 to the previous slot in the loop above */
246 cmds[cmd_index].keys[key_index] = 0;
248 cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
249 check_subcmd_length();
251 screen_status_printf(_("Deleted"));
253 /* repaint */
254 keydef_repaint();
256 /* update key conflict flags */
257 check_key_bindings(cmds, NULL, 0);
258 }
260 /* assigns a new key to a key slot */
261 static void
262 overwrite_key(int cmd_index, int key_index)
263 {
264 assert(key_index < MAX_COMMAND_KEYS);
266 char *buf = g_strdup_printf(_("Enter new key for %s: "),
267 cmds[cmd_index].name);
268 const int key = screen_getch(buf);
269 g_free(buf);
271 if (key == ERR) {
272 screen_status_printf(_("Aborted"));
273 return;
274 }
276 if (key == '\0') {
277 screen_status_printf(_("Ctrl-Space can't be used"));
278 return;
279 }
281 const command_t cmd = find_key_command(key, cmds);
282 if (cmd != CMD_NONE) {
283 screen_status_printf(_("Error: key %s is already used for %s"),
284 key2str(key), get_key_command_name(cmd));
285 screen_bell();
286 return;
287 }
289 cmds[cmd_index].keys[key_index] = key;
290 cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
292 screen_status_printf(_("Assigned %s to %s"),
293 key2str(key),cmds[cmd_index].name);
294 check_subcmd_length();
296 /* repaint */
297 keydef_repaint();
299 /* update key conflict flags */
300 check_key_bindings(cmds, NULL, 0);
301 }
303 /* assign a new key to a new slot */
304 static void
305 add_key(int cmd_index)
306 {
307 if (subcmd_n_keys < MAX_COMMAND_KEYS)
308 overwrite_key(cmd_index, subcmd_n_keys);
309 }
311 static const char *
312 list_callback(unsigned idx, gcc_unused void *data)
313 {
314 static char buf[256];
316 if (subcmd == -1) {
317 if (idx == command_item_apply())
318 return _("===> Apply key bindings ");
319 if (idx == command_item_save())
320 return _("===> Apply & Save key bindings ");
322 assert(idx < (unsigned) command_n_commands);
324 /*
325 * Format the lines in two aligned columnes for the key name and
326 * the description, like this:
327 *
328 * this-command - do this
329 * that-one - do that
330 */
331 size_t len = strlen(cmds[idx].name);
332 strncpy(buf, cmds[idx].name, sizeof(buf));
334 if (len < get_cmds_max_name_width(cmds))
335 memset(buf + len, ' ', get_cmds_max_name_width(cmds) - len);
337 g_snprintf(buf + get_cmds_max_name_width(cmds),
338 sizeof(buf) - get_cmds_max_name_width(cmds),
339 " - %s", _(cmds[idx].description));
341 return buf;
342 } else {
343 if (idx == subcmd_item_up())
344 return "[..]";
346 if (idx == subcmd_item_add()) {
347 g_snprintf(buf, sizeof(buf), "%d. %s",
348 idx, _("Add new key"));
349 return buf;
350 }
352 assert(subcmd_item_is_key(idx));
354 g_snprintf(buf, sizeof(buf),
355 "%d. %-20s (%d) ", idx,
356 key2str(cmds[subcmd].keys[subcmd_item_to_key_id(idx)]),
357 cmds[subcmd].keys[subcmd_item_to_key_id(idx)]);
358 return buf;
359 }
360 }
362 static void
363 keydef_init(WINDOW *w, int cols, int rows)
364 {
365 lw = list_window_init(w, cols, rows);
366 }
368 static void
369 keydef_resize(int cols, int rows)
370 {
371 list_window_resize(lw, cols, rows);
372 }
374 static void
375 keydef_exit(void)
376 {
377 list_window_free(lw);
378 if (cmds)
379 g_free(cmds);
380 cmds = NULL;
381 lw = NULL;
382 }
384 static void
385 keydef_open(gcc_unused struct mpdclient *c)
386 {
387 if (cmds == NULL) {
388 command_definition_t *current_cmds = get_command_definitions();
389 command_n_commands = 0;
390 while (current_cmds[command_n_commands].name)
391 command_n_commands++;
393 /* +1 for the terminator element */
394 size_t cmds_size = (command_n_commands + 1)
395 * sizeof(command_definition_t);
396 cmds = g_malloc0(cmds_size);
397 memcpy(cmds, current_cmds, cmds_size);
398 }
400 subcmd = -1;
401 list_window_set_length(lw, command_length());
402 }
404 static void
405 keydef_close(void)
406 {
407 if (cmds && !keybindings_changed()) {
408 g_free(cmds);
409 cmds = NULL;
410 } else
411 screen_status_printf(_("Note: Did you forget to \'Apply\' your changes?"));
412 }
414 static const char *
415 keydef_title(char *str, size_t size)
416 {
417 if (subcmd == -1)
418 return _("Edit key bindings");
420 g_snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name);
421 return str;
422 }
424 static void
425 keydef_paint(void)
426 {
427 list_window_paint(lw, list_callback, NULL);
428 }
430 static bool
431 keydef_cmd(gcc_unused struct mpdclient *c, command_t cmd)
432 {
433 if (cmd == CMD_LIST_RANGE_SELECT)
434 return false;
436 if (list_window_cmd(lw, cmd)) {
437 keydef_repaint();
438 return true;
439 }
441 switch(cmd) {
442 case CMD_PLAY:
443 if (subcmd == -1) {
444 if (lw->selected == command_item_apply()) {
445 apply_keys();
446 } else if (lw->selected == command_item_save()) {
447 apply_keys();
448 save_keys();
449 } else {
450 switch_to_subcmd_mode(lw->selected);
451 }
452 } else {
453 if (lw->selected == subcmd_item_up()) {
454 switch_to_command_mode();
455 } else if (lw->selected == subcmd_item_add()) {
456 add_key(subcmd);
457 } else {
458 /* just to be sure ;-) */
459 assert(subcmd_item_is_key(lw->selected));
460 overwrite_key(subcmd, subcmd_item_to_key_id(lw->selected));
461 }
462 }
463 return true;
464 case CMD_GO_PARENT_DIRECTORY:
465 case CMD_GO_ROOT_DIRECTORY:
466 if (subcmd != -1)
467 switch_to_command_mode();
468 return true;
469 case CMD_DELETE:
470 if (subcmd != -1 && subcmd_item_is_key(lw->selected))
471 delete_key(subcmd, subcmd_item_to_key_id(lw->selected));
473 return true;
474 case CMD_ADD:
475 if (subcmd != -1)
476 add_key(subcmd);
477 return true;
478 case CMD_SAVE_PLAYLIST:
479 apply_keys();
480 save_keys();
481 return true;
482 case CMD_LIST_FIND:
483 case CMD_LIST_RFIND:
484 case CMD_LIST_FIND_NEXT:
485 case CMD_LIST_RFIND_NEXT:
486 screen_find(lw, cmd, list_callback, NULL);
487 keydef_repaint();
488 return true;
490 default:
491 return false;
492 }
494 /* unreachable */
495 assert(0);
496 return false;
497 }
499 const struct screen_functions screen_keydef = {
500 .init = keydef_init,
501 .exit = keydef_exit,
502 .open = keydef_open,
503 .close = keydef_close,
504 .resize = keydef_resize,
505 .paint = keydef_paint,
506 .cmd = keydef_cmd,
507 .get_title = keydef_title,
508 };