Code

screen_keydef: remove LIST_ITEM_{SAVE,APPLY}_LABEL
[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
37 #define LIST_ITEM_APPLY()   ((unsigned)command_list_length)
38 #define LIST_ITEM_SAVE()    (LIST_ITEM_APPLY()+1)
39 #define LIST_LENGTH()       (LIST_ITEM_SAVE()+1)
42 static struct list_window *lw;
43 static unsigned command_list_length = 0;
44 static command_definition_t *cmds = NULL;
46 static int subcmd = -1;
47 static unsigned subcmd_length = 0;
48 static unsigned subcmd_addpos = 0;
50 static int
51 keybindings_changed(void)
52 {
53         command_definition_t *orginal_cmds = get_command_definitions();
54         size_t size = command_list_length * sizeof(command_definition_t);
56         return memcmp(orginal_cmds, cmds, size);
57 }
59 static void
60 apply_keys(void)
61 {
62         if (keybindings_changed()) {
63                 command_definition_t *orginal_cmds = get_command_definitions();
64                 size_t size = command_list_length * sizeof(command_definition_t);
66                 memcpy(orginal_cmds, cmds, size);
67                 screen_status_printf(_("You have new key bindings"));
68         } else
69                 screen_status_printf(_("Keybindings unchanged."));
70 }
72 static int
73 save_keys(void)
74 {
75         FILE *f;
76         char *filename;
78         if (check_user_conf_dir()) {
79                 screen_status_printf(_("Error: Unable to create directory ~/.ncmpc - %s"),
80                                      strerror(errno));
81                 screen_bell();
82                 return -1;
83         }
85         filename = get_user_key_binding_filename();
87         if ((f = fopen(filename,"w")) == NULL) {
88                 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
89                 screen_bell();
90                 g_free(filename);
91                 return -1;
92         }
94         if (write_key_bindings(f, KEYDEF_WRITE_HEADER))
95                 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
96         else
97                 screen_status_printf(_("Wrote %s"), filename);
99         g_free(filename);
100         return fclose(f);
103 static void
104 check_subcmd_length(void)
106         subcmd_length = 0;
107         while (subcmd_length < MAX_COMMAND_KEYS &&
108                cmds[subcmd].keys[subcmd_length] > 0)
109                 ++subcmd_length;
111         if (subcmd_length < MAX_COMMAND_KEYS) {
112                 subcmd_addpos = subcmd_length;
113                 subcmd_length++;
114         } else
115                 subcmd_addpos = 0;
116         subcmd_length += STATIC_SUB_ITEMS;
117         list_window_set_length(lw, subcmd_length);
120 static void
121 keydef_paint(void);
123 static void
124 keydef_repaint(void)
126         keydef_paint();
127         wrefresh(lw->w);
130 /**
131  * Delete a key from a given command's definition
132  * @param cmd_index the command
133  * @param key_index the key (see below)
134  */
135 static void
136 delete_key(int cmd_index, int key_index)
138         /* shift the keys to close the gap that appeared */
139         int i = key_index+1;
140         while (i < MAX_COMMAND_KEYS && cmds[cmd_index].keys[i])
141                 cmds[cmd_index].keys[key_index++] = cmds[cmd_index].keys[i++];
143         /* As key_index now holds the index of the last key slot that contained
144            a key, we use it to empty this slot, because this key has been copied
145            to the previous slot in the loop above */
146         cmds[cmd_index].keys[key_index] = 0;
148         cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
149         check_subcmd_length();
151         screen_status_printf(_("Deleted"));
153         /* repaint */
154         keydef_repaint();
156         /* update key conflict flags */
157         check_key_bindings(cmds, NULL, 0);
160 static void
161 assign_new_key(int cmd_index, int key_index)
163         int key;
164         char *buf;
165         command_t cmd;
167         buf = g_strdup_printf(_("Enter new key for %s: "), cmds[cmd_index].name);
168         key = screen_getch(buf);
169         g_free(buf);
171         if (key==ERR) {
172                 screen_status_printf(_("Aborted"));
173                 return;
174         }
176         cmd = find_key_command(key, cmds);
177         if (cmd != CMD_NONE && cmd != cmds[cmd_index].command) {
178                 screen_status_printf(_("Error: key %s is already used for %s"),
179                                      key2str(key),
180                                      get_key_command_name(cmd));
181                 screen_bell();
182                 return;
183         }
185         cmds[cmd_index].keys[key_index] = key;
186         cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
188         screen_status_printf(_("Assigned %s to %s"),
189                              key2str(key),cmds[cmd_index].name);
190         check_subcmd_length();
192         /* repaint */
193         keydef_repaint();
195         /* update key conflict flags */
196         check_key_bindings(cmds, NULL, 0);
199 static const char *
200 list_callback(unsigned idx, G_GNUC_UNUSED void *data)
202         static char buf[256];
204         if (subcmd < 0) {
205                 if (idx == LIST_ITEM_APPLY())
206                         return _("===> Apply key bindings ");
207                 else if (idx == LIST_ITEM_SAVE())
208                         return _("===> Apply & Save key bindings  ");
210                 assert(idx < (unsigned)command_list_length);
212                 return cmds[idx].name;
213         } else {
214                 if (idx == 0)
215                         return "[..]";
216                 idx--;
217                 if (idx == subcmd_addpos) {
218                         g_snprintf(buf, sizeof(buf), "%d. %s",
219                                    idx + 1, _("Add new key"));
220                         return buf;
221                 }
223                 assert(idx < MAX_COMMAND_KEYS && cmds[subcmd].keys[idx] > 0);
225                 g_snprintf(buf, sizeof(buf),
226                            "%d. %-20s   (%d) ",
227                            idx + 1,
228                            key2str(cmds[subcmd].keys[idx]),
229                            cmds[subcmd].keys[idx]);
230                 return buf;
231         }
234 static void
235 keydef_init(WINDOW *w, int cols, int rows)
237         lw = list_window_init(w, cols, rows);
240 static void
241 keydef_resize(int cols, int rows)
243         list_window_resize(lw, cols, rows);
246 static void
247 keydef_exit(void)
249         list_window_free(lw);
250         if (cmds)
251                 g_free(cmds);
252         cmds = NULL;
253         lw = NULL;
256 static void
257 keydef_open(G_GNUC_UNUSED struct mpdclient *c)
259         if (cmds == NULL) {
260                 command_definition_t *current_cmds = get_command_definitions();
261                 size_t cmds_size;
263                 command_list_length = 0;
264                 while (current_cmds[command_list_length].name)
265                         command_list_length++;
267                 cmds_size = (command_list_length+1) * sizeof(command_definition_t);
268                 cmds = g_malloc0(cmds_size);
269                 memcpy(cmds, current_cmds, cmds_size);
270                 command_list_length += STATIC_ITEMS;
271         }
273         subcmd = -1;
274         list_window_set_length(lw, LIST_LENGTH());
277 static void
278 keydef_close(void)
280         if (cmds && !keybindings_changed()) {
281                 g_free(cmds);
282                 cmds = NULL;
283         } else
284                 screen_status_printf(_("Note: Did you forget to \'Apply\' your changes?"));
287 static const char *
288 keydef_title(char *str, size_t size)
290         if (subcmd < 0)
291                 return _("Edit key bindings");
293         g_snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name);
294         return str;
297 static void
298 keydef_paint(void)
300         list_window_paint(lw, list_callback, NULL);
303 static bool
304 keydef_cmd(G_GNUC_UNUSED struct mpdclient *c, command_t cmd)
306         if (cmd == CMD_LIST_RANGE_SELECT)
307                 return false;
309         if (list_window_cmd(lw, cmd)) {
310                 keydef_repaint();
311                 return true;
312         }
314         switch(cmd) {
315         case CMD_PLAY:
316                 if (subcmd < 0) {
317                         if (lw->selected == LIST_ITEM_APPLY())
318                                 apply_keys();
319                         else if (lw->selected == LIST_ITEM_SAVE()) {
320                                 apply_keys();
321                                 save_keys();
322                         } else {
323                                 subcmd = lw->selected;
324                                 list_window_reset(lw);
325                                 check_subcmd_length();
327                                 keydef_repaint();
328                         }
329                 } else {
330                         if (lw->selected == 0) { /* up */
331                                 list_window_set_length(lw, LIST_LENGTH());
332                                 list_window_set_cursor(lw, subcmd);
333                                 subcmd = -1;
335                                 keydef_repaint();
336                         } else
337                                 assign_new_key(subcmd,
338                                                lw->selected - STATIC_SUB_ITEMS);
339                 }
340                 return true;
341         case CMD_GO_PARENT_DIRECTORY:
342                 if (subcmd >=0) {
343                         list_window_set_length(lw, LIST_LENGTH());
344                         list_window_set_cursor(lw, subcmd);
345                         subcmd = -1;
347                         keydef_repaint();
348                 }
349                 break;
350         case CMD_DELETE:
351                 if (subcmd >= 0 && lw->selected >= STATIC_SUB_ITEMS)
352                         delete_key(subcmd, lw->selected - STATIC_SUB_ITEMS);
353                 return true;
354                 break;
355         case CMD_SAVE_PLAYLIST:
356                 apply_keys();
357                 save_keys();
358                 break;
359         case CMD_LIST_FIND:
360         case CMD_LIST_RFIND:
361         case CMD_LIST_FIND_NEXT:
362         case CMD_LIST_RFIND_NEXT:
363                 screen_find(lw, cmd, list_callback, NULL);
364                 keydef_repaint();
365                 return true;
367         default:
368                 break;
369         }
371         return false;
374 const struct screen_functions screen_keydef = {
375         .init = keydef_init,
376         .exit = keydef_exit,
377         .open = keydef_open,
378         .close = keydef_close,
379         .resize = keydef_resize,
380         .paint = keydef_paint,
381         .cmd = keydef_cmd,
382         .get_title = keydef_title,
383 };