Code

fix unused parameter 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"
31 #include "gcc.h"
33 #include <errno.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <glib.h>
37 #include <ncurses.h>
39 #define STATIC_ITEMS      0
40 #define STATIC_SUB_ITEMS  1
41 #define BUFSIZE 256
43 #define LIST_ITEM_APPLY()   ((unsigned)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 unsigned command_list_length = 0;
53 static command_definition_t *cmds = NULL;
55 static int subcmd = -1;
56 static unsigned subcmd_length = 0;
57 static unsigned 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);
64   
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       screen_bell();
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       screen_bell();
103       g_free(filename);
104       return -1;
105     }
106   if( write_key_bindings(f, KEYDEF_WRITE_HEADER) )
107     screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
108   else
109     screen_status_printf(_("Wrote %s"), filename);
110   
111   g_free(filename);
112   return fclose(f);
115 static void
116 check_subcmd_length(void)
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;
132 static void
133 delete_key(int cmd_index, int key_index)
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();
143   lw->clear = 1;
144   lw->repaint = 1;
145   /* update key conflict flags */
146   check_key_bindings(cmds, NULL, 0);
149 static void
150 assign_new_key(WINDOW *w, int cmd_index, int key_index)
152   int key;
153   char *buf;
154   command_t cmd;
156   buf = g_strdup_printf(_("Enter new key for %s: "), cmds[cmd_index].name);
157   key = screen_getch(w, buf);
158   g_free(buf);
159   if( key==KEY_RESIZE )
160     screen_resize();
161   if( key==ERR )
162     {
163       screen_status_printf(_("Aborted!"));
164       return;
165     }
166   cmd = find_key_command(key, cmds);
167   if( cmd!=CMD_NONE && cmd!= cmds[cmd_index].command )
168     {
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     }
175   cmds[cmd_index].keys[key_index] = key;
176   cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
177   
178   screen_status_printf(_("Assigned %s to %s"), 
179                        key2str(key),cmds[cmd_index].name);
180   check_subcmd_length();
181   lw->repaint = 1;
182   /* update key conflict flags */
183   check_key_bindings(cmds, NULL, 0);
186 static const char *
187 list_callback(unsigned idx, int *highlight, mpd_unused void *data)
189         static char buf[BUFSIZE];
191         *highlight = 0;
192         if (subcmd < 0) {
193                 if (idx < (unsigned)command_list_length) {
194                         if (cmds[idx].flags & COMMAND_KEY_CONFLICT)
195                                 *highlight = 1;
196                         return cmds[idx].name;
197                 } else if (idx == LIST_ITEM_APPLY())
198                         return LIST_ITEM_APPLY_LABEL;
199                 else if (idx == LIST_ITEM_SAVE())
200                         return LIST_ITEM_SAVE_LABEL;
201         } else {
202                 if (idx == 0)
203                         return "[..]";
204                 idx--;
205                 if (idx < MAX_COMMAND_KEYS && cmds[subcmd].keys[idx] > 0) {
206                         g_snprintf(buf,
207                                    BUFSIZE, "%d. %-20s   (%d) ",
208                                    idx + 1,
209                                    key2str(cmds[subcmd].keys[idx]),
210                                    cmds[subcmd].keys[idx]);
211                         return buf;
212                 } else if (idx == subcmd_addpos) {
213                         g_snprintf(buf, BUFSIZE, _("%d. Add new key "), idx + 1);
214                         return buf;
215                 }
216         }
218         return NULL;
221 static void 
222 keydef_init(WINDOW *w, int cols, int rows)
224   lw = list_window_init(w, cols, rows);
227 static void
228 keydef_resize(int cols, int rows)
230   lw->cols = cols;
231   lw->rows = rows;
234 static void 
235 keydef_exit(void)
237   list_window_free(lw);
238   if( cmds )
239     g_free(cmds);
240   cmds = NULL;
241   lw = NULL;
244 static void 
245 keydef_open(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
247   if( cmds == NULL )
248     {
249       command_definition_t *current_cmds = get_command_definitions();
250       size_t cmds_size;
252       command_list_length = 0;
253       while( current_cmds[command_list_length].name )
254         command_list_length++;
256       cmds_size = (command_list_length+1)*sizeof(command_definition_t);
257       cmds = g_malloc0(cmds_size);
258       memcpy(cmds, current_cmds, cmds_size);
259       command_list_length += STATIC_ITEMS;
260       screen_status_printf(_("Welcome to the key editor!"));
261     }
263   subcmd = -1;
264   list_window_check_selected(lw, LIST_LENGTH());  
267 static void 
268 keydef_close(void)
270   if( cmds && !keybindings_changed() )
271     {
272       g_free(cmds);
273       cmds = NULL;
274     }
275   else
276     screen_status_printf(_("Note: Did you forget to \'Apply\' your changes?"));
279 static const char *
280 keydef_title(char *str, size_t size)
282   if( subcmd<0 )
283     return _("Edit key bindings");
284   
285   g_snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name);
286   return str;
289 static void 
290 keydef_paint(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
292   lw->clear = 1;
293   list_window_paint(lw, list_callback, NULL);
294   wrefresh(lw->w);
297 static void 
298 keydef_update(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
299 {  
300   if( lw->repaint )
301     {
302       list_window_paint(lw, list_callback, NULL);
303       wrefresh(lw->w);
304       lw->repaint = 0;
305     }
308 static int
309 keydef_cmd(screen_t *screen, mpd_unused mpdclient_t *c, command_t cmd)
311         int length = LIST_LENGTH();
313         if (subcmd >= 0)
314                 length = subcmd_length;
316         switch(cmd) {
317         case CMD_PLAY:
318                 if( subcmd<0 ) {
319                         if( lw->selected == LIST_ITEM_APPLY() )
320                                 apply_keys();
321                         else if( lw->selected == LIST_ITEM_SAVE() ) {
322                                 apply_keys();
323                                 save_keys();
324                         } else {
325                                 subcmd = lw->selected;
326                                 lw->selected=0;
327                                 check_subcmd_length();
328                         }
329                 } else {
330                         if (lw->selected == 0) { /* up */
331                                 lw->selected = subcmd;
332                                 subcmd = -1;
333                         } else
334                                 assign_new_key(screen->status_window.w,
335                                                subcmd,
336                                                lw->selected-STATIC_SUB_ITEMS);
337                 }
338                 lw->repaint = 1;
339                 lw->clear = 1;
340                 return 1;
341         case CMD_DELETE:
342                 if (subcmd >= 0 && lw->selected >= STATIC_SUB_ITEMS)
343                         delete_key(subcmd, lw->selected - STATIC_SUB_ITEMS);
344                 return 1;
345                 break;
346         case CMD_SAVE_PLAYLIST:
347                 apply_keys();
348                 save_keys();
349                 break;
350         case CMD_LIST_FIND:
351         case CMD_LIST_RFIND:
352         case CMD_LIST_FIND_NEXT:
353         case CMD_LIST_RFIND_NEXT:
354                 return screen_find(screen,
355                                    lw,  length,
356                                    cmd, list_callback, NULL);
358         default:
359                 break;
360         }
362         return list_window_cmd(lw, length, cmd);
365 static list_window_t *
366 keydef_lw(void)
368   return lw;
371 screen_functions_t *
372 get_screen_keydef(void)
374   static screen_functions_t functions;
376   memset(&functions, 0, sizeof(screen_functions_t));
377   functions.init   = keydef_init;
378   functions.exit   = keydef_exit;
379   functions.open   = keydef_open;
380   functions.close  = keydef_close;
381   functions.resize = keydef_resize;
382   functions.paint  = keydef_paint;
383   functions.update = keydef_update;
384   functions.cmd    = keydef_cmd;
385   functions.get_lw = keydef_lw;
386   functions.get_title = keydef_title;
388   return &functions;
392 #endif