Code

screen_*: eliminate redundant wrefresh() calls
[ncmpc.git] / src / screen_keydef.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2017 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 "options.h"
29 #include "Compiler.h"
31 #include <assert.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <glib.h>
36 static struct list_window *lw;
38 static command_definition_t *cmds = NULL;
40 /** the number of commands */
41 static unsigned command_n_commands = 0;
43 /**
44  * the position of the "apply" item. It's the same as command_n_commands,
45  * because array subscripts start at 0, while numbers of items start at 1.
46  */
47 gcc_pure
48 static inline unsigned
49 command_item_apply(void)
50 {
51         return command_n_commands;
52 }
54 /** the position of the "apply and save" item */
55 gcc_pure
56 static inline unsigned
57 command_item_save(void)
58 {
59         return command_item_apply() + 1;
60 }
62 /** the number of items in the "command" view */
63 gcc_pure
64 static inline unsigned
65 command_length(void)
66 {
67         return command_item_save() + 1;
68 }
71 /**
72  * The command being edited, represented by a array subscript to @cmds, or -1,
73  * if no command is being edited
74  */
75 static int subcmd = -1;
77 /** The number of keys assigned to the current command */
78 static unsigned subcmd_n_keys = 0;
80 /** The position of the up ("[..]") item */
81 gcc_const
82 static inline unsigned
83 subcmd_item_up(void)
84 {
85         return 0;
86 }
88 /** The position of the "add a key" item */
89 gcc_pure
90 static inline unsigned
91 subcmd_item_add(void)
92 {
93         return subcmd_n_keys + 1;
94 }
96 /** The number of items in the list_window, if there's a command being edited */
97 gcc_pure
98 static inline unsigned
99 subcmd_length(void)
101         return subcmd_item_add() + 1;
104 /** Check whether a given item is a key */
105 gcc_pure
106 static inline bool
107 subcmd_item_is_key(unsigned i)
109         return (i > subcmd_item_up() && i < subcmd_item_add());
112 /**
113  * Convert an item id (as in lw->selected) into a "key id", which is an array
114  * subscript to cmds[subcmd].keys.
115  */
116 gcc_const
117 static inline unsigned
118 subcmd_item_to_key_id(unsigned i)
120         return i - 1;
124 static int
125 keybindings_changed(void)
127         command_definition_t *orginal_cmds = get_command_definitions();
128         size_t size = command_n_commands * sizeof(command_definition_t);
130         return memcmp(orginal_cmds, cmds, size);
133 static void
134 apply_keys(void)
136         if (keybindings_changed()) {
137                 command_definition_t *orginal_cmds = get_command_definitions();
138                 size_t size = command_n_commands * sizeof(command_definition_t);
140                 memcpy(orginal_cmds, cmds, size);
141                 screen_status_printf(_("You have new key bindings"));
142         } else
143                 screen_status_printf(_("Keybindings unchanged."));
146 static int
147 save_keys(void)
149         char *allocated = NULL;
150         const char *filename = options.key_file;
151         if (filename == NULL) {
152                 if (!check_user_conf_dir()) {
153                         screen_status_printf(_("Error: Unable to create directory ~/.ncmpc - %s"),
154                                              strerror(errno));
155                         screen_bell();
156                         return -1;
157                 }
159                 filename = allocated = build_user_key_binding_filename();
160         }
162         FILE *f = fopen(filename, "w");
163         if (f == NULL) {
164                 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
165                 screen_bell();
166                 g_free(allocated);
167                 return -1;
168         }
170         if (write_key_bindings(f, KEYDEF_WRITE_HEADER))
171                 screen_status_printf(_("Wrote %s"), filename);
172         else
173                 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
175         g_free(allocated);
176         return fclose(f);
179 /* TODO: rename to check_n_keys / subcmd_count_keys? */
180 static void
181 check_subcmd_length(void)
183         unsigned i;
185         /* this loops counts the continous valid keys at the start of the the keys
186            array, so make sure you don't have gaps */
187         for (i = 0; i < MAX_COMMAND_KEYS; i++)
188                 if (cmds[subcmd].keys[i] == 0)
189                         break;
190         subcmd_n_keys = i;
192         list_window_set_length(lw, subcmd_length());
195 static void
196 keydef_paint(void);
198 /** lw->start the last time switch_to_subcmd_mode() was called */
199 static unsigned saved_start = 0;
201 static void
202 switch_to_subcmd_mode(int cmd)
204         assert(subcmd == -1);
206         saved_start = lw->start;
208         subcmd = cmd;
209         list_window_reset(lw);
210         check_subcmd_length();
212         keydef_paint();
215 static void
216 switch_to_command_mode(void)
218         assert(subcmd != -1);
220         list_window_set_length(lw, command_length());
221         list_window_set_cursor(lw, subcmd);
222         subcmd = -1;
224         lw->start = saved_start;
226         keydef_paint();
229 /**
230  * Delete a key from a given command's definition
231  * @param cmd_index the command
232  * @param key_index the key (see below)
233  */
234 static void
235 delete_key(int cmd_index, int key_index)
237         /* shift the keys to close the gap that appeared */
238         int i = key_index+1;
239         while (i < MAX_COMMAND_KEYS && cmds[cmd_index].keys[i])
240                 cmds[cmd_index].keys[key_index++] = cmds[cmd_index].keys[i++];
242         /* As key_index now holds the index of the last key slot that contained
243            a key, we use it to empty this slot, because this key has been copied
244            to the previous slot in the loop above */
245         cmds[cmd_index].keys[key_index] = 0;
247         cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
248         check_subcmd_length();
250         screen_status_printf(_("Deleted"));
252         /* repaint */
253         keydef_paint();
255         /* update key conflict flags */
256         check_key_bindings(cmds, NULL, 0);
259 /* assigns a new key to a key slot */
260 static void
261 overwrite_key(int cmd_index, int key_index)
263         assert(key_index < MAX_COMMAND_KEYS);
265         char *buf = g_strdup_printf(_("Enter new key for %s: "),
266                                     cmds[cmd_index].name);
267         const int key = screen_getch(buf);
268         g_free(buf);
270         if (key == ERR) {
271                 screen_status_printf(_("Aborted"));
272                 return;
273         }
275         if (key == '\0') {
276                 screen_status_printf(_("Ctrl-Space can't be used"));
277                 return;
278         }
280         const command_t cmd = find_key_command(key, cmds);
281         if (cmd != CMD_NONE) {
282                 screen_status_printf(_("Error: key %s is already used for %s"),
283                                      key2str(key), get_key_command_name(cmd));
284                 screen_bell();
285                 return;
286         }
288         cmds[cmd_index].keys[key_index] = key;
289         cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
291         screen_status_printf(_("Assigned %s to %s"),
292                              key2str(key),cmds[cmd_index].name);
293         check_subcmd_length();
295         /* repaint */
296         keydef_paint();
298         /* update key conflict flags */
299         check_key_bindings(cmds, NULL, 0);
302 /* assign a new key to a new slot */
303 static void
304 add_key(int cmd_index)
306         if (subcmd_n_keys < MAX_COMMAND_KEYS)
307                 overwrite_key(cmd_index, subcmd_n_keys);
310 static const char *
311 list_callback(unsigned idx, gcc_unused void *data)
313         static char buf[256];
315         if (subcmd == -1) {
316                 if (idx == command_item_apply())
317                         return _("===> Apply key bindings ");
318                 if (idx == command_item_save())
319                         return _("===> Apply & Save key bindings  ");
321                 assert(idx < (unsigned) command_n_commands);
323                 /*
324                  * Format the lines in two aligned columnes for the key name and
325                  * the description, like this:
326                  *
327                  *      this-command - do this
328                  *      that-one     - do that
329                  */
330                 size_t len = strlen(cmds[idx].name);
331                 strncpy(buf, cmds[idx].name, sizeof(buf));
333                 if (len < get_cmds_max_name_width(cmds))
334                         memset(buf + len, ' ', get_cmds_max_name_width(cmds) - len);
336                 g_snprintf(buf + get_cmds_max_name_width(cmds),
337                            sizeof(buf) - get_cmds_max_name_width(cmds),
338                            " - %s", _(cmds[idx].description));
340                 return buf;
341         } else {
342                 if (idx == subcmd_item_up())
343                         return "[..]";
345                 if (idx == subcmd_item_add()) {
346                         g_snprintf(buf, sizeof(buf), "%d. %s",
347                                    idx, _("Add new key"));
348                         return buf;
349                 }
351                 assert(subcmd_item_is_key(idx));
353                 g_snprintf(buf, sizeof(buf),
354                            "%d. %-20s   (%d) ", idx,
355                            key2str(cmds[subcmd].keys[subcmd_item_to_key_id(idx)]),
356                            cmds[subcmd].keys[subcmd_item_to_key_id(idx)]);
357                 return buf;
358         }
361 static void
362 keydef_init(WINDOW *w, int cols, int rows)
364         lw = list_window_init(w, cols, rows);
367 static void
368 keydef_resize(int cols, int rows)
370         list_window_resize(lw, cols, rows);
373 static void
374 keydef_exit(void)
376         list_window_free(lw);
377         if (cmds)
378                 g_free(cmds);
379         cmds = NULL;
380         lw = NULL;
383 static void
384 keydef_open(gcc_unused struct mpdclient *c)
386         if (cmds == NULL) {
387                 command_definition_t *current_cmds = get_command_definitions();
388                 command_n_commands = 0;
389                 while (current_cmds[command_n_commands].name)
390                         command_n_commands++;
392                 /* +1 for the terminator element */
393                 size_t cmds_size = (command_n_commands + 1)
394                         * sizeof(command_definition_t);
395                 cmds = g_malloc0(cmds_size);
396                 memcpy(cmds, current_cmds, cmds_size);
397         }
399         subcmd = -1;
400         list_window_set_length(lw, command_length());
403 static void
404 keydef_close(void)
406         if (cmds && !keybindings_changed()) {
407                 g_free(cmds);
408                 cmds = NULL;
409         } else
410                 screen_status_printf(_("Note: Did you forget to \'Apply\' your changes?"));
413 static const char *
414 keydef_title(char *str, size_t size)
416         if (subcmd == -1)
417                 return _("Edit key bindings");
419         g_snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name);
420         return str;
423 static void
424 keydef_paint(void)
426         list_window_paint(lw, list_callback, NULL);
429 static bool
430 keydef_cmd(gcc_unused struct mpdclient *c, command_t cmd)
432         if (cmd == CMD_LIST_RANGE_SELECT)
433                 return false;
435         if (list_window_cmd(lw, cmd)) {
436                 keydef_paint();
437                 return true;
438         }
440         switch(cmd) {
441         case CMD_PLAY:
442                 if (subcmd == -1) {
443                         if (lw->selected == command_item_apply()) {
444                                 apply_keys();
445                         } else if (lw->selected == command_item_save()) {
446                                 apply_keys();
447                                 save_keys();
448                         } else {
449                                 switch_to_subcmd_mode(lw->selected);
450                         }
451                 } else {
452                         if (lw->selected == subcmd_item_up()) {
453                                 switch_to_command_mode();
454                         } else if (lw->selected == subcmd_item_add()) {
455                                 add_key(subcmd);
456                         } else {
457                                 /* just to be sure ;-) */
458                                 assert(subcmd_item_is_key(lw->selected));
459                                 overwrite_key(subcmd, subcmd_item_to_key_id(lw->selected));
460                         }
461                 }
462                 return true;
463         case CMD_GO_PARENT_DIRECTORY:
464         case CMD_GO_ROOT_DIRECTORY:
465                 if (subcmd != -1)
466                         switch_to_command_mode();
467                 return true;
468         case CMD_DELETE:
469                 if (subcmd != -1 && subcmd_item_is_key(lw->selected))
470                         delete_key(subcmd, subcmd_item_to_key_id(lw->selected));
472                 return true;
473         case CMD_ADD:
474                 if (subcmd != -1)
475                         add_key(subcmd);
476                 return true;
477         case CMD_SAVE_PLAYLIST:
478                 apply_keys();
479                 save_keys();
480                 return true;
481         case CMD_LIST_FIND:
482         case CMD_LIST_RFIND:
483         case CMD_LIST_FIND_NEXT:
484         case CMD_LIST_RFIND_NEXT:
485                 screen_find(lw, cmd, list_callback, NULL);
486                 keydef_paint();
487                 return true;
489         default:
490                 return false;
491         }
493         /* unreachable */
494         assert(0);
495         return false;
498 const struct screen_functions screen_keydef = {
499         .init = keydef_init,
500         .exit = keydef_exit,
501         .open = keydef_open,
502         .close = keydef_close,
503         .resize = keydef_resize,
504         .paint = keydef_paint,
505         .cmd = keydef_cmd,
506         .get_title = keydef_title,
507 };