Code

*: use Compiler.h macros instead of glib.h
[ncmpc.git] / src / screen_keydef.c
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;
103 /** Check whether a given item is a key */
104 gcc_pure
105 static inline bool
106 subcmd_item_is_key(unsigned i)
108         return (i > subcmd_item_up() && i < subcmd_item_add());
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)
119         return i - 1;
123 static int
124 keybindings_changed(void)
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);
132 static void
133 apply_keys(void)
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."));
145 static int
146 save_keys(void)
148         FILE *f;
149         char *filename;
151         if (check_user_conf_dir()) {
152                 screen_status_printf(_("Error: Unable to create directory ~/.ncmpc - %s"),
153                                      strerror(errno));
154                 screen_bell();
155                 return -1;
156         }
158         filename = build_user_key_binding_filename();
160         if ((f = fopen(filename,"w")) == NULL) {
161                 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
162                 screen_bell();
163                 g_free(filename);
164                 return -1;
165         }
167         if (write_key_bindings(f, KEYDEF_WRITE_HEADER))
168                 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
169         else
170                 screen_status_printf(_("Wrote %s"), filename);
172         g_free(filename);
173         return fclose(f);
176 /* TODO: rename to check_n_keys / subcmd_count_keys? */
177 static void
178 check_subcmd_length(void)
180         unsigned i;
182         /* this loops counts the continous valid keys at the start of the the keys
183            array, so make sure you don't have gaps */
184         for (i = 0; i < MAX_COMMAND_KEYS; i++)
185                 if (cmds[subcmd].keys[i] == 0)
186                         break;
187         subcmd_n_keys = i;
189         list_window_set_length(lw, subcmd_length());
192 static void
193 keydef_paint(void);
195 static void
196 keydef_repaint(void)
198         keydef_paint();
199         wrefresh(lw->w);
202 /** lw->start the last time switch_to_subcmd_mode() was called */
203 static unsigned saved_start = 0;
205 static void
206 switch_to_subcmd_mode(int cmd)
208         assert(subcmd == -1);
210         saved_start = lw->start;
212         subcmd = cmd;
213         list_window_reset(lw);
214         check_subcmd_length();
216         keydef_repaint();
219 static void
220 switch_to_command_mode(void)
222         assert(subcmd != -1);
224         list_window_set_length(lw, command_length());
225         list_window_set_cursor(lw, subcmd);
226         subcmd = -1;
228         lw->start = saved_start;
230         keydef_repaint();
233 /**
234  * Delete a key from a given command's definition
235  * @param cmd_index the command
236  * @param key_index the key (see below)
237  */
238 static void
239 delete_key(int cmd_index, int key_index)
241         /* shift the keys to close the gap that appeared */
242         int i = key_index+1;
243         while (i < MAX_COMMAND_KEYS && cmds[cmd_index].keys[i])
244                 cmds[cmd_index].keys[key_index++] = cmds[cmd_index].keys[i++];
246         /* As key_index now holds the index of the last key slot that contained
247            a key, we use it to empty this slot, because this key has been copied
248            to the previous slot in the loop above */
249         cmds[cmd_index].keys[key_index] = 0;
251         cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
252         check_subcmd_length();
254         screen_status_printf(_("Deleted"));
256         /* repaint */
257         keydef_repaint();
259         /* update key conflict flags */
260         check_key_bindings(cmds, NULL, 0);
263 /* assigns a new key to a key slot */
264 static void
265 overwrite_key(int cmd_index, int key_index)
267         int key;
268         char *buf;
269         command_t cmd;
271         assert(key_index < MAX_COMMAND_KEYS);
273         buf = g_strdup_printf(_("Enter new key for %s: "), cmds[cmd_index].name);
274         key = screen_getch(buf);
275         g_free(buf);
277         if (key == ERR) {
278                 screen_status_printf(_("Aborted"));
279                 return;
280         }
282         if (key == '\0') {
283                 screen_status_printf(_("Ctrl-Space can't be used"));
284                 return;
285         }
287         cmd = find_key_command(key, cmds);
288         if (cmd != CMD_NONE) {
289                 screen_status_printf(_("Error: key %s is already used for %s"),
290                                      key2str(key), get_key_command_name(cmd));
291                 screen_bell();
292                 return;
293         }
295         cmds[cmd_index].keys[key_index] = key;
296         cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
298         screen_status_printf(_("Assigned %s to %s"),
299                              key2str(key),cmds[cmd_index].name);
300         check_subcmd_length();
302         /* repaint */
303         keydef_repaint();
305         /* update key conflict flags */
306         check_key_bindings(cmds, NULL, 0);
309 /* assign a new key to a new slot */
310 static void
311 add_key(int cmd_index)
313         if (subcmd_n_keys < MAX_COMMAND_KEYS)
314                 overwrite_key(cmd_index, subcmd_n_keys);
317 static const char *
318 list_callback(unsigned idx, gcc_unused void *data)
320         static char buf[256];
322         if (subcmd == -1) {
323                 if (idx == command_item_apply())
324                         return _("===> Apply key bindings ");
325                 if (idx == command_item_save())
326                         return _("===> Apply & Save key bindings  ");
328                 assert(idx < (unsigned) command_n_commands);
330                 /*
331                  * Format the lines in two aligned columnes for the key name and
332                  * the description, like this:
333                  *
334                  *      this-command - do this
335                  *      that-one     - do that
336                  */
337                 size_t len = strlen(cmds[idx].name);
338                 strncpy(buf, cmds[idx].name, sizeof(buf));
340                 if (len < get_cmds_max_name_width(cmds))
341                         memset(buf + len, ' ', get_cmds_max_name_width(cmds) - len);
343                 g_snprintf(buf + get_cmds_max_name_width(cmds),
344                            sizeof(buf) - get_cmds_max_name_width(cmds),
345                            " - %s", _(cmds[idx].description));
347                 return buf;
348         } else {
349                 if (idx == subcmd_item_up())
350                         return "[..]";
352                 if (idx == subcmd_item_add()) {
353                         g_snprintf(buf, sizeof(buf), "%d. %s",
354                                    idx, _("Add new key"));
355                         return buf;
356                 }
358                 assert(subcmd_item_is_key(idx));
360                 g_snprintf(buf, sizeof(buf),
361                            "%d. %-20s   (%d) ", idx,
362                            key2str(cmds[subcmd].keys[subcmd_item_to_key_id(idx)]),
363                            cmds[subcmd].keys[subcmd_item_to_key_id(idx)]);
364                 return buf;
365         }
368 static void
369 keydef_init(WINDOW *w, int cols, int rows)
371         lw = list_window_init(w, cols, rows);
374 static void
375 keydef_resize(int cols, int rows)
377         list_window_resize(lw, cols, rows);
380 static void
381 keydef_exit(void)
383         list_window_free(lw);
384         if (cmds)
385                 g_free(cmds);
386         cmds = NULL;
387         lw = NULL;
390 static void
391 keydef_open(gcc_unused struct mpdclient *c)
393         if (cmds == NULL) {
394                 command_definition_t *current_cmds = get_command_definitions();
395                 size_t cmds_size;
397                 command_n_commands = 0;
398                 while (current_cmds[command_n_commands].name)
399                         command_n_commands++;
401                 /* +1 for the terminator element */
402                 cmds_size = (command_n_commands + 1) * sizeof(command_definition_t);
403                 cmds = g_malloc0(cmds_size);
404                 memcpy(cmds, current_cmds, cmds_size);
405         }
407         subcmd = -1;
408         list_window_set_length(lw, command_length());
411 static void
412 keydef_close(void)
414         if (cmds && !keybindings_changed()) {
415                 g_free(cmds);
416                 cmds = NULL;
417         } else
418                 screen_status_printf(_("Note: Did you forget to \'Apply\' your changes?"));
421 static const char *
422 keydef_title(char *str, size_t size)
424         if (subcmd == -1)
425                 return _("Edit key bindings");
427         g_snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name);
428         return str;
431 static void
432 keydef_paint(void)
434         list_window_paint(lw, list_callback, NULL);
437 static bool
438 keydef_cmd(gcc_unused struct mpdclient *c, command_t cmd)
440         if (cmd == CMD_LIST_RANGE_SELECT)
441                 return false;
443         if (list_window_cmd(lw, cmd)) {
444                 keydef_repaint();
445                 return true;
446         }
448         switch(cmd) {
449         case CMD_PLAY:
450                 if (subcmd == -1) {
451                         if (lw->selected == command_item_apply()) {
452                                 apply_keys();
453                         } else if (lw->selected == command_item_save()) {
454                                 apply_keys();
455                                 save_keys();
456                         } else {
457                                 switch_to_subcmd_mode(lw->selected);
458                         }
459                 } else {
460                         if (lw->selected == subcmd_item_up()) {
461                                 switch_to_command_mode();
462                         } else if (lw->selected == subcmd_item_add()) {
463                                 add_key(subcmd);
464                         } else {
465                                 /* just to be sure ;-) */
466                                 assert(subcmd_item_is_key(lw->selected));
467                                 overwrite_key(subcmd, subcmd_item_to_key_id(lw->selected));
468                         }
469                 }
470                 return true;
471         case CMD_GO_PARENT_DIRECTORY:
472         case CMD_GO_ROOT_DIRECTORY:
473                 if (subcmd != -1)
474                         switch_to_command_mode();
475                 return true;
476         case CMD_DELETE:
477                 if (subcmd != -1 && subcmd_item_is_key(lw->selected))
478                         delete_key(subcmd, subcmd_item_to_key_id(lw->selected));
480                 return true;
481         case CMD_ADD:
482                 if (subcmd != -1)
483                         add_key(subcmd);
484                 return true;
485         case CMD_SAVE_PLAYLIST:
486                 apply_keys();
487                 save_keys();
488                 return true;
489         case CMD_LIST_FIND:
490         case CMD_LIST_RFIND:
491         case CMD_LIST_FIND_NEXT:
492         case CMD_LIST_RFIND_NEXT:
493                 screen_find(lw, cmd, list_callback, NULL);
494                 keydef_repaint();
495                 return true;
497         default:
498                 return false;
499         }
501         /* unreachable */
502         assert(0);
503         return false;
506 const struct screen_functions screen_keydef = {
507         .init = keydef_init,
508         .exit = keydef_exit,
509         .open = keydef_open,
510         .close = keydef_close,
511         .resize = keydef_resize,
512         .paint = keydef_paint,
513         .cmd = keydef_cmd,
514         .get_title = keydef_title,
515 };