- if (flag && keypad_xmit) {
- TPUTS_TRACE("keypad_xmit");
- putp(keypad_xmit);
- _nc_flush();
- } else if (!flag && keypad_local) {
- TPUTS_TRACE("keypad_local");
- putp(keypad_local);
- _nc_flush();
- }
-
- if (flag && !SP->_tried) {
- _nc_init_keytry();
- SP->_tried = TRUE;
+ int rc = ERR;
+
+ if (sp != 0) {
+#ifdef USE_PTHREADS
+ /*
+ * We might have this situation in a multithreaded application that
+ * has wgetch() reading in more than one thread. putp() and below
+ * may use SP explicitly.
+ */
+ if (_nc_use_pthreads && sp != CURRENT_SCREEN) {
+ SCREEN *save_sp;
+
+ /* cannot use use_screen(), since that is not in tinfo library */
+ _nc_lock_global(curses);
+ save_sp = CURRENT_SCREEN;
+ _nc_set_screen(sp);
+ rc = _nc_keypad(sp, flag);
+ _nc_set_screen(save_sp);
+ _nc_unlock_global(curses);
+ } else
+#endif
+ {
+#ifdef USE_TERM_DRIVER
+ rc = CallDriver_1(sp, kpad, flag);
+ if (rc == OK)
+ sp->_keypad_on = flag;
+#else
+ if (flag) {
+ (void) NCURSES_SP_NAME(_nc_putp_flush) (NCURSES_SP_ARGx
+ "keypad_xmit",
+ keypad_xmit);
+ } else if (!flag && keypad_local) {
+ (void) NCURSES_SP_NAME(_nc_putp_flush) (NCURSES_SP_ARGx
+ "keypad_local",
+ keypad_local);
+ }
+
+ if (flag && !sp->_tried) {
+ _nc_init_keytry(sp);
+ sp->_tried = TRUE;
+ }
+ sp->_keypad_on = flag;
+ rc = OK;
+#endif
+ }