Code

891b9b3cb61bac1f673d1d45908712839d84f09d
[ncmpc.git] / src / screen_utils.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2017 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
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.
9  *
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.
14  *
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.
18  */
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)
34 {
35         if (options.audible_bell)
36                 beep();
37         if (options.visible_bell)
38                 flash();
39 }
41 static bool
42 ignore_key(int key)
43 {
44         return key == ERR;
45 }
47 int
48 screen_getch(const char *prompt)
49 {
50         WINDOW *w = screen.status_bar.window.w;
52         colors_use(w, COLOR_STATUS_ALERT);
53         werase(w);
54         wmove(w, 0, 0);
55         waddstr(w, prompt);
57         echo();
58         curs_set(1);
60         int key;
61         while (ignore_key(key = wgetch(w))) {}
63 #ifdef HAVE_GETMOUSE
64         /* ignore mouse events */
65         if (key == KEY_MOUSE)
66                 return screen_getch(prompt);
67 #endif
69         noecho();
70         curs_set(0);
72         return key;
73 }
75 bool
76 screen_get_yesno(const char *prompt, bool def)
77 {
78         /* NOTE: if one day a translator decides to use a multi-byte character
79            for one of the yes/no keys, we'll have to parse it properly */
81         int key = tolower(screen_getch(prompt));
82         if (key == YES[0])
83                 return true;
84         else if (key == NO[0])
85                 return false;
86         else
87                 return def;
88 }
90 char *
91 screen_readln(const char *prompt,
92               const char *value,
93               GList **history,
94               GCompletion *gcmp)
95 {
96         struct window *window = &screen.status_bar.window;
97         WINDOW *w = window->w;
98         char *line = NULL;
100         wmove(w, 0,0);
101         curs_set(1);
102         colors_use(w, COLOR_STATUS_ALERT);
103         line = wreadln(w, prompt, value, window->cols, history, gcmp);
104         curs_set(0);
105         return line;
108 char *
109 screen_read_password(const char *prompt)
111         struct window *window = &screen.status_bar.window;
112         WINDOW *w = window->w;
114         wmove(w, 0,0);
115         curs_set(1);
116         colors_use(w, COLOR_STATUS_ALERT);
118         if (prompt == NULL)
119                 prompt = _("Password");
120         char *ret = wreadln_masked(w, prompt, NULL, window->cols, NULL, NULL);
122         curs_set(0);
123         return ret;
126 void
127 screen_display_completion_list(GList *list)
129         static GList *prev_list = NULL;
130         static guint prev_length = 0;
131         static guint offset = 0;
132         WINDOW *w = screen.main_window.w;
134         unsigned length = g_list_length(list);
135         if (list == prev_list && length == prev_length) {
136                 offset += screen.main_window.rows;
137                 if (offset >= length)
138                         offset = 0;
139         } else {
140                 prev_list = list;
141                 prev_length = length;
142                 offset = 0;
143         }
145         colors_use(w, COLOR_STATUS_ALERT);
147         unsigned y = 0;
148         while (y < screen.main_window.rows) {
149                 GList *item = g_list_nth(list, y+offset);
151                 wmove(w, y++, 0);
152                 wclrtoeol(w);
153                 if (item) {
154                         gchar *tmp = g_strdup(item->data);
155                         waddstr(w, g_basename(tmp));
156                         g_free(tmp);
157                 }
158         }
160         wrefresh(w);
161         colors_use(w, COLOR_LIST);