Code

screen_lyrics: set current.song
[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);
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         char *ret;
105         if (w == NULL) {
106                 int rows, cols;
107                 getmaxyx(stdscr, rows, cols);
108                 /* create window for input */
109                 w = newwin(1,  cols, rows-1, 0);
110                 leaveok(w, FALSE);
111                 keypad(w, TRUE);
112         }
114         wmove(w, 0,0);
115         curs_set(1);
116         colors_use(w, COLOR_STATUS_ALERT);
118         if (prompt == NULL)
119                 prompt = _("Password: ");
120         ret = wreadln_masked(w, prompt, NULL, COLS, NULL, NULL);
122         curs_set(0);
123         return ret;
126 static gint
127 _screen_auth(struct mpdclient *c, gint recursion)
129         mpd_clearError(c->connection);
130         if (recursion > 2)
131                 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)
136                 return  _screen_auth(c, ++recursion);
137         return 0;
140 gint
141 screen_auth(struct mpdclient *c)
143         gint ret = _screen_auth(c, 0);
144         mpdclient_update(c);
145         curs_set(0);
146         return ret;
149 /* query user for a string and find it in a list window */
150 int
151 screen_find(screen_t *screen,
152             list_window_t *lw,
153             int rows,
154             command_t findcmd,
155             list_window_callback_fn_t callback_fn,
156             void *callback_data)
158         int reversed = 0;
159         int retval = 0;
160         const char *prompt = FIND_PROMPT;
161         char *value = options.find_show_last_pattern ? (char *) -1 : NULL;
163         if (findcmd == CMD_LIST_RFIND || findcmd == CMD_LIST_RFIND_NEXT) {
164                 prompt = RFIND_PROMPT;
165                 reversed = 1;
166         }
168         switch (findcmd) {
169         case CMD_LIST_FIND:
170         case CMD_LIST_RFIND:
171                 if (screen->findbuf) {
172                         g_free(screen->findbuf);
173                         screen->findbuf=NULL;
174                 }
175                 /* 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);
186                 if (!screen->findbuf || !screen->findbuf[0])
187                         return 1;
189                 if (reversed)
190                         retval = list_window_rfind(lw,
191                                                    callback_fn,
192                                                    callback_data,
193                                                    screen->findbuf,
194                                                    options.find_wrap,
195                                                    rows);
196                 else
197                         retval = list_window_find(lw,
198                                                   callback_fn,
199                                                   callback_data,
200                                                   screen->findbuf,
201                                                   options.find_wrap);
203                 if (retval == 0)
204                         lw->repaint  = 1;
205                 else {
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 guint prev_length = 0;
221         static guint offset = 0;
222         WINDOW *w = screen->main_window.w;
223         guint length, y=0;
225         length = g_list_length(list);
226         if (list == prev_list && length == prev_length) {
227                 offset += screen->main_window.rows;
228                 if (offset >= length)
229                         offset = 0;
230         } else {
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                 GList *item = g_list_nth(list, y+offset);
240                 wmove(w, y++, 0);
241                 wclrtoeol(w);
242                 if (item) {
243                         gchar *tmp = g_strdup(item->data);
244                         waddstr(w, basename(tmp));
245                         g_free(tmp);
246                 }
247         }
249         wrefresh(w);
250         doupdate();
251         colors_use(w, COLOR_LIST);
254 void
255 set_xterm_title(const char *format, ...)
257         /* the current xterm title exists under the WM_NAME property */
258         /* and can be retreived with xprop -id $WINDOWID */
260         if (options.enable_xterm_title) {
261                 if (g_getenv("WINDOWID")) {
262                         char *msg;
263                         va_list ap;
265                         va_start(ap,format);
266                         msg = g_strdup_vprintf(format,ap);
267                         va_end(ap);
268                         printf("%c]0;%s%c", '\033', msg, '\007');
269                         g_free(msg);
270                 } else
271                         options.enable_xterm_title = FALSE;
272         }