]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/base/lib_getch.c
ncurses 5.7 - patch 20090117
[ncurses.git] / ncurses / base / lib_getch.c
index f77df69e77255f09ee64a1ee27e1890bd90c70f6..a3812bee76e8003cdf946bcb9aa8503fc9099e38 100644 (file)
@@ -41,7 +41,7 @@
 
 #include <curses.priv.h>
 
 
 #include <curses.priv.h>
 
-MODULE_ID("$Id: lib_getch.c,v 1.91 2008/05/31 21:47:48 tom Exp $")
+MODULE_ID("$Id: lib_getch.c,v 1.99 2008/09/20 19:46:13 tom Exp $")
 
 #include <fifo_defs.h>
 
 
 #include <fifo_defs.h>
 
@@ -76,6 +76,13 @@ set_escdelay(int value)
 }
 #endif
 
 }
 #endif
 
+static int
+_nc_use_meta(WINDOW *win)
+{
+    SCREEN *sp = _nc_screen_of(win);
+    return (sp ? sp->_use_meta : 0);
+}
+
 #ifdef NCURSES_WGETCH_EVENTS
 #define TWAIT_MASK 7
 #else
 #ifdef NCURSES_WGETCH_EVENTS
 #define TWAIT_MASK 7
 #else
@@ -249,21 +256,66 @@ fifo_clear(SCREEN *sp)
 static int kgetch(SCREEN *EVENTLIST_2nd(_nc_eventlist * evl));
 
 static void
 static int kgetch(SCREEN *EVENTLIST_2nd(_nc_eventlist * evl));
 
 static void
-refresh_if_needed(WINDOW *win)
+recur_wrefresh(WINDOW *win)
 {
 {
-    if ((is_wintouched(win) || (win->_flags & _HASMOVED))
-       && !(win->_flags & _ISPAD)) {
+#ifdef USE_PTHREADS
+    SCREEN *sp = _nc_screen_of(win);
+    if (_nc_use_pthreads && sp != SP) {
+       SCREEN *save_SP;
+
+       /* temporarily switch to the window's screen to check/refresh */
+       _nc_lock_global(curses);
+       save_SP = SP;
+       _nc_set_screen(sp);
+       recur_wrefresh(win);
+       _nc_set_screen(save_SP);
+       _nc_unlock_global(curses);
+    } else
+#endif
+       if ((is_wintouched(win) || (win->_flags & _HASMOVED))
+           && !(win->_flags & _ISPAD)) {
        wrefresh(win);
     }
 }
 
        wrefresh(win);
     }
 }
 
+static int
+recur_wgetnstr(WINDOW *win, char *buf)
+{
+    SCREEN *sp = _nc_screen_of(win);
+    int rc;
+
+    if (sp != 0) {
+#ifdef USE_PTHREADS
+       if (_nc_use_pthreads && sp != SP) {
+           SCREEN *save_SP;
+
+           /* temporarily switch to the window's screen to get cooked input */
+           _nc_lock_global(curses);
+           save_SP = SP;
+           _nc_set_screen(sp);
+           rc = recur_wgetnstr(win, buf);
+           _nc_set_screen(save_SP);
+           _nc_unlock_global(curses);
+       } else
+#endif
+       {
+           sp->_called_wgetch = TRUE;
+           rc = wgetnstr(win, buf, MAXCOLUMNS);
+           sp->_called_wgetch = FALSE;
+       }
+    } else {
+       rc = ERR;
+    }
+    return rc;
+}
+
 NCURSES_EXPORT(int)
 _nc_wgetch(WINDOW *win,
           unsigned long *result,
           int use_meta
           EVENTLIST_2nd(_nc_eventlist * evl))
 {
 NCURSES_EXPORT(int)
 _nc_wgetch(WINDOW *win,
           unsigned long *result,
           int use_meta
           EVENTLIST_2nd(_nc_eventlist * evl))
 {
-    SCREEN *sp = _nc_screen_of(win);
+    SCREEN *sp;
     int ch;
 #ifdef NCURSES_WGETCH_EVENTS
     long event_delay = -1;
     int ch;
 #ifdef NCURSES_WGETCH_EVENTS
     long event_delay = -1;
@@ -272,12 +324,14 @@ _nc_wgetch(WINDOW *win,
     T((T_CALLED("_nc_wgetch(%p)"), win));
 
     *result = 0;
     T((T_CALLED("_nc_wgetch(%p)"), win));
 
     *result = 0;
+
+    sp = _nc_screen_of(win);
     if (win == 0 || sp == 0) {
        returnCode(ERR);
     }
 
     if (cooked_key_in_fifo()) {
     if (win == 0 || sp == 0) {
        returnCode(ERR);
     }
 
     if (cooked_key_in_fifo()) {
-       refresh_if_needed(win);
+       recur_wrefresh(win);
        *result = fifo_pull(sp);
        returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
     }
        *result = fifo_pull(sp);
        returnCode(*result >= KEY_MIN ? KEY_CODE_YES : OK);
     }
@@ -302,9 +356,7 @@ _nc_wgetch(WINDOW *win,
 
        TR(TRACE_IEVENT, ("filling queue in cooked mode"));
 
 
        TR(TRACE_IEVENT, ("filling queue in cooked mode"));
 
-       sp->_called_wgetch = TRUE;
-       rc = wgetnstr(win, buf, MAXCOLUMNS);
-       sp->_called_wgetch = FALSE;
+       rc = recur_wgetnstr(win, buf);
 
        /* ungetch in reverse order */
 #ifdef NCURSES_WGETCH_EVENTS
 
        /* ungetch in reverse order */
 #ifdef NCURSES_WGETCH_EVENTS
@@ -327,9 +379,9 @@ _nc_wgetch(WINDOW *win,
     if (win->_use_keypad != sp->_keypad_on)
        _nc_keypad(sp, win->_use_keypad);
 
     if (win->_use_keypad != sp->_keypad_on)
        _nc_keypad(sp, win->_use_keypad);
 
-    refresh_if_needed(win);
+    recur_wrefresh(win);
 
 
-    if (!win->_notimeout && (win->_delay >= 0 || sp->_cbreak > 1)) {
+    if (win->_notimeout || (win->_delay >= 0) || (sp->_cbreak > 1)) {
        if (head == -1) {       /* fifo is empty */
            int delay;
            int rc;
        if (head == -1) {       /* fifo is empty */
            int delay;
            int rc;
@@ -355,8 +407,9 @@ _nc_wgetch(WINDOW *win,
                returnCode(KEY_CODE_YES);
            }
 #endif
                returnCode(KEY_CODE_YES);
            }
 #endif
-           if (!rc)
+           if (!rc) {
                returnCode(ERR);
                returnCode(ERR);
+           }
        }
        /* else go on to read data available */
     }
        }
        /* else go on to read data available */
     }
@@ -479,14 +532,13 @@ _nc_wgetch(WINDOW *win,
 NCURSES_EXPORT(int)
 wgetch_events(WINDOW *win, _nc_eventlist * evl)
 {
 NCURSES_EXPORT(int)
 wgetch_events(WINDOW *win, _nc_eventlist * evl)
 {
-    SCREEN *sp = _nc_screen_of(win);
     int code;
     unsigned long value;
 
     T((T_CALLED("wgetch_events(%p,%p)"), win, evl));
     code = _nc_wgetch(win,
                      &value,
     int code;
     unsigned long value;
 
     T((T_CALLED("wgetch_events(%p,%p)"), win, evl));
     code = _nc_wgetch(win,
                      &value,
-                     sp->_use_meta
+                     _nc_use_meta(win)
                      EVENTLIST_2nd(evl));
     if (code != ERR)
        code = value;
                      EVENTLIST_2nd(evl));
     if (code != ERR)
        code = value;
@@ -497,14 +549,13 @@ wgetch_events(WINDOW *win, _nc_eventlist * evl)
 NCURSES_EXPORT(int)
 wgetch(WINDOW *win)
 {
 NCURSES_EXPORT(int)
 wgetch(WINDOW *win)
 {
-    SCREEN *sp = _nc_screen_of(win);
     int code;
     unsigned long value;
 
     T((T_CALLED("wgetch(%p)"), win));
     code = _nc_wgetch(win,
                      &value,
     int code;
     unsigned long value;
 
     T((T_CALLED("wgetch(%p)"), win));
     code = _nc_wgetch(win,
                      &value,
-                     (sp ? sp->_use_meta : 0)
+                     _nc_use_meta(win)
                      EVENTLIST_2nd((_nc_eventlist *) 0));
     if (code != ERR)
        code = value;
                      EVENTLIST_2nd((_nc_eventlist *) 0));
     if (code != ERR)
        code = value;