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)
41 #define LIST_ITEM_SAVE_LABEL _("===> Apply & Save key bindings ")
42 #define LIST_ITEM_APPLY_LABEL _("===> Apply key bindings ")
45 static struct list_window *lw;
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 directory ~/.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);
104 }
106 static void
107 check_subcmd_length(void)
108 {
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;
120 list_window_set_length(lw, subcmd_length);
121 }
123 static void
124 keydef_paint(void);
126 static void
127 keydef_repaint(void)
128 {
129 keydef_paint();
130 wrefresh(lw->w);
131 }
133 /**
134 * Delete a key from a given command's definition
135 * @param cmd_index the command
136 * @param key_index the key (see below)
137 */
138 static void
139 delete_key(int cmd_index, int key_index)
140 {
141 /* shift the keys to close the gap that appeared */
142 int i = key_index+1;
143 while (i < MAX_COMMAND_KEYS && cmds[cmd_index].keys[i])
144 cmds[cmd_index].keys[key_index++] = cmds[cmd_index].keys[i++];
146 /* As key_index now holds the index of the last key slot that contained
147 a key, we use it to empty this slot, because this key has been copied
148 to the previous slot in the loop above */
149 cmds[cmd_index].keys[key_index] = 0;
151 cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
152 check_subcmd_length();
154 screen_status_printf(_("Deleted"));
156 /* repaint */
157 keydef_repaint();
159 /* update key conflict flags */
160 check_key_bindings(cmds, NULL, 0);
161 }
163 static void
164 assign_new_key(int cmd_index, int key_index)
165 {
166 int key;
167 char *buf;
168 command_t cmd;
170 buf = g_strdup_printf(_("Enter new key for %s: "), cmds[cmd_index].name);
171 key = screen_getch(buf);
172 g_free(buf);
174 if (key==ERR) {
175 screen_status_printf(_("Aborted"));
176 return;
177 }
179 cmd = find_key_command(key, cmds);
180 if (cmd != CMD_NONE && cmd != cmds[cmd_index].command) {
181 screen_status_printf(_("Error: key %s is already used for %s"),
182 key2str(key),
183 get_key_command_name(cmd));
184 screen_bell();
185 return;
186 }
188 cmds[cmd_index].keys[key_index] = key;
189 cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
191 screen_status_printf(_("Assigned %s to %s"),
192 key2str(key),cmds[cmd_index].name);
193 check_subcmd_length();
195 /* repaint */
196 keydef_repaint();
198 /* update key conflict flags */
199 check_key_bindings(cmds, NULL, 0);
200 }
202 static const char *
203 list_callback(unsigned idx, G_GNUC_UNUSED void *data)
204 {
205 static char buf[256];
207 if (subcmd < 0) {
208 if (idx == LIST_ITEM_APPLY())
209 return LIST_ITEM_APPLY_LABEL;
210 else if (idx == LIST_ITEM_SAVE())
211 return LIST_ITEM_SAVE_LABEL;
213 assert(idx < (unsigned)command_list_length);
215 return cmds[idx].name;
216 } else {
217 if (idx == 0)
218 return "[..]";
219 idx--;
220 if (idx == subcmd_addpos) {
221 g_snprintf(buf, sizeof(buf), "%d. %s",
222 idx + 1, _("Add new key"));
223 return buf;
224 }
226 assert(idx < MAX_COMMAND_KEYS && cmds[subcmd].keys[idx] > 0);
228 g_snprintf(buf, sizeof(buf),
229 "%d. %-20s (%d) ",
230 idx + 1,
231 key2str(cmds[subcmd].keys[idx]),
232 cmds[subcmd].keys[idx]);
233 return buf;
234 }
235 }
237 static void
238 keydef_init(WINDOW *w, int cols, int rows)
239 {
240 lw = list_window_init(w, cols, rows);
241 }
243 static void
244 keydef_resize(int cols, int rows)
245 {
246 list_window_resize(lw, cols, rows);
247 }
249 static void
250 keydef_exit(void)
251 {
252 list_window_free(lw);
253 if (cmds)
254 g_free(cmds);
255 cmds = NULL;
256 lw = NULL;
257 }
259 static void
260 keydef_open(G_GNUC_UNUSED struct mpdclient *c)
261 {
262 if (cmds == NULL) {
263 command_definition_t *current_cmds = get_command_definitions();
264 size_t cmds_size;
266 command_list_length = 0;
267 while (current_cmds[command_list_length].name)
268 command_list_length++;
270 cmds_size = (command_list_length+1) * sizeof(command_definition_t);
271 cmds = g_malloc0(cmds_size);
272 memcpy(cmds, current_cmds, cmds_size);
273 command_list_length += STATIC_ITEMS;
274 }
276 subcmd = -1;
277 list_window_set_length(lw, LIST_LENGTH());
278 }
280 static void
281 keydef_close(void)
282 {
283 if (cmds && !keybindings_changed()) {
284 g_free(cmds);
285 cmds = NULL;
286 } else
287 screen_status_printf(_("Note: Did you forget to \'Apply\' your changes?"));
288 }
290 static const char *
291 keydef_title(char *str, size_t size)
292 {
293 if (subcmd < 0)
294 return _("Edit key bindings");
296 g_snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name);
297 return str;
298 }
300 static void
301 keydef_paint(void)
302 {
303 list_window_paint(lw, list_callback, NULL);
304 }
306 static bool
307 keydef_cmd(G_GNUC_UNUSED struct mpdclient *c, command_t cmd)
308 {
309 if (cmd == CMD_LIST_RANGE_SELECT)
310 return false;
312 if (list_window_cmd(lw, cmd)) {
313 keydef_repaint();
314 return true;
315 }
317 switch(cmd) {
318 case CMD_PLAY:
319 if (subcmd < 0) {
320 if (lw->selected == LIST_ITEM_APPLY())
321 apply_keys();
322 else if (lw->selected == LIST_ITEM_SAVE()) {
323 apply_keys();
324 save_keys();
325 } else {
326 subcmd = lw->selected;
327 list_window_reset(lw);
328 check_subcmd_length();
330 keydef_repaint();
331 }
332 } else {
333 if (lw->selected == 0) { /* up */
334 list_window_set_length(lw, LIST_LENGTH());
335 list_window_set_cursor(lw, subcmd);
336 subcmd = -1;
338 keydef_repaint();
339 } else
340 assign_new_key(subcmd,
341 lw->selected - STATIC_SUB_ITEMS);
342 }
343 return true;
344 case CMD_GO_PARENT_DIRECTORY:
345 if (subcmd >=0) {
346 list_window_set_length(lw, LIST_LENGTH());
347 list_window_set_cursor(lw, subcmd);
348 subcmd = -1;
350 keydef_repaint();
351 }
352 break;
353 case CMD_DELETE:
354 if (subcmd >= 0 && lw->selected >= STATIC_SUB_ITEMS)
355 delete_key(subcmd, lw->selected - STATIC_SUB_ITEMS);
356 return true;
357 break;
358 case CMD_SAVE_PLAYLIST:
359 apply_keys();
360 save_keys();
361 break;
362 case CMD_LIST_FIND:
363 case CMD_LIST_RFIND:
364 case CMD_LIST_FIND_NEXT:
365 case CMD_LIST_RFIND_NEXT:
366 screen_find(lw, cmd, list_callback, NULL);
367 keydef_repaint();
368 return true;
370 default:
371 break;
372 }
374 return false;
375 }
377 const struct screen_functions screen_keydef = {
378 .init = keydef_init,
379 .exit = keydef_exit,
380 .open = keydef_open,
381 .close = keydef_close,
382 .resize = keydef_resize,
383 .paint = keydef_paint,
384 .cmd = keydef_cmd,
385 .get_title = keydef_title,
386 };