Code

screen: don't compile disabled sources
[ncmpc.git] / src / screen_keydef.c
1 /*
2  * (c) 2004 by Kalle Wallin <kaw@linux.se>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  *
17  */
19 #include "ncmpc.h"
20 #include "mpdclient.h"
21 #include "options.h"
22 #include "conf.h"
23 #include "command.h"
24 #include "screen.h"
25 #include "screen_utils.h"
26 #include "gcc.h"
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <glib.h>
32 #include <ncurses.h>
34 #define STATIC_ITEMS      0
35 #define STATIC_SUB_ITEMS  1
36 #define BUFSIZE 256
38 #define LIST_ITEM_APPLY()   ((unsigned)command_list_length)
39 #define LIST_ITEM_SAVE()    (LIST_ITEM_APPLY()+1)
40 #define LIST_LENGTH()       (LIST_ITEM_SAVE()+1)
42 #define LIST_ITEM_SAVE_LABEL  _("===> Apply & Save key bindings  ")
43 #define LIST_ITEM_APPLY_LABEL _("===> Apply key bindings ")
46 static list_window_t *lw = NULL;
47 static unsigned command_list_length = 0;
48 static command_definition_t *cmds = NULL;
50 static int subcmd = -1;
51 static unsigned subcmd_length = 0;
52 static unsigned subcmd_addpos = 0;
54 static int
55 keybindings_changed(void)
56 {
57         command_definition_t *orginal_cmds = get_command_definitions();
58         size_t size = command_list_length * sizeof(command_definition_t);
60         return memcmp(orginal_cmds, cmds, size);
61 }
63 static void
64 apply_keys(void)
65 {
66         if (keybindings_changed()) {
67                 command_definition_t *orginal_cmds = get_command_definitions();
68                 size_t size = command_list_length * sizeof(command_definition_t);
70                 memcpy(orginal_cmds, cmds, size);
71                 screen_status_printf(_("You have new key bindings!"));
72         } else
73                 screen_status_printf(_("Keybindings unchanged."));
74 }
76 static int
77 save_keys(void)
78 {
79         FILE *f;
80         char *filename;
82         if (check_user_conf_dir()) {
83                 screen_status_printf(_("Error: Unable to create direcory ~/.ncmpc - %s"),
84                                      strerror(errno));
85                 screen_bell();
86                 return -1;
87         }
89         filename = get_user_key_binding_filename();
91         if ((f = fopen(filename,"w")) == NULL) {
92                 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
93                 screen_bell();
94                 g_free(filename);
95                 return -1;
96         }
98         if (write_key_bindings(f, KEYDEF_WRITE_HEADER))
99                 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
100         else
101                 screen_status_printf(_("Wrote %s"), filename);
103         g_free(filename);
104         return fclose(f);
107 static void
108 check_subcmd_length(void)
110         subcmd_length = 0;
111         while (subcmd_length < MAX_COMMAND_KEYS &&
112                cmds[subcmd].keys[subcmd_length] > 0)
113                 ++subcmd_length;
115         if (subcmd_length < MAX_COMMAND_KEYS) {
116                 subcmd_addpos = subcmd_length;
117                 subcmd_length++;
118         } else
119                 subcmd_addpos = 0;
120         subcmd_length += STATIC_SUB_ITEMS;
123 static void
124 keydef_paint(mpdclient_t *c);
126 static void
127 keydef_repaint(void)
129         keydef_paint(NULL);
130         wrefresh(lw->w);
133 static void
134 delete_key(int cmd_index, int key_index)
136         int i = key_index+1;
138         screen_status_printf(_("Deleted"));
139         while (i < MAX_COMMAND_KEYS && cmds[cmd_index].keys[i])
140                 cmds[cmd_index].keys[key_index++] = cmds[cmd_index].keys[i++];
141         cmds[cmd_index].keys[key_index] = 0;
142         cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
143         check_subcmd_length();
145         /* repaint */
146         keydef_repaint();
148         /* update key conflict flags */
149         check_key_bindings(cmds, NULL, 0);
152 static void
153 assign_new_key(WINDOW *w, int cmd_index, int key_index)
155         int key;
156         char *buf;
157         command_t cmd;
159         buf = g_strdup_printf(_("Enter new key for %s: "), cmds[cmd_index].name);
160         key = screen_getch(w, buf);
161         g_free(buf);
163         if (key == KEY_RESIZE)
164                 screen_resize();
166         if (key==ERR) {
167                 screen_status_printf(_("Aborted!"));
168                 return;
169         }
171         cmd = find_key_command(key, cmds);
172         if (cmd != CMD_NONE && cmd != cmds[cmd_index].command) {
173                 screen_status_printf(_("Error: key %s is already used for %s"),
174                                      key2str(key),
175                                      get_key_command_name(cmd));
176                 screen_bell();
177                 return;
178         }
180         cmds[cmd_index].keys[key_index] = key;
181         cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
183         screen_status_printf(_("Assigned %s to %s"),
184                              key2str(key),cmds[cmd_index].name);
185         check_subcmd_length();
187         /* repaint */
188         keydef_repaint();
190         /* update key conflict flags */
191         check_key_bindings(cmds, NULL, 0);
194 static const char *
195 list_callback(unsigned idx, int *highlight, mpd_unused void *data)
197         static char buf[BUFSIZE];
199         if (subcmd < 0) {
200                 if (idx < (unsigned)command_list_length) {
201                         if (cmds[idx].flags & COMMAND_KEY_CONFLICT)
202                                 *highlight = 1;
203                         return cmds[idx].name;
204                 } else if (idx == LIST_ITEM_APPLY())
205                         return LIST_ITEM_APPLY_LABEL;
206                 else if (idx == LIST_ITEM_SAVE())
207                         return LIST_ITEM_SAVE_LABEL;
208         } else {
209                 if (idx == 0)
210                         return "[..]";
211                 idx--;
212                 if (idx < MAX_COMMAND_KEYS && cmds[subcmd].keys[idx] > 0) {
213                         g_snprintf(buf,
214                                    BUFSIZE, "%d. %-20s   (%d) ",
215                                    idx + 1,
216                                    key2str(cmds[subcmd].keys[idx]),
217                                    cmds[subcmd].keys[idx]);
218                         return buf;
219                 } else if (idx == subcmd_addpos) {
220                         g_snprintf(buf, BUFSIZE, _("%d. Add new key "), idx + 1);
221                         return buf;
222                 }
223         }
225         return NULL;
228 static void
229 keydef_init(WINDOW *w, int cols, int rows)
231         lw = list_window_init(w, cols, rows);
234 static void
235 keydef_resize(int cols, int rows)
237         lw->cols = cols;
238         lw->rows = rows;
241 static void
242 keydef_exit(void)
244         list_window_free(lw);
245         if (cmds)
246                 g_free(cmds);
247         cmds = NULL;
248         lw = NULL;
251 static void
252 keydef_open(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
254         if (cmds == NULL) {
255                 command_definition_t *current_cmds = get_command_definitions();
256                 size_t cmds_size;
258                 command_list_length = 0;
259                 while (current_cmds[command_list_length].name)
260                         command_list_length++;
262                 cmds_size = (command_list_length+1) * sizeof(command_definition_t);
263                 cmds = g_malloc0(cmds_size);
264                 memcpy(cmds, current_cmds, cmds_size);
265                 command_list_length += STATIC_ITEMS;
266                 screen_status_printf(_("Welcome to the key editor!"));
267         }
269         subcmd = -1;
270         list_window_check_selected(lw, LIST_LENGTH());
273 static void
274 keydef_close(void)
276         if (cmds && !keybindings_changed()) {
277                 g_free(cmds);
278                 cmds = NULL;
279         } else
280                 screen_status_printf(_("Note: Did you forget to \'Apply\' your changes?"));
283 static const char *
284 keydef_title(char *str, size_t size)
286         if (subcmd < 0)
287                 return _("Edit key bindings");
289         g_snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name);
290         return str;
293 static void
294 keydef_paint(mpd_unused mpdclient_t *c)
296         list_window_paint(lw, list_callback, NULL);
299 static int
300 keydef_cmd(screen_t *screen, mpd_unused mpdclient_t *c, command_t cmd)
302         int length = LIST_LENGTH();
304         if (subcmd >= 0)
305                 length = subcmd_length;
307         if (list_window_cmd(lw, length, cmd)) {
308                 keydef_repaint();
309                 return 1;
310         }
312         switch(cmd) {
313         case CMD_PLAY:
314                 if (subcmd < 0) {
315                         if (lw->selected == LIST_ITEM_APPLY())
316                                 apply_keys();
317                         else if (lw->selected == LIST_ITEM_SAVE()) {
318                                 apply_keys();
319                                 save_keys();
320                         } else {
321                                 subcmd = lw->selected;
322                                 lw->selected=0;
323                                 check_subcmd_length();
325                                 keydef_repaint();
326                         }
327                 } else {
328                         if (lw->selected == 0) { /* up */
329                                 lw->selected = subcmd;
330                                 subcmd = -1;
332                                 keydef_repaint();
333                         } else
334                                 assign_new_key(screen->status_window.w,
335                                                subcmd,
336                                                lw->selected - STATIC_SUB_ITEMS);
337                 }
338                 return 1;
339         case CMD_DELETE:
340                 if (subcmd >= 0 && lw->selected >= STATIC_SUB_ITEMS)
341                         delete_key(subcmd, lw->selected - STATIC_SUB_ITEMS);
342                 return 1;
343                 break;
344         case CMD_SAVE_PLAYLIST:
345                 apply_keys();
346                 save_keys();
347                 break;
348         case CMD_LIST_FIND:
349         case CMD_LIST_RFIND:
350         case CMD_LIST_FIND_NEXT:
351         case CMD_LIST_RFIND_NEXT:
352                 screen_find(screen,
353                             lw, length,
354                             cmd, list_callback, NULL);
355                 keydef_repaint();
356                 return 1;
358         default:
359                 break;
360         }
362         return 0;
365 const struct screen_functions screen_keydef = {
366         .init = keydef_init,
367         .exit = keydef_exit,
368         .open = keydef_open,
369         .close = keydef_close,
370         .resize = keydef_resize,
371         .paint = keydef_paint,
372         .cmd = keydef_cmd,
373         .get_title = keydef_title,
374 };