Code

use the glib main event loop
[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);
58   
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     {
105       int rows, cols;
106       getmaxyx(stdscr, rows, cols);
107       /* create window for input */
108       w = newwin(1,  cols, rows-1, 0);
109       leaveok(w, FALSE);
110       keypad(w, TRUE);  
111     } 
112   wmove(w, 0,0);
113   curs_set(1);
114   colors_use(w, COLOR_STATUS_ALERT);
115   if(prompt == NULL)
116     return wreadln_masked(w, _("Password: "), NULL, COLS, NULL, NULL);
117   else
118     return wreadln_masked(w, prompt, NULL, COLS, NULL, NULL);
119   curs_set(0);
121     
122 static gint
123 _screen_auth(struct mpdclient *c, gint recursion)
125    mpd_clearError(c->connection);
126   if(recursion > 2) 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 ) return  _screen_auth(c, ++recursion);
131    return 0;
134 gint
135 screen_auth(struct mpdclient *c)
137         gint ret = _screen_auth(c, 0);
138         mpdclient_update(c);
139         curs_set(0);
140         return ret;
143 /* query user for a string and find it in a list window */
144 int 
145 screen_find(screen_t *screen,
146             list_window_t *lw, 
147             int rows,
148             command_t findcmd,
149             list_window_callback_fn_t callback_fn,
150             void *callback_data)
152         int reversed = 0;
153         int retval = 0;
154         const char *prompt = FIND_PROMPT;
155         char *value = options.find_show_last_pattern ? (char *) -1 : NULL;
157         if (findcmd == CMD_LIST_RFIND || findcmd == CMD_LIST_RFIND_NEXT) {
158                 prompt = RFIND_PROMPT;
159                 reversed = 1;
160         }
162   switch(findcmd)
163     {
164     case CMD_LIST_FIND:
165     case CMD_LIST_RFIND:
166       if( screen->findbuf )
167         {
168           g_free(screen->findbuf);
169           screen->findbuf=NULL;
170         }
171       /* 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);
180       if( !screen->findbuf || !screen->findbuf[0] )
181         return 1; 
182       if( reversed )
183         retval = list_window_rfind(lw, 
184                                    callback_fn,
185                                    callback_data, 
186                                    screen->findbuf,
187                                    options.find_wrap,
188                                    rows);
189       else
190         retval = list_window_find(lw,
191                                   callback_fn,
192                                   callback_data,
193                                   screen->findbuf,
194                                   options.find_wrap);
195       if( retval == 0 )
196         {
197           lw->repaint  = 1;
198         }
199       else
200         {
201           screen_status_printf(_("Unable to find \'%s\'"), screen->findbuf);
202           screen_bell();
203         }
204       return 1;
205     default:
206       break;
207     }
208   return 0;
211 void
212 screen_display_completion_list(screen_t *screen, GList *list)
214   static GList *prev_list = NULL;
215   static gint prev_length = 0;
216   static gint offset = 0;
217   WINDOW *w = screen->main_window.w;
218   gint length, y=0;
220   length = g_list_length(list);
221   if( list==prev_list && length==prev_length )
222     {
223       offset += screen->main_window.rows;
224       if( offset>=length )
225         offset=0;
226     }
227   else
228     {
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     {
237       GList *item = g_list_nth(list, y+offset);
239       wmove(w, y++, 0);
240       wclrtoeol(w);
241       if( item )
242         {
243           gchar *tmp = g_strdup(item->data);
244           waddstr(w, basename(tmp));
245           g_free(tmp);
246         }
247     }
248   wrefresh(w);
249   doupdate();
250   colors_use(w, COLOR_LIST);
253 void
254 set_xterm_title(const char *format, ...)
256   /* the current xterm title exists under the WM_NAME property */
257   /* and can be retreived with xprop -id $WINDOWID */
259   if( options.enable_xterm_title )
260     {
261       if( g_getenv("WINDOWID") )
262         {
263           char *msg;
264           va_list ap;
265           
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         }
272       else
273         options.enable_xterm_title = FALSE;
274     }