2dda43db9225fcfeee086e97fb35a97a5a8221f9
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 "libmpdclient.h"
32 #include "options.h"
33 #include "conf.h"
34 #include "mpc.h"
35 #include "command.h"
36 #include "screen.h"
37 #include "screen_utils.h"
39 #define STATIC_ITEMS 0
40 #define STATIC_SUB_ITEMS 1
41 #define BUFSIZE 256
43 #define LIST_ITEM_APPLY() (command_list_length)
44 #define LIST_ITEM_SAVE() (LIST_ITEM_APPLY()+1)
45 #define LIST_LENGTH() (LIST_ITEM_SAVE()+1)
47 #define LIST_ITEM_SAVE_LABEL _("===> Apply & Save key bindings ")
48 #define LIST_ITEM_APPLY_LABEL _("===> Apply key bindings ")
51 static list_window_t *lw = NULL;
52 static int command_list_length = 0;
53 static command_definition_t *cmds = NULL;
55 static int subcmd = -1;
56 static int subcmd_length = 0;
57 static int subcmd_addpos = 0;
59 static int
60 keybindings_changed(void)
61 {
62 command_definition_t *orginal_cmds = get_command_definitions();
63 size_t size = command_list_length*sizeof(command_definition_t);
65 return memcmp(orginal_cmds, cmds, size);
66 }
68 static void
69 apply_keys(void)
70 {
71 if( keybindings_changed() )
72 {
73 command_definition_t *orginal_cmds = get_command_definitions();
74 size_t size = command_list_length*sizeof(command_definition_t);
76 memcpy(orginal_cmds, cmds, size);
77 screen_status_printf(_("You have new key bindings!"));
78 }
79 else
80 screen_status_printf(_("Keybindings unchanged."));
81 }
83 static int
84 save_keys(void)
85 {
86 FILE *f;
87 char *filename;
89 if( check_user_conf_dir() )
90 {
91 screen_status_printf(_("Error: Unable to create direcory ~/.ncmpc - %s"),
92 strerror(errno));
93 beep();
94 return -1;
95 }
97 filename = get_user_key_binding_filename();
99 if( (f=fopen(filename,"w")) == NULL )
100 {
101 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
102 beep();
103 g_free(filename);
104 return -1;
105 }
106 if( write_key_bindings(f) )
107 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
108 else
109 screen_status_printf(_("Wrote %s"), filename);
111 g_free(filename);
112 return fclose(f);
113 }
115 static void
116 check_subcmd_length(void)
117 {
118 subcmd_length = 0;
119 while( subcmd_length<MAX_COMMAND_KEYS && cmds[subcmd].keys[subcmd_length]>0 )
120 subcmd_length ++;
122 if( subcmd_length<MAX_COMMAND_KEYS )
123 {
124 subcmd_addpos = subcmd_length;
125 subcmd_length++;
126 }
127 else
128 subcmd_addpos = 0;
129 subcmd_length += STATIC_SUB_ITEMS;
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;
142 check_subcmd_length();
143 lw->clear = 1;
144 lw->repaint = 1;
145 }
147 static void
148 assign_new_key(WINDOW *w, int cmd_index, int key_index)
149 {
150 int key;
151 char buf[BUFSIZE];
152 command_t cmd;
154 snprintf(buf, BUFSIZE, _("Enter new key for %s: "), cmds[cmd_index].name);
155 key = screen_getch(w, buf);
156 if( key==KEY_RESIZE )
157 screen_resize();
158 if( key==ERR )
159 {
160 screen_status_printf(_("Aborted!"));
161 return;
162 }
163 cmd = find_key_command(key, cmds);
164 if( cmd!=CMD_NONE && cmd!= cmds[cmd_index].command )
165 {
166 screen_status_printf(_("Error: key %s is already used for %s"),
167 key2str(key),
168 get_key_command_name(cmd));
169 beep();
170 return;
171 }
172 cmds[cmd_index].keys[key_index] = key;
173 screen_status_printf(_("Assigned %s to %s"),
174 key2str(key),cmds[cmd_index].name);
175 check_subcmd_length();
176 lw->repaint = 1;
177 }
179 static char *
180 list_callback(int index, int *highlight, void *data)
181 {
182 static char buf[BUFSIZE];
184 if( subcmd <0 )
185 {
186 if( index<command_list_length )
187 return cmds[index].name;
188 else if( index==LIST_ITEM_APPLY() )
189 return LIST_ITEM_APPLY_LABEL;
190 else if( index==LIST_ITEM_SAVE() )
191 return LIST_ITEM_SAVE_LABEL;
192 }
193 else
194 {
195 if( index== 0 )
196 return "[..]";
197 index--;
198 if( index<MAX_COMMAND_KEYS && cmds[subcmd].keys[index]>0 )
199 {
200 snprintf(buf,
201 BUFSIZE, "%d. %-20s (%d) ",
202 index+1,
203 key2str(cmds[subcmd].keys[index]),
204 cmds[subcmd].keys[index]);
205 return buf;
206 }
207 else if ( index==subcmd_addpos )
208 {
209 snprintf(buf, BUFSIZE, _("%d. Add new key "), index+1 );
210 return buf;
211 }
212 }
214 return NULL;
215 }
217 static void
218 keydef_init(WINDOW *w, int cols, int rows)
219 {
220 lw = list_window_init(w, cols, rows);
221 }
223 static void
224 keydef_resize(int cols, int rows)
225 {
226 lw->cols = cols;
227 lw->rows = rows;
228 }
230 static void
231 keydef_exit(void)
232 {
233 list_window_free(lw);
234 if( cmds )
235 g_free(cmds);
236 cmds = NULL;
237 lw = NULL;
238 }
240 static void
241 keydef_open(screen_t *screen, mpd_client_t *c)
242 {
243 if( cmds == NULL )
244 {
245 command_definition_t *current_cmds = get_command_definitions();
246 size_t cmds_size;
248 command_list_length = 0;
249 while( current_cmds[command_list_length].name )
250 command_list_length++;
252 cmds_size = (command_list_length+1)*sizeof(command_definition_t);
253 cmds = g_malloc0(cmds_size);
254 memcpy(cmds, current_cmds, cmds_size);
255 command_list_length += STATIC_ITEMS;
256 screen_status_printf(_("Welcome to the key editor!"));
257 }
259 subcmd = -1;
260 list_window_check_selected(lw, LIST_LENGTH());
261 }
263 static void
264 keydef_close(void)
265 {
266 if( cmds && !keybindings_changed() )
267 {
268 g_free(cmds);
269 cmds = NULL;
270 }
271 else
272 screen_status_printf(_("Note: Did you forget to \'Apply\' your changes?"));
273 }
275 static char *
276 keydef_title(char *str, size_t size)
277 {
278 if( subcmd<0 )
279 return _("Edit key bindings");
281 snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name);
282 return str;
283 }
285 static void
286 keydef_paint(screen_t *screen, mpd_client_t *c)
287 {
288 lw->clear = 1;
289 list_window_paint(lw, list_callback, NULL);
290 wrefresh(lw->w);
291 }
293 static void
294 keydef_update(screen_t *screen, mpd_client_t *c)
295 {
296 if( lw->repaint )
297 {
298 list_window_paint(lw, list_callback, NULL);
299 wrefresh(lw->w);
300 lw->repaint = 0;
301 }
302 }
304 static int
305 keydef_cmd(screen_t *screen, mpd_client_t *c, command_t cmd)
306 {
307 int length = LIST_LENGTH();
309 if( subcmd>=0 )
310 length = subcmd_length;
312 switch(cmd)
313 {
314 case CMD_PLAY:
315 if( subcmd<0 )
316 {
317 if( lw->selected == LIST_ITEM_APPLY() )
318 apply_keys();
319 else if( lw->selected == LIST_ITEM_SAVE() )
320 {
321 apply_keys();
322 save_keys();
323 }
324 else
325 {
326 subcmd = lw->selected;
327 lw->selected=0;
328 check_subcmd_length();
329 }
330 }
331 else
332 {
333 if( lw->selected == 0 ) /* up */
334 {
335 lw->selected = subcmd;
336 subcmd = -1;
337 }
338 else
339 assign_new_key(screen->status_window.w,
340 subcmd,
341 lw->selected-STATIC_SUB_ITEMS);
342 }
343 lw->repaint = 1;
344 lw->clear = 1;
345 return 1;
346 case CMD_DELETE:
347 if( subcmd>=0 && lw->selected-STATIC_SUB_ITEMS>=0 )
348 delete_key(subcmd, lw->selected-STATIC_SUB_ITEMS);
349 return 1;
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 return screen_find(screen, c,
356 lw, length,
357 cmd, list_callback);
359 default:
360 break;
361 }
363 return list_window_cmd(lw, length, cmd);
364 }
366 static list_window_t *
367 keydef_lw(void)
368 {
369 return lw;
370 }
372 screen_functions_t *
373 get_screen_keydef(void)
374 {
375 static screen_functions_t functions;
377 memset(&functions, 0, sizeof(screen_functions_t));
378 functions.init = keydef_init;
379 functions.exit = keydef_exit;
380 functions.open = keydef_open;
381 functions.close = keydef_close;
382 functions.resize = keydef_resize;
383 functions.paint = keydef_paint;
384 functions.update = keydef_update;
385 functions.cmd = keydef_cmd;
386 functions.get_lw = keydef_lw;
387 functions.get_title = keydef_title;
389 return &functions;
390 }
393 #endif