Code

screen_utils: move KEY_MOUSE check to ignore_key(), avoid recursion
[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
45 #ifdef HAVE_GETMOUSE
46                 /* ignore mouse events */
47                 key == KEY_MOUSE ||
48 #endif
49                 key == ERR;
50 }
52 int
53 screen_getch(const char *prompt)
54 {
55         WINDOW *w = screen.status_bar.window.w;
57         colors_use(w, COLOR_STATUS_ALERT);
58         werase(w);
59         wmove(w, 0, 0);
60         waddstr(w, prompt);
62         echo();
63         curs_set(1);
65         int key;
66         while (ignore_key(key = wgetch(w))) {}
68         noecho();
69         curs_set(0);
71         return key;
72 }
74 bool
75 screen_get_yesno(const char *prompt, bool def)
76 {
77         /* NOTE: if one day a translator decides to use a multi-byte character
78            for one of the yes/no keys, we'll have to parse it properly */
80         int key = tolower(screen_getch(prompt));
81         if (key == YES[0])
82                 return true;
83         else if (key == NO[0])
84                 return false;
85         else
86                 return def;
87 }
89 char *
90 screen_readln(const char *prompt,
91               const char *value,
92               GList **history,
93               GCompletion *gcmp)
94 {
95         struct window *window = &screen.status_bar.window;
96         WINDOW *w = window->w;
97         char *line = NULL;
99         wmove(w, 0,0);
100         curs_set(1);
101         colors_use(w, COLOR_STATUS_ALERT);
102         line = wreadln(w, prompt, value, window->cols, history, gcmp);
103         curs_set(0);
104         return line;
107 char *
108 screen_read_password(const char *prompt)
110         struct window *window = &screen.status_bar.window;
111         WINDOW *w = window->w;
113         wmove(w, 0,0);
114         curs_set(1);
115         colors_use(w, COLOR_STATUS_ALERT);
117         if (prompt == NULL)
118                 prompt = _("Password");
119         char *ret = wreadln_masked(w, prompt, NULL, window->cols, NULL, NULL);
121         curs_set(0);
122         return ret;
125 void
126 screen_display_completion_list(GList *list)
128         static GList *prev_list = NULL;
129         static guint prev_length = 0;
130         static guint offset = 0;
131         WINDOW *w = screen.main_window.w;
133         unsigned length = g_list_length(list);
134         if (list == prev_list && length == prev_length) {
135                 offset += screen.main_window.rows;
136                 if (offset >= length)
137                         offset = 0;
138         } else {
139                 prev_list = list;
140                 prev_length = length;
141                 offset = 0;
142         }
144         colors_use(w, COLOR_STATUS_ALERT);
146         unsigned y = 0;
147         while (y < screen.main_window.rows) {
148                 GList *item = g_list_nth(list, y+offset);
150                 wmove(w, y++, 0);
151                 wclrtoeol(w);
152                 if (item) {
153                         gchar *tmp = g_strdup(item->data);
154                         waddstr(w, g_basename(tmp));
155                         g_free(tmp);
156                 }
157         }
159         wrefresh(w);
160         colors_use(w, COLOR_LIST);