Code

screen_utils: Use werase() instead of wclear().
[ncmpc.git] / src / screen_utils.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "screen_utils.h"
21 #include "screen.h"
22 #include "mpdclient.h"
23 #include "config.h"
24 #include "i18n.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         werase(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;
121 static gint
122 _screen_auth(struct mpdclient *c, gint recursion)
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;
144 gint
145 screen_auth(struct mpdclient *c)
147         gint ret = _screen_auth(c, 0);
148         mpdclient_update(c);
149         curs_set(0);
150         return ret;
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)
161         int reversed = 0;
162         bool found;
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                 found = reversed
193                         ? list_window_rfind(lw,
194                                             callback_fn, callback_data,
195                                             screen.findbuf,
196                                             options.find_wrap,
197                                             options.bell_on_wrap,
198                                             rows)
199                         : list_window_find(lw,
200                                            callback_fn, callback_data,
201                                            screen.findbuf,
202                                            options.find_wrap,
203                                            options.bell_on_wrap);
204                 if (!found) {
205                         screen_status_printf(_("Unable to find \'%s\'"),
206                                              screen.findbuf);
207                         screen_bell();
208                 }
209                 return 1;
210         default:
211                 break;
212         }
213         return 0;
216 void
217 screen_display_completion_list(GList *list)
219         static GList *prev_list = NULL;
220         static guint prev_length = 0;
221         static guint offset = 0;
222         WINDOW *w = screen.main_window.w;
223         guint length, y=0;
225         length = g_list_length(list);
226         if (list == prev_list && length == prev_length) {
227                 offset += screen.main_window.rows;
228                 if (offset >= length)
229                         offset = 0;
230         } else {
231                 prev_list = list;
232                 prev_length = length;
233                 offset = 0;
234         }
236         colors_use(w, COLOR_STATUS_ALERT);
237         while (y < screen.main_window.rows) {
238                 GList *item = g_list_nth(list, y+offset);
240                 wmove(w, y++, 0);
241                 wclrtoeol(w);
242                 if (item) {
243                         gchar *tmp = g_strdup(item->data);
244                         waddstr(w, g_basename(tmp));
245                         g_free(tmp);
246                 }
247         }
249         wrefresh(w);
250         doupdate();
251         colors_use(w, COLOR_LIST);
254 #ifndef NCMPC_MINI
255 void
256 set_xterm_title(const char *format, ...)
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         }
275 #endif