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 #ifndef DISABLE_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;
153 command_t cmd;
155 buf = g_strdup_printf(_("Enter new key for %s: "), cmds[cmd_index].name);
156 key = screen_getch(w, buf);
157 g_free(buf);
158 if( key==KEY_RESIZE )
159 screen_resize();
160 if( key==ERR )
161 {
162 screen_status_printf(_("Aborted!"));
163 return;
164 }
165 cmd = find_key_command(key, cmds);
166 if( cmd!=CMD_NONE && cmd!= cmds[cmd_index].command )
167 {
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 }
174 cmds[cmd_index].keys[key_index] = key;
175 cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
177 screen_status_printf(_("Assigned %s to %s"),
178 key2str(key),cmds[cmd_index].name);
179 check_subcmd_length();
180 lw->repaint = 1;
181 /* update key conflict flags */
182 check_key_bindings(cmds, NULL, 0);
183 }
185 static char *
186 list_callback(int index, int *highlight, void *data)
187 {
188 static char buf[BUFSIZE];
190 *highlight = 0;
191 if( subcmd <0 )
192 {
193 if( index<command_list_length )
194 {
195 if( cmds[index].flags & COMMAND_KEY_CONFLICT )
196 *highlight = 1;
197 return cmds[index].name;
198 }
199 else if( index==LIST_ITEM_APPLY() )
200 return LIST_ITEM_APPLY_LABEL;
201 else if( index==LIST_ITEM_SAVE() )
202 return LIST_ITEM_SAVE_LABEL;
203 }
204 else
205 {
206 if( index== 0 )
207 return "[..]";
208 index--;
209 if( index<MAX_COMMAND_KEYS && cmds[subcmd].keys[index]>0 )
210 {
211 g_snprintf(buf,
212 BUFSIZE, "%d. %-20s (%d) ",
213 index+1,
214 key2str(cmds[subcmd].keys[index]),
215 cmds[subcmd].keys[index]);
216 return buf;
217 }
218 else if ( index==subcmd_addpos )
219 {
220 g_snprintf(buf, BUFSIZE, _("%d. Add new key "), index+1 );
221 return buf;
222 }
223 }
225 return NULL;
226 }
228 static void
229 keydef_init(WINDOW *w, int cols, int rows)
230 {
231 lw = list_window_init(w, cols, rows);
232 }
234 static void
235 keydef_resize(int cols, int rows)
236 {
237 lw->cols = cols;
238 lw->rows = rows;
239 }
241 static void
242 keydef_exit(void)
243 {
244 list_window_free(lw);
245 if( cmds )
246 g_free(cmds);
247 cmds = NULL;
248 lw = NULL;
249 }
251 static void
252 keydef_open(screen_t *screen, mpdclient_t *c)
253 {
254 if( cmds == NULL )
255 {
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 screen_status_printf(_("Welcome to the key editor!"));
268 }
270 subcmd = -1;
271 list_window_check_selected(lw, LIST_LENGTH());
272 }
274 static void
275 keydef_close(void)
276 {
277 if( cmds && !keybindings_changed() )
278 {
279 g_free(cmds);
280 cmds = NULL;
281 }
282 else
283 screen_status_printf(_("Note: Did you forget to \'Apply\' your changes?"));
284 }
286 static char *
287 keydef_title(char *str, size_t size)
288 {
289 if( subcmd<0 )
290 return _("Edit key bindings");
292 g_snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name);
293 return str;
294 }
296 static void
297 keydef_paint(screen_t *screen, mpdclient_t *c)
298 {
299 lw->clear = 1;
300 list_window_paint(lw, list_callback, NULL);
301 wrefresh(lw->w);
302 }
304 static void
305 keydef_update(screen_t *screen, mpdclient_t *c)
306 {
307 if( lw->repaint )
308 {
309 list_window_paint(lw, list_callback, NULL);
310 wrefresh(lw->w);
311 lw->repaint = 0;
312 }
313 }
315 static int
316 keydef_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
317 {
318 int length = LIST_LENGTH();
320 if( subcmd>=0 )
321 length = subcmd_length;
323 switch(cmd)
324 {
325 case CMD_PLAY:
326 if( subcmd<0 )
327 {
328 if( lw->selected == LIST_ITEM_APPLY() )
329 apply_keys();
330 else if( lw->selected == LIST_ITEM_SAVE() )
331 {
332 apply_keys();
333 save_keys();
334 }
335 else
336 {
337 subcmd = lw->selected;
338 lw->selected=0;
339 check_subcmd_length();
340 }
341 }
342 else
343 {
344 if( lw->selected == 0 ) /* up */
345 {
346 lw->selected = subcmd;
347 subcmd = -1;
348 }
349 else
350 assign_new_key(screen->status_window.w,
351 subcmd,
352 lw->selected-STATIC_SUB_ITEMS);
353 }
354 lw->repaint = 1;
355 lw->clear = 1;
356 return 1;
357 case CMD_DELETE:
358 if( subcmd>=0 && lw->selected-STATIC_SUB_ITEMS>=0 )
359 delete_key(subcmd, lw->selected-STATIC_SUB_ITEMS);
360 return 1;
361 break;
362 case CMD_SAVE_PLAYLIST:
363 apply_keys();
364 save_keys();
365 break;
366 case CMD_LIST_FIND:
367 case CMD_LIST_RFIND:
368 case CMD_LIST_FIND_NEXT:
369 case CMD_LIST_RFIND_NEXT:
370 return screen_find(screen, c,
371 lw, length,
372 cmd, list_callback, NULL);
374 default:
375 break;
376 }
378 return list_window_cmd(lw, length, cmd);
379 }
381 static list_window_t *
382 keydef_lw(void)
383 {
384 return lw;
385 }
387 screen_functions_t *
388 get_screen_keydef(void)
389 {
390 static screen_functions_t functions;
392 memset(&functions, 0, sizeof(screen_functions_t));
393 functions.init = keydef_init;
394 functions.exit = keydef_exit;
395 functions.open = keydef_open;
396 functions.close = keydef_close;
397 functions.resize = keydef_resize;
398 functions.paint = keydef_paint;
399 functions.update = keydef_update;
400 functions.cmd = keydef_cmd;
401 functions.get_lw = keydef_lw;
402 functions.get_title = keydef_title;
404 return &functions;
405 }
408 #endif