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, 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 char *prompt,
85 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, char *prompt)
101 {
102 return screen_readln(w, prompt, NULL, NULL, NULL);
103 }
105 char *
106 screen_getstr_masked(WINDOW *w, char *prompt)
107 {
108 return screen_readln(w, prompt, NULL, NULL, NULL);
109 }
112 char *
113 screen_read_password(WINDOW *w, char *prompt)
114 {
115 if(w == NULL)
116 {
117 int rows, cols;
118 getmaxyx(stdscr, rows, cols);
119 /* create window for input */
120 w = newwin(1, cols, rows-1, 0);
121 leaveok(w, FALSE);
122 keypad(w, TRUE);
123 }
124 wmove(w, 0,0);
125 curs_set(1);
126 colors_use(w, COLOR_STATUS_ALERT);
127 if(prompt == NULL)
128 return wreadln_masked(w, _("Password: "), NULL, COLS, NULL, NULL);
129 else
130 return wreadln_masked(w, prompt, NULL, COLS, NULL, NULL);
131 curs_set(0);
132 }
134 gint
135 _screen_auth(mpdclient_t *c, gint recursion)
136 {
137 mpd_clearError(c->connection);
138 if(recursion > 2) return 1;
139 mpd_sendPasswordCommand(c->connection, screen_read_password(NULL, NULL));
140 mpd_finishCommand(c->connection);
141 mpdclient_update(c);
142 if( c->connection->errorCode == MPD_ACK_ERROR_PASSWORD ) return _screen_auth(c, ++recursion);
143 return 0;
144 }
146 gint
147 screen_auth(mpdclient_t *c)
148 {
149 _screen_auth(c, 0);
150 mpdclient_update(c);
151 curs_set(0);
152 }
154 /* query user for a string and find it in a list window */
155 int
156 screen_find(screen_t *screen,
157 mpdclient_t *c,
158 list_window_t *lw,
159 int rows,
160 command_t findcmd,
161 list_window_callback_fn_t callback_fn,
162 void *callback_data)
163 {
164 int reversed = 0;
165 int retval = 0;
166 char *prompt = FIND_PROMPT;
167 char *value = options.find_show_last_pattern ? (char *) -1 : NULL;
169 if( findcmd==CMD_LIST_RFIND ||findcmd==CMD_LIST_RFIND_NEXT )
170 {
171 prompt = RFIND_PROMPT;
172 reversed = 1;
173 }
175 switch(findcmd)
176 {
177 case CMD_LIST_FIND:
178 case CMD_LIST_RFIND:
179 if( screen->findbuf )
180 {
181 g_free(screen->findbuf);
182 screen->findbuf=NULL;
183 }
184 /* continue... */
185 case CMD_LIST_FIND_NEXT:
186 case CMD_LIST_RFIND_NEXT:
187 if( !screen->findbuf )
188 screen->findbuf=screen_readln(screen->status_window.w,
189 prompt,
190 value,
191 &screen->find_history,
192 NULL);
193 if( !screen->findbuf || !screen->findbuf[0] )
194 return 1;
195 if( reversed )
196 retval = list_window_rfind(lw,
197 callback_fn,
198 callback_data,
199 screen->findbuf,
200 options.find_wrap,
201 rows);
202 else
203 retval = list_window_find(lw,
204 callback_fn,
205 callback_data,
206 screen->findbuf,
207 options.find_wrap);
208 if( retval == 0 )
209 {
210 lw->repaint = 1;
211 }
212 else
213 {
214 screen_status_printf(_("Unable to find \'%s\'"), screen->findbuf);
215 screen_bell();
216 }
217 return 1;
218 default:
219 break;
220 }
221 return 0;
222 }
224 void
225 screen_display_completion_list(screen_t *screen, GList *list)
226 {
227 static GList *prev_list = NULL;
228 static gint prev_length = 0;
229 static gint offset = 0;
230 WINDOW *w = screen->main_window.w;
231 gint length, y=0;
233 length = g_list_length(list);
234 if( list==prev_list && length==prev_length )
235 {
236 offset += screen->main_window.rows;
237 if( offset>=length )
238 offset=0;
239 }
240 else
241 {
242 prev_list = list;
243 prev_length = length;
244 offset = 0;
245 }
247 colors_use(w, COLOR_STATUS_ALERT);
248 while( y<screen->main_window.rows )
249 {
250 GList *item = g_list_nth(list, y+offset);
252 wmove(w, y++, 0);
253 wclrtoeol(w);
254 if( item )
255 {
256 gchar *tmp = g_strdup(item->data);
257 waddstr(w, basename(tmp));
258 g_free(tmp);
259 }
260 }
261 wrefresh(w);
262 doupdate();
263 colors_use(w, COLOR_LIST);
264 }
266 void
267 set_xterm_title(char *format, ...)
268 {
269 /* the current xterm title exists under the WM_NAME property */
270 /* and can be retreived with xprop -id $WINDOWID */
272 if( options.enable_xterm_title )
273 {
274 if( g_getenv("WINDOWID") )
275 {
276 char *msg;
277 va_list ap;
279 va_start(ap,format);
280 msg = g_strdup_vprintf(format,ap);
281 va_end(ap);
282 printf("%c]0;%s%c", '\033', msg, '\007');
283 g_free(msg);
284 }
285 else
286 options.enable_xterm_title = FALSE;
287 }
288 }