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 <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 char *
102 screen_read_password(WINDOW *w, const char *prompt)
103 {
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;
125 }
127 /* query user for a string and find it in a list window */
128 int
129 screen_find(list_window_t *lw,
130 int rows,
131 command_t findcmd,
132 list_window_callback_fn_t callback_fn,
133 void *callback_data)
134 {
135 int reversed = 0;
136 bool found;
137 const char *prompt = FIND_PROMPT;
138 char *value = options.find_show_last_pattern ? (char *) -1 : NULL;
140 if (findcmd == CMD_LIST_RFIND || findcmd == CMD_LIST_RFIND_NEXT) {
141 prompt = RFIND_PROMPT;
142 reversed = 1;
143 }
145 switch (findcmd) {
146 case CMD_LIST_FIND:
147 case CMD_LIST_RFIND:
148 if (screen.findbuf) {
149 g_free(screen.findbuf);
150 screen.findbuf=NULL;
151 }
152 /* continue... */
154 case CMD_LIST_FIND_NEXT:
155 case CMD_LIST_RFIND_NEXT:
156 if (!screen.findbuf)
157 screen.findbuf=screen_readln(screen.status_window.w,
158 prompt,
159 value,
160 &screen.find_history,
161 NULL);
163 if (screen.findbuf == NULL)
164 return 1;
166 found = reversed
167 ? list_window_rfind(lw,
168 callback_fn, callback_data,
169 screen.findbuf,
170 options.find_wrap,
171 options.bell_on_wrap,
172 rows)
173 : list_window_find(lw,
174 callback_fn, callback_data,
175 screen.findbuf,
176 options.find_wrap,
177 options.bell_on_wrap);
178 if (!found) {
179 screen_status_printf(_("Unable to find \'%s\'"),
180 screen.findbuf);
181 screen_bell();
182 }
183 return 1;
184 default:
185 break;
186 }
187 return 0;
188 }
190 /* query user for a string and jump to the entry
191 * which begins with this string while the users types */
192 void
193 screen_jump(struct list_window *lw,
194 list_window_callback_fn_t callback_fn,
195 void *callback_data)
196 {
197 char *search_str, *iter;
198 const int WRLN_MAX_LINE_SIZE = 1024;
199 int key = 65;
200 command_t cmd;
202 if (screen.findbuf) {
203 g_free(screen.findbuf);
204 screen.findbuf = NULL;
205 }
206 screen.findbuf = g_malloc0(WRLN_MAX_LINE_SIZE);
207 /* In screen.findbuf is the whole string which is displayed in the status_window
208 * and search_str is the string the user entered (without the prompt) */
209 search_str = screen.findbuf + g_snprintf(screen.findbuf, WRLN_MAX_LINE_SIZE, "%s: ", JUMP_PROMPT);
210 iter = search_str;
212 /* unfortunately wgetch returns "next/previous-page" not as an ascii-char */
213 while(!g_ascii_iscntrl(key) && key != KEY_NPAGE && key != KEY_PPAGE) {
214 key = screen_getch(screen.status_window.w, screen.findbuf);
215 /* if backspace or delete was pressed */
216 if (key == KEY_BACKSPACE || key == 330) {
217 int i;
218 /* don't end the loop */
219 key = 65;
220 if (search_str <= g_utf8_find_prev_char(screen.findbuf, iter))
221 iter = g_utf8_find_prev_char(screen.findbuf, iter);
222 for (i = 0; *(iter + i) != '\0'; i++)
223 *(iter + i) = '\0';
224 continue;
225 }
226 else {
227 *iter = key;
228 if (iter < screen.findbuf + WRLN_MAX_LINE_SIZE - 3)
229 ++iter;
230 }
231 list_window_jump(lw, callback_fn, callback_data, search_str);
232 /* repaint the list_window */
233 list_window_paint(lw, callback_fn, callback_data);
234 wrefresh(lw->w);
235 }
237 /* ncmpc should get the command */
238 ungetch(key);
239 if ((cmd=get_keyboard_command()) != CMD_NONE)
240 do_input_event(cmd);
241 }
243 void
244 screen_display_completion_list(GList *list)
245 {
246 static GList *prev_list = NULL;
247 static guint prev_length = 0;
248 static guint offset = 0;
249 WINDOW *w = screen.main_window.w;
250 guint length, y=0;
252 length = g_list_length(list);
253 if (list == prev_list && length == prev_length) {
254 offset += screen.main_window.rows;
255 if (offset >= length)
256 offset = 0;
257 } else {
258 prev_list = list;
259 prev_length = length;
260 offset = 0;
261 }
263 colors_use(w, COLOR_STATUS_ALERT);
264 while (y < screen.main_window.rows) {
265 GList *item = g_list_nth(list, y+offset);
267 wmove(w, y++, 0);
268 wclrtoeol(w);
269 if (item) {
270 gchar *tmp = g_strdup(item->data);
271 waddstr(w, g_basename(tmp));
272 g_free(tmp);
273 }
274 }
276 wrefresh(w);
277 doupdate();
278 colors_use(w, COLOR_LIST);
279 }
281 #ifndef NCMPC_MINI
282 void
283 set_xterm_title(const char *format, ...)
284 {
285 /* the current xterm title exists under the WM_NAME property */
286 /* and can be retrieved with xprop -id $WINDOWID */
288 if (options.enable_xterm_title) {
289 if (g_getenv("WINDOWID")) {
290 char *msg;
291 va_list ap;
293 va_start(ap,format);
294 msg = g_strdup_vprintf(format,ap);
295 va_end(ap);
296 printf("%c]0;%s%c", '\033', msg, '\007');
297 g_free(msg);
298 } else
299 options.enable_xterm_title = FALSE;
300 }
301 }
302 #endif