Code

screen_utils: don't call wmove() twice
[ncmpc.git] / src / screen_utils.c
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>
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=my_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         mpd_clearError(c->connection);
125         if (recursion > 2)
126                 return 1;
127         mpd_sendPasswordCommand(c->connection,  screen_read_password(NULL, NULL));
128         mpd_finishCommand(c->connection);
129         mpdclient_update(c);
130         if (c->connection->errorCode == MPD_ACK_ERROR_PASSWORD)
131                 return  _screen_auth(c, ++recursion);
132         return 0;
135 gint
136 screen_auth(struct mpdclient *c)
138         gint ret = _screen_auth(c, 0);
139         mpdclient_update(c);
140         curs_set(0);
141         return ret;
144 /* query user for a string and find it in a list window */
145 int
146 screen_find(screen_t *screen,
147             list_window_t *lw,
148             int rows,
149             command_t findcmd,
150             list_window_callback_fn_t callback_fn,
151             void *callback_data)
153         int reversed = 0;
154         int retval = 0;
155         const char *prompt = FIND_PROMPT;
156         char *value = options.find_show_last_pattern ? (char *) -1 : NULL;
158         if (findcmd == CMD_LIST_RFIND || findcmd == CMD_LIST_RFIND_NEXT) {
159                 prompt = RFIND_PROMPT;
160                 reversed = 1;
161         }
163         switch (findcmd) {
164         case CMD_LIST_FIND:
165         case CMD_LIST_RFIND:
166                 if (screen->findbuf) {
167                         g_free(screen->findbuf);
168                         screen->findbuf=NULL;
169                 }
170                 /* continue... */
172         case CMD_LIST_FIND_NEXT:
173         case CMD_LIST_RFIND_NEXT:
174                 if (!screen->findbuf)
175                         screen->findbuf=screen_readln(screen->status_window.w,
176                                                       prompt,
177                                                       value,
178                                                       &screen->find_history,
179                                                       NULL);
181                 if (!screen->findbuf || !screen->findbuf[0])
182                         return 1;
184                 if (reversed)
185                         retval = list_window_rfind(lw,
186                                                    callback_fn,
187                                                    callback_data,
188                                                    screen->findbuf,
189                                                    options.find_wrap,
190                                                    rows);
191                 else
192                         retval = list_window_find(lw,
193                                                   callback_fn,
194                                                   callback_data,
195                                                   screen->findbuf,
196                                                   options.find_wrap);
198                 if (retval != 0) {
199                         screen_status_printf(_("Unable to find \'%s\'"), screen->findbuf);
200                         screen_bell();
201                 }
202                 return 1;
203         default:
204                 break;
205         }
206         return 0;
209 void
210 screen_display_completion_list(screen_t *screen, GList *list)
212         static GList *prev_list = NULL;
213         static guint prev_length = 0;
214         static guint offset = 0;
215         WINDOW *w = screen->main_window.w;
216         guint length, y=0;
218         length = g_list_length(list);
219         if (list == prev_list && length == prev_length) {
220                 offset += screen->main_window.rows;
221                 if (offset >= length)
222                         offset = 0;
223         } else {
224                 prev_list = list;
225                 prev_length = length;
226                 offset = 0;
227         }
229         colors_use(w, COLOR_STATUS_ALERT);
230         while (y < screen->main_window.rows) {
231                 GList *item = g_list_nth(list, y+offset);
233                 wmove(w, y++, 0);
234                 wclrtoeol(w);
235                 if (item) {
236                         gchar *tmp = g_strdup(item->data);
237                         waddstr(w, basename(tmp));
238                         g_free(tmp);
239                 }
240         }
242         wrefresh(w);
243         doupdate();
244         colors_use(w, COLOR_LIST);
247 void
248 set_xterm_title(const char *format, ...)
250         /* the current xterm title exists under the WM_NAME property */
251         /* and can be retreived with xprop -id $WINDOWID */
253         if (options.enable_xterm_title) {
254                 if (g_getenv("WINDOWID")) {
255                         char *msg;
256                         va_list ap;
258                         va_start(ap,format);
259                         msg = g_strdup_vprintf(format,ap);
260                         va_end(ap);
261                         printf("%c]0;%s%c", '\033', msg, '\007');
262                         g_free(msg);
263                 } else
264                         options.enable_xterm_title = FALSE;
265         }