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