Code

wreadln: static buffer
[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 /* libcurses version */
122 static gchar *
123 _wreadln(WINDOW *w,
124          const gchar *prompt,
125          const gchar *initial_value,
126          gint x1,
127          GList **history,
128          GCompletion *gcmp,
129          gboolean masked)
131         struct wreadln wr = {
132                 .w = w,
133                 .masked = masked,
134                 .cursor = 0,
135                 .start = 0,
136         };
137         GList *hlist = NULL, *hcurrent = NULL;
138         gint key = 0, i;
140         /* turn off echo */
141         noecho();
142         /* make shure the cursor is visible */
143         curs_set(1);
144         /* print prompt string */
145         if (prompt)
146                 waddstr(w, prompt);
147         /* retrive y and x0 position */
148         getyx(w, wr.y, wr.x);
149         /* check the x1 value */
150         if (x1 <= wr.x || x1 > COLS)
151                 x1 = COLS;
152         wr.width = x1 - wr.x;
153         /* clear input area */
154         mvwhline(w, wr.y, wr.x, ' ', wr.width);
156         if (history) {
157                 /* append the a new line to our history list */
158                 *history = g_list_append(*history, g_malloc0(sizeof(wr.line)));
159                 /* hlist points to the current item in the history list */
160                 hlist = g_list_last(*history);
161                 hcurrent = hlist;
162         }
164         if (initial_value == (char *)-1) {
165                 /* get previous history entry */
166                 if (history && hlist->prev) {
167                         if (hlist == hcurrent)
168                                 /* save the current line */
169                                 g_strlcpy(hlist->data, wr.line, sizeof(wr.line));
171                         /* get previous line */
172                         hlist = hlist->prev;
173                         g_strlcpy(wr.line, hlist->data, sizeof(wr.line));
174                 }
175                 cursor_move_to_eol(&wr);
176                 drawline(&wr);
177         } else if (initial_value) {
178                 /* copy the initial value to the line buffer */
179                 g_strlcpy(wr.line, initial_value, sizeof(wr.line));
180                 cursor_move_to_eol(&wr);
181                 drawline(&wr);
182         }
184         while (key != 13 && key != '\n') {
185                 key = wgetch(w);
187                 /* check if key is a function key */
188                 for (i = 0; i < 63; i++)
189                         if (key == KEY_F(i)) {
190                                 key = KEY_F(1);
191                                 i = 64;
192                         }
194                 switch (key) {
195 #ifdef HAVE_GETMOUSE
196                 case KEY_MOUSE: /* ignore mouse events */
197 #endif
198                 case ERR: /* ingnore errors */
199                         break;
201                 case TAB:
202                         if (gcmp) {
203                                 char *prefix = NULL;
204                                 GList *list;
206                                 if (wrln_pre_completion_callback)
207                                         wrln_pre_completion_callback(gcmp, wr.line,
208                                                                      wrln_completion_callback_data);
209                                 list = g_completion_complete(gcmp, wr.line, &prefix);
210                                 if (prefix) {
211                                         g_strlcpy(wr.line, prefix, sizeof(wr.line));
212                                         cursor_move_to_eol(&wr);
213                                         g_free(prefix);
214                                 } else
215                                         screen_bell();
217                                 if (wrln_post_completion_callback)
218                                         wrln_post_completion_callback(gcmp, wr.line, list,
219                                                                       wrln_completion_callback_data);
220                         }
221                         break;
223                 case KEY_CTRL_G:
224                         screen_bell();
225                         if (history) {
226                                 g_free(hcurrent->data);
227                                 hcurrent->data = NULL;
228                                 *history = g_list_delete_link(*history, hcurrent);
229                         }
230                         return NULL;
232                 case KEY_LEFT:
233                 case KEY_CTRL_B:
234                         cursor_move_left(&wr);
235                         break;
236                 case KEY_RIGHT:
237                 case KEY_CTRL_F:
238                         cursor_move_right(&wr);
239                         break;
240                 case KEY_HOME:
241                 case KEY_CTRL_A:
242                         wr.cursor = 0;
243                         wr.start = 0;
244                         break;
245                 case KEY_END:
246                 case KEY_CTRL_E:
247                         cursor_move_to_eol(&wr);
248                         break;
249                 case KEY_CTRL_K:
250                         wr.line[wr.cursor] = 0;
251                         break;
252                 case KEY_CTRL_U:
253                         wr.cursor = utf8_width(wr.line);
254                         for (i = 0; i < wr.cursor; i++)
255                                 wr.line[i] = '\0';
256                         wr.cursor = 0;
257                         break;
258                 case 127:
259                 case KEY_BCKSPC:        /* handle backspace: copy all */
260                 case KEY_BACKSPACE:     /* chars starting from curpos */
261                         if (wr.cursor > 0) {/* - 1 from buf[n+1] to buf   */
262                                 for (i = wr.cursor - 1; wr.line[i] != 0; i++)
263                                         wr.line[i] = wr.line[i + 1];
264                                 cursor_move_left(&wr);
265                         }
266                         break;
267                 case KEY_DC:            /* handle delete key. As above */
268                 case KEY_CTRL_D:
269                         if (wr.cursor <= (gint)utf8_width(wr.line) - 1) {
270                                 for (i = wr.cursor; wr.line[i] != 0; i++)
271                                         wr.line[i] = wr.line[i + 1];
272                         }
273                         break;
274                 case KEY_UP:
275                 case KEY_CTRL_P:
276                         /* get previous history entry */
277                         if (history && hlist->prev) {
278                                 if (hlist == hcurrent)
279                                         /* save the current line */
280                                         g_strlcpy(hlist->data, wr.line,
281                                                   sizeof(wr.line));
283                                 /* get previous line */
284                                 hlist = hlist->prev;
285                                 g_strlcpy(wr.line, hlist->data,
286                                           sizeof(wr.line));
287                         }
288                         cursor_move_to_eol(&wr);
289                         break;
290                 case KEY_DOWN:
291                 case KEY_CTRL_N:
292                         /* get next history entry */
293                         if (history && hlist->next) {
294                                 /* get next line */
295                                 hlist = hlist->next;
296                                 g_strlcpy(wr.line, hlist->data,
297                                           sizeof(wr.line));
298                         }
299                         cursor_move_to_eol(&wr);
300                         break;
302                 case '\n':
303                 case 13:
304                 case KEY_IC:
305                 case KEY_PPAGE:
306                 case KEY_NPAGE:
307                 case KEY_F(1):
308                         /* ignore char */
309                         break;
310                 default:
311                         if (key >= 32) {
312                                 if (strlen(wr.line + wr.cursor)) { /* if the cursor is */
313                                         /* not at the last pos */
314                                         gchar *tmp = NULL;
315                                         gsize size = strlen(wr.line + wr.cursor) + 1;
317                                         tmp = g_malloc0(size);
318                                         g_strlcpy (tmp, wr.line + wr.cursor, size);
319                                         wr.line[wr.cursor] = key;
320                                         wr.line[wr.cursor + 1] = 0;
321                                         g_strlcat(&wr.line[wr.cursor + 1], tmp, size);
322                                         g_free(tmp);
323                                         cursor_move_right(&wr);
324                                 } else {
325                                         wr.line[wr.cursor + 1] = 0;
326                                         wr.line[wr.cursor] = key;
327                                         cursor_move_right(&wr);
328                                 }
329                         }
330                 }
332                 drawline(&wr);
333         }
335         /* update history */
336         if (history) {
337                 if (strlen(wr.line)) {
338                         /* update the current history entry */
339                         size_t size = strlen(wr.line) + 1;
340                         hcurrent->data = g_realloc(hcurrent->data, size);
341                         g_strlcpy(hcurrent->data, wr.line, size);
342                 } else {
343                         /* the line was empty - remove the current history entry */
344                         g_free(hcurrent->data);
345                         hcurrent->data = NULL;
346                         *history = g_list_delete_link(*history, hcurrent);
347                 }
349                 while (g_list_length(*history) > wrln_max_history_length) {
350                         GList *first = g_list_first(*history);
352                         /* remove the oldest history entry  */
353                         g_free(first->data);
354                         first->data = NULL;
355                         *history = g_list_delete_link(*history, first);
356                 }
357         }
359         return g_strdup(wr.line);
362 gchar *
363 wreadln(WINDOW *w,
364         const gchar *prompt,
365         const gchar *initial_value,
366         gint x1,
367         GList **history,
368         GCompletion *gcmp)
370         return  _wreadln(w, prompt, initial_value, x1, history, gcmp, FALSE);
373 gchar *
374 wreadln_masked(WINDOW *w,
375                const gchar *prompt,
376                const gchar *initial_value,
377                gint x1,
378                GList **history,
379                GCompletion *gcmp)
381         return  _wreadln(w, prompt, initial_value, x1, history, gcmp, TRUE);