Code

Update copyright notices
[ncmpc.git] / src / screen_keydef.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 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.
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.
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 "i18n.h"
21 #include "mpdclient.h"
22 #include "options.h"
23 #include "conf.h"
24 #include "command.h"
25 #include "screen.h"
26 #include "screen_utils.h"
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <glib.h>
33 #define STATIC_ITEMS      0
34 #define STATIC_SUB_ITEMS  1
35 #define BUFSIZE 256
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)
41 #define LIST_ITEM_SAVE_LABEL  _("===> Apply & Save key bindings  ")
42 #define LIST_ITEM_APPLY_LABEL _("===> Apply key bindings ")
45 static list_window_t *lw = NULL;
46 static unsigned command_list_length = 0;
47 static command_definition_t *cmds = NULL;
49 static int subcmd = -1;
50 static unsigned subcmd_length = 0;
51 static unsigned subcmd_addpos = 0;
53 static int
54 keybindings_changed(void)
55 {
56         command_definition_t *orginal_cmds = get_command_definitions();
57         size_t size = command_list_length * sizeof(command_definition_t);
59         return memcmp(orginal_cmds, cmds, size);
60 }
62 static void
63 apply_keys(void)
64 {
65         if (keybindings_changed()) {
66                 command_definition_t *orginal_cmds = get_command_definitions();
67                 size_t size = command_list_length * sizeof(command_definition_t);
69                 memcpy(orginal_cmds, cmds, size);
70                 screen_status_printf(_("You have new key bindings"));
71         } else
72                 screen_status_printf(_("Keybindings unchanged."));
73 }
75 static int
76 save_keys(void)
77 {
78         FILE *f;
79         char *filename;
81         if (check_user_conf_dir()) {
82                 screen_status_printf(_("Error: Unable to create direcory ~/.ncmpc - %s"),
83                                      strerror(errno));
84                 screen_bell();
85                 return -1;
86         }
88         filename = get_user_key_binding_filename();
90         if ((f = fopen(filename,"w")) == NULL) {
91                 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
92                 screen_bell();
93                 g_free(filename);
94                 return -1;
95         }
97         if (write_key_bindings(f, KEYDEF_WRITE_HEADER))
98                 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
99         else
100                 screen_status_printf(_("Wrote %s"), filename);
102         g_free(filename);
103         return fclose(f);
106 static void
107 check_subcmd_length(void)
109         subcmd_length = 0;
110         while (subcmd_length < MAX_COMMAND_KEYS &&
111                cmds[subcmd].keys[subcmd_length] > 0)
112                 ++subcmd_length;
114         if (subcmd_length < MAX_COMMAND_KEYS) {
115                 subcmd_addpos = subcmd_length;
116                 subcmd_length++;
117         } else
118                 subcmd_addpos = 0;
119         subcmd_length += STATIC_SUB_ITEMS;
122 static void
123 keydef_paint(void);
125 static void
126 keydef_repaint(void)
128         keydef_paint();
129         wrefresh(lw->w);
132 static void
133 delete_key(int cmd_index, int key_index)
135         int i = key_index+1;
137         screen_status_printf(_("Deleted"));
138         while (i < MAX_COMMAND_KEYS && cmds[cmd_index].keys[i])
139                 cmds[cmd_index].keys[key_index++] = cmds[cmd_index].keys[i++];
140         cmds[cmd_index].keys[key_index] = 0;
141         cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
142         check_subcmd_length();
144         /* repaint */
145         keydef_repaint();
147         /* update key conflict flags */
148         check_key_bindings(cmds, NULL, 0);
151 static void
152 assign_new_key(WINDOW *w, int cmd_index, int key_index)
154         int key;
155         char *buf;
156         command_t cmd;
158         buf = g_strdup_printf(_("Enter new key for %s: "), cmds[cmd_index].name);
159         key = screen_getch(w, buf);
160         g_free(buf);
162         if (key==ERR) {
163                 screen_status_printf(_("Aborted"));
164                 return;
165         }
167         cmd = find_key_command(key, cmds);
168         if (cmd != CMD_NONE && cmd != cmds[cmd_index].command) {
169                 screen_status_printf(_("Error: key %s is already used for %s"),
170                                      key2str(key),
171                                      get_key_command_name(cmd));
172                 screen_bell();
173                 return;
174         }
176         cmds[cmd_index].keys[key_index] = key;
177         cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
179         screen_status_printf(_("Assigned %s to %s"),
180                              key2str(key),cmds[cmd_index].name);
181         check_subcmd_length();
183         /* repaint */
184         keydef_repaint();
186         /* update key conflict flags */
187         check_key_bindings(cmds, NULL, 0);
190 static const char *
191 list_callback(unsigned idx, bool *highlight, G_GNUC_UNUSED void *data)
193         static char buf[BUFSIZE];
195         if (subcmd < 0) {
196                 if (idx < (unsigned)command_list_length) {
197                         if (cmds[idx].flags & COMMAND_KEY_CONFLICT)
198                                 *highlight = true;
199                         return cmds[idx].name;
200                 } else if (idx == LIST_ITEM_APPLY())
201                         return LIST_ITEM_APPLY_LABEL;
202                 else if (idx == LIST_ITEM_SAVE())
203                         return LIST_ITEM_SAVE_LABEL;
204         } else {
205                 if (idx == 0)
206                         return "[..]";
207                 idx--;
208                 if (idx < MAX_COMMAND_KEYS && cmds[subcmd].keys[idx] > 0) {
209                         g_snprintf(buf,
210                                    BUFSIZE, "%d. %-20s   (%d) ",
211                                    idx + 1,
212                                    key2str(cmds[subcmd].keys[idx]),
213                                    cmds[subcmd].keys[idx]);
214                         return buf;
215                 } else if (idx == subcmd_addpos) {
216                         g_snprintf(buf, BUFSIZE, "%d. %s",
217                                    idx + 1, _("Add new key"));
218                         return buf;
219                 }
220         }
222         return NULL;
225 static void
226 keydef_init(WINDOW *w, int cols, int rows)
228         lw = list_window_init(w, cols, rows);
231 static void
232 keydef_resize(int cols, int rows)
234         lw->cols = cols;
235         lw->rows = rows;
238 static void
239 keydef_exit(void)
241         list_window_free(lw);
242         if (cmds)
243                 g_free(cmds);
244         cmds = NULL;
245         lw = NULL;
248 static void
249 keydef_open(G_GNUC_UNUSED mpdclient_t *c)
251         if (cmds == NULL) {
252                 command_definition_t *current_cmds = get_command_definitions();
253                 size_t cmds_size;
255                 command_list_length = 0;
256                 while (current_cmds[command_list_length].name)
257                         command_list_length++;
259                 cmds_size = (command_list_length+1) * sizeof(command_definition_t);
260                 cmds = g_malloc0(cmds_size);
261                 memcpy(cmds, current_cmds, cmds_size);
262                 command_list_length += STATIC_ITEMS;
263         }
265         subcmd = -1;
266         list_window_check_selected(lw, LIST_LENGTH());
269 static void
270 keydef_close(void)
272         if (cmds && !keybindings_changed()) {
273                 g_free(cmds);
274                 cmds = NULL;
275         } else
276                 screen_status_printf(_("Note: Did you forget to \'Apply\' your changes?"));
279 static const char *
280 keydef_title(char *str, size_t size)
282         if (subcmd < 0)
283                 return _("Edit key bindings");
285         g_snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name);
286         return str;
289 static void
290 keydef_paint(void)
292         list_window_paint(lw, list_callback, NULL);
295 static bool
296 keydef_cmd(G_GNUC_UNUSED mpdclient_t *c, command_t cmd)
298         int length = LIST_LENGTH();
300         if (subcmd >= 0)
301                 length = subcmd_length;
303         if (list_window_cmd(lw, length, cmd)) {
304                 keydef_repaint();
305                 return true;
306         }
308         switch(cmd) {
309         case CMD_PLAY:
310                 if (subcmd < 0) {
311                         if (lw->selected == LIST_ITEM_APPLY())
312                                 apply_keys();
313                         else if (lw->selected == LIST_ITEM_SAVE()) {
314                                 apply_keys();
315                                 save_keys();
316                         } else {
317                                 subcmd = lw->selected;
318                                 lw->selected=0;
319                                 check_subcmd_length();
321                                 keydef_repaint();
322                         }
323                 } else {
324                         if (lw->selected == 0) { /* up */
325                                 lw->selected = subcmd;
326                                 subcmd = -1;
328                                 keydef_repaint();
329                         } else
330                                 assign_new_key(screen.status_window.w,
331                                                subcmd,
332                                                lw->selected - STATIC_SUB_ITEMS);
333                 }
334                 return true;
335         case CMD_DELETE:
336                 if (subcmd >= 0 && lw->selected >= STATIC_SUB_ITEMS)
337                         delete_key(subcmd, lw->selected - STATIC_SUB_ITEMS);
338                 return true;
339                 break;
340         case CMD_SAVE_PLAYLIST:
341                 apply_keys();
342                 save_keys();
343                 break;
344         case CMD_LIST_FIND:
345         case CMD_LIST_RFIND:
346         case CMD_LIST_FIND_NEXT:
347         case CMD_LIST_RFIND_NEXT:
348                 screen_find(lw, length,
349                             cmd, list_callback, NULL);
350                 keydef_repaint();
351                 return true;
353         default:
354                 break;
355         }
357         return false;
360 const struct screen_functions screen_keydef = {
361         .init = keydef_init,
362         .exit = keydef_exit,
363         .open = keydef_open,
364         .close = keydef_close,
365         .resize = keydef_resize,
366         .paint = keydef_paint,
367         .cmd = keydef_cmd,
368         .get_title = keydef_title,
369 };