1 /*
2 * (c) 2004 by Kalle Wallin <kaw@linux.se>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 */
19 #include "screen_utils.h"
20 #include "screen.h"
21 #include "mpdclient.h"
22 #include "config.h"
23 #include "i18n.h"
24 #include "support.h"
25 #include "options.h"
26 #include "colors.h"
27 #include "wreadln.h"
29 #include <stdlib.h>
30 #include <unistd.h>
32 #define FIND_PROMPT _("Find: ")
33 #define RFIND_PROMPT _("Find backward: ")
35 void
36 screen_bell(void)
37 {
38 if (options.audible_bell)
39 beep();
40 if (options.visible_bell)
41 flash();
42 }
44 int
45 screen_getch(WINDOW *w, const char *prompt)
46 {
47 int key = -1;
49 colors_use(w, COLOR_STATUS_ALERT);
50 wclear(w);
51 wmove(w, 0, 0);
52 waddstr(w, prompt);
54 echo();
55 curs_set(1);
57 while ((key = wgetch(w)) == ERR)
58 ;
60 #ifdef HAVE_GETMOUSE
61 /* ignore mouse events */
62 if (key == KEY_MOUSE)
63 return screen_getch(w, prompt);
64 #endif
66 noecho();
67 curs_set(0);
69 return key;
70 }
72 char *
73 screen_readln(WINDOW *w,
74 const char *prompt,
75 const char *value,
76 GList **history,
77 GCompletion *gcmp)
78 {
79 char *line = NULL;
81 wmove(w, 0,0);
82 curs_set(1);
83 colors_use(w, COLOR_STATUS_ALERT);
84 line = wreadln(w, prompt, value, COLS, history, gcmp);
85 curs_set(0);
86 return line;
87 }
89 char *
90 screen_getstr(WINDOW *w, const char *prompt)
91 {
92 return screen_readln(w, prompt, NULL, NULL, NULL);
93 }
95 static char *
96 screen_read_password(WINDOW *w, const char *prompt)
97 {
98 char *ret;
100 if (w == NULL) {
101 int rows, cols;
102 getmaxyx(stdscr, rows, cols);
103 /* create window for input */
104 w = newwin(1, cols, rows-1, 0);
105 leaveok(w, FALSE);
106 keypad(w, TRUE);
107 }
109 wmove(w, 0,0);
110 curs_set(1);
111 colors_use(w, COLOR_STATUS_ALERT);
113 if (prompt == NULL)
114 prompt = _("Password: ");
115 ret = wreadln_masked(w, prompt, NULL, COLS, NULL, NULL);
117 curs_set(0);
118 return ret;
119 }
121 static gint
122 _screen_auth(struct mpdclient *c, gint recursion)
123 {
124 char *password;
126 mpd_clearError(c->connection);
127 if (recursion > 2)
128 return 1;
130 password = screen_read_password(NULL, NULL);
131 if (password == NULL)
132 return 1;
134 mpd_sendPasswordCommand(c->connection, password);
135 g_free(password);
137 mpd_finishCommand(c->connection);
138 mpdclient_update(c);
139 if (c->connection->errorCode == MPD_ACK_ERROR_PASSWORD)
140 return _screen_auth(c, ++recursion);
141 return 0;
142 }
144 gint
145 screen_auth(struct mpdclient *c)
146 {
147 gint ret = _screen_auth(c, 0);
148 mpdclient_update(c);
149 curs_set(0);
150 return ret;
151 }
153 /* query user for a string and find it in a list window */
154 int
155 screen_find(list_window_t *lw,
156 int rows,
157 command_t findcmd,
158 list_window_callback_fn_t callback_fn,
159 void *callback_data)
160 {
161 int reversed = 0;
162 int retval = 0;
163 const char *prompt = FIND_PROMPT;
164 char *value = options.find_show_last_pattern ? (char *) -1 : NULL;
166 if (findcmd == CMD_LIST_RFIND || findcmd == CMD_LIST_RFIND_NEXT) {
167 prompt = RFIND_PROMPT;
168 reversed = 1;
169 }
171 switch (findcmd) {
172 case CMD_LIST_FIND:
173 case CMD_LIST_RFIND:
174 if (screen.findbuf) {
175 g_free(screen.findbuf);
176 screen.findbuf=NULL;
177 }
178 /* continue... */
180 case CMD_LIST_FIND_NEXT:
181 case CMD_LIST_RFIND_NEXT:
182 if (!screen.findbuf)
183 screen.findbuf=screen_readln(screen.status_window.w,
184 prompt,
185 value,
186 &screen.find_history,
187 NULL);
189 if (screen.findbuf == NULL)
190 return 1;
192 if (reversed)
193 retval = list_window_rfind(lw,
194 callback_fn,
195 callback_data,
196 screen.findbuf,
197 options.find_wrap,
198 rows);
199 else
200 retval = list_window_find(lw,
201 callback_fn,
202 callback_data,
203 screen.findbuf,
204 options.find_wrap);
206 if (retval != 0) {
207 screen_status_printf(_("Unable to find \'%s\'"),
208 screen.findbuf);
209 screen_bell();
210 }
211 return 1;
212 default:
213 break;
214 }
215 return 0;
216 }
218 void
219 screen_display_completion_list(GList *list)
220 {
221 static GList *prev_list = NULL;
222 static guint prev_length = 0;
223 static guint offset = 0;
224 WINDOW *w = screen.main_window.w;
225 guint length, y=0;
227 length = g_list_length(list);
228 if (list == prev_list && length == prev_length) {
229 offset += screen.main_window.rows;
230 if (offset >= length)
231 offset = 0;
232 } else {
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 GList *item = g_list_nth(list, y+offset);
242 wmove(w, y++, 0);
243 wclrtoeol(w);
244 if (item) {
245 gchar *tmp = g_strdup(item->data);
246 waddstr(w, g_basename(tmp));
247 g_free(tmp);
248 }
249 }
251 wrefresh(w);
252 doupdate();
253 colors_use(w, COLOR_LIST);
254 }
256 #ifndef NCMPC_MINI
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 if (g_getenv("WINDOWID")) {
265 char *msg;
266 va_list ap;
268 va_start(ap,format);
269 msg = g_strdup_vprintf(format,ap);
270 va_end(ap);
271 printf("%c]0;%s%c", '\033', msg, '\007');
272 g_free(msg);
273 } else
274 options.enable_xterm_title = FALSE;
275 }
276 }
277 #endif