Code

screen: added struct names
[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);
61   timeout(-1);
63   while( (key=my_wgetch(w)) == ERR )
64     ;
66 #ifdef HAVE_GETMOUSE
67   /* ignore mouse events */
68   if( key==KEY_MOUSE )
69     return screen_getch(w, prompt);
70 #endif
72   noecho();
73   curs_set(0);
74   timeout(SCREEN_TIMEOUT);
76   return key;
77 }
79 char *
80 screen_readln(WINDOW *w,
81               const char *prompt,
82               const char *value,
83               GList **history,
84               GCompletion *gcmp)
85 {
86   char *line = NULL;
88   wmove(w, 0,0);
89   curs_set(1);
90   colors_use(w, COLOR_STATUS_ALERT);
91   line = wreadln(w, prompt, value, COLS, history, gcmp);
92   curs_set(0);
93   return line;
94 }
96 char *
97 screen_getstr(WINDOW *w, const char *prompt)
98 {
99   return screen_readln(w, prompt, NULL, NULL, NULL);
102 static char *
103 screen_read_password(WINDOW *w, const char *prompt)
105   if(w == NULL)
106     {
107       int rows, cols;
108       getmaxyx(stdscr, rows, cols);
109       /* create window for input */
110       w = newwin(1,  cols, rows-1, 0);
111       leaveok(w, FALSE);
112       keypad(w, TRUE);  
113     } 
114   wmove(w, 0,0);
115   curs_set(1);
116   colors_use(w, COLOR_STATUS_ALERT);
117   if(prompt == NULL)
118     return wreadln_masked(w, _("Password: "), NULL, COLS, NULL, NULL);
119   else
120     return wreadln_masked(w, prompt, NULL, COLS, NULL, NULL);
121   curs_set(0);
123     
124 static gint
125 _screen_auth(struct mpdclient *c, gint recursion)
127    mpd_clearError(c->connection);
128   if(recursion > 2) return 1;
129   mpd_sendPasswordCommand(c->connection,  screen_read_password(NULL, NULL));   
130   mpd_finishCommand(c->connection);
131    mpdclient_update(c);
132    if(  c->connection->errorCode == MPD_ACK_ERROR_PASSWORD ) return  _screen_auth(c, ++recursion);
133    return 0;
136 gint
137 screen_auth(struct mpdclient *c)
139         gint ret = _screen_auth(c, 0);
140         mpdclient_update(c);
141         curs_set(0);
142         return ret;
145 /* query user for a string and find it in a list window */
146 int 
147 screen_find(screen_t *screen,
148             list_window_t *lw, 
149             int rows,
150             command_t findcmd,
151             list_window_callback_fn_t callback_fn,
152             void *callback_data)
154         int reversed = 0;
155         int retval = 0;
156         const char *prompt = FIND_PROMPT;
157         char *value = options.find_show_last_pattern ? (char *) -1 : NULL;
159         if (findcmd == CMD_LIST_RFIND || findcmd == CMD_LIST_RFIND_NEXT) {
160                 prompt = RFIND_PROMPT;
161                 reversed = 1;
162         }
164   switch(findcmd)
165     {
166     case CMD_LIST_FIND:
167     case CMD_LIST_RFIND:
168       if( screen->findbuf )
169         {
170           g_free(screen->findbuf);
171           screen->findbuf=NULL;
172         }
173       /* continue... */
174     case CMD_LIST_FIND_NEXT:
175     case CMD_LIST_RFIND_NEXT:
176       if( !screen->findbuf )
177         screen->findbuf=screen_readln(screen->status_window.w,
178                                       prompt,
179                                       value,
180                                       &screen->find_history,
181                                       NULL);
182       if( !screen->findbuf || !screen->findbuf[0] )
183         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);
197       if( retval == 0 )
198         {
199           lw->repaint  = 1;
200         }
201       else
202         {
203           screen_status_printf(_("Unable to find \'%s\'"), screen->findbuf);
204           screen_bell();
205         }
206       return 1;
207     default:
208       break;
209     }
210   return 0;
213 void
214 screen_display_completion_list(screen_t *screen, GList *list)
216   static GList *prev_list = NULL;
217   static gint prev_length = 0;
218   static gint offset = 0;
219   WINDOW *w = screen->main_window.w;
220   gint length, y=0;
222   length = g_list_length(list);
223   if( list==prev_list && length==prev_length )
224     {
225       offset += screen->main_window.rows;
226       if( offset>=length )
227         offset=0;
228     }
229   else
230     {
231       prev_list = list;
232       prev_length = length;
233       offset = 0;
234     }
236   colors_use(w, COLOR_STATUS_ALERT);
237   while( y<screen->main_window.rows )
238     {
239       GList *item = g_list_nth(list, y+offset);
241       wmove(w, y++, 0);
242       wclrtoeol(w);
243       if( item )
244         {
245           gchar *tmp = g_strdup(item->data);
246           waddstr(w, basename(tmp));
247           g_free(tmp);
248         }
249     }
250   wrefresh(w);
251   doupdate();
252   colors_use(w, COLOR_LIST);
255 void
256 set_xterm_title(const char *format, ...)
258   /* the current xterm title exists under the WM_NAME property */
259   /* and can be retreived with xprop -id $WINDOWID */
261   if( options.enable_xterm_title )
262     {
263       if( g_getenv("WINDOWID") )
264         {
265           char *msg;
266           va_list ap;
267           
268           va_start(ap,format);
269           msg = g_strdup_vprintf(format,ap);
270           va_end(ap);
271           printf("%c]0;%s%c", '\033', msg, '\007'); 
272           g_free(msg);
273         }
274       else
275         options.enable_xterm_title = FALSE;
276     }