1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
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 "screen_keydef.h"
21 #include "screen_interface.h"
22 #include "screen_message.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 <errno.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 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 static void
134 delete_key(int cmd_index, int key_index)
135 {
136 int i = key_index+1;
138 screen_status_printf(_("Deleted"));
139 while (i < MAX_COMMAND_KEYS && cmds[cmd_index].keys[i])
140 cmds[cmd_index].keys[key_index++] = cmds[cmd_index].keys[i++];
141 cmds[cmd_index].keys[key_index] = 0;
142 cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
143 check_subcmd_length();
145 /* repaint */
146 keydef_repaint();
148 /* update key conflict flags */
149 check_key_bindings(cmds, NULL, 0);
150 }
152 static void
153 assign_new_key(int cmd_index, int key_index)
154 {
155 int key;
156 char *buf;
157 command_t cmd;
159 buf = g_strdup_printf(_("Enter new key for %s: "), cmds[cmd_index].name);
160 key = screen_getch(buf);
161 g_free(buf);
163 if (key==ERR) {
164 screen_status_printf(_("Aborted"));
165 return;
166 }
168 cmd = find_key_command(key, cmds);
169 if (cmd != CMD_NONE && cmd != cmds[cmd_index].command) {
170 screen_status_printf(_("Error: key %s is already used for %s"),
171 key2str(key),
172 get_key_command_name(cmd));
173 screen_bell();
174 return;
175 }
177 cmds[cmd_index].keys[key_index] = key;
178 cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
180 screen_status_printf(_("Assigned %s to %s"),
181 key2str(key),cmds[cmd_index].name);
182 check_subcmd_length();
184 /* repaint */
185 keydef_repaint();
187 /* update key conflict flags */
188 check_key_bindings(cmds, NULL, 0);
189 }
191 static const char *
192 list_callback(unsigned idx, bool *highlight, G_GNUC_UNUSED char** sc, G_GNUC_UNUSED void *data)
193 {
194 static char buf[BUFSIZE];
196 if (subcmd < 0) {
197 if (idx < (unsigned)command_list_length) {
198 if (cmds[idx].flags & COMMAND_KEY_CONFLICT)
199 *highlight = true;
200 return cmds[idx].name;
201 } else if (idx == LIST_ITEM_APPLY())
202 return LIST_ITEM_APPLY_LABEL;
203 else if (idx == LIST_ITEM_SAVE())
204 return LIST_ITEM_SAVE_LABEL;
205 } else {
206 if (idx == 0)
207 return "[..]";
208 idx--;
209 if (idx < MAX_COMMAND_KEYS && cmds[subcmd].keys[idx] > 0) {
210 g_snprintf(buf,
211 BUFSIZE, "%d. %-20s (%d) ",
212 idx + 1,
213 key2str(cmds[subcmd].keys[idx]),
214 cmds[subcmd].keys[idx]);
215 return buf;
216 } else if (idx == subcmd_addpos) {
217 g_snprintf(buf, BUFSIZE, "%d. %s",
218 idx + 1, _("Add new key"));
219 return buf;
220 }
221 }
223 return NULL;
224 }
226 static void
227 keydef_init(WINDOW *w, int cols, int rows)
228 {
229 lw = list_window_init(w, cols, rows);
230 }
232 static void
233 keydef_resize(int cols, int rows)
234 {
235 lw->cols = cols;
236 lw->rows = rows;
237 }
239 static void
240 keydef_exit(void)
241 {
242 list_window_free(lw);
243 if (cmds)
244 g_free(cmds);
245 cmds = NULL;
246 lw = NULL;
247 }
249 static void
250 keydef_open(G_GNUC_UNUSED struct mpdclient *c)
251 {
252 if (cmds == NULL) {
253 command_definition_t *current_cmds = get_command_definitions();
254 size_t cmds_size;
256 command_list_length = 0;
257 while (current_cmds[command_list_length].name)
258 command_list_length++;
260 cmds_size = (command_list_length+1) * sizeof(command_definition_t);
261 cmds = g_malloc0(cmds_size);
262 memcpy(cmds, current_cmds, cmds_size);
263 command_list_length += STATIC_ITEMS;
264 }
266 subcmd = -1;
267 list_window_set_length(lw, LIST_LENGTH());
268 }
270 static void
271 keydef_close(void)
272 {
273 if (cmds && !keybindings_changed()) {
274 g_free(cmds);
275 cmds = NULL;
276 } else
277 screen_status_printf(_("Note: Did you forget to \'Apply\' your changes?"));
278 }
280 static const char *
281 keydef_title(char *str, size_t size)
282 {
283 if (subcmd < 0)
284 return _("Edit key bindings");
286 g_snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name);
287 return str;
288 }
290 static void
291 keydef_paint(void)
292 {
293 list_window_paint(lw, list_callback, NULL);
294 }
296 static bool
297 keydef_cmd(G_GNUC_UNUSED struct mpdclient *c, command_t cmd)
298 {
299 if (cmd == CMD_LIST_RANGE_SELECT)
300 return false;
302 if (list_window_cmd(lw, cmd)) {
303 keydef_repaint();
304 return true;
305 }
307 switch(cmd) {
308 case CMD_PLAY:
309 if (subcmd < 0) {
310 if (lw->selected == LIST_ITEM_APPLY())
311 apply_keys();
312 else if (lw->selected == LIST_ITEM_SAVE()) {
313 apply_keys();
314 save_keys();
315 } else {
316 subcmd = lw->selected;
317 list_window_reset(lw);
318 check_subcmd_length();
320 keydef_repaint();
321 }
322 } else {
323 if (lw->selected == 0) { /* up */
324 list_window_set_length(lw, LIST_LENGTH());
325 list_window_set_cursor(lw, subcmd);
326 subcmd = -1;
328 keydef_repaint();
329 } else
330 assign_new_key(subcmd,
331 lw->selected - STATIC_SUB_ITEMS);
332 }
333 return true;
334 case CMD_GO_PARENT_DIRECTORY:
335 if (subcmd >=0) {
336 list_window_set_length(lw, LIST_LENGTH());
337 list_window_set_cursor(lw, subcmd);
338 subcmd = -1;
340 keydef_repaint();
341 }
342 break;
343 case CMD_DELETE:
344 if (subcmd >= 0 && lw->selected >= STATIC_SUB_ITEMS)
345 delete_key(subcmd, lw->selected - STATIC_SUB_ITEMS);
346 return true;
347 break;
348 case CMD_SAVE_PLAYLIST:
349 apply_keys();
350 save_keys();
351 break;
352 case CMD_LIST_FIND:
353 case CMD_LIST_RFIND:
354 case CMD_LIST_FIND_NEXT:
355 case CMD_LIST_RFIND_NEXT:
356 screen_find(lw, cmd, list_callback, NULL);
357 keydef_repaint();
358 return true;
360 default:
361 break;
362 }
364 return false;
365 }
367 const struct screen_functions screen_keydef = {
368 .init = keydef_init,
369 .exit = keydef_exit,
370 .open = keydef_open,
371 .close = keydef_close,
372 .resize = keydef_resize,
373 .paint = keydef_paint,
374 .cmd = keydef_cmd,
375 .get_title = keydef_title,
376 };