Code

d8e56df5821671114f9ffadd35dd19b663991924
[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(WINDOW *w, const char *prompt)
52 {
53         int key = -1;
55         colors_use(w, COLOR_STATUS_ALERT);
56         werase(w);
57         wmove(w, 0, 0);
58         waddstr(w, prompt);
60         echo();
61         curs_set(1);
63         while ((key = wgetch(w)) == ERR)
64                 ;
66 #ifdef HAVE_GETMOUSE
67         /* ignore mouse events */
68         if (key == KEY_MOUSE)
69                 return screen_getch(w, prompt);
70 #endif
72         noecho();
73         curs_set(0);
75         return key;
76 }
78 char *
79 screen_readln(WINDOW *w,
80               const char *prompt,
81               const char *value,
82               GList **history,
83               GCompletion *gcmp)
84 {
85         char *line = NULL;
87         wmove(w, 0,0);
88         curs_set(1);
89         colors_use(w, COLOR_STATUS_ALERT);
90         line = wreadln(w, prompt, value, COLS, history, gcmp);
91         curs_set(0);
92         return line;
93 }
95 char *
96 screen_getstr(WINDOW *w, const char *prompt)
97 {
98         return screen_readln(w, prompt, NULL, NULL, NULL);
99 }
101 static char *
102 screen_read_password(WINDOW *w, const char *prompt)
104         char *ret;
106         if (w == NULL) {
107                 int rows, cols;
108                 getmaxyx(stdscr, rows, cols);
109                 /* create window for input */
110                 w = newwin(1,  cols, rows-1, 0);
111                 leaveok(w, FALSE);
112                 keypad(w, TRUE);
113         }
115         wmove(w, 0,0);
116         curs_set(1);
117         colors_use(w, COLOR_STATUS_ALERT);
119         if (prompt == NULL)
120                 prompt = _("Password");
121         ret = wreadln_masked(w, prompt, NULL, COLS, NULL, NULL);
123         curs_set(0);
124         return ret;
127 static gint
128 _screen_auth(struct mpdclient *c, gint recursion)
130         char *password;
132         mpd_connection_clear_error(c->connection);
133         if (recursion > 2)
134                 return 1;
136         password = screen_read_password(NULL, NULL);
137         if (password == NULL)
138                 return 1;
140         mpd_send_password(c->connection, password);
141         g_free(password);
143         mpd_response_finish(c->connection);
144         mpdclient_update(c);
146         if (mpd_connection_get_error(c->connection) == MPD_ERROR_SERVER &&
147             mpd_connection_get_server_error(c->connection) == MPD_SERVER_ERROR_PASSWORD)
148                 return  _screen_auth(c, ++recursion);
149         return 0;
152 gint
153 screen_auth(struct mpdclient *c)
155         gint ret = _screen_auth(c, 0);
156         mpdclient_update(c);
157         curs_set(0);
158         return ret;
161 /* query user for a string and find it in a list window */
162 int
163 screen_find(list_window_t *lw,
164             int rows,
165             command_t findcmd,
166             list_window_callback_fn_t callback_fn,
167             void *callback_data)
169         int reversed = 0;
170         bool found;
171         const char *prompt = FIND_PROMPT;
172         char *value = options.find_show_last_pattern ? (char *) -1 : NULL;
174         if (findcmd == CMD_LIST_RFIND || findcmd == CMD_LIST_RFIND_NEXT) {
175                 prompt = RFIND_PROMPT;
176                 reversed = 1;
177         }
179         switch (findcmd) {
180         case CMD_LIST_FIND:
181         case CMD_LIST_RFIND:
182                 if (screen.findbuf) {
183                         g_free(screen.findbuf);
184                         screen.findbuf=NULL;
185                 }
186                 /* continue... */
188         case CMD_LIST_FIND_NEXT:
189         case CMD_LIST_RFIND_NEXT:
190                 if (!screen.findbuf)
191                         screen.findbuf=screen_readln(screen.status_window.w,
192                                                      prompt,
193                                                      value,
194                                                      &screen.find_history,
195                                                      NULL);
197                 if (screen.findbuf == NULL)
198                         return 1;
200                 found = reversed
201                         ? list_window_rfind(lw,
202                                             callback_fn, callback_data,
203                                             screen.findbuf,
204                                             options.find_wrap,
205                                             options.bell_on_wrap,
206                                             rows)
207                         : list_window_find(lw,
208                                            callback_fn, callback_data,
209                                            screen.findbuf,
210                                            options.find_wrap,
211                                            options.bell_on_wrap);
212                 if (!found) {
213                         screen_status_printf(_("Unable to find \'%s\'"),
214                                              screen.findbuf);
215                         screen_bell();
216                 }
217                 return 1;
218         default:
219                 break;
220         }
221         return 0;
224 /* query user for a string and jump to the entry
225  * which begins with this string while the users types */
226 void
227 screen_jump(struct list_window *lw,
228                 list_window_callback_fn_t callback_fn,
229                 void *callback_data)
231         char *search_str, *iter;
232         const int WRLN_MAX_LINE_SIZE = 1024;
233         int key = 65;
234         command_t cmd;
236         if (screen.findbuf) {
237                 g_free(screen.findbuf);
238                 screen.findbuf = NULL;
239         }
240         screen.findbuf = g_malloc0(WRLN_MAX_LINE_SIZE);
241         /* In screen.findbuf is the whole string which is displayed in the status_window
242          * and search_str is the string the user entered (without the prompt) */
243         search_str = screen.findbuf + g_snprintf(screen.findbuf, WRLN_MAX_LINE_SIZE, "%s: ", JUMP_PROMPT);
244         iter = search_str;
246         /* unfortunately wgetch returns "next/previous-page" not as an ascii-char */
247         while(!g_ascii_iscntrl(key) && key != KEY_NPAGE && key != KEY_PPAGE) {
248                 key = screen_getch(screen.status_window.w, screen.findbuf);
249                 /* if backspace or delete was pressed */
250                 if (key == KEY_BACKSPACE || key == 330) {
251                         int i;
252                         /* don't end the loop */
253                         key = 65;
254                         if (search_str <= g_utf8_find_prev_char(screen.findbuf, iter))
255                                 iter = g_utf8_find_prev_char(screen.findbuf, iter);
256                         for (i = 0; *(iter + i) != '\0'; i++)
257                                 *(iter + i) = '\0';
258                         continue;
259                 }
260                 else {
261                         *iter = key;
262                         if (iter < screen.findbuf + WRLN_MAX_LINE_SIZE - 3)
263                                 ++iter;
264                 }
265                 list_window_jump(lw, callback_fn, callback_data, search_str);
266                 /* repaint the list_window */
267                 list_window_paint(lw, callback_fn, callback_data);
268                 wrefresh(lw->w);
269         }
271         /* ncmpc should get the command */
272         ungetch(key);
273         if ((cmd=get_keyboard_command()) != CMD_NONE)
274                 do_input_event(cmd);
277 void
278 screen_display_completion_list(GList *list)
280         static GList *prev_list = NULL;
281         static guint prev_length = 0;
282         static guint offset = 0;
283         WINDOW *w = screen.main_window.w;
284         guint length, y=0;
286         length = g_list_length(list);
287         if (list == prev_list && length == prev_length) {
288                 offset += screen.main_window.rows;
289                 if (offset >= length)
290                         offset = 0;
291         } else {
292                 prev_list = list;
293                 prev_length = length;
294                 offset = 0;
295         }
297         colors_use(w, COLOR_STATUS_ALERT);
298         while (y < screen.main_window.rows) {
299                 GList *item = g_list_nth(list, y+offset);
301                 wmove(w, y++, 0);
302                 wclrtoeol(w);
303                 if (item) {
304                         gchar *tmp = g_strdup(item->data);
305                         waddstr(w, g_basename(tmp));
306                         g_free(tmp);
307                 }
308         }
310         wrefresh(w);
311         doupdate();
312         colors_use(w, COLOR_LIST);
315 #ifndef NCMPC_MINI
316 void
317 set_xterm_title(const char *format, ...)
319         /* the current xterm title exists under the WM_NAME property */
320         /* and can be retrieved with xprop -id $WINDOWID */
322         if (options.enable_xterm_title) {
323                 if (g_getenv("WINDOWID")) {
324                         char *msg;
325                         va_list ap;
327                         va_start(ap,format);
328                         msg = g_strdup_vprintf(format,ap);
329                         va_end(ap);
330                         printf("%c]0;%s%c", '\033', msg, '\007');
331                         g_free(msg);
332                 } else
333                         options.enable_xterm_title = FALSE;
334         }
336 #endif