Code

Made the xterm title dynamic, added configuration option xterm-title-format
[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, 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=wgetch(w)) == ERR )
67     ;
68   
69   if( key==KEY_RESIZE )
70     screen_resize();
72   noecho();
73   curs_set(0);
74   timeout(SCREEN_TIMEOUT);
76   return key;
77 }
79 char *
80 screen_readln(WINDOW *w, 
81               char *prompt, 
82               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, char *prompt)
98 {
99   return screen_readln(w, prompt, NULL, NULL, NULL);
103 /* query user for a string and find it in a list window */
104 int 
105 screen_find(screen_t *screen,
106             mpdclient_t *c,
107             list_window_t *lw, 
108             int rows,
109             command_t findcmd,
110             list_window_callback_fn_t callback_fn)
112   int reversed = 0;
113   int retval   = 0;
114   char *prompt = FIND_PROMPT;
116   if( findcmd==CMD_LIST_RFIND ||findcmd==CMD_LIST_RFIND_NEXT ) 
117     {
118       prompt = RFIND_PROMPT;
119       reversed = 1;
120     }
122   switch(findcmd)
123     {
124     case CMD_LIST_FIND:
125     case CMD_LIST_RFIND:
126       if( screen->findbuf )
127         {
128           g_free(screen->findbuf);
129           screen->findbuf=NULL;
130         }
131       /* continue... */
132     case CMD_LIST_FIND_NEXT:
133     case CMD_LIST_RFIND_NEXT:
134       if( !screen->findbuf )
135         screen->findbuf=screen_readln(screen->status_window.w,
136                                       prompt,
137                                       (char *) -1, //NULL,
138                                       &screen->find_history,
139                                       NULL);
140       if( !screen->findbuf || !screen->findbuf[0] )
141         return 1; 
142       if( reversed )
143         retval = list_window_rfind(lw, 
144                                    callback_fn,
145                                    c, 
146                                    screen->findbuf,
147                                    options.find_wrap,
148                                    rows);
149       else
150         retval = list_window_find(lw,
151                                   callback_fn,
152                                   c,
153                                   screen->findbuf,
154                                   options.find_wrap);
155       if( retval == 0 )
156         {
157           lw->repaint  = 1;
158         }
159       else
160         {
161           screen_status_printf(_("Unable to find \'%s\'"), screen->findbuf);
162           screen_bell();
163         }
164       return 1;
165     default:
166       break;
167     }
168   return 0;
171 void
172 screen_display_completion_list(screen_t *screen, GList *list)
174   static GList *prev_list = NULL;
175   static gint prev_length = 0;
176   static gint offset = 0;
177   WINDOW *w = screen->main_window.w;
178   gint length, y=0;
180   length = g_list_length(list);
181   if( list==prev_list && length==prev_length )
182     {
183       offset += screen->main_window.rows;
184       if( offset>=length )
185         offset=0;
186     }
187   else
188     {
189       prev_list = list;
190       prev_length = length;
191       offset = 0;
192     }
194   colors_use(w, COLOR_STATUS_ALERT);
195   while( y<screen->main_window.rows )
196     {
197       GList *item = g_list_nth(list, y+offset);
199       wmove(w, y++, 0);
200       wclrtoeol(w);
201       if( item )
202         {
203           gchar *tmp = g_strdup(item->data);
204           waddstr(w, basename(tmp));
205           g_free(tmp);
206         }
207     }
208   wrefresh(w);
209   doupdate();
210   colors_use(w, COLOR_LIST);
213 void
214 set_xterm_title(char *format, ...)
216   /* the current xterm title exists under the WM_NAME property */
217   /* and can be retreived with xprop -id $WINDOWID */
219   if( options.enable_xterm_title )
220     {
221       if( g_getenv("WINDOWID") )
222         {
223           char buffer[512];
224           va_list ap;
225           
226           va_start(ap,format);
227           vsnprintf(buffer,sizeof(buffer),format,ap);
228           va_end(ap);
229           printf("%c]0;%s%c", '\033', buffer, '\007'); 
230         }
231       else
232         options.enable_xterm_title = FALSE;
233     }