Code

fix shadow warnings
[ncmpc.git] / src / screen_keydef.c
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 "config.h"
23 #ifndef  DISABLE_KEYDEF_SCREEN
24 #include "ncmpc.h"
25 #include "mpdclient.h"
26 #include "options.h"
27 #include "conf.h"
28 #include "command.h"
29 #include "screen.h"
30 #include "screen_utils.h"
32 #include <errno.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <glib.h>
36 #include <ncurses.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);
63   
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);
109   
110   g_free(filename);
111   return fclose(f);
114 static void
115 check_subcmd_length(void)
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;
131 static void
132 delete_key(int cmd_index, int key_index)
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);
148 static void
149 assign_new_key(WINDOW *w, int cmd_index, int key_index)
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;
176   
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);
185 static const char *
186 list_callback(int idx, int *highlight, void *data)
188         static char buf[BUFSIZE];
190         *highlight = 0;
191         if (subcmd < 0) {
192                 if (idx < command_list_length) {
193                         if (cmds[idx].flags & COMMAND_KEY_CONFLICT)
194                                 *highlight = 1;
195                         return cmds[idx].name;
196                 } else if (idx == LIST_ITEM_APPLY())
197                         return LIST_ITEM_APPLY_LABEL;
198                 else if (idx == LIST_ITEM_SAVE())
199                         return LIST_ITEM_SAVE_LABEL;
200         } else {
201                 if (idx == 0)
202                         return "[..]";
203                 idx--;
204                 if (idx < MAX_COMMAND_KEYS && cmds[subcmd].keys[idx] > 0) {
205                         g_snprintf(buf,
206                                    BUFSIZE, "%d. %-20s   (%d) ",
207                                    idx + 1,
208                                    key2str(cmds[subcmd].keys[idx]),
209                                    cmds[subcmd].keys[idx]);
210                         return buf;
211                 } else if (idx == subcmd_addpos) {
212                         g_snprintf(buf, BUFSIZE, _("%d. Add new key "), idx + 1);
213                         return buf;
214                 }
215         }
217         return NULL;
220 static void 
221 keydef_init(WINDOW *w, int cols, int rows)
223   lw = list_window_init(w, cols, rows);
226 static void
227 keydef_resize(int cols, int rows)
229   lw->cols = cols;
230   lw->rows = rows;
233 static void 
234 keydef_exit(void)
236   list_window_free(lw);
237   if( cmds )
238     g_free(cmds);
239   cmds = NULL;
240   lw = NULL;
243 static void 
244 keydef_open(screen_t *screen, mpdclient_t *c)
246   if( cmds == NULL )
247     {
248       command_definition_t *current_cmds = get_command_definitions();
249       size_t cmds_size;
251       command_list_length = 0;
252       while( current_cmds[command_list_length].name )
253         command_list_length++;
255       cmds_size = (command_list_length+1)*sizeof(command_definition_t);
256       cmds = g_malloc0(cmds_size);
257       memcpy(cmds, current_cmds, cmds_size);
258       command_list_length += STATIC_ITEMS;
259       screen_status_printf(_("Welcome to the key editor!"));
260     }
262   subcmd = -1;
263   list_window_check_selected(lw, LIST_LENGTH());  
266 static void 
267 keydef_close(void)
269   if( cmds && !keybindings_changed() )
270     {
271       g_free(cmds);
272       cmds = NULL;
273     }
274   else
275     screen_status_printf(_("Note: Did you forget to \'Apply\' your changes?"));
278 static const char *
279 keydef_title(char *str, size_t size)
281   if( subcmd<0 )
282     return _("Edit key bindings");
283   
284   g_snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name);
285   return str;
288 static void 
289 keydef_paint(screen_t *screen, mpdclient_t *c)
291   lw->clear = 1;
292   list_window_paint(lw, list_callback, NULL);
293   wrefresh(lw->w);
296 static void 
297 keydef_update(screen_t *screen, mpdclient_t *c)
298 {  
299   if( lw->repaint )
300     {
301       list_window_paint(lw, list_callback, NULL);
302       wrefresh(lw->w);
303       lw->repaint = 0;
304     }
307 static int
308 keydef_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
310         int length = LIST_LENGTH();
312         if (subcmd >= 0)
313                 length = subcmd_length;
315         switch(cmd) {
316         case CMD_PLAY:
317                 if( subcmd<0 ) {
318                         if( lw->selected == LIST_ITEM_APPLY() )
319                                 apply_keys();
320                         else if( lw->selected == LIST_ITEM_SAVE() ) {
321                                 apply_keys();
322                                 save_keys();
323                         } else {
324                                 subcmd = lw->selected;
325                                 lw->selected=0;
326                                 check_subcmd_length();
327                         }
328                 } else {
329                         if (lw->selected == 0) { /* up */
330                                 lw->selected = subcmd;
331                                 subcmd = -1;
332                         } else
333                                 assign_new_key(screen->status_window.w,
334                                                subcmd,
335                                                lw->selected-STATIC_SUB_ITEMS);
336                 }
337                 lw->repaint = 1;
338                 lw->clear = 1;
339                 return 1;
340         case CMD_DELETE:
341                 if (subcmd >= 0 && lw->selected - STATIC_SUB_ITEMS >= 0)
342                         delete_key(subcmd, lw->selected - STATIC_SUB_ITEMS);
343                 return 1;
344                 break;
345         case CMD_SAVE_PLAYLIST:
346                 apply_keys();
347                 save_keys();
348                 break;
349         case CMD_LIST_FIND:
350         case CMD_LIST_RFIND:
351         case CMD_LIST_FIND_NEXT:
352         case CMD_LIST_RFIND_NEXT:
353                 return screen_find(screen,
354                                    lw,  length,
355                                    cmd, list_callback, NULL);
357         default:
358                 break;
359         }
361         return list_window_cmd(lw, length, cmd);
364 static list_window_t *
365 keydef_lw(void)
367   return lw;
370 screen_functions_t *
371 get_screen_keydef(void)
373   static screen_functions_t functions;
375   memset(&functions, 0, sizeof(screen_functions_t));
376   functions.init   = keydef_init;
377   functions.exit   = keydef_exit;
378   functions.open   = keydef_open;
379   functions.close  = keydef_close;
380   functions.resize = keydef_resize;
381   functions.paint  = keydef_paint;
382   functions.update = keydef_update;
383   functions.cmd    = keydef_cmd;
384   functions.get_lw = keydef_lw;
385   functions.get_title = keydef_title;
387   return &functions;
391 #endif