strfsong: implement "%disc%"
[ncmpc.git] / src / screen_utils.c
blob4bb18750fcdb0d92c5e638afc8d0378f422a5fa4
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "screen_utils.h"
21 #include "screen.h"
22 #include "mpdclient.h"
23 #include "config.h"
24 #include "i18n.h"
25 #include "options.h"
26 #include "colors.h"
27 #include "wreadln.h"
29 #include <mpd/client.h>
30 #include <ctype.h>
32 void
33 screen_bell(void)
35 if (options.audible_bell)
36 beep();
37 if (options.visible_bell)
38 flash();
41 int
42 screen_getch(const char *prompt)
44 WINDOW *w = screen.status_bar.window.w;
46 colors_use(w, COLOR_STATUS_ALERT);
47 werase(w);
48 wmove(w, 0, 0);
49 waddstr(w, prompt);
51 echo();
52 curs_set(1);
54 int key;
55 while ((key = wgetch(w)) == ERR)
58 #ifdef HAVE_GETMOUSE
59 /* ignore mouse events */
60 if (key == KEY_MOUSE)
61 return screen_getch(prompt);
62 #endif
64 noecho();
65 curs_set(0);
67 return key;
70 bool
71 screen_get_yesno(const char *prompt, bool def)
73 /* NOTE: if one day a translator decides to use a multi-byte character
74 for one of the yes/no keys, we'll have to parse it properly */
76 int key = tolower(screen_getch(prompt));
77 if (key == YES[0])
78 return true;
79 else if (key == NO[0])
80 return false;
81 else
82 return def;
85 char *
86 screen_readln(const char *prompt,
87 const char *value,
88 GList **history,
89 GCompletion *gcmp)
91 struct window *window = &screen.status_bar.window;
92 WINDOW *w = window->w;
93 char *line = NULL;
95 wmove(w, 0,0);
96 curs_set(1);
97 colors_use(w, COLOR_STATUS_ALERT);
98 line = wreadln(w, prompt, value, window->cols, history, gcmp);
99 curs_set(0);
100 return line;
103 char *
104 screen_read_password(const char *prompt)
106 struct window *window = &screen.status_bar.window;
107 WINDOW *w = window->w;
109 wmove(w, 0,0);
110 curs_set(1);
111 colors_use(w, COLOR_STATUS_ALERT);
113 if (prompt == NULL)
114 prompt = _("Password");
115 char *ret = wreadln_masked(w, prompt, NULL, window->cols, NULL, NULL);
117 curs_set(0);
118 return ret;
121 void
122 screen_display_completion_list(GList *list)
124 static GList *prev_list = NULL;
125 static guint prev_length = 0;
126 static guint offset = 0;
127 WINDOW *w = screen.main_window.w;
129 unsigned length = g_list_length(list);
130 if (list == prev_list && length == prev_length) {
131 offset += screen.main_window.rows;
132 if (offset >= length)
133 offset = 0;
134 } else {
135 prev_list = list;
136 prev_length = length;
137 offset = 0;
140 colors_use(w, COLOR_STATUS_ALERT);
142 unsigned y = 0;
143 while (y < screen.main_window.rows) {
144 GList *item = g_list_nth(list, y+offset);
146 wmove(w, y++, 0);
147 wclrtoeol(w);
148 if (item) {
149 gchar *tmp = g_strdup(item->data);
150 waddstr(w, g_basename(tmp));
151 g_free(tmp);
155 wrefresh(w);
156 doupdate();
157 colors_use(w, COLOR_LIST);
160 #ifndef NCMPC_MINI
161 void
162 set_xterm_title(const char *format, ...)
164 /* the current xterm title exists under the WM_NAME property */
165 /* and can be retrieved with xprop -id $WINDOWID */
167 if (options.enable_xterm_title) {
168 if (g_getenv("WINDOWID")) {
169 va_list ap;
170 va_start(ap,format);
171 char *msg = g_strdup_vprintf(format,ap);
172 va_end(ap);
173 printf("\033]0;%s\033\\", msg);
174 fflush(stdout);
175 g_free(msg);
176 } else
177 options.enable_xterm_title = FALSE;
180 #endif