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 static struct list_window *lw;
36 static command_definition_t *cmds = NULL;
38 /** the number of commands */
39 static unsigned command_n_commands = 0;
41 /**
42 * the position of the "apply" item. It's the same as command_n_commands,
43 * because array subscripts start at 0, while numbers of items start at 1.
44 */
45 #define command_item_apply (command_n_commands)
47 /** the position of the "apply and save" item */
48 #define command_item_save (command_item_apply + 1)
50 /** the number of items on the "command" view */
51 #define command_length (command_item_save + 1)
54 /**
55 * The command being edited, represented by a array subscript to @cmds, or -1,
56 * if no command is being edited
57 */
58 static int subcmd = -1;
60 /** The number of keys assigned to the current command */
61 static unsigned subcmd_n_keys = 0;
63 /** The position of the up ("[..]") item */
64 #define subcmd_item_up 0
66 /** The position of the "add a key" item */
67 #define subcmd_item_add (subcmd_n_keys + 1)
69 /** The number of items in the list_window, if there's a command being edited */
70 #define subcmd_length (subcmd_item_add + 1)
72 /** Check whether a given item is a key */
73 #define subcmd_item_is_key(i) \
74 ((i) > subcmd_item_up && (i) < subcmd_item_add)
76 /**
77 * Convert an item id (as in lw->selected) into a "key id", which is an array
78 * subscript to cmds[subcmd].keys.
79 */
80 #define subcmd_item_to_key_id(i) ((i) - 1)
83 static int
84 keybindings_changed(void)
85 {
86 command_definition_t *orginal_cmds = get_command_definitions();
87 size_t size = command_n_commands * sizeof(command_definition_t);
89 return memcmp(orginal_cmds, cmds, size);
90 }
92 static void
93 apply_keys(void)
94 {
95 if (keybindings_changed()) {
96 command_definition_t *orginal_cmds = get_command_definitions();
97 size_t size = command_n_commands * sizeof(command_definition_t);
99 memcpy(orginal_cmds, cmds, size);
100 screen_status_printf(_("You have new key bindings"));
101 } else
102 screen_status_printf(_("Keybindings unchanged."));
103 }
105 static int
106 save_keys(void)
107 {
108 FILE *f;
109 char *filename;
111 if (check_user_conf_dir()) {
112 screen_status_printf(_("Error: Unable to create directory ~/.ncmpc - %s"),
113 strerror(errno));
114 screen_bell();
115 return -1;
116 }
118 filename = get_user_key_binding_filename();
120 if ((f = fopen(filename,"w")) == NULL) {
121 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
122 screen_bell();
123 g_free(filename);
124 return -1;
125 }
127 if (write_key_bindings(f, KEYDEF_WRITE_HEADER))
128 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
129 else
130 screen_status_printf(_("Wrote %s"), filename);
132 g_free(filename);
133 return fclose(f);
134 }
136 /* TODO: rename to check_n_keys / subcmd_count_keys? */
137 static void
138 check_subcmd_length(void)
139 {
140 unsigned i;
142 /* this loops counts the continous valid keys at the start of the the keys
143 array, so make sure you don't have gaps */
144 for (i = 0; i < MAX_COMMAND_KEYS; i++)
145 if (cmds[subcmd].keys[i] == 0)
146 break;
147 subcmd_n_keys = i;
149 list_window_set_length(lw, subcmd_length);
150 }
152 static void
153 keydef_paint(void);
155 static void
156 keydef_repaint(void)
157 {
158 keydef_paint();
159 wrefresh(lw->w);
160 }
162 /**
163 * Delete a key from a given command's definition
164 * @param cmd_index the command
165 * @param key_index the key (see below)
166 */
167 static void
168 delete_key(int cmd_index, int key_index)
169 {
170 /* shift the keys to close the gap that appeared */
171 int i = key_index+1;
172 while (i < MAX_COMMAND_KEYS && cmds[cmd_index].keys[i])
173 cmds[cmd_index].keys[key_index++] = cmds[cmd_index].keys[i++];
175 /* As key_index now holds the index of the last key slot that contained
176 a key, we use it to empty this slot, because this key has been copied
177 to the previous slot in the loop above */
178 cmds[cmd_index].keys[key_index] = 0;
180 cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
181 check_subcmd_length();
183 screen_status_printf(_("Deleted"));
185 /* repaint */
186 keydef_repaint();
188 /* update key conflict flags */
189 check_key_bindings(cmds, NULL, 0);
190 }
192 static void
193 assign_new_key(int cmd_index, int key_index)
194 {
195 int key;
196 char *buf;
197 command_t cmd;
199 buf = g_strdup_printf(_("Enter new key for %s: "), cmds[cmd_index].name);
200 key = screen_getch(buf);
201 g_free(buf);
203 if (key==ERR) {
204 screen_status_printf(_("Aborted"));
205 return;
206 }
208 cmd = find_key_command(key, cmds);
209 if (cmd != CMD_NONE && cmd != cmds[cmd_index].command) {
210 screen_status_printf(_("Error: key %s is already used for %s"),
211 key2str(key),
212 get_key_command_name(cmd));
213 screen_bell();
214 return;
215 }
217 cmds[cmd_index].keys[key_index] = key;
218 cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
220 screen_status_printf(_("Assigned %s to %s"),
221 key2str(key),cmds[cmd_index].name);
222 check_subcmd_length();
224 /* repaint */
225 keydef_repaint();
227 /* update key conflict flags */
228 check_key_bindings(cmds, NULL, 0);
229 }
231 static const char *
232 list_callback(unsigned idx, G_GNUC_UNUSED void *data)
233 {
234 static char buf[256];
236 if (subcmd == -1) {
237 if (idx == command_item_apply)
238 return _("===> Apply key bindings ");
239 if (idx == command_item_save)
240 return _("===> Apply & Save key bindings ");
242 assert(idx < (unsigned) command_n_commands);
244 return cmds[idx].name;
245 } else {
246 if (idx == subcmd_item_up)
247 return "[..]";
249 if (idx == subcmd_item_add) {
250 g_snprintf(buf, sizeof(buf), "%d. %s",
251 idx, _("Add new key"));
252 return buf;
253 }
255 assert(subcmd_item_is_key(idx));
257 g_snprintf(buf, sizeof(buf),
258 "%d. %-20s (%d) ", idx,
259 key2str(cmds[subcmd].keys[subcmd_item_to_key_id(idx)]),
260 cmds[subcmd].keys[subcmd_item_to_key_id(idx)]);
261 return buf;
262 }
263 }
265 static void
266 keydef_init(WINDOW *w, int cols, int rows)
267 {
268 lw = list_window_init(w, cols, rows);
269 }
271 static void
272 keydef_resize(int cols, int rows)
273 {
274 list_window_resize(lw, cols, rows);
275 }
277 static void
278 keydef_exit(void)
279 {
280 list_window_free(lw);
281 if (cmds)
282 g_free(cmds);
283 cmds = NULL;
284 lw = NULL;
285 }
287 static void
288 keydef_open(G_GNUC_UNUSED struct mpdclient *c)
289 {
290 if (cmds == NULL) {
291 command_definition_t *current_cmds = get_command_definitions();
292 size_t cmds_size;
294 command_n_commands = 0;
295 while (current_cmds[command_n_commands].name)
296 command_n_commands++;
298 /* +1 for the terminator element */
299 cmds_size = (command_n_commands + 1) * sizeof(command_definition_t);
300 cmds = g_malloc0(cmds_size);
301 memcpy(cmds, current_cmds, cmds_size);
302 }
304 subcmd = -1;
305 list_window_set_length(lw, command_length);
306 }
308 static void
309 keydef_close(void)
310 {
311 if (cmds && !keybindings_changed()) {
312 g_free(cmds);
313 cmds = NULL;
314 } else
315 screen_status_printf(_("Note: Did you forget to \'Apply\' your changes?"));
316 }
318 static const char *
319 keydef_title(char *str, size_t size)
320 {
321 if (subcmd == -1)
322 return _("Edit key bindings");
324 g_snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name);
325 return str;
326 }
328 static void
329 keydef_paint(void)
330 {
331 list_window_paint(lw, list_callback, NULL);
332 }
334 static bool
335 keydef_cmd(G_GNUC_UNUSED struct mpdclient *c, command_t cmd)
336 {
337 if (cmd == CMD_LIST_RANGE_SELECT)
338 return false;
340 if (list_window_cmd(lw, cmd)) {
341 keydef_repaint();
342 return true;
343 }
345 switch(cmd) {
346 case CMD_PLAY:
347 if (subcmd == -1) {
348 if (lw->selected == command_item_apply) {
349 apply_keys();
350 } else if (lw->selected == command_item_save) {
351 apply_keys();
352 save_keys();
353 } else {
354 subcmd = lw->selected;
355 list_window_reset(lw);
356 check_subcmd_length();
358 keydef_repaint();
359 }
360 } else {
361 if (lw->selected == subcmd_item_up) {
362 list_window_set_length(lw, command_length);
363 list_window_set_cursor(lw, subcmd);
364 subcmd = -1;
366 keydef_repaint();
367 } else {
368 /* TODO: subcmd_item_add should be handled
369 separately, just for clarity */
370 assign_new_key(subcmd, subcmd_item_to_key_id(lw->selected));
371 }
372 }
373 return true;
374 case CMD_GO_PARENT_DIRECTORY:
375 if (subcmd != -1) {
376 list_window_set_length(lw, command_length);
377 list_window_set_cursor(lw, subcmd);
378 subcmd = -1;
380 keydef_repaint();
381 }
382 return true;
383 case CMD_DELETE:
384 if (subcmd != -1 && subcmd_item_is_key(lw->selected))
385 delete_key(subcmd, subcmd_item_to_key_id(lw->selected));
387 return true;
388 case CMD_SAVE_PLAYLIST:
389 apply_keys();
390 save_keys();
391 return true;
392 case CMD_LIST_FIND:
393 case CMD_LIST_RFIND:
394 case CMD_LIST_FIND_NEXT:
395 case CMD_LIST_RFIND_NEXT:
396 screen_find(lw, cmd, list_callback, NULL);
397 keydef_repaint();
398 return true;
400 default:
401 return false;
402 }
404 /* unreachable */
405 assert(0);
406 return false;
407 }
409 const struct screen_functions screen_keydef = {
410 .init = keydef_init,
411 .exit = keydef_exit,
412 .open = keydef_open,
413 .close = keydef_close,
414 .resize = keydef_resize,
415 .paint = keydef_paint,
416 .cmd = keydef_cmd,
417 .get_title = keydef_title,
418 };