1 /****************************************************************************
2 * Copyright (c) 1998-2009,2010 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
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 *
33 * and: Juergen Pfeifer *
34 ****************************************************************************/
39 ** Common data that may/may not be allocated, but is referenced globally
43 #include <curses.priv.h>
45 MODULE_ID("$Id: lib_data.c,v 1.62 2011/09/26 09:48:08 tom Exp $")
48 * OS/2's native linker complains if we don't initialize public data when
49 * constructing a dll (reported by J.J.G.Ripoll).
52 NCURSES_EXPORT(WINDOW *)
53 NCURSES_PUBLIC_VAR(stdscr) (void)
55 return CURRENT_SCREEN ? StdScreen(CURRENT_SCREEN) : 0;
57 NCURSES_EXPORT(WINDOW *)
58 NCURSES_PUBLIC_VAR(curscr) (void)
60 return CURRENT_SCREEN ? CurScreen(CURRENT_SCREEN) : 0;
62 NCURSES_EXPORT(WINDOW *)
63 NCURSES_PUBLIC_VAR(newscr) (void)
65 return CURRENT_SCREEN ? NewScreen(CURRENT_SCREEN) : 0;
68 NCURSES_EXPORT_VAR(WINDOW *) stdscr = 0;
69 NCURSES_EXPORT_VAR(WINDOW *) curscr = 0;
70 NCURSES_EXPORT_VAR(WINDOW *) newscr = 0;
73 NCURSES_EXPORT_VAR(SCREEN *) _nc_screen_chain = 0;
76 * The variable 'SP' will be defined as a function on systems that cannot link
77 * data-only modules, since it is used in a lot of places within ncurses and we
78 * cannot guarantee that any application will use any particular function. We
79 * put the WINDOW variables in this module, because it appears that any
80 * application that uses them will also use 'SP'.
82 * This module intentionally does not reference other ncurses modules, to avoid
83 * module coupling that increases the size of the executable.
86 static SCREEN *my_screen;
88 NCURSES_EXPORT(SCREEN *)
95 _nc_alloc_screen(void)
97 return ((my_screen = _nc_alloc_screen_sp()) != 0);
101 _nc_set_screen(SCREEN *sp)
108 NCURSES_EXPORT_VAR(SCREEN *) SP = NULL; /* Some linkers require initialized data... */
111 #define CHARS_0s { '\0' }
113 #define TGETENT_0 { 0L, FALSE, NULL, NULL, NULL }
114 #define TGETENT_0s { TGETENT_0, TGETENT_0, TGETENT_0, TGETENT_0 }
116 NCURSES_EXPORT_VAR(NCURSES_GLOBALS) _nc_globals = {
117 0, /* have_sigwinch */
118 0, /* cleanup_nested */
120 FALSE, /* init_signals */
121 FALSE, /* init_screen */
123 NULL, /* comp_sourcename */
124 NULL, /* comp_termtype */
126 FALSE, /* have_tic_directory */
127 FALSE, /* keep_tic_directory */
128 0, /* tic_directory */
133 NULL, /* first_name */
134 NULL, /* keyname_table */
138 NULL, /* safeprint_buf */
139 0, /* safeprint_used */
141 TGETENT_0s, /* tgetent_cache */
142 0, /* tgetent_index */
143 0, /* tgetent_sequence */
145 #ifndef USE_SP_WINDOWLIST
146 0, /* _nc_windowlist */
149 #if USE_HOME_TERMINFO
150 NULL, /* home_terminfo */
153 #if !USE_SAFE_SPRINTF
154 0, /* safeprint_cols */
155 0, /* safeprint_rows */
158 #ifdef USE_TERM_DRIVER
163 FALSE, /* init_trace */
164 CHARS_0s, /* trace_fname */
168 NULL, /* tracearg_buf */
169 0, /* tracearg_used */
171 NULL, /* tracebuf_ptr */
172 0, /* tracebuf_used */
174 CHARS_0s, /* tracechr_buf */
176 NULL, /* tracedmp_buf */
177 0, /* tracedmp_used */
179 NULL, /* tracetry_buf */
180 0, /* tracetry_used */
182 { CHARS_0s, CHARS_0s }, /* traceatr_color_buf */
183 0, /* traceatr_color_sel */
184 -1, /* traceatr_color_last */
185 #if !defined(USE_PTHREADS) && USE_REENTRANT
186 0, /* nested_tracef */
190 PTHREAD_MUTEX_INITIALIZER, /* mutex_curses */
191 PTHREAD_MUTEX_INITIALIZER, /* mutex_tst_tracef */
192 PTHREAD_MUTEX_INITIALIZER, /* mutex_tracef */
193 0, /* nested_tracef */
194 0, /* use_pthreads */
196 #if USE_PTHREADS_EINTR
201 #define STACK_FRAME_0 { { 0 }, 0 }
202 #define STACK_FRAME_0s { STACK_FRAME_0 }
203 #define NUM_VARS_0s { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }
205 #define RIPOFF_0 { 0,0,0 }
206 #define RIPOFF_0s { RIPOFF_0 }
208 NCURSES_EXPORT_VAR(NCURSES_PRESCREEN) _nc_prescreen = {
210 FALSE, /* filter_mode */
211 A_NORMAL, /* previous_attr */
212 #ifndef USE_SP_RIPOFF
213 RIPOFF_0s, /* ripoff */
220 NULL, /* tparam_base */
222 STACK_FRAME_0s, /* stack */
232 NUM_VARS_0s, /* dynamic_var */
233 NUM_VARS_0s, /* static_vars */
235 NULL, /* saved_tty */
236 #if NCURSES_NO_PADDING
237 FALSE, /* flag to set if padding disabled */
240 #if BROKEN_LINKER || USE_REENTRANT
241 NULL, /* real_acs_map */
249 NULL, /* _tputs_trace */
256 * wgetch() and other functions with a WINDOW* parameter may use a SCREEN*
257 * internally, and it is useful to allow those to be invoked without switching
258 * SCREEN's, e.g., for multi-threaded applications.
260 NCURSES_EXPORT(SCREEN *)
261 _nc_screen_of(WINDOW *win)
266 sp = WINDOW_EXT(win, screen);
271 /******************************************************************************/
274 init_global_mutexes(void)
276 static bool initialized = FALSE;
280 _nc_mutex_init(&_nc_globals.mutex_curses);
281 _nc_mutex_init(&_nc_globals.mutex_tst_tracef);
282 _nc_mutex_init(&_nc_globals.mutex_tracef);
287 _nc_init_pthreads(void)
289 if (_nc_use_pthreads)
291 # if USE_WEAK_SYMBOLS
292 if ((pthread_mutex_init) == 0)
294 if ((pthread_mutex_lock) == 0)
296 if ((pthread_mutex_unlock) == 0)
298 if ((pthread_mutex_trylock) == 0)
300 if ((pthread_mutexattr_settype) == 0)
303 _nc_use_pthreads = 1;
304 init_global_mutexes();
308 * Use recursive mutexes if we have them - they're part of Unix98.
309 * For the cases where we do not, _nc_mutex_trylock() is used to avoid a
310 * deadlock, at the expense of memory leaks and unexpected failures that
311 * may not be handled by typical clients.
313 * FIXME - need configure check for PTHREAD_MUTEX_RECURSIVE, define it to
314 * PTHREAD_MUTEX_NORMAL if not supported.
317 _nc_mutex_init(pthread_mutex_t * obj)
319 pthread_mutexattr_t recattr;
321 if (_nc_use_pthreads) {
322 pthread_mutexattr_init(&recattr);
323 pthread_mutexattr_settype(&recattr, PTHREAD_MUTEX_RECURSIVE);
324 pthread_mutex_init(obj, &recattr);
329 _nc_mutex_lock(pthread_mutex_t * obj)
331 if (_nc_use_pthreads == 0)
333 return pthread_mutex_lock(obj);
337 _nc_mutex_trylock(pthread_mutex_t * obj)
339 if (_nc_use_pthreads == 0)
341 return pthread_mutex_trylock(obj);
345 _nc_mutex_unlock(pthread_mutex_t * obj)
347 if (_nc_use_pthreads == 0)
349 return pthread_mutex_unlock(obj);
351 #endif /* USE_PTHREADS */
353 #if defined(USE_PTHREADS) || USE_PTHREADS_EINTR
356 * NB: sigprocmask(2) is global but pthread_sigmask(3p)
357 * only for the calling thread.
360 _nc_sigprocmask(int how, const sigset_t * newmask, sigset_t * oldmask)
362 if ((pthread_sigmask))
363 return pthread_sigmask(how, newmask, oldmask);
365 return sigprocmask(how, newmask, oldmask);
368 #endif /* USE_PTHREADS */