blob: 4ee9119e5c164425f6265c01fc8ae6be0dbe8c4e [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
Steve Kondikae271bc2015-11-15 02:50:53 +01002 * Copyright (c) 1998-2011,2012 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05303 * *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
11 * *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
14 * *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
22 * *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
26 * authorization. *
27 ****************************************************************************/
28
29/****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 * and: Thomas E. Dickey 1996-on *
Steve Kondikae271bc2015-11-15 02:50:53 +010033 * and: Juergen Pfeifer 2009 *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053034 ****************************************************************************/
35
36/*
37** lib_ungetch.c
38**
39** The routine ungetch().
40**
41*/
42
43#include <curses.priv.h>
44
Steve Kondikae271bc2015-11-15 02:50:53 +010045MODULE_ID("$Id: lib_ungetch.c,v 1.16 2012/08/04 17:38:53 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053046
47#include <fifo_defs.h>
48
49#ifdef TRACE
50NCURSES_EXPORT(void)
51_nc_fifo_dump(SCREEN *sp)
52{
53 int i;
54 T(("head = %d, tail = %d, peek = %d", head, tail, peek));
55 for (i = 0; i < 10; i++)
56 T(("char %d = %s", i, _nc_tracechar(sp, sp->_fifo[i])));
57}
58#endif /* TRACE */
59
60NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +010061safe_ungetch(SCREEN *sp, int ch)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053062{
63 int rc = ERR;
64
Steve Kondikae271bc2015-11-15 02:50:53 +010065 T((T_CALLED("ungetch(%p,%s)"), (void *) sp, _nc_tracechar(sp, ch)));
66
67 if (sp != 0 && tail >= 0) {
68 if (head < 0) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053069 head = 0;
70 t_inc();
71 peek = tail; /* no raw keys */
Steve Kondikae271bc2015-11-15 02:50:53 +010072 } else {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053073 h_dec();
Steve Kondikae271bc2015-11-15 02:50:53 +010074 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053075
76 sp->_fifo[head] = ch;
77 T(("ungetch %s ok", _nc_tracechar(sp, ch)));
78#ifdef TRACE
79 if (USE_TRACEF(TRACE_IEVENT)) {
80 _nc_fifo_dump(sp);
81 _nc_unlock_global(tracef);
82 }
83#endif
84 rc = OK;
85 }
Steve Kondikae271bc2015-11-15 02:50:53 +010086 returnCode(rc);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053087}
88
89NCURSES_EXPORT(int)
90ungetch(int ch)
91{
Steve Kondikae271bc2015-11-15 02:50:53 +010092 return safe_ungetch(CURRENT_SCREEN, ch);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053093}