Code

screen: unsigned integers for screen dimensions
[ncmpc.git] / src / screen_utils.c
1 /* 
2  * $Id$
3  *
4  * (c) 2004 by Kalle Wallin <kaw@linux.se>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
21 #include "screen_utils.h"
22 #include "screen.h"
23 #include "mpdclient.h"
24 #include "config.h"
25 #include "ncmpc.h"
26 #include "support.h"
27 #include "options.h"
28 #include "colors.h"
29 #include "wreadln.h"
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <string.h>
35 #define FIND_PROMPT  _("Find: ")
36 #define RFIND_PROMPT _("Find backward: ")
38 void
39 screen_bell(void)
40 {
41         if (options.audible_bell)
42                 beep();
43         if (options.visible_bell)
44                 flash();
45 }
47 int
48 screen_getch(WINDOW *w, const char *prompt)
49 {
50         int key = -1;
51         int prompt_len = strlen(prompt);
53         colors_use(w, COLOR_STATUS_ALERT);
54         wclear(w);
55         wmove(w, 0, 0);
56         waddstr(w, prompt);
57         wmove(w, 0, prompt_len);
59         echo();
60         curs_set(1);
62         while ((key=my_wgetch(w)) == ERR)
63                 ;
65 #ifdef HAVE_GETMOUSE
66         /* ignore mouse events */
67         if (key == KEY_MOUSE)
68                 return screen_getch(w, prompt);
69 #endif
71         noecho();
72         curs_set(0);
74         return key;
75 }
77 char *
78 screen_readln(WINDOW *w,
79               const char *prompt,
80               const char *value,
81               GList **history,
82               GCompletion *gcmp)
83 {
84         char *line = NULL;
86         wmove(w, 0,0);
87         curs_set(1);
88         colors_use(w, COLOR_STATUS_ALERT);
89         line = wreadln(w, prompt, value, COLS, history, gcmp);
90         curs_set(0);
91         return line;
92 }
94 char *
95 screen_getstr(WINDOW *w, const char *prompt)
96 {
97         return screen_readln(w, prompt, NULL, NULL, NULL);
98 }
100 static char *
101 screen_read_password(WINDOW *w, const char *prompt)
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                 return wreadln_masked(w, _("Password: "), NULL, COLS, NULL, NULL);
118         else
119                 return wreadln_masked(w, prompt, NULL, COLS, NULL, NULL);
121         curs_set(0);
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                         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;
214 void
215 screen_display_completion_list(screen_t *screen, GList *list)
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);
252 void
253 set_xterm_title(const char *format, ...)
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         }