Code

screen_keydef: document delete_key
[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 #define STATIC_ITEMS      0
35 #define STATIC_SUB_ITEMS  1
36 #define BUFSIZE 256
38 #define LIST_ITEM_APPLY()   ((unsigned)command_list_length)
39 #define LIST_ITEM_SAVE()    (LIST_ITEM_APPLY()+1)
40 #define LIST_LENGTH()       (LIST_ITEM_SAVE()+1)
42 #define LIST_ITEM_SAVE_LABEL  _("===> Apply & Save key bindings  ")
43 #define LIST_ITEM_APPLY_LABEL _("===> Apply key bindings ")
46 static struct list_window *lw;
47 static unsigned command_list_length = 0;
48 static command_definition_t *cmds = NULL;
50 static int subcmd = -1;
51 static unsigned subcmd_length = 0;
52 static unsigned subcmd_addpos = 0;
54 static int
55 keybindings_changed(void)
56 {
57         command_definition_t *orginal_cmds = get_command_definitions();
58         size_t size = command_list_length * sizeof(command_definition_t);
60         return memcmp(orginal_cmds, cmds, size);
61 }
63 static void
64 apply_keys(void)
65 {
66         if (keybindings_changed()) {
67                 command_definition_t *orginal_cmds = get_command_definitions();
68                 size_t size = command_list_length * sizeof(command_definition_t);
70                 memcpy(orginal_cmds, cmds, size);
71                 screen_status_printf(_("You have new key bindings"));
72         } else
73                 screen_status_printf(_("Keybindings unchanged."));
74 }
76 static int
77 save_keys(void)
78 {
79         FILE *f;
80         char *filename;
82         if (check_user_conf_dir()) {
83                 screen_status_printf(_("Error: Unable to create directory ~/.ncmpc - %s"),
84                                      strerror(errno));
85                 screen_bell();
86                 return -1;
87         }
89         filename = get_user_key_binding_filename();
91         if ((f = fopen(filename,"w")) == NULL) {
92                 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
93                 screen_bell();
94                 g_free(filename);
95                 return -1;
96         }
98         if (write_key_bindings(f, KEYDEF_WRITE_HEADER))
99                 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
100         else
101                 screen_status_printf(_("Wrote %s"), filename);
103         g_free(filename);
104         return fclose(f);
107 static void
108 check_subcmd_length(void)
110         subcmd_length = 0;
111         while (subcmd_length < MAX_COMMAND_KEYS &&
112                cmds[subcmd].keys[subcmd_length] > 0)
113                 ++subcmd_length;
115         if (subcmd_length < MAX_COMMAND_KEYS) {
116                 subcmd_addpos = subcmd_length;
117                 subcmd_length++;
118         } else
119                 subcmd_addpos = 0;
120         subcmd_length += STATIC_SUB_ITEMS;
121         list_window_set_length(lw, subcmd_length);
124 static void
125 keydef_paint(void);
127 static void
128 keydef_repaint(void)
130         keydef_paint();
131         wrefresh(lw->w);
134 /**
135  * Delete a key from a given command's definition
136  * @param cmd_index the command
137  * @param key_index the key (see below)
138  */
139 static void
140 delete_key(int cmd_index, int key_index)
142         /* shift the keys to close the gap that appeared */
143         int i = key_index+1;
144         while (i < MAX_COMMAND_KEYS && cmds[cmd_index].keys[i])
145                 cmds[cmd_index].keys[key_index++] = cmds[cmd_index].keys[i++];
147         /* As key_index now holds the index of the last key slot that contained
148            a key, we use it to empty this slot, because this key has been copied
149            to the previous slot in the loop above */
150         cmds[cmd_index].keys[key_index] = 0;
152         cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
153         check_subcmd_length();
155         screen_status_printf(_("Deleted"));
157         /* repaint */
158         keydef_repaint();
160         /* update key conflict flags */
161         check_key_bindings(cmds, NULL, 0);
164 static void
165 assign_new_key(int cmd_index, int key_index)
167         int key;
168         char *buf;
169         command_t cmd;
171         buf = g_strdup_printf(_("Enter new key for %s: "), cmds[cmd_index].name);
172         key = screen_getch(buf);
173         g_free(buf);
175         if (key==ERR) {
176                 screen_status_printf(_("Aborted"));
177                 return;
178         }
180         cmd = find_key_command(key, cmds);
181         if (cmd != CMD_NONE && cmd != cmds[cmd_index].command) {
182                 screen_status_printf(_("Error: key %s is already used for %s"),
183                                      key2str(key),
184                                      get_key_command_name(cmd));
185                 screen_bell();
186                 return;
187         }
189         cmds[cmd_index].keys[key_index] = key;
190         cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
192         screen_status_printf(_("Assigned %s to %s"),
193                              key2str(key),cmds[cmd_index].name);
194         check_subcmd_length();
196         /* repaint */
197         keydef_repaint();
199         /* update key conflict flags */
200         check_key_bindings(cmds, NULL, 0);
203 static const char *
204 list_callback(unsigned idx, G_GNUC_UNUSED void *data)
206         static char buf[BUFSIZE];
208         if (subcmd < 0) {
209                 if (idx == LIST_ITEM_APPLY())
210                         return LIST_ITEM_APPLY_LABEL;
211                 else if (idx == LIST_ITEM_SAVE())
212                         return LIST_ITEM_SAVE_LABEL;
214                 assert(idx < (unsigned)command_list_length);
216                 return cmds[idx].name;
217         } else {
218                 if (idx == 0)
219                         return "[..]";
220                 idx--;
221                 if (idx == subcmd_addpos) {
222                         g_snprintf(buf, BUFSIZE, "%d. %s",
223                                    idx + 1, _("Add new key"));
224                         return buf;
225                 }
227                 assert(idx < MAX_COMMAND_KEYS && cmds[subcmd].keys[idx] > 0);
229                 g_snprintf(buf,
230                            BUFSIZE, "%d. %-20s   (%d) ",
231                            idx + 1,
232                            key2str(cmds[subcmd].keys[idx]),
233                            cmds[subcmd].keys[idx]);
234                 return buf;
235         }
238 static void
239 keydef_init(WINDOW *w, int cols, int rows)
241         lw = list_window_init(w, cols, rows);
244 static void
245 keydef_resize(int cols, int rows)
247         list_window_resize(lw, cols, rows);
250 static void
251 keydef_exit(void)
253         list_window_free(lw);
254         if (cmds)
255                 g_free(cmds);
256         cmds = NULL;
257         lw = NULL;
260 static void
261 keydef_open(G_GNUC_UNUSED struct mpdclient *c)
263         if (cmds == NULL) {
264                 command_definition_t *current_cmds = get_command_definitions();
265                 size_t cmds_size;
267                 command_list_length = 0;
268                 while (current_cmds[command_list_length].name)
269                         command_list_length++;
271                 cmds_size = (command_list_length+1) * sizeof(command_definition_t);
272                 cmds = g_malloc0(cmds_size);
273                 memcpy(cmds, current_cmds, cmds_size);
274                 command_list_length += STATIC_ITEMS;
275         }
277         subcmd = -1;
278         list_window_set_length(lw, LIST_LENGTH());
281 static void
282 keydef_close(void)
284         if (cmds && !keybindings_changed()) {
285                 g_free(cmds);
286                 cmds = NULL;
287         } else
288                 screen_status_printf(_("Note: Did you forget to \'Apply\' your changes?"));
291 static const char *
292 keydef_title(char *str, size_t size)
294         if (subcmd < 0)
295                 return _("Edit key bindings");
297         g_snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name);
298         return str;
301 static void
302 keydef_paint(void)
304         list_window_paint(lw, list_callback, NULL);
307 static bool
308 keydef_cmd(G_GNUC_UNUSED struct mpdclient *c, command_t cmd)
310         if (cmd == CMD_LIST_RANGE_SELECT)
311                 return false;
313         if (list_window_cmd(lw, cmd)) {
314                 keydef_repaint();
315                 return true;
316         }
318         switch(cmd) {
319         case CMD_PLAY:
320                 if (subcmd < 0) {
321                         if (lw->selected == LIST_ITEM_APPLY())
322                                 apply_keys();
323                         else if (lw->selected == LIST_ITEM_SAVE()) {
324                                 apply_keys();
325                                 save_keys();
326                         } else {
327                                 subcmd = lw->selected;
328                                 list_window_reset(lw);
329                                 check_subcmd_length();
331                                 keydef_repaint();
332                         }
333                 } else {
334                         if (lw->selected == 0) { /* up */
335                                 list_window_set_length(lw, LIST_LENGTH());
336                                 list_window_set_cursor(lw, subcmd);
337                                 subcmd = -1;
339                                 keydef_repaint();
340                         } else
341                                 assign_new_key(subcmd,
342                                                lw->selected - STATIC_SUB_ITEMS);
343                 }
344                 return true;
345         case CMD_GO_PARENT_DIRECTORY:
346                 if (subcmd >=0) {
347                         list_window_set_length(lw, LIST_LENGTH());
348                         list_window_set_cursor(lw, subcmd);
349                         subcmd = -1;
351                         keydef_repaint();
352                 }
353                 break;
354         case CMD_DELETE:
355                 if (subcmd >= 0 && lw->selected >= STATIC_SUB_ITEMS)
356                         delete_key(subcmd, lw->selected - STATIC_SUB_ITEMS);
357                 return true;
358                 break;
359         case CMD_SAVE_PLAYLIST:
360                 apply_keys();
361                 save_keys();
362                 break;
363         case CMD_LIST_FIND:
364         case CMD_LIST_RFIND:
365         case CMD_LIST_FIND_NEXT:
366         case CMD_LIST_RFIND_NEXT:
367                 screen_find(lw, cmd, list_callback, NULL);
368                 keydef_repaint();
369                 return true;
371         default:
372                 break;
373         }
375         return false;
378 const struct screen_functions screen_keydef = {
379         .init = keydef_init,
380         .exit = keydef_exit,
381         .open = keydef_open,
382         .close = keydef_close,
383         .resize = keydef_resize,
384         .paint = keydef_paint,
385         .cmd = keydef_cmd,
386         .get_title = keydef_title,
387 };