Code

disable more features with --enable-mini
[ncmpc.git] / src / screen_utils.c
1 /*
2  * (c) 2004 by Kalle Wallin <kaw@linux.se>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  *
17  */
19 #include "screen_utils.h"
20 #include "screen.h"
21 #include "mpdclient.h"
22 #include "config.h"
23 #include "i18n.h"
24 #include "support.h"
25 #include "options.h"
26 #include "colors.h"
27 #include "wreadln.h"
29 #include <stdlib.h>
30 #include <unistd.h>
32 #define FIND_PROMPT  _("Find: ")
33 #define RFIND_PROMPT _("Find backward: ")
35 void
36 screen_bell(void)
37 {
38         if (options.audible_bell)
39                 beep();
40         if (options.visible_bell)
41                 flash();
42 }
44 int
45 screen_getch(WINDOW *w, const char *prompt)
46 {
47         int key = -1;
49         colors_use(w, COLOR_STATUS_ALERT);
50         wclear(w);
51         wmove(w, 0, 0);
52         waddstr(w, prompt);
54         echo();
55         curs_set(1);
57         while ((key = wgetch(w)) == ERR)
58                 ;
60 #ifdef HAVE_GETMOUSE
61         /* ignore mouse events */
62         if (key == KEY_MOUSE)
63                 return screen_getch(w, prompt);
64 #endif
66         noecho();
67         curs_set(0);
69         return key;
70 }
72 char *
73 screen_readln(WINDOW *w,
74               const char *prompt,
75               const char *value,
76               GList **history,
77               GCompletion *gcmp)
78 {
79         char *line = NULL;
81         wmove(w, 0,0);
82         curs_set(1);
83         colors_use(w, COLOR_STATUS_ALERT);
84         line = wreadln(w, prompt, value, COLS, history, gcmp);
85         curs_set(0);
86         return line;
87 }
89 char *
90 screen_getstr(WINDOW *w, const char *prompt)
91 {
92         return screen_readln(w, prompt, NULL, NULL, NULL);
93 }
95 static char *
96 screen_read_password(WINDOW *w, const char *prompt)
97 {
98         char *ret;
100         if (w == NULL) {
101                 int rows, cols;
102                 getmaxyx(stdscr, rows, cols);
103                 /* create window for input */
104                 w = newwin(1,  cols, rows-1, 0);
105                 leaveok(w, FALSE);
106                 keypad(w, TRUE);
107         }
109         wmove(w, 0,0);
110         curs_set(1);
111         colors_use(w, COLOR_STATUS_ALERT);
113         if (prompt == NULL)
114                 prompt = _("Password: ");
115         ret = wreadln_masked(w, prompt, NULL, COLS, NULL, NULL);
117         curs_set(0);
118         return ret;
121 static gint
122 _screen_auth(struct mpdclient *c, gint recursion)
124         mpd_clearError(c->connection);
125         if (recursion > 2)
126                 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)
131                 return  _screen_auth(c, ++recursion);
132         return 0;
135 gint
136 screen_auth(struct mpdclient *c)
138         gint ret = _screen_auth(c, 0);
139         mpdclient_update(c);
140         curs_set(0);
141         return ret;
144 /* query user for a string and find it in a list window */
145 int
146 screen_find(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         case CMD_LIST_FIND:
164         case CMD_LIST_RFIND:
165                 if (screen.findbuf) {
166                         g_free(screen.findbuf);
167                         screen.findbuf=NULL;
168                 }
169                 /* continue... */
171         case CMD_LIST_FIND_NEXT:
172         case CMD_LIST_RFIND_NEXT:
173                 if (!screen.findbuf)
174                         screen.findbuf=screen_readln(screen.status_window.w,
175                                                      prompt,
176                                                      value,
177                                                      &screen.find_history,
178                                                      NULL);
180                 if (!screen.findbuf || !screen.findbuf[0])
181                         return 1;
183                 if (reversed)
184                         retval = list_window_rfind(lw,
185                                                    callback_fn,
186                                                    callback_data,
187                                                    screen.findbuf,
188                                                    options.find_wrap,
189                                                    rows);
190                 else
191                         retval = list_window_find(lw,
192                                                   callback_fn,
193                                                   callback_data,
194                                                   screen.findbuf,
195                                                   options.find_wrap);
197                 if (retval != 0) {
198                         screen_status_printf(_("Unable to find \'%s\'"),
199                                              screen.findbuf);
200                         screen_bell();
201                 }
202                 return 1;
203         default:
204                 break;
205         }
206         return 0;
209 void
210 screen_display_completion_list(GList *list)
212         static GList *prev_list = NULL;
213         static guint prev_length = 0;
214         static guint offset = 0;
215         WINDOW *w = screen.main_window.w;
216         guint length, y=0;
218         length = g_list_length(list);
219         if (list == prev_list && length == prev_length) {
220                 offset += screen.main_window.rows;
221                 if (offset >= length)
222                         offset = 0;
223         } else {
224                 prev_list = list;
225                 prev_length = length;
226                 offset = 0;
227         }
229         colors_use(w, COLOR_STATUS_ALERT);
230         while (y < screen.main_window.rows) {
231                 GList *item = g_list_nth(list, y+offset);
233                 wmove(w, y++, 0);
234                 wclrtoeol(w);
235                 if (item) {
236                         gchar *tmp = g_strdup(item->data);
237                         waddstr(w, g_basename(tmp));
238                         g_free(tmp);
239                 }
240         }
242         wrefresh(w);
243         doupdate();
244         colors_use(w, COLOR_LIST);
247 #ifndef NCMPC_MINI
248 void
249 set_xterm_title(const char *format, ...)
251         /* the current xterm title exists under the WM_NAME property */
252         /* and can be retreived with xprop -id $WINDOWID */
254         if (options.enable_xterm_title) {
255                 if (g_getenv("WINDOWID")) {
256                         char *msg;
257                         va_list ap;
259                         va_start(ap,format);
260                         msg = g_strdup_vprintf(format,ap);
261                         va_end(ap);
262                         printf("%c]0;%s%c", '\033', msg, '\007');
263                         g_free(msg);
264                 } else
265                         options.enable_xterm_title = FALSE;
266         }
268 #endif