6ee1eba68fbf392de6ea5b8c0d7d54697545cde2
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 "ncmpc.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>
31 #include <string.h>
33 #define FIND_PROMPT _("Find: ")
34 #define RFIND_PROMPT _("Find backward: ")
36 void
37 screen_bell(void)
38 {
39 if (options.audible_bell)
40 beep();
41 if (options.visible_bell)
42 flash();
43 }
45 int
46 screen_getch(WINDOW *w, const char *prompt)
47 {
48 int key = -1;
49 int prompt_len = strlen(prompt);
51 colors_use(w, COLOR_STATUS_ALERT);
52 wclear(w);
53 wmove(w, 0, 0);
54 waddstr(w, prompt);
55 wmove(w, 0, prompt_len);
57 echo();
58 curs_set(1);
60 while ((key=my_wgetch(w)) == ERR)
61 ;
63 #ifdef HAVE_GETMOUSE
64 /* ignore mouse events */
65 if (key == KEY_MOUSE)
66 return screen_getch(w, prompt);
67 #endif
69 noecho();
70 curs_set(0);
72 return key;
73 }
75 char *
76 screen_readln(WINDOW *w,
77 const char *prompt,
78 const char *value,
79 GList **history,
80 GCompletion *gcmp)
81 {
82 char *line = NULL;
84 wmove(w, 0,0);
85 curs_set(1);
86 colors_use(w, COLOR_STATUS_ALERT);
87 line = wreadln(w, prompt, value, COLS, history, gcmp);
88 curs_set(0);
89 return line;
90 }
92 char *
93 screen_getstr(WINDOW *w, const char *prompt)
94 {
95 return screen_readln(w, prompt, NULL, NULL, NULL);
96 }
98 static char *
99 screen_read_password(WINDOW *w, const char *prompt)
100 {
101 char *ret;
103 if (w == NULL) {
104 int rows, cols;
105 getmaxyx(stdscr, rows, cols);
106 /* create window for input */
107 w = newwin(1, cols, rows-1, 0);
108 leaveok(w, FALSE);
109 keypad(w, TRUE);
110 }
112 wmove(w, 0,0);
113 curs_set(1);
114 colors_use(w, COLOR_STATUS_ALERT);
116 if (prompt == NULL)
117 prompt = _("Password: ");
118 ret = wreadln_masked(w, prompt, NULL, COLS, NULL, NULL);
120 curs_set(0);
121 return ret;
122 }
124 static gint
125 _screen_auth(struct mpdclient *c, gint recursion)
126 {
127 mpd_clearError(c->connection);
128 if (recursion > 2)
129 return 1;
130 mpd_sendPasswordCommand(c->connection, screen_read_password(NULL, NULL));
131 mpd_finishCommand(c->connection);
132 mpdclient_update(c);
133 if (c->connection->errorCode == MPD_ACK_ERROR_PASSWORD)
134 return _screen_auth(c, ++recursion);
135 return 0;
136 }
138 gint
139 screen_auth(struct mpdclient *c)
140 {
141 gint ret = _screen_auth(c, 0);
142 mpdclient_update(c);
143 curs_set(0);
144 return ret;
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 case CMD_LIST_FIND:
168 case CMD_LIST_RFIND:
169 if (screen->findbuf) {
170 g_free(screen->findbuf);
171 screen->findbuf=NULL;
172 }
173 /* continue... */
175 case CMD_LIST_FIND_NEXT:
176 case CMD_LIST_RFIND_NEXT:
177 if (!screen->findbuf)
178 screen->findbuf=screen_readln(screen->status_window.w,
179 prompt,
180 value,
181 &screen->find_history,
182 NULL);
184 if (!screen->findbuf || !screen->findbuf[0])
185 return 1;
187 if (reversed)
188 retval = list_window_rfind(lw,
189 callback_fn,
190 callback_data,
191 screen->findbuf,
192 options.find_wrap,
193 rows);
194 else
195 retval = list_window_find(lw,
196 callback_fn,
197 callback_data,
198 screen->findbuf,
199 options.find_wrap);
201 if (retval == 0)
202 lw->repaint = 1;
203 else {
204 screen_status_printf(_("Unable to find \'%s\'"), screen->findbuf);
205 screen_bell();
206 }
207 return 1;
208 default:
209 break;
210 }
211 return 0;
212 }
214 void
215 screen_display_completion_list(screen_t *screen, GList *list)
216 {
217 static GList *prev_list = NULL;
218 static guint prev_length = 0;
219 static guint offset = 0;
220 WINDOW *w = screen->main_window.w;
221 guint length, y=0;
223 length = g_list_length(list);
224 if (list == prev_list && length == prev_length) {
225 offset += screen->main_window.rows;
226 if (offset >= length)
227 offset = 0;
228 } else {
229 prev_list = list;
230 prev_length = length;
231 offset = 0;
232 }
234 colors_use(w, COLOR_STATUS_ALERT);
235 while (y < screen->main_window.rows) {
236 GList *item = g_list_nth(list, y+offset);
238 wmove(w, y++, 0);
239 wclrtoeol(w);
240 if (item) {
241 gchar *tmp = g_strdup(item->data);
242 waddstr(w, basename(tmp));
243 g_free(tmp);
244 }
245 }
247 wrefresh(w);
248 doupdate();
249 colors_use(w, COLOR_LIST);
250 }
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 }