Code

screen_lyrics: set current.song
[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         if (subcmd < 0) {
192                 if (idx < (unsigned)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(mpd_unused screen_t *screen, mpd_unused 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(mpd_unused screen_t *screen, mpd_unused 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(mpd_unused screen_t *screen, mpd_unused 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, mpd_unused 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)
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 const struct screen_functions screen_keydef = {
365         .init = keydef_init,
366         .exit = keydef_exit,
367         .open = keydef_open,
368         .close = keydef_close,
369         .resize = keydef_resize,
370         .paint = keydef_paint,
371         .update = keydef_update,
372         .cmd = keydef_cmd,
373         .get_title = keydef_title,
374 };
376 #endif