1ffee6b1659ad1fe01eb0ea86181992693ba8756
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 "i18n.h"
21 #include "mpdclient.h"
22 #include "options.h"
23 #include "conf.h"
24 #include "command.h"
25 #include "screen.h"
26 #include "screen_utils.h"
28 #include <errno.h>
29 #include <stdlib.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 list_window_t *lw = NULL;
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 }
122 static void
123 keydef_paint(void);
125 static void
126 keydef_repaint(void)
127 {
128 keydef_paint();
129 wrefresh(lw->w);
130 }
132 static void
133 delete_key(int cmd_index, int key_index)
134 {
135 int i = key_index+1;
137 screen_status_printf(_("Deleted"));
138 while (i < MAX_COMMAND_KEYS && cmds[cmd_index].keys[i])
139 cmds[cmd_index].keys[key_index++] = cmds[cmd_index].keys[i++];
140 cmds[cmd_index].keys[key_index] = 0;
141 cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
142 check_subcmd_length();
144 /* repaint */
145 keydef_repaint();
147 /* update key conflict flags */
148 check_key_bindings(cmds, NULL, 0);
149 }
151 static void
152 assign_new_key(WINDOW *w, int cmd_index, int key_index)
153 {
154 int key;
155 char *buf;
156 command_t cmd;
158 buf = g_strdup_printf(_("Enter new key for %s: "), cmds[cmd_index].name);
159 key = screen_getch(w, buf);
160 g_free(buf);
162 if (key==ERR) {
163 screen_status_printf(_("Aborted"));
164 return;
165 }
167 cmd = find_key_command(key, cmds);
168 if (cmd != CMD_NONE && cmd != cmds[cmd_index].command) {
169 screen_status_printf(_("Error: key %s is already used for %s"),
170 key2str(key),
171 get_key_command_name(cmd));
172 screen_bell();
173 return;
174 }
176 cmds[cmd_index].keys[key_index] = key;
177 cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
179 screen_status_printf(_("Assigned %s to %s"),
180 key2str(key),cmds[cmd_index].name);
181 check_subcmd_length();
183 /* repaint */
184 keydef_repaint();
186 /* update key conflict flags */
187 check_key_bindings(cmds, NULL, 0);
188 }
190 static const char *
191 list_callback(unsigned idx, bool *highlight, G_GNUC_UNUSED void *data)
192 {
193 static char buf[BUFSIZE];
195 if (subcmd < 0) {
196 if (idx < (unsigned)command_list_length) {
197 if (cmds[idx].flags & COMMAND_KEY_CONFLICT)
198 *highlight = true;
199 return cmds[idx].name;
200 } else if (idx == LIST_ITEM_APPLY())
201 return LIST_ITEM_APPLY_LABEL;
202 else if (idx == LIST_ITEM_SAVE())
203 return LIST_ITEM_SAVE_LABEL;
204 } else {
205 if (idx == 0)
206 return "[..]";
207 idx--;
208 if (idx < MAX_COMMAND_KEYS && cmds[subcmd].keys[idx] > 0) {
209 g_snprintf(buf,
210 BUFSIZE, "%d. %-20s (%d) ",
211 idx + 1,
212 key2str(cmds[subcmd].keys[idx]),
213 cmds[subcmd].keys[idx]);
214 return buf;
215 } else if (idx == subcmd_addpos) {
216 g_snprintf(buf, BUFSIZE, "%d. %s",
217 idx + 1, _("Add new key"));
218 return buf;
219 }
220 }
222 return NULL;
223 }
225 static void
226 keydef_init(WINDOW *w, int cols, int rows)
227 {
228 lw = list_window_init(w, cols, rows);
229 }
231 static void
232 keydef_resize(int cols, int rows)
233 {
234 lw->cols = cols;
235 lw->rows = rows;
236 }
238 static void
239 keydef_exit(void)
240 {
241 list_window_free(lw);
242 if (cmds)
243 g_free(cmds);
244 cmds = NULL;
245 lw = NULL;
246 }
248 static void
249 keydef_open(G_GNUC_UNUSED mpdclient_t *c)
250 {
251 if (cmds == NULL) {
252 command_definition_t *current_cmds = get_command_definitions();
253 size_t cmds_size;
255 command_list_length = 0;
256 while (current_cmds[command_list_length].name)
257 command_list_length++;
259 cmds_size = (command_list_length+1) * sizeof(command_definition_t);
260 cmds = g_malloc0(cmds_size);
261 memcpy(cmds, current_cmds, cmds_size);
262 command_list_length += STATIC_ITEMS;
263 }
265 subcmd = -1;
266 list_window_check_selected(lw, LIST_LENGTH());
267 }
269 static void
270 keydef_close(void)
271 {
272 if (cmds && !keybindings_changed()) {
273 g_free(cmds);
274 cmds = NULL;
275 } else
276 screen_status_printf(_("Note: Did you forget to \'Apply\' your changes?"));
277 }
279 static const char *
280 keydef_title(char *str, size_t size)
281 {
282 if (subcmd < 0)
283 return _("Edit key bindings");
285 g_snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name);
286 return str;
287 }
289 static void
290 keydef_paint(void)
291 {
292 list_window_paint(lw, list_callback, NULL);
293 }
295 static bool
296 keydef_cmd(G_GNUC_UNUSED mpdclient_t *c, command_t cmd)
297 {
298 int length = LIST_LENGTH();
300 if (subcmd >= 0)
301 length = subcmd_length;
303 if (cmd == CMD_LIST_VISUAL_SELECT)
304 return false;
306 if (list_window_cmd(lw, length, cmd)) {
307 keydef_repaint();
308 return true;
309 }
311 switch(cmd) {
312 case CMD_PLAY:
313 if (subcmd < 0) {
314 if (lw->selected == LIST_ITEM_APPLY())
315 apply_keys();
316 else if (lw->selected == LIST_ITEM_SAVE()) {
317 apply_keys();
318 save_keys();
319 } else {
320 subcmd = lw->selected;
321 lw->selected=0;
322 lw->selected_start = lw->selected;
323 lw->selected_end = lw->selected;
324 check_subcmd_length();
326 keydef_repaint();
327 }
328 } else {
329 if (lw->selected == 0) { /* up */
330 lw->selected = subcmd;
331 lw->selected_start = lw->selected;
332 lw->selected_end = lw->selected;
333 subcmd = -1;
335 keydef_repaint();
336 } else
337 assign_new_key(screen.status_window.w,
338 subcmd,
339 lw->selected - STATIC_SUB_ITEMS);
340 }
341 return true;
342 case CMD_DELETE:
343 if (subcmd >= 0 && lw->selected >= STATIC_SUB_ITEMS)
344 delete_key(subcmd, lw->selected - STATIC_SUB_ITEMS);
345 return true;
346 break;
347 case CMD_SAVE_PLAYLIST:
348 apply_keys();
349 save_keys();
350 break;
351 case CMD_LIST_FIND:
352 case CMD_LIST_RFIND:
353 case CMD_LIST_FIND_NEXT:
354 case CMD_LIST_RFIND_NEXT:
355 screen_find(lw, length,
356 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 };