Code

screen_lyrics: use screen_text
[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 "i18n.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"
27 #include <errno.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <glib.h>
32 #define STATIC_ITEMS      0
33 #define STATIC_SUB_ITEMS  1
34 #define BUFSIZE 256
36 #define LIST_ITEM_APPLY()   ((unsigned)command_list_length)
37 #define LIST_ITEM_SAVE()    (LIST_ITEM_APPLY()+1)
38 #define LIST_LENGTH()       (LIST_ITEM_SAVE()+1)
40 #define LIST_ITEM_SAVE_LABEL  _("===> Apply & Save key bindings  ")
41 #define LIST_ITEM_APPLY_LABEL _("===> Apply key bindings ")
44 static list_window_t *lw = NULL;
45 static unsigned command_list_length = 0;
46 static command_definition_t *cmds = NULL;
48 static int subcmd = -1;
49 static unsigned subcmd_length = 0;
50 static unsigned subcmd_addpos = 0;
52 static int
53 keybindings_changed(void)
54 {
55         command_definition_t *orginal_cmds = get_command_definitions();
56         size_t size = command_list_length * sizeof(command_definition_t);
58         return memcmp(orginal_cmds, cmds, size);
59 }
61 static void
62 apply_keys(void)
63 {
64         if (keybindings_changed()) {
65                 command_definition_t *orginal_cmds = get_command_definitions();
66                 size_t size = command_list_length * sizeof(command_definition_t);
68                 memcpy(orginal_cmds, cmds, size);
69                 screen_status_printf(_("You have new key bindings"));
70         } else
71                 screen_status_printf(_("Keybindings unchanged."));
72 }
74 static int
75 save_keys(void)
76 {
77         FILE *f;
78         char *filename;
80         if (check_user_conf_dir()) {
81                 screen_status_printf(_("Error: Unable to create direcory ~/.ncmpc - %s"),
82                                      strerror(errno));
83                 screen_bell();
84                 return -1;
85         }
87         filename = get_user_key_binding_filename();
89         if ((f = fopen(filename,"w")) == NULL) {
90                 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
91                 screen_bell();
92                 g_free(filename);
93                 return -1;
94         }
96         if (write_key_bindings(f, KEYDEF_WRITE_HEADER))
97                 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
98         else
99                 screen_status_printf(_("Wrote %s"), filename);
101         g_free(filename);
102         return fclose(f);
105 static void
106 check_subcmd_length(void)
108         subcmd_length = 0;
109         while (subcmd_length < MAX_COMMAND_KEYS &&
110                cmds[subcmd].keys[subcmd_length] > 0)
111                 ++subcmd_length;
113         if (subcmd_length < MAX_COMMAND_KEYS) {
114                 subcmd_addpos = subcmd_length;
115                 subcmd_length++;
116         } else
117                 subcmd_addpos = 0;
118         subcmd_length += STATIC_SUB_ITEMS;
121 static void
122 keydef_paint(void);
124 static void
125 keydef_repaint(void)
127         keydef_paint();
128         wrefresh(lw->w);
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;
140         cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
141         check_subcmd_length();
143         /* repaint */
144         keydef_repaint();
146         /* update key conflict flags */
147         check_key_bindings(cmds, NULL, 0);
150 static void
151 assign_new_key(WINDOW *w, int cmd_index, int key_index)
153         int key;
154         char *buf;
155         command_t cmd;
157         buf = g_strdup_printf(_("Enter new key for %s: "), cmds[cmd_index].name);
158         key = screen_getch(w, buf);
159         g_free(buf);
161         if (key==ERR) {
162                 screen_status_printf(_("Aborted"));
163                 return;
164         }
166         cmd = find_key_command(key, cmds);
167         if (cmd != CMD_NONE && cmd != cmds[cmd_index].command) {
168                 screen_status_printf(_("Error: key %s is already used for %s"),
169                                      key2str(key),
170                                      get_key_command_name(cmd));
171                 screen_bell();
172                 return;
173         }
175         cmds[cmd_index].keys[key_index] = key;
176         cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
178         screen_status_printf(_("Assigned %s to %s"),
179                              key2str(key),cmds[cmd_index].name);
180         check_subcmd_length();
182         /* repaint */
183         keydef_repaint();
185         /* update key conflict flags */
186         check_key_bindings(cmds, NULL, 0);
189 static const char *
190 list_callback(unsigned idx, bool *highlight, G_GNUC_UNUSED void *data)
192         static char buf[BUFSIZE];
194         if (subcmd < 0) {
195                 if (idx < (unsigned)command_list_length) {
196                         if (cmds[idx].flags & COMMAND_KEY_CONFLICT)
197                                 *highlight = true;
198                         return cmds[idx].name;
199                 } else if (idx == LIST_ITEM_APPLY())
200                         return LIST_ITEM_APPLY_LABEL;
201                 else if (idx == LIST_ITEM_SAVE())
202                         return LIST_ITEM_SAVE_LABEL;
203         } else {
204                 if (idx == 0)
205                         return "[..]";
206                 idx--;
207                 if (idx < MAX_COMMAND_KEYS && cmds[subcmd].keys[idx] > 0) {
208                         g_snprintf(buf,
209                                    BUFSIZE, "%d. %-20s   (%d) ",
210                                    idx + 1,
211                                    key2str(cmds[subcmd].keys[idx]),
212                                    cmds[subcmd].keys[idx]);
213                         return buf;
214                 } else if (idx == subcmd_addpos) {
215                         g_snprintf(buf, BUFSIZE, "%d. %s",
216                                    idx + 1, _("Add new key"));
217                         return buf;
218                 }
219         }
221         return NULL;
224 static void
225 keydef_init(WINDOW *w, int cols, int rows)
227         lw = list_window_init(w, cols, rows);
230 static void
231 keydef_resize(int cols, int rows)
233         lw->cols = cols;
234         lw->rows = rows;
237 static void
238 keydef_exit(void)
240         list_window_free(lw);
241         if (cmds)
242                 g_free(cmds);
243         cmds = NULL;
244         lw = NULL;
247 static void
248 keydef_open(G_GNUC_UNUSED mpdclient_t *c)
250         if (cmds == NULL) {
251                 command_definition_t *current_cmds = get_command_definitions();
252                 size_t cmds_size;
254                 command_list_length = 0;
255                 while (current_cmds[command_list_length].name)
256                         command_list_length++;
258                 cmds_size = (command_list_length+1) * sizeof(command_definition_t);
259                 cmds = g_malloc0(cmds_size);
260                 memcpy(cmds, current_cmds, cmds_size);
261                 command_list_length += STATIC_ITEMS;
262         }
264         subcmd = -1;
265         list_window_check_selected(lw, LIST_LENGTH());
268 static void
269 keydef_close(void)
271         if (cmds && !keybindings_changed()) {
272                 g_free(cmds);
273                 cmds = NULL;
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");
284         g_snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name);
285         return str;
288 static void
289 keydef_paint(void)
291         list_window_paint(lw, list_callback, NULL);
294 static bool
295 keydef_cmd(G_GNUC_UNUSED mpdclient_t *c, command_t cmd)
297         int length = LIST_LENGTH();
299         if (subcmd >= 0)
300                 length = subcmd_length;
302         if (list_window_cmd(lw, length, cmd)) {
303                 keydef_repaint();
304                 return true;
305         }
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();
320                                 keydef_repaint();
321                         }
322                 } else {
323                         if (lw->selected == 0) { /* up */
324                                 lw->selected = subcmd;
325                                 subcmd = -1;
327                                 keydef_repaint();
328                         } else
329                                 assign_new_key(screen.status_window.w,
330                                                subcmd,
331                                                lw->selected - STATIC_SUB_ITEMS);
332                 }
333                 return true;
334         case CMD_DELETE:
335                 if (subcmd >= 0 && lw->selected >= STATIC_SUB_ITEMS)
336                         delete_key(subcmd, lw->selected - STATIC_SUB_ITEMS);
337                 return true;
338                 break;
339         case CMD_SAVE_PLAYLIST:
340                 apply_keys();
341                 save_keys();
342                 break;
343         case CMD_LIST_FIND:
344         case CMD_LIST_RFIND:
345         case CMD_LIST_FIND_NEXT:
346         case CMD_LIST_RFIND_NEXT:
347                 screen_find(lw, length,
348                             cmd, list_callback, NULL);
349                 keydef_repaint();
350                 return true;
352         default:
353                 break;
354         }
356         return false;
359 const struct screen_functions screen_keydef = {
360         .init = keydef_init,
361         .exit = keydef_exit,
362         .open = keydef_open,
363         .close = keydef_close,
364         .resize = keydef_resize,
365         .paint = keydef_paint,
366         .cmd = keydef_cmd,
367         .get_title = keydef_title,
368 };