Code

screen_utils: don't pass WINDOW to screen_readln()
[ncmpc.git] / src / screen_utils.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "screen_utils.h"
21 #include "screen.h"
22 #include "mpdclient.h"
23 #include "config.h"
24 #include "i18n.h"
25 #include "options.h"
26 #include "colors.h"
27 #include "wreadln.h"
28 #ifndef NCMPC_H
29 #include "ncmpc.h"
30 #endif /* NCMPC_H */
32 #include <mpd/client.h>
34 #include <stdlib.h>
35 #include <unistd.h>
37 #define FIND_PROMPT  _("Find")
38 #define RFIND_PROMPT _("Find backward")
39 #define JUMP_PROMPT _("Jump")
41 void
42 screen_bell(void)
43 {
44         if (options.audible_bell)
45                 beep();
46         if (options.visible_bell)
47                 flash();
48 }
50 int
51 screen_getch(const char *prompt)
52 {
53         WINDOW *w = screen.status_window.w;
54         int key = -1;
56         colors_use(w, COLOR_STATUS_ALERT);
57         werase(w);
58         wmove(w, 0, 0);
59         waddstr(w, prompt);
61         echo();
62         curs_set(1);
64         while ((key = wgetch(w)) == ERR)
65                 ;
67 #ifdef HAVE_GETMOUSE
68         /* ignore mouse events */
69         if (key == KEY_MOUSE)
70                 return screen_getch(prompt);
71 #endif
73         noecho();
74         curs_set(0);
76         return key;
77 }
79 char *
80 screen_readln(const char *prompt,
81               const char *value,
82               GList **history,
83               GCompletion *gcmp)
84 {
85         struct window *window = &screen.status_window;
86         WINDOW *w = window->w;
87         char *line = NULL;
89         wmove(w, 0,0);
90         curs_set(1);
91         colors_use(w, COLOR_STATUS_ALERT);
92         line = wreadln(w, prompt, value, window->cols, history, gcmp);
93         curs_set(0);
94         return line;
95 }
97 char *
98 screen_read_password(const char *prompt)
99 {
100         struct window *window = &screen.status_window;
101         WINDOW *w = window->w;
102         char *ret;
104         wmove(w, 0,0);
105         curs_set(1);
106         colors_use(w, COLOR_STATUS_ALERT);
108         if (prompt == NULL)
109                 prompt = _("Password");
110         ret = wreadln_masked(w, prompt, NULL, window->cols, NULL, NULL);
112         curs_set(0);
113         return ret;
116 /* query user for a string and find it in a list window */
117 int
118 screen_find(list_window_t *lw,
119             int rows,
120             command_t findcmd,
121             list_window_callback_fn_t callback_fn,
122             void *callback_data)
124         int reversed = 0;
125         bool found;
126         const char *prompt = FIND_PROMPT;
127         char *value = options.find_show_last_pattern ? (char *) -1 : NULL;
129         if (findcmd == CMD_LIST_RFIND || findcmd == CMD_LIST_RFIND_NEXT) {
130                 prompt = RFIND_PROMPT;
131                 reversed = 1;
132         }
134         switch (findcmd) {
135         case CMD_LIST_FIND:
136         case CMD_LIST_RFIND:
137                 if (screen.findbuf) {
138                         g_free(screen.findbuf);
139                         screen.findbuf=NULL;
140                 }
141                 /* continue... */
143         case CMD_LIST_FIND_NEXT:
144         case CMD_LIST_RFIND_NEXT:
145                 if (!screen.findbuf)
146                         screen.findbuf=screen_readln(prompt,
147                                                      value,
148                                                      &screen.find_history,
149                                                      NULL);
151                 if (screen.findbuf == NULL)
152                         return 1;
154                 found = reversed
155                         ? list_window_rfind(lw,
156                                             callback_fn, callback_data,
157                                             screen.findbuf,
158                                             options.find_wrap,
159                                             options.bell_on_wrap,
160                                             rows)
161                         : list_window_find(lw,
162                                            callback_fn, callback_data,
163                                            screen.findbuf,
164                                            options.find_wrap,
165                                            options.bell_on_wrap);
166                 if (!found) {
167                         screen_status_printf(_("Unable to find \'%s\'"),
168                                              screen.findbuf);
169                         screen_bell();
170                 }
171                 return 1;
172         default:
173                 break;
174         }
175         return 0;
178 /* query user for a string and jump to the entry
179  * which begins with this string while the users types */
180 void
181 screen_jump(struct list_window *lw,
182                 list_window_callback_fn_t callback_fn,
183                 void *callback_data)
185         char *search_str, *iter;
186         const int WRLN_MAX_LINE_SIZE = 1024;
187         int key = 65;
188         command_t cmd;
190         if (screen.findbuf) {
191                 g_free(screen.findbuf);
192                 screen.findbuf = NULL;
193         }
194         screen.findbuf = g_malloc0(WRLN_MAX_LINE_SIZE);
195         /* In screen.findbuf is the whole string which is displayed in the status_window
196          * and search_str is the string the user entered (without the prompt) */
197         search_str = screen.findbuf + g_snprintf(screen.findbuf, WRLN_MAX_LINE_SIZE, "%s: ", JUMP_PROMPT);
198         iter = search_str;
200         /* unfortunately wgetch returns "next/previous-page" not as an ascii-char */
201         while(!g_ascii_iscntrl(key) && key != KEY_NPAGE && key != KEY_PPAGE) {
202                 key = screen_getch(screen.findbuf);
203                 /* if backspace or delete was pressed */
204                 if (key == KEY_BACKSPACE || key == 330) {
205                         int i;
206                         /* don't end the loop */
207                         key = 65;
208                         if (search_str <= g_utf8_find_prev_char(screen.findbuf, iter))
209                                 iter = g_utf8_find_prev_char(screen.findbuf, iter);
210                         for (i = 0; *(iter + i) != '\0'; i++)
211                                 *(iter + i) = '\0';
212                         continue;
213                 }
214                 else {
215                         *iter = key;
216                         if (iter < screen.findbuf + WRLN_MAX_LINE_SIZE - 3)
217                                 ++iter;
218                 }
219                 list_window_jump(lw, callback_fn, callback_data, search_str);
220                 /* repaint the list_window */
221                 list_window_paint(lw, callback_fn, callback_data);
222                 wrefresh(lw->w);
223         }
225         /* ncmpc should get the command */
226         ungetch(key);
227         if ((cmd=get_keyboard_command()) != CMD_NONE)
228                 do_input_event(cmd);
231 void
232 screen_display_completion_list(GList *list)
234         static GList *prev_list = NULL;
235         static guint prev_length = 0;
236         static guint offset = 0;
237         WINDOW *w = screen.main_window.w;
238         guint length, y=0;
240         length = g_list_length(list);
241         if (list == prev_list && length == prev_length) {
242                 offset += screen.main_window.rows;
243                 if (offset >= length)
244                         offset = 0;
245         } else {
246                 prev_list = list;
247                 prev_length = length;
248                 offset = 0;
249         }
251         colors_use(w, COLOR_STATUS_ALERT);
252         while (y < screen.main_window.rows) {
253                 GList *item = g_list_nth(list, y+offset);
255                 wmove(w, y++, 0);
256                 wclrtoeol(w);
257                 if (item) {
258                         gchar *tmp = g_strdup(item->data);
259                         waddstr(w, g_basename(tmp));
260                         g_free(tmp);
261                 }
262         }
264         wrefresh(w);
265         doupdate();
266         colors_use(w, COLOR_LIST);
269 #ifndef NCMPC_MINI
270 void
271 set_xterm_title(const char *format, ...)
273         /* the current xterm title exists under the WM_NAME property */
274         /* and can be retrieved with xprop -id $WINDOWID */
276         if (options.enable_xterm_title) {
277                 if (g_getenv("WINDOWID")) {
278                         char *msg;
279                         va_list ap;
281                         va_start(ap,format);
282                         msg = g_strdup_vprintf(format,ap);
283                         va_end(ap);
284                         printf("%c]0;%s%c", '\033', msg, '\007');
285                         g_free(msg);
286                 } else
287                         options.enable_xterm_title = FALSE;
288         }
290 #endif