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_message.h"
22 #include "screen.h"
23 #include "mpdclient.h"
24 #include "config.h"
25 #include "i18n.h"
26 #include "options.h"
27 #include "colors.h"
28 #include "wreadln.h"
29 #ifndef NCMPC_H
30 #include "ncmpc.h"
31 #endif /* NCMPC_H */
33 #include <mpd/client.h>
35 #include <stdlib.h>
36 #include <unistd.h>
38 #define FIND_PROMPT _("Find")
39 #define RFIND_PROMPT _("Find backward")
40 #define JUMP_PROMPT _("Jump")
42 void
43 screen_bell(void)
44 {
45 if (options.audible_bell)
46 beep();
47 if (options.visible_bell)
48 flash();
49 }
51 int
52 screen_getch(const char *prompt)
53 {
54 WINDOW *w = screen.status_bar.window.w;
55 int key = -1;
57 colors_use(w, COLOR_STATUS_ALERT);
58 werase(w);
59 wmove(w, 0, 0);
60 waddstr(w, prompt);
62 echo();
63 curs_set(1);
65 while ((key = wgetch(w)) == ERR)
66 ;
68 #ifdef HAVE_GETMOUSE
69 /* ignore mouse events */
70 if (key == KEY_MOUSE)
71 return screen_getch(prompt);
72 #endif
74 noecho();
75 curs_set(0);
77 return key;
78 }
80 char *
81 screen_readln(const char *prompt,
82 const char *value,
83 GList **history,
84 GCompletion *gcmp)
85 {
86 struct window *window = &screen.status_bar.window;
87 WINDOW *w = window->w;
88 char *line = NULL;
90 wmove(w, 0,0);
91 curs_set(1);
92 colors_use(w, COLOR_STATUS_ALERT);
93 line = wreadln(w, prompt, value, window->cols, history, gcmp);
94 curs_set(0);
95 return line;
96 }
98 char *
99 screen_read_password(const char *prompt)
100 {
101 struct window *window = &screen.status_bar.window;
102 WINDOW *w = window->w;
103 char *ret;
105 wmove(w, 0,0);
106 curs_set(1);
107 colors_use(w, COLOR_STATUS_ALERT);
109 if (prompt == NULL)
110 prompt = _("Password");
111 ret = wreadln_masked(w, prompt, NULL, window->cols, NULL, NULL);
113 curs_set(0);
114 return ret;
115 }
117 /* query user for a string and find it in a list window */
118 int
119 screen_find(list_window_t *lw,
120 int rows,
121 command_t findcmd,
122 list_window_callback_fn_t callback_fn,
123 void *callback_data)
124 {
125 int reversed = 0;
126 bool found;
127 const char *prompt = FIND_PROMPT;
128 char *value = options.find_show_last_pattern ? (char *) -1 : NULL;
130 if (findcmd == CMD_LIST_RFIND || findcmd == CMD_LIST_RFIND_NEXT) {
131 prompt = RFIND_PROMPT;
132 reversed = 1;
133 }
135 switch (findcmd) {
136 case CMD_LIST_FIND:
137 case CMD_LIST_RFIND:
138 if (screen.findbuf) {
139 g_free(screen.findbuf);
140 screen.findbuf=NULL;
141 }
142 /* continue... */
144 case CMD_LIST_FIND_NEXT:
145 case CMD_LIST_RFIND_NEXT:
146 if (!screen.findbuf)
147 screen.findbuf=screen_readln(prompt,
148 value,
149 &screen.find_history,
150 NULL);
152 if (screen.findbuf == NULL)
153 return 1;
155 found = reversed
156 ? list_window_rfind(lw,
157 callback_fn, callback_data,
158 screen.findbuf,
159 options.find_wrap,
160 options.bell_on_wrap,
161 rows)
162 : list_window_find(lw,
163 callback_fn, callback_data,
164 screen.findbuf,
165 options.find_wrap,
166 options.bell_on_wrap);
167 if (!found) {
168 screen_status_printf(_("Unable to find \'%s\'"),
169 screen.findbuf);
170 screen_bell();
171 }
172 return 1;
173 default:
174 break;
175 }
176 return 0;
177 }
179 /* query user for a string and jump to the entry
180 * which begins with this string while the users types */
181 void
182 screen_jump(struct list_window *lw,
183 list_window_callback_fn_t callback_fn,
184 void *callback_data)
185 {
186 char *search_str, *iter;
187 const int WRLN_MAX_LINE_SIZE = 1024;
188 int key = 65;
189 command_t cmd;
191 if (screen.findbuf) {
192 g_free(screen.findbuf);
193 screen.findbuf = NULL;
194 }
195 screen.findbuf = g_malloc0(WRLN_MAX_LINE_SIZE);
196 /* In screen.findbuf is the whole string which is displayed in the status_window
197 * and search_str is the string the user entered (without the prompt) */
198 search_str = screen.findbuf + g_snprintf(screen.findbuf, WRLN_MAX_LINE_SIZE, "%s: ", JUMP_PROMPT);
199 iter = search_str;
201 /* unfortunately wgetch returns "next/previous-page" not as an ascii-char */
202 while(!g_ascii_iscntrl(key) && key != KEY_NPAGE && key != KEY_PPAGE) {
203 key = screen_getch(screen.findbuf);
204 /* if backspace or delete was pressed */
205 if (key == KEY_BACKSPACE || key == 330) {
206 int i;
207 /* don't end the loop */
208 key = 65;
209 if (search_str <= g_utf8_find_prev_char(screen.findbuf, iter))
210 iter = g_utf8_find_prev_char(screen.findbuf, iter);
211 for (i = 0; *(iter + i) != '\0'; i++)
212 *(iter + i) = '\0';
213 continue;
214 }
215 else {
216 *iter = key;
217 if (iter < screen.findbuf + WRLN_MAX_LINE_SIZE - 3)
218 ++iter;
219 }
220 list_window_jump(lw, callback_fn, callback_data, search_str);
221 /* repaint the list_window */
222 list_window_paint(lw, callback_fn, callback_data);
223 wrefresh(lw->w);
224 }
226 /* ncmpc should get the command */
227 ungetch(key);
228 if ((cmd=get_keyboard_command()) != CMD_NONE)
229 do_input_event(cmd);
230 }
232 void
233 screen_display_completion_list(GList *list)
234 {
235 static GList *prev_list = NULL;
236 static guint prev_length = 0;
237 static guint offset = 0;
238 WINDOW *w = screen.main_window.w;
239 guint length, y=0;
241 length = g_list_length(list);
242 if (list == prev_list && length == prev_length) {
243 offset += screen.main_window.rows;
244 if (offset >= length)
245 offset = 0;
246 } else {
247 prev_list = list;
248 prev_length = length;
249 offset = 0;
250 }
252 colors_use(w, COLOR_STATUS_ALERT);
253 while (y < screen.main_window.rows) {
254 GList *item = g_list_nth(list, y+offset);
256 wmove(w, y++, 0);
257 wclrtoeol(w);
258 if (item) {
259 gchar *tmp = g_strdup(item->data);
260 waddstr(w, g_basename(tmp));
261 g_free(tmp);
262 }
263 }
265 wrefresh(w);
266 doupdate();
267 colors_use(w, COLOR_LIST);
268 }
270 #ifndef NCMPC_MINI
271 void
272 set_xterm_title(const char *format, ...)
273 {
274 /* the current xterm title exists under the WM_NAME property */
275 /* and can be retrieved with xprop -id $WINDOWID */
277 if (options.enable_xterm_title) {
278 if (g_getenv("WINDOWID")) {
279 char *msg;
280 va_list ap;
282 va_start(ap,format);
283 msg = g_strdup_vprintf(format,ap);
284 va_end(ap);
285 printf("%c]0;%s%c", '\033', msg, '\007');
286 g_free(msg);
287 } else
288 options.enable_xterm_title = FALSE;
289 }
290 }
291 #endif