Code

list_window: removed property "repaint"
[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>
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)
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;
124 static gint
125 _screen_auth(struct mpdclient *c, gint recursion)
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;
138 gint
139 screen_auth(struct mpdclient *c)
141         gint ret = _screen_auth(c, 0);
142         mpdclient_update(c);
143         curs_set(0);
144         return ret;
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)
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                         screen_status_printf(_("Unable to find \'%s\'"), screen->findbuf);
203                         screen_bell();
204                 }
205                 return 1;
206         default:
207                 break;
208         }
209         return 0;
212 void
213 screen_display_completion_list(screen_t *screen, GList *list)
215         static GList *prev_list = NULL;
216         static guint prev_length = 0;
217         static guint offset = 0;
218         WINDOW *w = screen->main_window.w;
219         guint length, y=0;
221         length = g_list_length(list);
222         if (list == prev_list && length == prev_length) {
223                 offset += screen->main_window.rows;
224                 if (offset >= length)
225                         offset = 0;
226         } else {
227                 prev_list = list;
228                 prev_length = length;
229                 offset = 0;
230         }
232         colors_use(w, COLOR_STATUS_ALERT);
233         while (y < screen->main_window.rows) {
234                 GList *item = g_list_nth(list, y+offset);
236                 wmove(w, y++, 0);
237                 wclrtoeol(w);
238                 if (item) {
239                         gchar *tmp = g_strdup(item->data);
240                         waddstr(w, basename(tmp));
241                         g_free(tmp);
242                 }
243         }
245         wrefresh(w);
246         doupdate();
247         colors_use(w, COLOR_LIST);
250 void
251 set_xterm_title(const char *format, ...)
253         /* the current xterm title exists under the WM_NAME property */
254         /* and can be retreived with xprop -id $WINDOWID */
256         if (options.enable_xterm_title) {
257                 if (g_getenv("WINDOWID")) {
258                         char *msg;
259                         va_list ap;
261                         va_start(ap,format);
262                         msg = g_strdup_vprintf(format,ap);
263                         va_end(ap);
264                         printf("%c]0;%s%c", '\033', msg, '\007');
265                         g_free(msg);
266                 } else
267                         options.enable_xterm_title = FALSE;
268         }