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 <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
36 #define BUFSIZE 256
38 #define LIST_ITEM_APPLY() ((unsigned)command_list_length)
39 #define LIST_ITEM_SAVE() (LIST_ITEM_APPLY()+1)
40 #define LIST_LENGTH() (LIST_ITEM_SAVE()+1)
42 #define LIST_ITEM_SAVE_LABEL _("===> Apply & Save key bindings ")
43 #define LIST_ITEM_APPLY_LABEL _("===> Apply key bindings ")
46 static struct list_window *lw;
47 static unsigned command_list_length = 0;
48 static command_definition_t *cmds = NULL;
50 static int subcmd = -1;
51 static unsigned subcmd_length = 0;
52 static unsigned subcmd_addpos = 0;
54 static int
55 keybindings_changed(void)
56 {
57 command_definition_t *orginal_cmds = get_command_definitions();
58 size_t size = command_list_length * sizeof(command_definition_t);
60 return memcmp(orginal_cmds, cmds, size);
61 }
63 static void
64 apply_keys(void)
65 {
66 if (keybindings_changed()) {
67 command_definition_t *orginal_cmds = get_command_definitions();
68 size_t size = command_list_length * sizeof(command_definition_t);
70 memcpy(orginal_cmds, cmds, size);
71 screen_status_printf(_("You have new key bindings"));
72 } else
73 screen_status_printf(_("Keybindings unchanged."));
74 }
76 static int
77 save_keys(void)
78 {
79 FILE *f;
80 char *filename;
82 if (check_user_conf_dir()) {
83 screen_status_printf(_("Error: Unable to create directory ~/.ncmpc - %s"),
84 strerror(errno));
85 screen_bell();
86 return -1;
87 }
89 filename = get_user_key_binding_filename();
91 if ((f = fopen(filename,"w")) == NULL) {
92 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
93 screen_bell();
94 g_free(filename);
95 return -1;
96 }
98 if (write_key_bindings(f, KEYDEF_WRITE_HEADER))
99 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
100 else
101 screen_status_printf(_("Wrote %s"), filename);
103 g_free(filename);
104 return fclose(f);
105 }
107 static void
108 check_subcmd_length(void)
109 {
110 subcmd_length = 0;
111 while (subcmd_length < MAX_COMMAND_KEYS &&
112 cmds[subcmd].keys[subcmd_length] > 0)
113 ++subcmd_length;
115 if (subcmd_length < MAX_COMMAND_KEYS) {
116 subcmd_addpos = subcmd_length;
117 subcmd_length++;
118 } else
119 subcmd_addpos = 0;
120 subcmd_length += STATIC_SUB_ITEMS;
121 list_window_set_length(lw, subcmd_length);
122 }
124 static void
125 keydef_paint(void);
127 static void
128 keydef_repaint(void)
129 {
130 keydef_paint();
131 wrefresh(lw->w);
132 }
134 static void
135 delete_key(int cmd_index, int key_index)
136 {
137 int i = key_index+1;
139 screen_status_printf(_("Deleted"));
140 while (i < MAX_COMMAND_KEYS && cmds[cmd_index].keys[i])
141 cmds[cmd_index].keys[key_index++] = cmds[cmd_index].keys[i++];
142 cmds[cmd_index].keys[key_index] = 0;
143 cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
144 check_subcmd_length();
146 /* repaint */
147 keydef_repaint();
149 /* update key conflict flags */
150 check_key_bindings(cmds, NULL, 0);
151 }
153 static void
154 assign_new_key(int cmd_index, int key_index)
155 {
156 int key;
157 char *buf;
158 command_t cmd;
160 buf = g_strdup_printf(_("Enter new key for %s: "), cmds[cmd_index].name);
161 key = screen_getch(buf);
162 g_free(buf);
164 if (key==ERR) {
165 screen_status_printf(_("Aborted"));
166 return;
167 }
169 cmd = find_key_command(key, cmds);
170 if (cmd != CMD_NONE && cmd != cmds[cmd_index].command) {
171 screen_status_printf(_("Error: key %s is already used for %s"),
172 key2str(key),
173 get_key_command_name(cmd));
174 screen_bell();
175 return;
176 }
178 cmds[cmd_index].keys[key_index] = key;
179 cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
181 screen_status_printf(_("Assigned %s to %s"),
182 key2str(key),cmds[cmd_index].name);
183 check_subcmd_length();
185 /* repaint */
186 keydef_repaint();
188 /* update key conflict flags */
189 check_key_bindings(cmds, NULL, 0);
190 }
192 static const char *
193 list_callback(unsigned idx, bool *highlight, G_GNUC_UNUSED char** sc, G_GNUC_UNUSED void *data)
194 {
195 static char buf[BUFSIZE];
197 if (subcmd < 0) {
198 if (idx == LIST_ITEM_APPLY())
199 return LIST_ITEM_APPLY_LABEL;
200 else if (idx == LIST_ITEM_SAVE())
201 return LIST_ITEM_SAVE_LABEL;
203 assert(idx < (unsigned)command_list_length);
205 if (cmds[idx].flags & COMMAND_KEY_CONFLICT)
206 *highlight = true;
207 return cmds[idx].name;
208 } else {
209 if (idx == 0)
210 return "[..]";
211 idx--;
212 if (idx == subcmd_addpos) {
213 g_snprintf(buf, BUFSIZE, "%d. %s",
214 idx + 1, _("Add new key"));
215 return buf;
216 }
218 assert(idx < MAX_COMMAND_KEYS && cmds[subcmd].keys[idx] > 0);
220 g_snprintf(buf,
221 BUFSIZE, "%d. %-20s (%d) ",
222 idx + 1,
223 key2str(cmds[subcmd].keys[idx]),
224 cmds[subcmd].keys[idx]);
225 return buf;
226 }
227 }
229 static void
230 keydef_init(WINDOW *w, int cols, int rows)
231 {
232 lw = list_window_init(w, cols, rows);
233 }
235 static void
236 keydef_resize(int cols, int rows)
237 {
238 lw->cols = cols;
239 lw->rows = rows;
240 }
242 static void
243 keydef_exit(void)
244 {
245 list_window_free(lw);
246 if (cmds)
247 g_free(cmds);
248 cmds = NULL;
249 lw = NULL;
250 }
252 static void
253 keydef_open(G_GNUC_UNUSED struct mpdclient *c)
254 {
255 if (cmds == NULL) {
256 command_definition_t *current_cmds = get_command_definitions();
257 size_t cmds_size;
259 command_list_length = 0;
260 while (current_cmds[command_list_length].name)
261 command_list_length++;
263 cmds_size = (command_list_length+1) * sizeof(command_definition_t);
264 cmds = g_malloc0(cmds_size);
265 memcpy(cmds, current_cmds, cmds_size);
266 command_list_length += STATIC_ITEMS;
267 }
269 subcmd = -1;
270 list_window_set_length(lw, LIST_LENGTH());
271 }
273 static void
274 keydef_close(void)
275 {
276 if (cmds && !keybindings_changed()) {
277 g_free(cmds);
278 cmds = NULL;
279 } else
280 screen_status_printf(_("Note: Did you forget to \'Apply\' your changes?"));
281 }
283 static const char *
284 keydef_title(char *str, size_t size)
285 {
286 if (subcmd < 0)
287 return _("Edit key bindings");
289 g_snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name);
290 return str;
291 }
293 static void
294 keydef_paint(void)
295 {
296 list_window_paint(lw, list_callback, NULL);
297 }
299 static bool
300 keydef_cmd(G_GNUC_UNUSED struct mpdclient *c, command_t cmd)
301 {
302 if (cmd == CMD_LIST_RANGE_SELECT)
303 return false;
305 if (list_window_cmd(lw, cmd)) {
306 keydef_repaint();
307 return true;
308 }
310 switch(cmd) {
311 case CMD_PLAY:
312 if (subcmd < 0) {
313 if (lw->selected == LIST_ITEM_APPLY())
314 apply_keys();
315 else if (lw->selected == LIST_ITEM_SAVE()) {
316 apply_keys();
317 save_keys();
318 } else {
319 subcmd = lw->selected;
320 list_window_reset(lw);
321 check_subcmd_length();
323 keydef_repaint();
324 }
325 } else {
326 if (lw->selected == 0) { /* up */
327 list_window_set_length(lw, LIST_LENGTH());
328 list_window_set_cursor(lw, subcmd);
329 subcmd = -1;
331 keydef_repaint();
332 } else
333 assign_new_key(subcmd,
334 lw->selected - STATIC_SUB_ITEMS);
335 }
336 return true;
337 case CMD_GO_PARENT_DIRECTORY:
338 if (subcmd >=0) {
339 list_window_set_length(lw, LIST_LENGTH());
340 list_window_set_cursor(lw, subcmd);
341 subcmd = -1;
343 keydef_repaint();
344 }
345 break;
346 case CMD_DELETE:
347 if (subcmd >= 0 && lw->selected >= STATIC_SUB_ITEMS)
348 delete_key(subcmd, lw->selected - STATIC_SUB_ITEMS);
349 return true;
350 break;
351 case CMD_SAVE_PLAYLIST:
352 apply_keys();
353 save_keys();
354 break;
355 case CMD_LIST_FIND:
356 case CMD_LIST_RFIND:
357 case CMD_LIST_FIND_NEXT:
358 case CMD_LIST_RFIND_NEXT:
359 screen_find(lw, cmd, list_callback, NULL);
360 keydef_repaint();
361 return true;
363 default:
364 break;
365 }
367 return false;
368 }
370 const struct screen_functions screen_keydef = {
371 .init = keydef_init,
372 .exit = keydef_exit,
373 .open = keydef_open,
374 .close = keydef_close,
375 .resize = keydef_resize,
376 .paint = keydef_paint,
377 .cmd = keydef_cmd,
378 .get_title = keydef_title,
379 };