Code

screen_keydef: use inline functions instead of macros for non-constants
[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"
29 #include <assert.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <glib.h>
34 static struct list_window *lw;
36 static command_definition_t *cmds = NULL;
38 /** the number of commands */
39 static unsigned command_n_commands = 0;
41 /**
42  * the position of the "apply" item. It's the same as command_n_commands,
43  * because array subscripts start at 0, while numbers of items start at 1.
44  */
45 static G_GNUC_PURE inline unsigned
46 command_item_apply(void)
47 {
48         return command_n_commands;
49 }
51 /** the position of the "apply and save" item */
52 static G_GNUC_PURE inline unsigned
53 command_item_save(void)
54 {
55         return command_item_apply() + 1;
56 }
58 /** the number of items in the "command" view */
59 static G_GNUC_PURE inline unsigned
60 command_length(void)
61 {
62         return command_item_save() + 1;
63 }
66 /**
67  * The command being edited, represented by a array subscript to @cmds, or -1,
68  * if no command is being edited
69  */
70 static int subcmd = -1;
72 /** The number of keys assigned to the current command */
73 static unsigned subcmd_n_keys = 0;
75 /** The position of the up ("[..]") item */
76 static G_GNUC_CONST inline unsigned
77 subcmd_item_up(void)
78 {
79         return 0;
80 }
82 /** The position of the "add a key" item */
83 static G_GNUC_PURE inline unsigned
84 subcmd_item_add(void)
85 {
86         return subcmd_n_keys + 1;
87 }
89 /** The number of items in the list_window, if there's a command being edited */
90 static G_GNUC_PURE inline unsigned
91 subcmd_length(void)
92 {
93         return subcmd_item_add() + 1;
94 }
96 /** Check whether a given item is a key */
97 static G_GNUC_PURE inline bool
98 subcmd_item_is_key(unsigned i)
99 {
100         return (i > subcmd_item_up() && i < subcmd_item_add());
103 /**
104  * Convert an item id (as in lw->selected) into a "key id", which is an array
105  * subscript to cmds[subcmd].keys.
106  */
107 static G_GNUC_CONST inline unsigned
108 subcmd_item_to_key_id(unsigned i)
110         return i - 1;
114 static int
115 keybindings_changed(void)
117         command_definition_t *orginal_cmds = get_command_definitions();
118         size_t size = command_n_commands * sizeof(command_definition_t);
120         return memcmp(orginal_cmds, cmds, size);
123 static void
124 apply_keys(void)
126         if (keybindings_changed()) {
127                 command_definition_t *orginal_cmds = get_command_definitions();
128                 size_t size = command_n_commands * sizeof(command_definition_t);
130                 memcpy(orginal_cmds, cmds, size);
131                 screen_status_printf(_("You have new key bindings"));
132         } else
133                 screen_status_printf(_("Keybindings unchanged."));
136 static int
137 save_keys(void)
139         FILE *f;
140         char *filename;
142         if (check_user_conf_dir()) {
143                 screen_status_printf(_("Error: Unable to create directory ~/.ncmpc - %s"),
144                                      strerror(errno));
145                 screen_bell();
146                 return -1;
147         }
149         filename = build_user_key_binding_filename();
151         if ((f = fopen(filename,"w")) == NULL) {
152                 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
153                 screen_bell();
154                 g_free(filename);
155                 return -1;
156         }
158         if (write_key_bindings(f, KEYDEF_WRITE_HEADER))
159                 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
160         else
161                 screen_status_printf(_("Wrote %s"), filename);
163         g_free(filename);
164         return fclose(f);
167 /* TODO: rename to check_n_keys / subcmd_count_keys? */
168 static void
169 check_subcmd_length(void)
171         unsigned i;
173         /* this loops counts the continous valid keys at the start of the the keys
174            array, so make sure you don't have gaps */
175         for (i = 0; i < MAX_COMMAND_KEYS; i++)
176                 if (cmds[subcmd].keys[i] == 0)
177                         break;
178         subcmd_n_keys = i;
180         list_window_set_length(lw, subcmd_length());
183 static void
184 keydef_paint(void);
186 static void
187 keydef_repaint(void)
189         keydef_paint();
190         wrefresh(lw->w);
193 /** lw->start the last time switch_to_subcmd_mode() was called */
194 static unsigned saved_start = 0;
196 static void
197 switch_to_subcmd_mode(int cmd)
199         assert(subcmd == -1);
201         saved_start = lw->start;
203         subcmd = cmd;
204         list_window_reset(lw);
205         check_subcmd_length();
207         keydef_repaint();
210 static void
211 switch_to_command_mode(void)
213         assert(subcmd != -1);
215         list_window_set_length(lw, command_length());
216         list_window_set_cursor(lw, subcmd);
217         subcmd = -1;
219         lw->start = saved_start;
221         keydef_repaint();
224 /**
225  * Delete a key from a given command's definition
226  * @param cmd_index the command
227  * @param key_index the key (see below)
228  */
229 static void
230 delete_key(int cmd_index, int key_index)
232         /* shift the keys to close the gap that appeared */
233         int i = key_index+1;
234         while (i < MAX_COMMAND_KEYS && cmds[cmd_index].keys[i])
235                 cmds[cmd_index].keys[key_index++] = cmds[cmd_index].keys[i++];
237         /* As key_index now holds the index of the last key slot that contained
238            a key, we use it to empty this slot, because this key has been copied
239            to the previous slot in the loop above */
240         cmds[cmd_index].keys[key_index] = 0;
242         cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
243         check_subcmd_length();
245         screen_status_printf(_("Deleted"));
247         /* repaint */
248         keydef_repaint();
250         /* update key conflict flags */
251         check_key_bindings(cmds, NULL, 0);
254 /* assigns a new key to a key slot */
255 static void
256 overwrite_key(int cmd_index, int key_index)
258         int key;
259         char *buf;
260         command_t cmd;
262         assert(key_index < MAX_COMMAND_KEYS);
264         buf = g_strdup_printf(_("Enter new key for %s: "), cmds[cmd_index].name);
265         key = screen_getch(buf);
266         g_free(buf);
268         if (key==ERR) {
269                 screen_status_printf(_("Aborted"));
270                 return;
271         }
273         cmd = find_key_command(key, cmds);
274         if (cmd != CMD_NONE) {
275                 screen_status_printf(_("Error: key %s is already used for %s"),
276                                      key2str(key), get_key_command_name(cmd));
277                 screen_bell();
278                 return;
279         }
281         cmds[cmd_index].keys[key_index] = key;
282         cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
284         screen_status_printf(_("Assigned %s to %s"),
285                              key2str(key),cmds[cmd_index].name);
286         check_subcmd_length();
288         /* repaint */
289         keydef_repaint();
291         /* update key conflict flags */
292         check_key_bindings(cmds, NULL, 0);
295 /* assign a new key to a new slot */
296 static void
297 add_key(int cmd_index)
299         if (subcmd_n_keys < MAX_COMMAND_KEYS)
300                 overwrite_key(cmd_index, subcmd_n_keys);
303 static const char *
304 list_callback(unsigned idx, G_GNUC_UNUSED void *data)
306         static char buf[256];
308         if (subcmd == -1) {
309                 if (idx == command_item_apply())
310                         return _("===> Apply key bindings ");
311                 if (idx == command_item_save())
312                         return _("===> Apply & Save key bindings  ");
314                 assert(idx < (unsigned) command_n_commands);
316                 /*
317                  * Format the lines in two aligned columnes for the key name and
318                  * the description, like this:
319                  *
320                  *      this-command - do this
321                  *      that-one     - do that
322                  */
323                 size_t len = strlen(cmds[idx].name);
324                 strncpy(buf, cmds[idx].name, sizeof(buf));
326                 if (len < get_cmds_max_name_width(cmds))
327                         memset(buf + len, ' ', get_cmds_max_name_width(cmds) - len);
329                 g_snprintf(buf + get_cmds_max_name_width(cmds),
330                            sizeof(buf) - get_cmds_max_name_width(cmds),
331                            " - %s", _(cmds[idx].description));
333                 return buf;
334         } else {
335                 if (idx == subcmd_item_up())
336                         return "[..]";
338                 if (idx == subcmd_item_add()) {
339                         g_snprintf(buf, sizeof(buf), "%d. %s",
340                                    idx, _("Add new key"));
341                         return buf;
342                 }
344                 assert(subcmd_item_is_key(idx));
346                 g_snprintf(buf, sizeof(buf),
347                            "%d. %-20s   (%d) ", idx,
348                            key2str(cmds[subcmd].keys[subcmd_item_to_key_id(idx)]),
349                            cmds[subcmd].keys[subcmd_item_to_key_id(idx)]);
350                 return buf;
351         }
354 static void
355 keydef_init(WINDOW *w, int cols, int rows)
357         lw = list_window_init(w, cols, rows);
360 static void
361 keydef_resize(int cols, int rows)
363         list_window_resize(lw, cols, rows);
366 static void
367 keydef_exit(void)
369         list_window_free(lw);
370         if (cmds)
371                 g_free(cmds);
372         cmds = NULL;
373         lw = NULL;
376 static void
377 keydef_open(G_GNUC_UNUSED struct mpdclient *c)
379         if (cmds == NULL) {
380                 command_definition_t *current_cmds = get_command_definitions();
381                 size_t cmds_size;
383                 command_n_commands = 0;
384                 while (current_cmds[command_n_commands].name)
385                         command_n_commands++;
387                 /* +1 for the terminator element */
388                 cmds_size = (command_n_commands + 1) * sizeof(command_definition_t);
389                 cmds = g_malloc0(cmds_size);
390                 memcpy(cmds, current_cmds, cmds_size);
391         }
393         subcmd = -1;
394         list_window_set_length(lw, command_length());
397 static void
398 keydef_close(void)
400         if (cmds && !keybindings_changed()) {
401                 g_free(cmds);
402                 cmds = NULL;
403         } else
404                 screen_status_printf(_("Note: Did you forget to \'Apply\' your changes?"));
407 static const char *
408 keydef_title(char *str, size_t size)
410         if (subcmd == -1)
411                 return _("Edit key bindings");
413         g_snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name);
414         return str;
417 static void
418 keydef_paint(void)
420         list_window_paint(lw, list_callback, NULL);
423 static bool
424 keydef_cmd(G_GNUC_UNUSED struct mpdclient *c, command_t cmd)
426         if (cmd == CMD_LIST_RANGE_SELECT)
427                 return false;
429         if (list_window_cmd(lw, cmd)) {
430                 keydef_repaint();
431                 return true;
432         }
434         switch(cmd) {
435         case CMD_PLAY:
436                 if (subcmd == -1) {
437                         if (lw->selected == command_item_apply()) {
438                                 apply_keys();
439                         } else if (lw->selected == command_item_save()) {
440                                 apply_keys();
441                                 save_keys();
442                         } else {
443                                 switch_to_subcmd_mode(lw->selected);
444                         }
445                 } else {
446                         if (lw->selected == subcmd_item_up()) {
447                                 switch_to_command_mode();
448                         } else if (lw->selected == subcmd_item_add()) {
449                                 add_key(subcmd);
450                         } else {
451                                 /* just to be sure ;-) */
452                                 assert(subcmd_item_is_key(lw->selected));
453                                 overwrite_key(subcmd, subcmd_item_to_key_id(lw->selected));
454                         }
455                 }
456                 return true;
457         case CMD_GO_PARENT_DIRECTORY:
458         case CMD_GO_ROOT_DIRECTORY:
459                 if (subcmd != -1)
460                         switch_to_command_mode();
461                 return true;
462         case CMD_DELETE:
463                 if (subcmd != -1 && subcmd_item_is_key(lw->selected))
464                         delete_key(subcmd, subcmd_item_to_key_id(lw->selected));
466                 return true;
467         case CMD_ADD:
468                 if (subcmd != -1)
469                         add_key(subcmd);
470                 return true;
471         case CMD_SAVE_PLAYLIST:
472                 apply_keys();
473                 save_keys();
474                 return true;
475         case CMD_LIST_FIND:
476         case CMD_LIST_RFIND:
477         case CMD_LIST_FIND_NEXT:
478         case CMD_LIST_RFIND_NEXT:
479                 screen_find(lw, cmd, list_callback, NULL);
480                 keydef_repaint();
481                 return true;
483         default:
484                 return false;
485         }
487         /* unreachable */
488         assert(0);
489         return false;
492 const struct screen_functions screen_keydef = {
493         .init = keydef_init,
494         .exit = keydef_exit,
495         .open = keydef_open,
496         .close = keydef_close,
497         .resize = keydef_resize,
498         .paint = keydef_paint,
499         .cmd = keydef_cmd,
500         .get_title = keydef_title,
501 };