Code

screen_auth() returns result
[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 <stdlib.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <glib.h>
25 #include <ncurses.h>
27 #include "config.h"
28 #include "ncmpc.h"
29 #include "mpdclient.h"
30 #include "support.h"
31 #include "command.h"
32 #include "options.h"
33 #include "list_window.h"
34 #include "colors.h"
35 #include "wreadln.h"
36 #include "screen.h"
38 #define FIND_PROMPT  _("Find: ")
39 #define RFIND_PROMPT _("Find backward: ")
41 void
42 screen_bell(void)
43 {
44   if( options.audible_bell )
45     beep();
46   if( options.visible_bell )
47     flash();
48 }
50 int
51 screen_getch(WINDOW *w, const char *prompt)
52 {
53   int key = -1;
54   int prompt_len = strlen(prompt);
56   colors_use(w, COLOR_STATUS_ALERT);
57   wclear(w);  
58   wmove(w, 0, 0);
59   waddstr(w, prompt);
60   wmove(w, 0, prompt_len);
61   
62   echo();
63   curs_set(1);
64   timeout(-1);
66   while( (key=my_wgetch(w)) == ERR )
67     ;
69 #ifdef HAVE_GETMOUSE
70   /* ignore mouse events */
71   if( key==KEY_MOUSE )
72     return screen_getch(w, prompt);
73 #endif
75   noecho();
76   curs_set(0);
77   timeout(SCREEN_TIMEOUT);
79   return key;
80 }
82 char *
83 screen_readln(WINDOW *w,
84               const char *prompt,
85               const char *value,
86               GList **history,
87               GCompletion *gcmp)
88 {
89   char *line = NULL;
91   wmove(w, 0,0);
92   curs_set(1);
93   colors_use(w, COLOR_STATUS_ALERT);
94   line = wreadln(w, prompt, value, COLS, history, gcmp);
95   curs_set(0);
96   return line;
97 }
99 char *
100 screen_getstr(WINDOW *w, const char *prompt)
102   return screen_readln(w, prompt, NULL, NULL, NULL);
105 static char *
106 screen_read_password(WINDOW *w, const char *prompt)
108   if(w == NULL)
109     {
110       int rows, cols;
111       getmaxyx(stdscr, rows, cols);
112       /* create window for input */
113       w = newwin(1,  cols, rows-1, 0);
114       leaveok(w, FALSE);
115       keypad(w, TRUE);  
116     } 
117   wmove(w, 0,0);
118   curs_set(1);
119   colors_use(w, COLOR_STATUS_ALERT);
120   if(prompt == NULL)
121     return wreadln_masked(w, _("Password: "), NULL, COLS, NULL, NULL);
122   else
123     return wreadln_masked(w, prompt, NULL, COLS, NULL, NULL);
124   curs_set(0);
126     
127 static gint
128 _screen_auth(mpdclient_t *c, gint recursion)
130    mpd_clearError(c->connection);
131   if(recursion > 2) return 1;
132   mpd_sendPasswordCommand(c->connection,  screen_read_password(NULL, NULL));   
133   mpd_finishCommand(c->connection);
134    mpdclient_update(c);
135    if(  c->connection->errorCode == MPD_ACK_ERROR_PASSWORD ) return  _screen_auth(c, ++recursion);
136    return 0;
139 gint
140 screen_auth(mpdclient_t *c)
142         gint ret = _screen_auth(c, 0);
143         mpdclient_update(c);
144         curs_set(0);
145         return ret;
148 /* query user for a string and find it in a list window */
149 int 
150 screen_find(screen_t *screen,
151             list_window_t *lw, 
152             int rows,
153             command_t findcmd,
154             list_window_callback_fn_t callback_fn,
155             void *callback_data)
157         int reversed = 0;
158         int retval = 0;
159         const char *prompt = FIND_PROMPT;
160         char *value = options.find_show_last_pattern ? (char *) -1 : NULL;
162         if (findcmd == CMD_LIST_RFIND || findcmd == CMD_LIST_RFIND_NEXT) {
163                 prompt = RFIND_PROMPT;
164                 reversed = 1;
165         }
167   switch(findcmd)
168     {
169     case CMD_LIST_FIND:
170     case CMD_LIST_RFIND:
171       if( screen->findbuf )
172         {
173           g_free(screen->findbuf);
174           screen->findbuf=NULL;
175         }
176       /* continue... */
177     case CMD_LIST_FIND_NEXT:
178     case CMD_LIST_RFIND_NEXT:
179       if( !screen->findbuf )
180         screen->findbuf=screen_readln(screen->status_window.w,
181                                       prompt,
182                                       value,
183                                       &screen->find_history,
184                                       NULL);
185       if( !screen->findbuf || !screen->findbuf[0] )
186         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);
200       if( retval == 0 )
201         {
202           lw->repaint  = 1;
203         }
204       else
205         {
206           screen_status_printf(_("Unable to find \'%s\'"), screen->findbuf);
207           screen_bell();
208         }
209       return 1;
210     default:
211       break;
212     }
213   return 0;
216 void
217 screen_display_completion_list(screen_t *screen, GList *list)
219   static GList *prev_list = NULL;
220   static gint prev_length = 0;
221   static gint offset = 0;
222   WINDOW *w = screen->main_window.w;
223   gint length, y=0;
225   length = g_list_length(list);
226   if( list==prev_list && length==prev_length )
227     {
228       offset += screen->main_window.rows;
229       if( offset>=length )
230         offset=0;
231     }
232   else
233     {
234       prev_list = list;
235       prev_length = length;
236       offset = 0;
237     }
239   colors_use(w, COLOR_STATUS_ALERT);
240   while( y<screen->main_window.rows )
241     {
242       GList *item = g_list_nth(list, y+offset);
244       wmove(w, y++, 0);
245       wclrtoeol(w);
246       if( item )
247         {
248           gchar *tmp = g_strdup(item->data);
249           waddstr(w, basename(tmp));
250           g_free(tmp);
251         }
252     }
253   wrefresh(w);
254   doupdate();
255   colors_use(w, COLOR_LIST);
258 void
259 set_xterm_title(const char *format, ...)
261   /* the current xterm title exists under the WM_NAME property */
262   /* and can be retreived with xprop -id $WINDOWID */
264   if( options.enable_xterm_title )
265     {
266       if( g_getenv("WINDOWID") )
267         {
268           char *msg;
269           va_list ap;
270           
271           va_start(ap,format);
272           msg = g_strdup_vprintf(format,ap);
273           va_end(ap);
274           printf("%c]0;%s%c", '\033', msg, '\007'); 
275           g_free(msg);
276         }
277       else
278         options.enable_xterm_title = FALSE;
279     }