1 /*
2 * $Id$
3 *
4 * (c) 2004 by Kalle Wallin <kaw@linux.se>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <glib.h>
25 #include <ncurses.h>
27 #include "config.h"
29 #ifdef ENABLE_KEYDEF_SCREEN
30 #include "ncmpc.h"
31 #include "mpdclient.h"
32 #include "options.h"
33 #include "conf.h"
34 #include "command.h"
35 #include "screen.h"
36 #include "screen_utils.h"
38 #define STATIC_ITEMS 0
39 #define STATIC_SUB_ITEMS 1
40 #define BUFSIZE 256
42 #define LIST_ITEM_APPLY() (command_list_length)
43 #define LIST_ITEM_SAVE() (LIST_ITEM_APPLY()+1)
44 #define LIST_LENGTH() (LIST_ITEM_SAVE()+1)
46 #define LIST_ITEM_SAVE_LABEL _("===> Apply & Save key bindings ")
47 #define LIST_ITEM_APPLY_LABEL _("===> Apply key bindings ")
50 static list_window_t *lw = NULL;
51 static int command_list_length = 0;
52 static command_definition_t *cmds = NULL;
54 static int subcmd = -1;
55 static int subcmd_length = 0;
56 static int subcmd_addpos = 0;
58 static int
59 keybindings_changed(void)
60 {
61 command_definition_t *orginal_cmds = get_command_definitions();
62 size_t size = command_list_length*sizeof(command_definition_t);
64 return memcmp(orginal_cmds, cmds, size);
65 }
67 static void
68 apply_keys(void)
69 {
70 if( keybindings_changed() )
71 {
72 command_definition_t *orginal_cmds = get_command_definitions();
73 size_t size = command_list_length*sizeof(command_definition_t);
75 memcpy(orginal_cmds, cmds, size);
76 screen_status_printf(_("You have new key bindings!"));
77 }
78 else
79 screen_status_printf(_("Keybindings unchanged."));
80 }
82 static int
83 save_keys(void)
84 {
85 FILE *f;
86 char *filename;
88 if( check_user_conf_dir() )
89 {
90 screen_status_printf(_("Error: Unable to create direcory ~/.ncmpc - %s"),
91 strerror(errno));
92 screen_bell();
93 return -1;
94 }
96 filename = get_user_key_binding_filename();
98 if( (f=fopen(filename,"w")) == NULL )
99 {
100 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
101 screen_bell();
102 g_free(filename);
103 return -1;
104 }
105 if( write_key_bindings(f, KEYDEF_WRITE_HEADER) )
106 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
107 else
108 screen_status_printf(_("Wrote %s"), filename);
110 g_free(filename);
111 return fclose(f);
112 }
114 static void
115 check_subcmd_length(void)
116 {
117 subcmd_length = 0;
118 while( subcmd_length<MAX_COMMAND_KEYS && cmds[subcmd].keys[subcmd_length]>0 )
119 subcmd_length ++;
121 if( subcmd_length<MAX_COMMAND_KEYS )
122 {
123 subcmd_addpos = subcmd_length;
124 subcmd_length++;
125 }
126 else
127 subcmd_addpos = 0;
128 subcmd_length += STATIC_SUB_ITEMS;
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();
142 lw->clear = 1;
143 lw->repaint = 1;
144 /* update key conflict flags */
145 check_key_bindings(cmds, NULL, 0);
146 }
148 static void
149 assign_new_key(WINDOW *w, int cmd_index, int key_index)
150 {
151 int key;
152 char buf[BUFSIZE];
153 command_t cmd;
155 snprintf(buf, BUFSIZE, _("Enter new key for %s: "), cmds[cmd_index].name);
156 key = screen_getch(w, buf);
157 if( key==KEY_RESIZE )
158 screen_resize();
159 if( key==ERR )
160 {
161 screen_status_printf(_("Aborted!"));
162 return;
163 }
164 cmd = find_key_command(key, cmds);
165 if( cmd!=CMD_NONE && cmd!= cmds[cmd_index].command )
166 {
167 screen_status_printf(_("Error: key %s is already used for %s"),
168 key2str(key),
169 get_key_command_name(cmd));
170 screen_bell();
171 return;
172 }
173 cmds[cmd_index].keys[key_index] = key;
174 cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
176 screen_status_printf(_("Assigned %s to %s"),
177 key2str(key),cmds[cmd_index].name);
178 check_subcmd_length();
179 lw->repaint = 1;
180 /* update key conflict flags */
181 check_key_bindings(cmds, NULL, 0);
182 }
184 static char *
185 list_callback(int index, int *highlight, void *data)
186 {
187 static char buf[BUFSIZE];
189 *highlight = 0;
190 if( subcmd <0 )
191 {
192 if( index<command_list_length )
193 {
194 if( cmds[index].flags & COMMAND_KEY_CONFLICT )
195 *highlight = 1;
196 return cmds[index].name;
197 }
198 else if( index==LIST_ITEM_APPLY() )
199 return LIST_ITEM_APPLY_LABEL;
200 else if( index==LIST_ITEM_SAVE() )
201 return LIST_ITEM_SAVE_LABEL;
202 }
203 else
204 {
205 if( index== 0 )
206 return "[..]";
207 index--;
208 if( index<MAX_COMMAND_KEYS && cmds[subcmd].keys[index]>0 )
209 {
210 snprintf(buf,
211 BUFSIZE, "%d. %-20s (%d) ",
212 index+1,
213 key2str(cmds[subcmd].keys[index]),
214 cmds[subcmd].keys[index]);
215 return buf;
216 }
217 else if ( index==subcmd_addpos )
218 {
219 snprintf(buf, BUFSIZE, _("%d. Add new key "), index+1 );
220 return buf;
221 }
222 }
224 return NULL;
225 }
227 static void
228 keydef_init(WINDOW *w, int cols, int rows)
229 {
230 lw = list_window_init(w, cols, rows);
231 }
233 static void
234 keydef_resize(int cols, int rows)
235 {
236 lw->cols = cols;
237 lw->rows = rows;
238 }
240 static void
241 keydef_exit(void)
242 {
243 list_window_free(lw);
244 if( cmds )
245 g_free(cmds);
246 cmds = NULL;
247 lw = NULL;
248 }
250 static void
251 keydef_open(screen_t *screen, mpdclient_t *c)
252 {
253 if( cmds == NULL )
254 {
255 command_definition_t *current_cmds = get_command_definitions();
256 size_t cmds_size;
258 command_list_length = 0;
259 while( current_cmds[command_list_length].name )
260 command_list_length++;
262 cmds_size = (command_list_length+1)*sizeof(command_definition_t);
263 cmds = g_malloc0(cmds_size);
264 memcpy(cmds, current_cmds, cmds_size);
265 command_list_length += STATIC_ITEMS;
266 screen_status_printf(_("Welcome to the key editor!"));
267 }
269 subcmd = -1;
270 list_window_check_selected(lw, LIST_LENGTH());
271 }
273 static void
274 keydef_close(void)
275 {
276 if( cmds && !keybindings_changed() )
277 {
278 g_free(cmds);
279 cmds = NULL;
280 }
281 else
282 screen_status_printf(_("Note: Did you forget to \'Apply\' your changes?"));
283 }
285 static char *
286 keydef_title(char *str, size_t size)
287 {
288 if( subcmd<0 )
289 return _("Edit key bindings");
291 snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name);
292 return str;
293 }
295 static void
296 keydef_paint(screen_t *screen, mpdclient_t *c)
297 {
298 lw->clear = 1;
299 list_window_paint(lw, list_callback, NULL);
300 wrefresh(lw->w);
301 }
303 static void
304 keydef_update(screen_t *screen, mpdclient_t *c)
305 {
306 if( lw->repaint )
307 {
308 list_window_paint(lw, list_callback, NULL);
309 wrefresh(lw->w);
310 lw->repaint = 0;
311 }
312 }
314 static int
315 keydef_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
316 {
317 int length = LIST_LENGTH();
319 if( subcmd>=0 )
320 length = subcmd_length;
322 switch(cmd)
323 {
324 case CMD_PLAY:
325 if( subcmd<0 )
326 {
327 if( lw->selected == LIST_ITEM_APPLY() )
328 apply_keys();
329 else if( lw->selected == LIST_ITEM_SAVE() )
330 {
331 apply_keys();
332 save_keys();
333 }
334 else
335 {
336 subcmd = lw->selected;
337 lw->selected=0;
338 check_subcmd_length();
339 }
340 }
341 else
342 {
343 if( lw->selected == 0 ) /* up */
344 {
345 lw->selected = subcmd;
346 subcmd = -1;
347 }
348 else
349 assign_new_key(screen->status_window.w,
350 subcmd,
351 lw->selected-STATIC_SUB_ITEMS);
352 }
353 lw->repaint = 1;
354 lw->clear = 1;
355 return 1;
356 case CMD_DELETE:
357 if( subcmd>=0 && lw->selected-STATIC_SUB_ITEMS>=0 )
358 delete_key(subcmd, lw->selected-STATIC_SUB_ITEMS);
359 return 1;
360 break;
361 case CMD_SAVE_PLAYLIST:
362 apply_keys();
363 save_keys();
364 break;
365 case CMD_LIST_FIND:
366 case CMD_LIST_RFIND:
367 case CMD_LIST_FIND_NEXT:
368 case CMD_LIST_RFIND_NEXT:
369 return screen_find(screen, c,
370 lw, length,
371 cmd, list_callback);
373 default:
374 break;
375 }
377 return list_window_cmd(lw, length, cmd);
378 }
380 static list_window_t *
381 keydef_lw(void)
382 {
383 return lw;
384 }
386 screen_functions_t *
387 get_screen_keydef(void)
388 {
389 static screen_functions_t functions;
391 memset(&functions, 0, sizeof(screen_functions_t));
392 functions.init = keydef_init;
393 functions.exit = keydef_exit;
394 functions.open = keydef_open;
395 functions.close = keydef_close;
396 functions.resize = keydef_resize;
397 functions.paint = keydef_paint;
398 functions.update = keydef_update;
399 functions.cmd = keydef_cmd;
400 functions.get_lw = keydef_lw;
401 functions.get_title = keydef_title;
403 return &functions;
404 }
407 #endif