1 /*
2 * $Id$
3 *
4 * (c) 2004 by Kalle Wallin <kaw@linux.se>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <glib.h>
25 #include <ncurses.h>
27 #include "config.h"
28 #include "ncmpc.h"
29 #include "mpdclient.h"
30 #include "support.h"
31 #include "command.h"
32 #include "options.h"
33 #include "list_window.h"
34 #include "colors.h"
35 #include "wreadln.h"
36 #include "screen.h"
38 #define FIND_PROMPT _("Find: ")
39 #define RFIND_PROMPT _("Find backward: ")
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;
54 int prompt_len = strlen(prompt);
56 colors_use(w, COLOR_STATUS_ALERT);
57 wclear(w);
58 wmove(w, 0, 0);
59 waddstr(w, prompt);
60 wmove(w, 0, prompt_len);
62 echo();
63 curs_set(1);
64 timeout(-1);
66 while( (key=my_wgetch(w)) == ERR )
67 ;
69 #ifdef HAVE_GETMOUSE
70 /* ignore mouse events */
71 if( key==KEY_MOUSE )
72 return screen_getch(w, prompt);
73 #endif
75 noecho();
76 curs_set(0);
77 timeout(SCREEN_TIMEOUT);
79 return key;
80 }
82 char *
83 screen_readln(WINDOW *w,
84 const char *prompt,
85 const char *value,
86 GList **history,
87 GCompletion *gcmp)
88 {
89 char *line = NULL;
91 wmove(w, 0,0);
92 curs_set(1);
93 colors_use(w, COLOR_STATUS_ALERT);
94 line = wreadln(w, prompt, value, COLS, history, gcmp);
95 curs_set(0);
96 return line;
97 }
99 char *
100 screen_getstr(WINDOW *w, const char *prompt)
101 {
102 return screen_readln(w, prompt, NULL, NULL, NULL);
103 }
105 static char *
106 screen_read_password(WINDOW *w, const char *prompt)
107 {
108 if(w == NULL)
109 {
110 int rows, cols;
111 getmaxyx(stdscr, rows, cols);
112 /* create window for input */
113 w = newwin(1, cols, rows-1, 0);
114 leaveok(w, FALSE);
115 keypad(w, TRUE);
116 }
117 wmove(w, 0,0);
118 curs_set(1);
119 colors_use(w, COLOR_STATUS_ALERT);
120 if(prompt == NULL)
121 return wreadln_masked(w, _("Password: "), NULL, COLS, NULL, NULL);
122 else
123 return wreadln_masked(w, prompt, NULL, COLS, NULL, NULL);
124 curs_set(0);
125 }
127 static gint
128 _screen_auth(mpdclient_t *c, gint recursion)
129 {
130 mpd_clearError(c->connection);
131 if(recursion > 2) return 1;
132 mpd_sendPasswordCommand(c->connection, screen_read_password(NULL, NULL));
133 mpd_finishCommand(c->connection);
134 mpdclient_update(c);
135 if( c->connection->errorCode == MPD_ACK_ERROR_PASSWORD ) return _screen_auth(c, ++recursion);
136 return 0;
137 }
139 gint
140 screen_auth(mpdclient_t *c)
141 {
142 _screen_auth(c, 0);
143 mpdclient_update(c);
144 curs_set(0);
145 }
147 /* query user for a string and find it in a list window */
148 int
149 screen_find(screen_t *screen,
150 list_window_t *lw,
151 int rows,
152 command_t findcmd,
153 list_window_callback_fn_t callback_fn,
154 void *callback_data)
155 {
156 int reversed = 0;
157 int retval = 0;
158 const char *prompt = FIND_PROMPT;
159 char *value = options.find_show_last_pattern ? (char *) -1 : NULL;
161 if (findcmd == CMD_LIST_RFIND || findcmd == CMD_LIST_RFIND_NEXT) {
162 prompt = RFIND_PROMPT;
163 reversed = 1;
164 }
166 switch(findcmd)
167 {
168 case CMD_LIST_FIND:
169 case CMD_LIST_RFIND:
170 if( screen->findbuf )
171 {
172 g_free(screen->findbuf);
173 screen->findbuf=NULL;
174 }
175 /* continue... */
176 case CMD_LIST_FIND_NEXT:
177 case CMD_LIST_RFIND_NEXT:
178 if( !screen->findbuf )
179 screen->findbuf=screen_readln(screen->status_window.w,
180 prompt,
181 value,
182 &screen->find_history,
183 NULL);
184 if( !screen->findbuf || !screen->findbuf[0] )
185 return 1;
186 if( reversed )
187 retval = list_window_rfind(lw,
188 callback_fn,
189 callback_data,
190 screen->findbuf,
191 options.find_wrap,
192 rows);
193 else
194 retval = list_window_find(lw,
195 callback_fn,
196 callback_data,
197 screen->findbuf,
198 options.find_wrap);
199 if( retval == 0 )
200 {
201 lw->repaint = 1;
202 }
203 else
204 {
205 screen_status_printf(_("Unable to find \'%s\'"), screen->findbuf);
206 screen_bell();
207 }
208 return 1;
209 default:
210 break;
211 }
212 return 0;
213 }
215 void
216 screen_display_completion_list(screen_t *screen, GList *list)
217 {
218 static GList *prev_list = NULL;
219 static gint prev_length = 0;
220 static gint offset = 0;
221 WINDOW *w = screen->main_window.w;
222 gint length, y=0;
224 length = g_list_length(list);
225 if( list==prev_list && length==prev_length )
226 {
227 offset += screen->main_window.rows;
228 if( offset>=length )
229 offset=0;
230 }
231 else
232 {
233 prev_list = list;
234 prev_length = length;
235 offset = 0;
236 }
238 colors_use(w, COLOR_STATUS_ALERT);
239 while( y<screen->main_window.rows )
240 {
241 GList *item = g_list_nth(list, y+offset);
243 wmove(w, y++, 0);
244 wclrtoeol(w);
245 if( item )
246 {
247 gchar *tmp = g_strdup(item->data);
248 waddstr(w, basename(tmp));
249 g_free(tmp);
250 }
251 }
252 wrefresh(w);
253 doupdate();
254 colors_use(w, COLOR_LIST);
255 }
257 void
258 set_xterm_title(const char *format, ...)
259 {
260 /* the current xterm title exists under the WM_NAME property */
261 /* and can be retreived with xprop -id $WINDOWID */
263 if( options.enable_xterm_title )
264 {
265 if( g_getenv("WINDOWID") )
266 {
267 char *msg;
268 va_list ap;
270 va_start(ap,format);
271 msg = g_strdup_vprintf(format,ap);
272 va_end(ap);
273 printf("%c]0;%s%c", '\033', msg, '\007');
274 g_free(msg);
275 }
276 else
277 options.enable_xterm_title = FALSE;
278 }
279 }