d93757a9c67bd5ca64f744dc2b8c79cd395727af
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 "i18n.h"
24 #include "conf.h"
25 #include "screen.h"
26 #include "screen_utils.h"
28 #include <errno.h>
29 #include <string.h>
30 #include <glib.h>
32 #define STATIC_ITEMS 0
33 #define STATIC_SUB_ITEMS 1
34 #define BUFSIZE 256
36 #define LIST_ITEM_APPLY() ((unsigned)command_list_length)
37 #define LIST_ITEM_SAVE() (LIST_ITEM_APPLY()+1)
38 #define LIST_LENGTH() (LIST_ITEM_SAVE()+1)
40 #define LIST_ITEM_SAVE_LABEL _("===> Apply & Save key bindings ")
41 #define LIST_ITEM_APPLY_LABEL _("===> Apply key bindings ")
44 static list_window_t *lw = NULL;
45 static unsigned command_list_length = 0;
46 static command_definition_t *cmds = NULL;
48 static int subcmd = -1;
49 static unsigned subcmd_length = 0;
50 static unsigned subcmd_addpos = 0;
52 static int
53 keybindings_changed(void)
54 {
55 command_definition_t *orginal_cmds = get_command_definitions();
56 size_t size = command_list_length * sizeof(command_definition_t);
58 return memcmp(orginal_cmds, cmds, size);
59 }
61 static void
62 apply_keys(void)
63 {
64 if (keybindings_changed()) {
65 command_definition_t *orginal_cmds = get_command_definitions();
66 size_t size = command_list_length * sizeof(command_definition_t);
68 memcpy(orginal_cmds, cmds, size);
69 screen_status_printf(_("You have new key bindings"));
70 } else
71 screen_status_printf(_("Keybindings unchanged."));
72 }
74 static int
75 save_keys(void)
76 {
77 FILE *f;
78 char *filename;
80 if (check_user_conf_dir()) {
81 screen_status_printf(_("Error: Unable to create directory ~/.ncmpc - %s"),
82 strerror(errno));
83 screen_bell();
84 return -1;
85 }
87 filename = get_user_key_binding_filename();
89 if ((f = fopen(filename,"w")) == NULL) {
90 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
91 screen_bell();
92 g_free(filename);
93 return -1;
94 }
96 if (write_key_bindings(f, KEYDEF_WRITE_HEADER))
97 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
98 else
99 screen_status_printf(_("Wrote %s"), filename);
101 g_free(filename);
102 return fclose(f);
103 }
105 static void
106 check_subcmd_length(void)
107 {
108 subcmd_length = 0;
109 while (subcmd_length < MAX_COMMAND_KEYS &&
110 cmds[subcmd].keys[subcmd_length] > 0)
111 ++subcmd_length;
113 if (subcmd_length < MAX_COMMAND_KEYS) {
114 subcmd_addpos = subcmd_length;
115 subcmd_length++;
116 } else
117 subcmd_addpos = 0;
118 subcmd_length += STATIC_SUB_ITEMS;
119 }
121 static void
122 keydef_paint(void);
124 static void
125 keydef_repaint(void)
126 {
127 keydef_paint();
128 wrefresh(lw->w);
129 }
131 static void
132 delete_key(int cmd_index, int key_index)
133 {
134 int i = key_index+1;
136 screen_status_printf(_("Deleted"));
137 while (i < MAX_COMMAND_KEYS && cmds[cmd_index].keys[i])
138 cmds[cmd_index].keys[key_index++] = cmds[cmd_index].keys[i++];
139 cmds[cmd_index].keys[key_index] = 0;
140 cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
141 check_subcmd_length();
143 /* repaint */
144 keydef_repaint();
146 /* update key conflict flags */
147 check_key_bindings(cmds, NULL, 0);
148 }
150 static void
151 assign_new_key(int cmd_index, int key_index)
152 {
153 int key;
154 char *buf;
155 command_t cmd;
157 buf = g_strdup_printf(_("Enter new key for %s: "), cmds[cmd_index].name);
158 key = screen_getch(buf);
159 g_free(buf);
161 if (key==ERR) {
162 screen_status_printf(_("Aborted"));
163 return;
164 }
166 cmd = find_key_command(key, cmds);
167 if (cmd != CMD_NONE && cmd != cmds[cmd_index].command) {
168 screen_status_printf(_("Error: key %s is already used for %s"),
169 key2str(key),
170 get_key_command_name(cmd));
171 screen_bell();
172 return;
173 }
175 cmds[cmd_index].keys[key_index] = key;
176 cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
178 screen_status_printf(_("Assigned %s to %s"),
179 key2str(key),cmds[cmd_index].name);
180 check_subcmd_length();
182 /* repaint */
183 keydef_repaint();
185 /* update key conflict flags */
186 check_key_bindings(cmds, NULL, 0);
187 }
189 static const char *
190 list_callback(unsigned idx, bool *highlight, G_GNUC_UNUSED char** sc, G_GNUC_UNUSED void *data)
191 {
192 static char buf[BUFSIZE];
194 if (subcmd < 0) {
195 if (idx < (unsigned)command_list_length) {
196 if (cmds[idx].flags & COMMAND_KEY_CONFLICT)
197 *highlight = true;
198 return cmds[idx].name;
199 } else if (idx == LIST_ITEM_APPLY())
200 return LIST_ITEM_APPLY_LABEL;
201 else if (idx == LIST_ITEM_SAVE())
202 return LIST_ITEM_SAVE_LABEL;
203 } else {
204 if (idx == 0)
205 return "[..]";
206 idx--;
207 if (idx < MAX_COMMAND_KEYS && cmds[subcmd].keys[idx] > 0) {
208 g_snprintf(buf,
209 BUFSIZE, "%d. %-20s (%d) ",
210 idx + 1,
211 key2str(cmds[subcmd].keys[idx]),
212 cmds[subcmd].keys[idx]);
213 return buf;
214 } else if (idx == subcmd_addpos) {
215 g_snprintf(buf, BUFSIZE, "%d. %s",
216 idx + 1, _("Add new key"));
217 return buf;
218 }
219 }
221 return NULL;
222 }
224 static void
225 keydef_init(WINDOW *w, int cols, int rows)
226 {
227 lw = list_window_init(w, cols, rows);
228 }
230 static void
231 keydef_resize(int cols, int rows)
232 {
233 lw->cols = cols;
234 lw->rows = rows;
235 }
237 static void
238 keydef_exit(void)
239 {
240 list_window_free(lw);
241 if (cmds)
242 g_free(cmds);
243 cmds = NULL;
244 lw = NULL;
245 }
247 static void
248 keydef_open(G_GNUC_UNUSED struct mpdclient *c)
249 {
250 if (cmds == NULL) {
251 command_definition_t *current_cmds = get_command_definitions();
252 size_t cmds_size;
254 command_list_length = 0;
255 while (current_cmds[command_list_length].name)
256 command_list_length++;
258 cmds_size = (command_list_length+1) * sizeof(command_definition_t);
259 cmds = g_malloc0(cmds_size);
260 memcpy(cmds, current_cmds, cmds_size);
261 command_list_length += STATIC_ITEMS;
262 }
264 subcmd = -1;
265 list_window_check_selected(lw, LIST_LENGTH());
266 }
268 static void
269 keydef_close(void)
270 {
271 if (cmds && !keybindings_changed()) {
272 g_free(cmds);
273 cmds = NULL;
274 } else
275 screen_status_printf(_("Note: Did you forget to \'Apply\' your changes?"));
276 }
278 static const char *
279 keydef_title(char *str, size_t size)
280 {
281 if (subcmd < 0)
282 return _("Edit key bindings");
284 g_snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name);
285 return str;
286 }
288 static void
289 keydef_paint(void)
290 {
291 list_window_paint(lw, list_callback, NULL);
292 }
294 static bool
295 keydef_cmd(G_GNUC_UNUSED struct mpdclient *c, command_t cmd)
296 {
297 int length = LIST_LENGTH();
299 if (subcmd >= 0)
300 length = subcmd_length;
302 if (cmd == CMD_LIST_RANGE_SELECT)
303 return false;
305 if (list_window_cmd(lw, length, 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 lw->selected=0;
321 lw->selected_start = lw->selected;
322 lw->selected_end = lw->selected;
323 check_subcmd_length();
325 keydef_repaint();
326 }
327 } else {
328 if (lw->selected == 0) { /* up */
329 lw->selected = subcmd;
330 lw->selected_start = lw->selected;
331 lw->selected_end = lw->selected;
332 subcmd = -1;
334 keydef_repaint();
335 } else
336 assign_new_key(subcmd,
337 lw->selected - STATIC_SUB_ITEMS);
338 }
339 return true;
340 case CMD_GO_PARENT_DIRECTORY:
341 if (subcmd >=0) {
342 lw->selected = subcmd;
343 lw->selected_start = lw->selected;
344 lw->selected_end = lw->selected;
345 subcmd = -1;
347 keydef_repaint();
348 }
349 break;
350 case CMD_DELETE:
351 if (subcmd >= 0 && lw->selected >= STATIC_SUB_ITEMS)
352 delete_key(subcmd, lw->selected - STATIC_SUB_ITEMS);
353 return true;
354 break;
355 case CMD_SAVE_PLAYLIST:
356 apply_keys();
357 save_keys();
358 break;
359 case CMD_LIST_FIND:
360 case CMD_LIST_RFIND:
361 case CMD_LIST_FIND_NEXT:
362 case CMD_LIST_RFIND_NEXT:
363 screen_find(lw, length,
364 cmd, list_callback, NULL);
365 keydef_repaint();
366 return true;
368 default:
369 break;
370 }
372 return false;
373 }
375 const struct screen_functions screen_keydef = {
376 .init = keydef_init,
377 .exit = keydef_exit,
378 .open = keydef_open,
379 .close = keydef_close,
380 .resize = keydef_resize,
381 .paint = keydef_paint,
382 .cmd = keydef_cmd,
383 .get_title = keydef_title,
384 };