Code

code style, indent with tabs X
[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 "config.h"
21 #ifndef  DISABLE_KEYDEF_SCREEN
22 #include "ncmpc.h"
23 #include "mpdclient.h"
24 #include "options.h"
25 #include "conf.h"
26 #include "command.h"
27 #include "screen.h"
28 #include "screen_utils.h"
29 #include "gcc.h"
31 #include <errno.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <glib.h>
35 #include <ncurses.h>
37 #define STATIC_ITEMS      0
38 #define STATIC_SUB_ITEMS  1
39 #define BUFSIZE 256
41 #define LIST_ITEM_APPLY()   ((unsigned)command_list_length)
42 #define LIST_ITEM_SAVE()    (LIST_ITEM_APPLY()+1)
43 #define LIST_LENGTH()       (LIST_ITEM_SAVE()+1)
45 #define LIST_ITEM_SAVE_LABEL  _("===> Apply & Save key bindings  ")
46 #define LIST_ITEM_APPLY_LABEL _("===> Apply key bindings ")
49 static list_window_t *lw = NULL;
50 static unsigned command_list_length = 0;
51 static command_definition_t *cmds = NULL;
53 static int subcmd = -1;
54 static unsigned subcmd_length = 0;
55 static unsigned subcmd_addpos = 0;
57 static int
58 keybindings_changed(void)
59 {
60         command_definition_t *orginal_cmds = get_command_definitions();
61         size_t size = command_list_length * sizeof(command_definition_t);
63         return memcmp(orginal_cmds, cmds, size);
64 }
66 static void
67 apply_keys(void)
68 {
69         if (keybindings_changed()) {
70                 command_definition_t *orginal_cmds = get_command_definitions();
71                 size_t size = command_list_length * sizeof(command_definition_t);
73                 memcpy(orginal_cmds, cmds, size);
74                 screen_status_printf(_("You have new key bindings!"));
75         } else
76                 screen_status_printf(_("Keybindings unchanged."));
77 }
79 static int
80 save_keys(void)
81 {
82         FILE *f;
83         char *filename;
85         if (check_user_conf_dir()) {
86                 screen_status_printf(_("Error: Unable to create direcory ~/.ncmpc - %s"),
87                                      strerror(errno));
88                 screen_bell();
89                 return -1;
90         }
92         filename = get_user_key_binding_filename();
94         if ((f = fopen(filename,"w")) == NULL) {
95                 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
96                 screen_bell();
97                 g_free(filename);
98                 return -1;
99         }
101         if (write_key_bindings(f, KEYDEF_WRITE_HEADER))
102                 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
103         else
104                 screen_status_printf(_("Wrote %s"), filename);
106         g_free(filename);
107         return fclose(f);
110 static void
111 check_subcmd_length(void)
113         subcmd_length = 0;
114         while (subcmd_length < MAX_COMMAND_KEYS &&
115                cmds[subcmd].keys[subcmd_length] > 0)
116                 ++subcmd_length;
118         if (subcmd_length < MAX_COMMAND_KEYS) {
119                 subcmd_addpos = subcmd_length;
120                 subcmd_length++;
121         } else
122                 subcmd_addpos = 0;
123         subcmd_length += STATIC_SUB_ITEMS;
126 static void
127 delete_key(int cmd_index, int key_index)
129         int i = key_index+1;
131         screen_status_printf(_("Deleted"));
132         while (i < MAX_COMMAND_KEYS && cmds[cmd_index].keys[i])
133                 cmds[cmd_index].keys[key_index++] = cmds[cmd_index].keys[i++];
134         cmds[cmd_index].keys[key_index] = 0;
135         cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
136         check_subcmd_length();
137         lw->clear = 1;
138         lw->repaint = 1;
139         /* update key conflict flags */
140         check_key_bindings(cmds, NULL, 0);
143 static void
144 assign_new_key(WINDOW *w, int cmd_index, int key_index)
146         int key;
147         char *buf;
148         command_t cmd;
150         buf = g_strdup_printf(_("Enter new key for %s: "), cmds[cmd_index].name);
151         key = screen_getch(w, buf);
152         g_free(buf);
154         if (key == KEY_RESIZE)
155                 screen_resize();
157         if (key==ERR) {
158                 screen_status_printf(_("Aborted!"));
159                 return;
160         }
162         cmd = find_key_command(key, cmds);
163         if (cmd != CMD_NONE && cmd != cmds[cmd_index].command) {
164                 screen_status_printf(_("Error: key %s is already used for %s"),
165                                      key2str(key),
166                                      get_key_command_name(cmd));
167                 screen_bell();
168                 return;
169         }
171         cmds[cmd_index].keys[key_index] = key;
172         cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
174         screen_status_printf(_("Assigned %s to %s"),
175                              key2str(key),cmds[cmd_index].name);
176         check_subcmd_length();
177         lw->repaint = 1;
178         /* update key conflict flags */
179         check_key_bindings(cmds, NULL, 0);
182 static const char *
183 list_callback(unsigned idx, int *highlight, mpd_unused void *data)
185         static char buf[BUFSIZE];
187         if (subcmd < 0) {
188                 if (idx < (unsigned)command_list_length) {
189                         if (cmds[idx].flags & COMMAND_KEY_CONFLICT)
190                                 *highlight = 1;
191                         return cmds[idx].name;
192                 } else if (idx == LIST_ITEM_APPLY())
193                         return LIST_ITEM_APPLY_LABEL;
194                 else if (idx == LIST_ITEM_SAVE())
195                         return LIST_ITEM_SAVE_LABEL;
196         } else {
197                 if (idx == 0)
198                         return "[..]";
199                 idx--;
200                 if (idx < MAX_COMMAND_KEYS && cmds[subcmd].keys[idx] > 0) {
201                         g_snprintf(buf,
202                                    BUFSIZE, "%d. %-20s   (%d) ",
203                                    idx + 1,
204                                    key2str(cmds[subcmd].keys[idx]),
205                                    cmds[subcmd].keys[idx]);
206                         return buf;
207                 } else if (idx == subcmd_addpos) {
208                         g_snprintf(buf, BUFSIZE, _("%d. Add new key "), idx + 1);
209                         return buf;
210                 }
211         }
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(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
242         if (cmds == NULL) {
243                 command_definition_t *current_cmds = get_command_definitions();
244                 size_t cmds_size;
246                 command_list_length = 0;
247                 while (current_cmds[command_list_length].name)
248                         command_list_length++;
250                 cmds_size = (command_list_length+1) * sizeof(command_definition_t);
251                 cmds = g_malloc0(cmds_size);
252                 memcpy(cmds, current_cmds, cmds_size);
253                 command_list_length += STATIC_ITEMS;
254                 screen_status_printf(_("Welcome to the key editor!"));
255         }
257         subcmd = -1;
258         list_window_check_selected(lw, LIST_LENGTH());
261 static void
262 keydef_close(void)
264         if (cmds && !keybindings_changed()) {
265                 g_free(cmds);
266                 cmds = NULL;
267         } else
268                 screen_status_printf(_("Note: Did you forget to \'Apply\' your changes?"));
271 static const char *
272 keydef_title(char *str, size_t size)
274         if (subcmd < 0)
275                 return _("Edit key bindings");
277         g_snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name);
278         return str;
281 static void
282 keydef_paint(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
284         lw->clear = 1;
285         list_window_paint(lw, list_callback, NULL);
286         wrefresh(lw->w);
289 static void
290 keydef_update(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
292         if (lw->repaint) {
293                 list_window_paint(lw, list_callback, NULL);
294                 wrefresh(lw->w);
295                 lw->repaint = 0;
296         }
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         switch(cmd) {
308         case CMD_PLAY:
309                 if (subcmd < 0) {
310                         if (lw->selected == LIST_ITEM_APPLY())
311                                 apply_keys();
312                         else if (lw->selected == LIST_ITEM_SAVE()) {
313                                 apply_keys();
314                                 save_keys();
315                         } else {
316                                 subcmd = lw->selected;
317                                 lw->selected=0;
318                                 check_subcmd_length();
319                         }
320                 } else {
321                         if (lw->selected == 0) { /* up */
322                                 lw->selected = subcmd;
323                                 subcmd = -1;
324                         } else
325                                 assign_new_key(screen->status_window.w,
326                                                subcmd,
327                                                lw->selected - STATIC_SUB_ITEMS);
328                 }
329                 lw->repaint = 1;
330                 lw->clear = 1;
331                 return 1;
332         case CMD_DELETE:
333                 if (subcmd >= 0 && lw->selected >= STATIC_SUB_ITEMS)
334                         delete_key(subcmd, lw->selected - STATIC_SUB_ITEMS);
335                 return 1;
336                 break;
337         case CMD_SAVE_PLAYLIST:
338                 apply_keys();
339                 save_keys();
340                 break;
341         case CMD_LIST_FIND:
342         case CMD_LIST_RFIND:
343         case CMD_LIST_FIND_NEXT:
344         case CMD_LIST_RFIND_NEXT:
345                 return screen_find(screen,
346                                    lw,  length,
347                                    cmd, list_callback, NULL);
349         default:
350                 break;
351         }
353         return list_window_cmd(lw, length, cmd);
356 const struct screen_functions screen_keydef = {
357         .init = keydef_init,
358         .exit = keydef_exit,
359         .open = keydef_open,
360         .close = keydef_close,
361         .resize = keydef_resize,
362         .paint = keydef_paint,
363         .update = keydef_update,
364         .cmd = keydef_cmd,
365         .get_title = keydef_title,
366 };
368 #endif