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, 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);
62 echo();
63 curs_set(1);
64 timeout(-1);
66 while( (key=wgetch(w)) == ERR )
67 ;
69 #ifdef ENABLE_RAW_MODE
70 if( key==KEY_SIGSTOP )
71 sigstop();
72 #endif
74 #ifdef HAVE_GETMOUSE
75 /* ignore mouse events */
76 if( key==KEY_MOUSE )
77 return screen_getch(w, prompt);
78 #endif
80 if( key==KEY_RESIZE )
81 {
82 screen_resize();
83 }
85 noecho();
86 curs_set(0);
87 timeout(SCREEN_TIMEOUT);
89 return key;
90 }
92 char *
93 screen_readln(WINDOW *w,
94 char *prompt,
95 char *value,
96 GList **history,
97 GCompletion *gcmp)
98 {
99 char *line = NULL;
101 wmove(w, 0,0);
102 curs_set(1);
103 colors_use(w, COLOR_STATUS_ALERT);
104 line = wreadln(w, prompt, value, COLS, history, gcmp);
105 curs_set(0);
106 return line;
107 }
109 char *
110 screen_getstr(WINDOW *w, char *prompt)
111 {
112 return screen_readln(w, prompt, NULL, NULL, NULL);
113 }
116 /* query user for a string and find it in a list window */
117 int
118 screen_find(screen_t *screen,
119 mpdclient_t *c,
120 list_window_t *lw,
121 int rows,
122 command_t findcmd,
123 list_window_callback_fn_t callback_fn)
124 {
125 int reversed = 0;
126 int retval = 0;
127 char *prompt = FIND_PROMPT;
129 if( findcmd==CMD_LIST_RFIND ||findcmd==CMD_LIST_RFIND_NEXT )
130 {
131 prompt = RFIND_PROMPT;
132 reversed = 1;
133 }
135 switch(findcmd)
136 {
137 case CMD_LIST_FIND:
138 case CMD_LIST_RFIND:
139 if( screen->findbuf )
140 {
141 g_free(screen->findbuf);
142 screen->findbuf=NULL;
143 }
144 /* continue... */
145 case CMD_LIST_FIND_NEXT:
146 case CMD_LIST_RFIND_NEXT:
147 if( !screen->findbuf )
148 screen->findbuf=screen_readln(screen->status_window.w,
149 prompt,
150 (char *) -1, //NULL,
151 &screen->find_history,
152 NULL);
153 if( !screen->findbuf || !screen->findbuf[0] )
154 return 1;
155 if( reversed )
156 retval = list_window_rfind(lw,
157 callback_fn,
158 c,
159 screen->findbuf,
160 options.find_wrap,
161 rows);
162 else
163 retval = list_window_find(lw,
164 callback_fn,
165 c,
166 screen->findbuf,
167 options.find_wrap);
168 if( retval == 0 )
169 {
170 lw->repaint = 1;
171 }
172 else
173 {
174 screen_status_printf(_("Unable to find \'%s\'"), screen->findbuf);
175 screen_bell();
176 }
177 return 1;
178 default:
179 break;
180 }
181 return 0;
182 }
184 void
185 screen_display_completion_list(screen_t *screen, GList *list)
186 {
187 static GList *prev_list = NULL;
188 static gint prev_length = 0;
189 static gint offset = 0;
190 WINDOW *w = screen->main_window.w;
191 gint length, y=0;
193 length = g_list_length(list);
194 if( list==prev_list && length==prev_length )
195 {
196 offset += screen->main_window.rows;
197 if( offset>=length )
198 offset=0;
199 }
200 else
201 {
202 prev_list = list;
203 prev_length = length;
204 offset = 0;
205 }
207 colors_use(w, COLOR_STATUS_ALERT);
208 while( y<screen->main_window.rows )
209 {
210 GList *item = g_list_nth(list, y+offset);
212 wmove(w, y++, 0);
213 wclrtoeol(w);
214 if( item )
215 {
216 gchar *tmp = g_strdup(item->data);
217 waddstr(w, basename(tmp));
218 g_free(tmp);
219 }
220 }
221 wrefresh(w);
222 doupdate();
223 colors_use(w, COLOR_LIST);
224 }
226 void
227 set_xterm_title(char *format, ...)
228 {
229 /* the current xterm title exists under the WM_NAME property */
230 /* and can be retreived with xprop -id $WINDOWID */
232 if( options.enable_xterm_title )
233 {
234 if( g_getenv("WINDOWID") )
235 {
236 char buffer[512];
237 va_list ap;
239 va_start(ap,format);
240 vsnprintf(buffer,sizeof(buffer),format,ap);
241 va_end(ap);
242 printf("%c]0;%s%c", '\033', buffer, '\007');
243 }
244 else
245 options.enable_xterm_title = FALSE;
246 }
247 }