Code

screen_utils: move KEY_MOUSE check to ignore_key(), avoid recursion
[ncmpc.git] / src / screen_utils.c
index 1203a938c8c4ca657c1f31b2a78bb7b316e470ce..8f69930d64126752b048164fcd1a8d7e9d0df379 100644 (file)
@@ -1,5 +1,5 @@
 /* ncmpc (Ncurses MPD Client)
- * (c) 2004-2010 The Music Player Daemon Project
+ * (c) 2004-2017 The Music Player Daemon Project
  * Project homepage: http://musicpd.org
  *
  * This program is free software; you can redistribute it and/or modify
@@ -27,6 +27,7 @@
 #include "wreadln.h"
 
 #include <mpd/client.h>
+#include <ctype.h>
 
 void
 screen_bell(void)
@@ -37,11 +38,21 @@ screen_bell(void)
                flash();
 }
 
+static bool
+ignore_key(int key)
+{
+       return
+#ifdef HAVE_GETMOUSE
+               /* ignore mouse events */
+               key == KEY_MOUSE ||
+#endif
+               key == ERR;
+}
+
 int
 screen_getch(const char *prompt)
 {
        WINDOW *w = screen.status_bar.window.w;
-       int key = -1;
 
        colors_use(w, COLOR_STATUS_ALERT);
        werase(w);
@@ -51,14 +62,8 @@ screen_getch(const char *prompt)
        echo();
        curs_set(1);
 
-       while ((key = wgetch(w)) == ERR)
-               ;
-
-#ifdef HAVE_GETMOUSE
-       /* ignore mouse events */
-       if (key == KEY_MOUSE)
-               return screen_getch(prompt);
-#endif
+       int key;
+       while (ignore_key(key = wgetch(w))) {}
 
        noecho();
        curs_set(0);
@@ -66,6 +71,21 @@ screen_getch(const char *prompt)
        return key;
 }
 
+bool
+screen_get_yesno(const char *prompt, bool def)
+{
+       /* NOTE: if one day a translator decides to use a multi-byte character
+          for one of the yes/no keys, we'll have to parse it properly */
+
+       int key = tolower(screen_getch(prompt));
+       if (key == YES[0])
+               return true;
+       else if (key == NO[0])
+               return false;
+       else
+               return def;
+}
+
 char *
 screen_readln(const char *prompt,
              const char *value,
@@ -89,7 +109,6 @@ screen_read_password(const char *prompt)
 {
        struct window *window = &screen.status_bar.window;
        WINDOW *w = window->w;
-       char *ret;
 
        wmove(w, 0,0);
        curs_set(1);
@@ -97,7 +116,7 @@ screen_read_password(const char *prompt)
 
        if (prompt == NULL)
                prompt = _("Password");
-       ret = wreadln_masked(w, prompt, NULL, window->cols, NULL, NULL);
+       char *ret = wreadln_masked(w, prompt, NULL, window->cols, NULL, NULL);
 
        curs_set(0);
        return ret;
@@ -110,9 +129,8 @@ screen_display_completion_list(GList *list)
        static guint prev_length = 0;
        static guint offset = 0;
        WINDOW *w = screen.main_window.w;
-       guint length, y=0;
 
-       length = g_list_length(list);
+       unsigned length = g_list_length(list);
        if (list == prev_list && length == prev_length) {
                offset += screen.main_window.rows;
                if (offset >= length)
@@ -124,6 +142,8 @@ screen_display_completion_list(GList *list)
        }
 
        colors_use(w, COLOR_STATUS_ALERT);
+
+       unsigned y = 0;
        while (y < screen.main_window.rows) {
                GList *item = g_list_nth(list, y+offset);
 
@@ -137,29 +157,5 @@ screen_display_completion_list(GList *list)
        }
 
        wrefresh(w);
-       doupdate();
        colors_use(w, COLOR_LIST);
 }
-
-#ifndef NCMPC_MINI
-void
-set_xterm_title(const char *format, ...)
-{
-       /* the current xterm title exists under the WM_NAME property */
-       /* and can be retrieved with xprop -id $WINDOWID */
-
-       if (options.enable_xterm_title) {
-               if (g_getenv("WINDOWID")) {
-                       char *msg;
-                       va_list ap;
-
-                       va_start(ap,format);
-                       msg = g_strdup_vprintf(format,ap);
-                       va_end(ap);
-                       printf("%c]0;%s%c", '\033', msg, '\007');
-                       g_free(msg);
-               } else
-                       options.enable_xterm_title = FALSE;
-       }
-}
-#endif