1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
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)
101 {
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;
123 }
125 static gint
126 _screen_auth(struct mpdclient *c, gint recursion)
127 {
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;
146 }
148 gint
149 screen_auth(struct mpdclient *c)
150 {
151 gint ret = _screen_auth(c, 0);
152 mpdclient_update(c);
153 curs_set(0);
154 return ret;
155 }
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)
164 {
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;
218 }
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)
226 {
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 or delete was pressed */
246 if (key == KEY_BACKSPACE || key == 330) {
247 int i;
248 /* don't end the loop */
249 key = 65;
250 if (search_str <= g_utf8_find_prev_char(screen.findbuf, iter))
251 iter = g_utf8_find_prev_char(screen.findbuf, iter);
252 for (i = 0; *(iter + i) != '\0'; i++)
253 *(iter + i) = '\0';
254 continue;
255 }
256 else {
257 *iter = key;
258 if (iter < screen.findbuf + WRLN_MAX_LINE_SIZE - 3)
259 ++iter;
260 }
261 list_window_jump(lw, callback_fn, callback_data, search_str);
262 /* repaint the list_window */
263 list_window_paint(lw, callback_fn, callback_data);
264 wrefresh(lw->w);
265 }
267 /* ncmpc should get the command */
268 ungetch(key);
269 if ((cmd=get_keyboard_command()) != CMD_NONE)
270 do_input_event(cmd);
271 }
273 void
274 screen_display_completion_list(GList *list)
275 {
276 static GList *prev_list = NULL;
277 static guint prev_length = 0;
278 static guint offset = 0;
279 WINDOW *w = screen.main_window.w;
280 guint length, y=0;
282 length = g_list_length(list);
283 if (list == prev_list && length == prev_length) {
284 offset += screen.main_window.rows;
285 if (offset >= length)
286 offset = 0;
287 } else {
288 prev_list = list;
289 prev_length = length;
290 offset = 0;
291 }
293 colors_use(w, COLOR_STATUS_ALERT);
294 while (y < screen.main_window.rows) {
295 GList *item = g_list_nth(list, y+offset);
297 wmove(w, y++, 0);
298 wclrtoeol(w);
299 if (item) {
300 gchar *tmp = g_strdup(item->data);
301 waddstr(w, g_basename(tmp));
302 g_free(tmp);
303 }
304 }
306 wrefresh(w);
307 doupdate();
308 colors_use(w, COLOR_LIST);
309 }
311 #ifndef NCMPC_MINI
312 void
313 set_xterm_title(const char *format, ...)
314 {
315 /* the current xterm title exists under the WM_NAME property */
316 /* and can be retrieved with xprop -id $WINDOWID */
318 if (options.enable_xterm_title) {
319 if (g_getenv("WINDOWID")) {
320 char *msg;
321 va_list ap;
323 va_start(ap,format);
324 msg = g_strdup_vprintf(format,ap);
325 va_end(ap);
326 printf("%c]0;%s%c", '\033', msg, '\007');
327 g_free(msg);
328 } else
329 options.enable_xterm_title = FALSE;
330 }
331 }
332 #endif