Code

Major cleanup of the mpd client code (mpc->mpdclient)
[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 <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);
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       beep();
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       beep();
102       g_free(filename);
103       return -1;
104     }
105   if( write_key_bindings(f) )
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;
141   check_subcmd_length();
142   lw->clear = 1;
143   lw->repaint = 1;
146 static void
147 assign_new_key(WINDOW *w, int cmd_index, int key_index)
149   int key;
150   char buf[BUFSIZE];
151   command_t cmd;
153   snprintf(buf, BUFSIZE, _("Enter new key for %s: "), cmds[cmd_index].name);
154   key = screen_getch(w, buf);
155   if( key==KEY_RESIZE )
156     screen_resize();
157   if( key==ERR )
158     {
159       screen_status_printf(_("Aborted!"));
160       return;
161     }
162   cmd = find_key_command(key, cmds);
163   if( cmd!=CMD_NONE && cmd!= cmds[cmd_index].command )
164     {
165       screen_status_printf(_("Error: key %s is already used for %s"), 
166                            key2str(key),
167                            get_key_command_name(cmd));
168       beep();
169       return;
170     }
171   cmds[cmd_index].keys[key_index] = key;
172   screen_status_printf(_("Assigned %s to %s"), 
173                        key2str(key),cmds[cmd_index].name);
174   check_subcmd_length();
175   lw->repaint = 1;
178 static char *
179 list_callback(int index, int *highlight, void *data)
181   static char buf[BUFSIZE];
183   if( subcmd <0 )
184     {
185       if( index<command_list_length )
186         return cmds[index].name;
187       else if( index==LIST_ITEM_APPLY() )
188         return LIST_ITEM_APPLY_LABEL;
189       else if( index==LIST_ITEM_SAVE() )
190         return LIST_ITEM_SAVE_LABEL;
191     }
192   else
193   {
194     if( index== 0 )
195       return "[..]";
196     index--;
197     if( index<MAX_COMMAND_KEYS && cmds[subcmd].keys[index]>0 )
198       {
199         snprintf(buf, 
200                  BUFSIZE, "%d. %-20s   (%d) ", 
201                  index+1, 
202                  key2str(cmds[subcmd].keys[index]),
203                  cmds[subcmd].keys[index]);
204         return buf;
205       } 
206     else if ( index==subcmd_addpos )
207       {
208         snprintf(buf, BUFSIZE, _("%d. Add new key "), index+1 );
209         return buf;
210       }
211   }
212   
213   return NULL;
216 static void 
217 keydef_init(WINDOW *w, int cols, int rows)
219   lw = list_window_init(w, cols, rows);
222 static void
223 keydef_resize(int cols, int rows)
225   lw->cols = cols;
226   lw->rows = rows;
229 static void 
230 keydef_exit(void)
232   list_window_free(lw);
233   if( cmds )
234     g_free(cmds);
235   cmds = NULL;
236   lw = NULL;
239 static void 
240 keydef_open(screen_t *screen, mpdclient_t *c)
242   if( cmds == NULL )
243     {
244       command_definition_t *current_cmds = get_command_definitions();
245       size_t cmds_size;
247       command_list_length = 0;
248       while( current_cmds[command_list_length].name )
249         command_list_length++;
251       cmds_size = (command_list_length+1)*sizeof(command_definition_t);
252       cmds = g_malloc0(cmds_size);
253       memcpy(cmds, current_cmds, cmds_size);
254       command_list_length += STATIC_ITEMS;
255       screen_status_printf(_("Welcome to the key editor!"));
256     }
258   subcmd = -1;
259   list_window_check_selected(lw, LIST_LENGTH());  
262 static void 
263 keydef_close(void)
265   if( cmds && !keybindings_changed() )
266     {
267       g_free(cmds);
268       cmds = NULL;
269     }
270   else
271     screen_status_printf(_("Note: Did you forget to \'Apply\' your changes?"));
274 static char *
275 keydef_title(char *str, size_t size)
277   if( subcmd<0 )
278     return _("Edit key bindings");
279   
280   snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name);
281   return str;
284 static void 
285 keydef_paint(screen_t *screen, mpdclient_t *c)
287   lw->clear = 1;
288   list_window_paint(lw, list_callback, NULL);
289   wrefresh(lw->w);
292 static void 
293 keydef_update(screen_t *screen, mpdclient_t *c)
294 {  
295   if( lw->repaint )
296     {
297       list_window_paint(lw, list_callback, NULL);
298       wrefresh(lw->w);
299       lw->repaint = 0;
300     }
303 static int 
304 keydef_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
306   int length = LIST_LENGTH();
308   if( subcmd>=0 )
309     length = subcmd_length;
311   switch(cmd)
312     {
313     case CMD_PLAY:
314       if( subcmd<0 )
315         {
316           if( lw->selected == LIST_ITEM_APPLY() )
317             apply_keys();
318           else if( lw->selected == LIST_ITEM_SAVE() )
319             {
320               apply_keys();
321               save_keys();
322             }
323           else
324             {
325               subcmd = lw->selected;
326               lw->selected=0;
327               check_subcmd_length();
328             }
329         }
330       else
331         {
332           if( lw->selected == 0 ) /* up */
333             {
334               lw->selected = subcmd;
335               subcmd = -1;
336             }
337           else
338             assign_new_key(screen->status_window.w, 
339                            subcmd,
340                            lw->selected-STATIC_SUB_ITEMS);
341         }
342       lw->repaint = 1;
343       lw->clear = 1;
344       return 1;
345     case CMD_DELETE:
346       if( subcmd>=0 && lw->selected-STATIC_SUB_ITEMS>=0 )
347         delete_key(subcmd, lw->selected-STATIC_SUB_ITEMS);
348       return 1;
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, c, 
355                          lw,  length,
356                          cmd, list_callback);
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