Code

wreadln: use memcpy() for both cases
[ncmpc.git] / src / wreadln.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 "wreadln.h"
20 #include "charset.h"
21 #include "screen_utils.h"
22 #include "config.h"
24 #include <stdlib.h>
25 #include <string.h>
26 #include <glib.h>
28 #define KEY_CTRL_A   1
29 #define KEY_CTRL_B   2
30 #define KEY_CTRL_C   3
31 #define KEY_CTRL_D   4
32 #define KEY_CTRL_E   5
33 #define KEY_CTRL_F   6
34 #define KEY_CTRL_G   7
35 #define KEY_CTRL_K   11
36 #define KEY_CTRL_N   14
37 #define KEY_CTRL_P   16
38 #define KEY_CTRL_U   21
39 #define KEY_CTRL_Z   26
40 #define KEY_BCKSPC   8
41 #define TAB          9
43 struct wreadln {
44         /** the ncurses window where this field is displayed */
45         WINDOW *const w;
47         /** the origin coordinates in the window */
48         gint x, y;
50         /** the screen width of the input field */
51         gint width;
53         /** is the input masked, i.e. characters displayed as '*'? */
54         const gboolean masked;
56         /** the byte position of the cursor */
57         gint cursor;
59         /** the byte position displayed at the origin (for horizontal
60             scrolling) */
61         gint start;
63         /** the current value */
64         gchar line[1024];
65 };
67 /** max items stored in the history list */
68 static const guint wrln_max_history_length = 32;
70 void *wrln_completion_callback_data = NULL;
71 wrln_gcmp_pre_cb_t wrln_pre_completion_callback = NULL;
72 wrln_gcmp_post_cb_t wrln_post_completion_callback = NULL;
74 /* move the cursor one step to the right */
75 static inline void cursor_move_right(struct wreadln *wr)
76 {
77         if (wr->cursor < (int)strlen(wr->line)) {
78                 ++wr->cursor;
79                 if (wr->cursor >= wr->width &&
80                     wr->start < wr->cursor - wr->width + 1)
81                         ++wr->start;
82         }
83 }
85 /* move the cursor one step to the left */
86 static inline void cursor_move_left(struct wreadln *wr)
87 {
88         if (wr->cursor > 0) {
89                 if (wr->cursor == wr->start && wr->start > 0)
90                         --wr->start;
91                 --wr->cursor;
92         }
93 }
95 /* move the cursor to the end of the line */
96 static inline void cursor_move_to_eol(struct wreadln *wr)
97 {
98         wr->cursor = strlen(wr->line);
99         if (wr->cursor >= wr->width)
100                 wr->start = wr->cursor - wr->width + 1;
103 /* draw line buffer and update cursor position */
104 static inline void drawline(const struct wreadln *wr)
106         wmove(wr->w, wr->y, wr->x);
107         /* clear input area */
108         whline(wr->w, ' ', wr->width);
109         /* print visible part of the line buffer */
110         if (wr->masked)
111                 whline(wr->w, '*', utf8_width(wr->line) - wr->start);
112         else
113                 waddnstr(wr->w, wr->line + wr->start, wr->width);
114         /* move the cursor to the correct position */
115         wmove(wr->w, wr->y, wr->x + wr->cursor - wr->start);
116         /* tell ncurses to redraw the screen */
117         doupdate();
120 static void
121 wreadln_insert_byte(struct wreadln *wr, gint key)
123         size_t rest = strlen(wr->line + wr->cursor) + 1;
124         const size_t length = 1;
126         memmove(wr->line + wr->cursor + length,
127                 wr->line + wr->cursor, rest);
128         wr->line[wr->cursor] = key;
130         cursor_move_right(wr);
133 /* libcurses version */
135 static gchar *
136 _wreadln(WINDOW *w,
137          const gchar *prompt,
138          const gchar *initial_value,
139          gint x1,
140          GList **history,
141          GCompletion *gcmp,
142          gboolean masked)
144         struct wreadln wr = {
145                 .w = w,
146                 .masked = masked,
147                 .cursor = 0,
148                 .start = 0,
149         };
150         GList *hlist = NULL, *hcurrent = NULL;
151         gint key = 0, i;
153         /* turn off echo */
154         noecho();
155         /* make shure the cursor is visible */
156         curs_set(1);
157         /* print prompt string */
158         if (prompt)
159                 waddstr(w, prompt);
160         /* retrive y and x0 position */
161         getyx(w, wr.y, wr.x);
162         /* check the x1 value */
163         if (x1 <= wr.x || x1 > COLS)
164                 x1 = COLS;
165         wr.width = x1 - wr.x;
166         /* clear input area */
167         mvwhline(w, wr.y, wr.x, ' ', wr.width);
169         if (history) {
170                 /* append the a new line to our history list */
171                 *history = g_list_append(*history, g_malloc0(sizeof(wr.line)));
172                 /* hlist points to the current item in the history list */
173                 hlist = g_list_last(*history);
174                 hcurrent = hlist;
175         }
177         if (initial_value == (char *)-1) {
178                 /* get previous history entry */
179                 if (history && hlist->prev) {
180                         if (hlist == hcurrent)
181                                 /* save the current line */
182                                 g_strlcpy(hlist->data, wr.line, sizeof(wr.line));
184                         /* get previous line */
185                         hlist = hlist->prev;
186                         g_strlcpy(wr.line, hlist->data, sizeof(wr.line));
187                 }
188                 cursor_move_to_eol(&wr);
189                 drawline(&wr);
190         } else if (initial_value) {
191                 /* copy the initial value to the line buffer */
192                 g_strlcpy(wr.line, initial_value, sizeof(wr.line));
193                 cursor_move_to_eol(&wr);
194                 drawline(&wr);
195         }
197         while (key != 13 && key != '\n') {
198                 key = wgetch(w);
200                 /* check if key is a function key */
201                 for (i = 0; i < 63; i++)
202                         if (key == KEY_F(i)) {
203                                 key = KEY_F(1);
204                                 i = 64;
205                         }
207                 switch (key) {
208 #ifdef HAVE_GETMOUSE
209                 case KEY_MOUSE: /* ignore mouse events */
210 #endif
211                 case ERR: /* ingnore errors */
212                         break;
214                 case TAB:
215                         if (gcmp) {
216                                 char *prefix = NULL;
217                                 GList *list;
219                                 if (wrln_pre_completion_callback)
220                                         wrln_pre_completion_callback(gcmp, wr.line,
221                                                                      wrln_completion_callback_data);
222                                 list = g_completion_complete(gcmp, wr.line, &prefix);
223                                 if (prefix) {
224                                         g_strlcpy(wr.line, prefix, sizeof(wr.line));
225                                         cursor_move_to_eol(&wr);
226                                         g_free(prefix);
227                                 } else
228                                         screen_bell();
230                                 if (wrln_post_completion_callback)
231                                         wrln_post_completion_callback(gcmp, wr.line, list,
232                                                                       wrln_completion_callback_data);
233                         }
234                         break;
236                 case KEY_CTRL_G:
237                         screen_bell();
238                         if (history) {
239                                 g_free(hcurrent->data);
240                                 hcurrent->data = NULL;
241                                 *history = g_list_delete_link(*history, hcurrent);
242                         }
243                         return NULL;
245                 case KEY_LEFT:
246                 case KEY_CTRL_B:
247                         cursor_move_left(&wr);
248                         break;
249                 case KEY_RIGHT:
250                 case KEY_CTRL_F:
251                         cursor_move_right(&wr);
252                         break;
253                 case KEY_HOME:
254                 case KEY_CTRL_A:
255                         wr.cursor = 0;
256                         wr.start = 0;
257                         break;
258                 case KEY_END:
259                 case KEY_CTRL_E:
260                         cursor_move_to_eol(&wr);
261                         break;
262                 case KEY_CTRL_K:
263                         wr.line[wr.cursor] = 0;
264                         break;
265                 case KEY_CTRL_U:
266                         wr.cursor = utf8_width(wr.line);
267                         for (i = 0; i < wr.cursor; i++)
268                                 wr.line[i] = '\0';
269                         wr.cursor = 0;
270                         break;
271                 case 127:
272                 case KEY_BCKSPC:        /* handle backspace: copy all */
273                 case KEY_BACKSPACE:     /* chars starting from curpos */
274                         if (wr.cursor > 0) {/* - 1 from buf[n+1] to buf   */
275                                 for (i = wr.cursor - 1; wr.line[i] != 0; i++)
276                                         wr.line[i] = wr.line[i + 1];
277                                 cursor_move_left(&wr);
278                         }
279                         break;
280                 case KEY_DC:            /* handle delete key. As above */
281                 case KEY_CTRL_D:
282                         if (wr.cursor <= (gint)utf8_width(wr.line) - 1) {
283                                 for (i = wr.cursor; wr.line[i] != 0; i++)
284                                         wr.line[i] = wr.line[i + 1];
285                         }
286                         break;
287                 case KEY_UP:
288                 case KEY_CTRL_P:
289                         /* get previous history entry */
290                         if (history && hlist->prev) {
291                                 if (hlist == hcurrent)
292                                         /* save the current line */
293                                         g_strlcpy(hlist->data, wr.line,
294                                                   sizeof(wr.line));
296                                 /* get previous line */
297                                 hlist = hlist->prev;
298                                 g_strlcpy(wr.line, hlist->data,
299                                           sizeof(wr.line));
300                         }
301                         cursor_move_to_eol(&wr);
302                         break;
303                 case KEY_DOWN:
304                 case KEY_CTRL_N:
305                         /* get next history entry */
306                         if (history && hlist->next) {
307                                 /* get next line */
308                                 hlist = hlist->next;
309                                 g_strlcpy(wr.line, hlist->data,
310                                           sizeof(wr.line));
311                         }
312                         cursor_move_to_eol(&wr);
313                         break;
315                 case '\n':
316                 case 13:
317                 case KEY_IC:
318                 case KEY_PPAGE:
319                 case KEY_NPAGE:
320                 case KEY_F(1):
321                         /* ignore char */
322                         break;
323                 default:
324                         if (key >= 32)
325                                 wreadln_insert_byte(&wr, key);
326                 }
328                 drawline(&wr);
329         }
331         /* update history */
332         if (history) {
333                 if (strlen(wr.line)) {
334                         /* update the current history entry */
335                         size_t size = strlen(wr.line) + 1;
336                         hcurrent->data = g_realloc(hcurrent->data, size);
337                         g_strlcpy(hcurrent->data, wr.line, size);
338                 } else {
339                         /* the line was empty - remove the current history entry */
340                         g_free(hcurrent->data);
341                         hcurrent->data = NULL;
342                         *history = g_list_delete_link(*history, hcurrent);
343                 }
345                 while (g_list_length(*history) > wrln_max_history_length) {
346                         GList *first = g_list_first(*history);
348                         /* remove the oldest history entry  */
349                         g_free(first->data);
350                         first->data = NULL;
351                         *history = g_list_delete_link(*history, first);
352                 }
353         }
355         return g_strdup(wr.line);
358 gchar *
359 wreadln(WINDOW *w,
360         const gchar *prompt,
361         const gchar *initial_value,
362         gint x1,
363         GList **history,
364         GCompletion *gcmp)
366         return  _wreadln(w, prompt, initial_value, x1, history, gcmp, FALSE);
369 gchar *
370 wreadln_masked(WINDOW *w,
371                const gchar *prompt,
372                const gchar *initial_value,
373                gint x1,
374                GList **history,
375                GCompletion *gcmp)
377         return  _wreadln(w, prompt, initial_value, x1, history, gcmp, TRUE);