Code

screen_utils: Added new function which jumps to an entry.
[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 <stdlib.h>
33 #include <unistd.h>
35 #define FIND_PROMPT  _("Find")
36 #define RFIND_PROMPT _("Find backward")
37 #define JUMP_PROMPT _("Jump")
39 void
40 screen_bell(void)
41 {
42         if (options.audible_bell)
43                 beep();
44         if (options.visible_bell)
45                 flash();
46 }
48 int
49 screen_getch(WINDOW *w, const char *prompt)
50 {
51         int key = -1;
53         colors_use(w, COLOR_STATUS_ALERT);
54         werase(w);
55         wmove(w, 0, 0);
56         waddstr(w, prompt);
58         echo();
59         curs_set(1);
61         while ((key = wgetch(w)) == ERR)
62                 ;
64 #ifdef HAVE_GETMOUSE
65         /* ignore mouse events */
66         if (key == KEY_MOUSE)
67                 return screen_getch(w, prompt);
68 #endif
70         noecho();
71         curs_set(0);
73         return key;
74 }
76 char *
77 screen_readln(WINDOW *w,
78               const char *prompt,
79               const char *value,
80               GList **history,
81               GCompletion *gcmp)
82 {
83         char *line = NULL;
85         wmove(w, 0,0);
86         curs_set(1);
87         colors_use(w, COLOR_STATUS_ALERT);
88         line = wreadln(w, prompt, value, COLS, history, gcmp);
89         curs_set(0);
90         return line;
91 }
93 char *
94 screen_getstr(WINDOW *w, const char *prompt)
95 {
96         return screen_readln(w, prompt, NULL, NULL, NULL);
97 }
99 static char *
100 screen_read_password(WINDOW *w, const char *prompt)
102         char *ret;
104         if (w == NULL) {
105                 int rows, cols;
106                 getmaxyx(stdscr, rows, cols);
107                 /* create window for input */
108                 w = newwin(1,  cols, rows-1, 0);
109                 leaveok(w, FALSE);
110                 keypad(w, TRUE);
111         }
113         wmove(w, 0,0);
114         curs_set(1);
115         colors_use(w, COLOR_STATUS_ALERT);
117         if (prompt == NULL)
118                 prompt = _("Password: ");
119         ret = wreadln_masked(w, prompt, NULL, COLS, NULL, NULL);
121         curs_set(0);
122         return ret;
125 static gint
126 _screen_auth(struct mpdclient *c, gint recursion)
128         char *password;
130         mpd_clearError(c->connection);
131         if (recursion > 2)
132                 return 1;
134         password = screen_read_password(NULL, NULL);
135         if (password == NULL)
136                 return 1;
138         mpd_sendPasswordCommand(c->connection, password);
139         g_free(password);
141         mpd_finishCommand(c->connection);
142         mpdclient_update(c);
143         if (c->connection->errorCode == MPD_ACK_ERROR_PASSWORD)
144                 return  _screen_auth(c, ++recursion);
145         return 0;
148 gint
149 screen_auth(struct mpdclient *c)
151         gint ret = _screen_auth(c, 0);
152         mpdclient_update(c);
153         curs_set(0);
154         return ret;
157 /* query user for a string and find it in a list window */
158 int
159 screen_find(list_window_t *lw,
160             int rows,
161             command_t findcmd,
162             list_window_callback_fn_t callback_fn,
163             void *callback_data)
165         int reversed = 0;
166         bool found;
167         const char *prompt = FIND_PROMPT;
168         char *value = options.find_show_last_pattern ? (char *) -1 : NULL;
170         if (findcmd == CMD_LIST_RFIND || findcmd == CMD_LIST_RFIND_NEXT) {
171                 prompt = RFIND_PROMPT;
172                 reversed = 1;
173         }
175         switch (findcmd) {
176         case CMD_LIST_FIND:
177         case CMD_LIST_RFIND:
178                 if (screen.findbuf) {
179                         g_free(screen.findbuf);
180                         screen.findbuf=NULL;
181                 }
182                 /* continue... */
184         case CMD_LIST_FIND_NEXT:
185         case CMD_LIST_RFIND_NEXT:
186                 if (!screen.findbuf)
187                         screen.findbuf=screen_readln(screen.status_window.w,
188                                                      prompt,
189                                                      value,
190                                                      &screen.find_history,
191                                                      NULL);
193                 if (screen.findbuf == NULL)
194                         return 1;
196                 found = reversed
197                         ? list_window_rfind(lw,
198                                             callback_fn, callback_data,
199                                             screen.findbuf,
200                                             options.find_wrap,
201                                             options.bell_on_wrap,
202                                             rows)
203                         : list_window_find(lw,
204                                            callback_fn, callback_data,
205                                            screen.findbuf,
206                                            options.find_wrap,
207                                            options.bell_on_wrap);
208                 if (!found) {
209                         screen_status_printf(_("Unable to find \'%s\'"),
210                                              screen.findbuf);
211                         screen_bell();
212                 }
213                 return 1;
214         default:
215                 break;
216         }
217         return 0;
220 /* query user for a string and jump to the entry
221  * which begins with this string while the users types */
222 void
223 screen_jump(struct list_window *lw,
224                 list_window_callback_fn_t callback_fn,
225                 void *callback_data)
227         char *search_str, *iter;
228         const int WRLN_MAX_LINE_SIZE = 1024;
229         int key = 65;
230         command_t cmd;
232         if (screen.findbuf) {
233                 g_free(screen.findbuf);
234                 screen.findbuf = NULL;
235         }
236         screen.findbuf = g_malloc0(WRLN_MAX_LINE_SIZE);
237         /* In screen.findbuf is the whole string which is displayed in the status_window
238          * and search_str is the string the user entered (without the prompt) */
239         search_str = screen.findbuf + g_snprintf(screen.findbuf, WRLN_MAX_LINE_SIZE, "%s: ", JUMP_PROMPT);
240         iter = search_str;
242         /* unfortunately wgetch returns "next/previous-page" not as an ascii-char */
243         while(!g_ascii_iscntrl(key) && key != KEY_NPAGE && key != KEY_PPAGE) {
244                 key = screen_getch(screen.status_window.w, screen.findbuf);
245                 /* if backspace was pressed */
246                 if (key == KEY_BACKSPACE) {
247                         /* don't end the loop */
248                         key = 65;
249                         if (search_str <= g_utf8_find_prev_char(screen.findbuf, iter))
250                                 iter = g_utf8_find_prev_char(screen.findbuf, iter);
251                         *iter = '\0';
252                         continue;
253                 }
254                 else {
255                         *iter = key;
256                         if (iter < screen.findbuf + WRLN_MAX_LINE_SIZE - 3)
257                                 ++iter;
258                 }
259                 list_window_jump(lw, callback_fn, callback_data, search_str);
260                 /* repaint the list_window */
261                 list_window_paint(lw, callback_fn, callback_data);
262                 wrefresh(lw->w);
263         }
265         /* ncmpc should get the command */
266         ungetch(key);
267         if ((cmd=get_keyboard_command()) != CMD_NONE)
268                 do_input_event(cmd);
271 void
272 screen_display_completion_list(GList *list)
274         static GList *prev_list = NULL;
275         static guint prev_length = 0;
276         static guint offset = 0;
277         WINDOW *w = screen.main_window.w;
278         guint length, y=0;
280         length = g_list_length(list);
281         if (list == prev_list && length == prev_length) {
282                 offset += screen.main_window.rows;
283                 if (offset >= length)
284                         offset = 0;
285         } else {
286                 prev_list = list;
287                 prev_length = length;
288                 offset = 0;
289         }
291         colors_use(w, COLOR_STATUS_ALERT);
292         while (y < screen.main_window.rows) {
293                 GList *item = g_list_nth(list, y+offset);
295                 wmove(w, y++, 0);
296                 wclrtoeol(w);
297                 if (item) {
298                         gchar *tmp = g_strdup(item->data);
299                         waddstr(w, g_basename(tmp));
300                         g_free(tmp);
301                 }
302         }
304         wrefresh(w);
305         doupdate();
306         colors_use(w, COLOR_LIST);
309 #ifndef NCMPC_MINI
310 void
311 set_xterm_title(const char *format, ...)
313         /* the current xterm title exists under the WM_NAME property */
314         /* and can be retreived with xprop -id $WINDOWID */
316         if (options.enable_xterm_title) {
317                 if (g_getenv("WINDOWID")) {
318                         char *msg;
319                         va_list ap;
321                         va_start(ap,format);
322                         msg = g_strdup_vprintf(format,ap);
323                         va_end(ap);
324                         printf("%c]0;%s%c", '\033', msg, '\007');
325                         g_free(msg);
326                 } else
327                         options.enable_xterm_title = FALSE;
328         }
330 #endif