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 "screen_utils.h"
22 #include "screen.h"
23 #include "mpdclient.h"
24 #include "config.h"
25 #include "ncmpc.h"
26 #include "support.h"
27 #include "options.h"
28 #include "colors.h"
29 #include "wreadln.h"
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <string.h>
35 #define FIND_PROMPT _("Find: ")
36 #define RFIND_PROMPT _("Find backward: ")
38 void
39 screen_bell(void)
40 {
41 if (options.audible_bell)
42 beep();
43 if (options.visible_bell)
44 flash();
45 }
47 int
48 screen_getch(WINDOW *w, const char *prompt)
49 {
50 int key = -1;
51 int prompt_len = strlen(prompt);
53 colors_use(w, COLOR_STATUS_ALERT);
54 wclear(w);
55 wmove(w, 0, 0);
56 waddstr(w, prompt);
57 wmove(w, 0, prompt_len);
59 echo();
60 curs_set(1);
62 while ((key=my_wgetch(w)) == ERR)
63 ;
65 #ifdef HAVE_GETMOUSE
66 /* ignore mouse events */
67 if (key == KEY_MOUSE)
68 return screen_getch(w, prompt);
69 #endif
71 noecho();
72 curs_set(0);
74 return key;
75 }
77 char *
78 screen_readln(WINDOW *w,
79 const char *prompt,
80 const char *value,
81 GList **history,
82 GCompletion *gcmp)
83 {
84 char *line = NULL;
86 wmove(w, 0,0);
87 curs_set(1);
88 colors_use(w, COLOR_STATUS_ALERT);
89 line = wreadln(w, prompt, value, COLS, history, gcmp);
90 curs_set(0);
91 return line;
92 }
94 char *
95 screen_getstr(WINDOW *w, const char *prompt)
96 {
97 return screen_readln(w, prompt, NULL, NULL, NULL);
98 }
100 static char *
101 screen_read_password(WINDOW *w, const char *prompt)
102 {
103 char *ret;
105 if (w == NULL) {
106 int rows, cols;
107 getmaxyx(stdscr, rows, cols);
108 /* create window for input */
109 w = newwin(1, cols, rows-1, 0);
110 leaveok(w, FALSE);
111 keypad(w, TRUE);
112 }
114 wmove(w, 0,0);
115 curs_set(1);
116 colors_use(w, COLOR_STATUS_ALERT);
118 if (prompt == NULL)
119 ret = wreadln_masked(w, _("Password: "), NULL, COLS, NULL, NULL);
120 else
121 ret = wreadln_masked(w, prompt, NULL, COLS, NULL, NULL);
123 curs_set(0);
124 return ret;
125 }
127 static gint
128 _screen_auth(struct mpdclient *c, gint recursion)
129 {
130 mpd_clearError(c->connection);
131 if (recursion > 2)
132 return 1;
133 mpd_sendPasswordCommand(c->connection, screen_read_password(NULL, NULL));
134 mpd_finishCommand(c->connection);
135 mpdclient_update(c);
136 if (c->connection->errorCode == MPD_ACK_ERROR_PASSWORD)
137 return _screen_auth(c, ++recursion);
138 return 0;
139 }
141 gint
142 screen_auth(struct mpdclient *c)
143 {
144 gint ret = _screen_auth(c, 0);
145 mpdclient_update(c);
146 curs_set(0);
147 return ret;
148 }
150 /* query user for a string and find it in a list window */
151 int
152 screen_find(screen_t *screen,
153 list_window_t *lw,
154 int rows,
155 command_t findcmd,
156 list_window_callback_fn_t callback_fn,
157 void *callback_data)
158 {
159 int reversed = 0;
160 int retval = 0;
161 const char *prompt = FIND_PROMPT;
162 char *value = options.find_show_last_pattern ? (char *) -1 : NULL;
164 if (findcmd == CMD_LIST_RFIND || findcmd == CMD_LIST_RFIND_NEXT) {
165 prompt = RFIND_PROMPT;
166 reversed = 1;
167 }
169 switch (findcmd) {
170 case CMD_LIST_FIND:
171 case CMD_LIST_RFIND:
172 if (screen->findbuf) {
173 g_free(screen->findbuf);
174 screen->findbuf=NULL;
175 }
176 /* continue... */
178 case CMD_LIST_FIND_NEXT:
179 case CMD_LIST_RFIND_NEXT:
180 if (!screen->findbuf)
181 screen->findbuf=screen_readln(screen->status_window.w,
182 prompt,
183 value,
184 &screen->find_history,
185 NULL);
187 if (!screen->findbuf || !screen->findbuf[0])
188 return 1;
190 if (reversed)
191 retval = list_window_rfind(lw,
192 callback_fn,
193 callback_data,
194 screen->findbuf,
195 options.find_wrap,
196 rows);
197 else
198 retval = list_window_find(lw,
199 callback_fn,
200 callback_data,
201 screen->findbuf,
202 options.find_wrap);
204 if (retval == 0)
205 lw->repaint = 1;
206 else {
207 screen_status_printf(_("Unable to find \'%s\'"), screen->findbuf);
208 screen_bell();
209 }
210 return 1;
211 default:
212 break;
213 }
214 return 0;
215 }
217 void
218 screen_display_completion_list(screen_t *screen, GList *list)
219 {
220 static GList *prev_list = NULL;
221 static guint prev_length = 0;
222 static guint offset = 0;
223 WINDOW *w = screen->main_window.w;
224 guint length, y=0;
226 length = g_list_length(list);
227 if (list == prev_list && length == prev_length) {
228 offset += screen->main_window.rows;
229 if (offset >= length)
230 offset = 0;
231 } else {
232 prev_list = list;
233 prev_length = length;
234 offset = 0;
235 }
237 colors_use(w, COLOR_STATUS_ALERT);
238 while (y < screen->main_window.rows) {
239 GList *item = g_list_nth(list, y+offset);
241 wmove(w, y++, 0);
242 wclrtoeol(w);
243 if (item) {
244 gchar *tmp = g_strdup(item->data);
245 waddstr(w, basename(tmp));
246 g_free(tmp);
247 }
248 }
250 wrefresh(w);
251 doupdate();
252 colors_use(w, COLOR_LIST);
253 }
255 void
256 set_xterm_title(const char *format, ...)
257 {
258 /* the current xterm title exists under the WM_NAME property */
259 /* and can be retreived with xprop -id $WINDOWID */
261 if (options.enable_xterm_title) {
262 if (g_getenv("WINDOWID")) {
263 char *msg;
264 va_list ap;
266 va_start(ap,format);
267 msg = g_strdup_vprintf(format,ap);
268 va_end(ap);
269 printf("%c]0;%s%c", '\033', msg, '\007');
270 g_free(msg);
271 } else
272 options.enable_xterm_title = FALSE;
273 }
274 }