4e42b386f2975d5002dddfa603572876a480c554
1 /*
2 * (c) 2004 by Kalle Wallin <kaw@linux.se>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 */
19 #include "i18n.h"
20 #include "mpdclient.h"
21 #include "options.h"
22 #include "conf.h"
23 #include "command.h"
24 #include "screen.h"
25 #include "screen_utils.h"
26 #include "gcc.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 direcory ~/.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, int *highlight, mpd_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 = 1;
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. Add new key "), idx + 1);
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(mpd_unused mpdclient_t *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 screen_status_printf(_("Welcome to the key editor!"));
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 int
296 keydef_cmd(mpd_unused mpdclient_t *c, command_t cmd)
297 {
298 int length = LIST_LENGTH();
300 if (subcmd >= 0)
301 length = subcmd_length;
303 if (list_window_cmd(lw, length, cmd)) {
304 keydef_repaint();
305 return 1;
306 }
308 switch(cmd) {
309 case CMD_PLAY:
310 if (subcmd < 0) {
311 if (lw->selected == LIST_ITEM_APPLY())
312 apply_keys();
313 else if (lw->selected == LIST_ITEM_SAVE()) {
314 apply_keys();
315 save_keys();
316 } else {
317 subcmd = lw->selected;
318 lw->selected=0;
319 check_subcmd_length();
321 keydef_repaint();
322 }
323 } else {
324 if (lw->selected == 0) { /* up */
325 lw->selected = subcmd;
326 subcmd = -1;
328 keydef_repaint();
329 } else
330 assign_new_key(screen.status_window.w,
331 subcmd,
332 lw->selected - STATIC_SUB_ITEMS);
333 }
334 return 1;
335 case CMD_DELETE:
336 if (subcmd >= 0 && lw->selected >= STATIC_SUB_ITEMS)
337 delete_key(subcmd, lw->selected - STATIC_SUB_ITEMS);
338 return 1;
339 break;
340 case CMD_SAVE_PLAYLIST:
341 apply_keys();
342 save_keys();
343 break;
344 case CMD_LIST_FIND:
345 case CMD_LIST_RFIND:
346 case CMD_LIST_FIND_NEXT:
347 case CMD_LIST_RFIND_NEXT:
348 screen_find(lw, length,
349 cmd, list_callback, NULL);
350 keydef_repaint();
351 return 1;
353 default:
354 break;
355 }
357 return 0;
358 }
360 const struct screen_functions screen_keydef = {
361 .init = keydef_init,
362 .exit = keydef_exit,
363 .open = keydef_open,
364 .close = keydef_close,
365 .resize = keydef_resize,
366 .paint = keydef_paint,
367 .cmd = keydef_cmd,
368 .get_title = keydef_title,
369 };