1 /****************************************************************************
2 * Copyright 2018,2020 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996-on *
34 * and: Juergen Pfeifer *
35 ****************************************************************************/
40 ** Common data that may/may not be allocated, but is referenced globally
44 #include <curses.priv.h>
46 MODULE_ID("$Id: lib_data.c,v 1.81 2020/06/13 22:01:14 tom Exp $")
49 * OS/2's native linker complains if we don't initialize public data when
50 * constructing a dll (reported by J.J.G.Ripoll).
53 NCURSES_EXPORT(WINDOW *)
54 NCURSES_PUBLIC_VAR(stdscr) (void)
56 return CURRENT_SCREEN ? StdScreen(CURRENT_SCREEN) : 0;
58 NCURSES_EXPORT(WINDOW *)
59 NCURSES_PUBLIC_VAR(curscr) (void)
61 return CURRENT_SCREEN ? CurScreen(CURRENT_SCREEN) : 0;
63 NCURSES_EXPORT(WINDOW *)
64 NCURSES_PUBLIC_VAR(newscr) (void)
66 return CURRENT_SCREEN ? NewScreen(CURRENT_SCREEN) : 0;
69 NCURSES_EXPORT_VAR(WINDOW *) stdscr = 0;
70 NCURSES_EXPORT_VAR(WINDOW *) curscr = 0;
71 NCURSES_EXPORT_VAR(WINDOW *) newscr = 0;
74 NCURSES_EXPORT_VAR(SCREEN *) _nc_screen_chain = 0;
77 * The variable 'SP' will be defined as a function on systems that cannot link
78 * data-only modules, since it is used in a lot of places within ncurses and we
79 * cannot guarantee that any application will use any particular function. We
80 * put the WINDOW variables in this module, because it appears that any
81 * application that uses them will also use 'SP'.
83 * This module intentionally does not reference other ncurses modules, to avoid
84 * module coupling that increases the size of the executable.
87 static SCREEN *my_screen;
89 NCURSES_EXPORT(SCREEN *)
96 _nc_alloc_screen(void)
98 my_screen = _nc_alloc_screen_sp();
99 T(("_nc_alloc_screen_sp %p", my_screen));
100 return (my_screen != 0);
104 _nc_set_screen(SCREEN *sp)
111 NCURSES_EXPORT_VAR(SCREEN *) SP = NULL; /* Some linkers require initialized data... */
114 #define CHARS_0s { '\0' }
116 #define TGETENT_0 { 0L, FALSE, NULL, NULL, NULL }
117 #define TGETENT_0s { TGETENT_0, TGETENT_0, TGETENT_0, TGETENT_0 }
119 NCURSES_EXPORT_VAR(NCURSES_GLOBALS) _nc_globals = {
120 0, /* have_sigtstp */
121 0, /* have_sigwinch */
122 0, /* cleanup_nested */
124 FALSE, /* init_signals */
125 FALSE, /* init_screen */
127 NULL, /* comp_sourcename */
128 NULL, /* comp_termtype */
130 FALSE, /* have_tic_directory */
131 FALSE, /* keep_tic_directory */
132 0, /* tic_directory */
137 NULL, /* first_name */
138 NULL, /* keyname_table */
139 0, /* init_keyname */
143 2048, /* getstr_limit */
145 NULL, /* safeprint_buf */
146 0, /* safeprint_used */
148 TGETENT_0s, /* tgetent_cache */
149 0, /* tgetent_index */
150 0, /* tgetent_sequence */
156 { { 0, 0 } }, /* dbd_vars */
159 NULL, /* cached_tparm */
161 #endif /* HAVE_TSEARCH */
163 #ifdef USE_TERM_DRIVER
167 #ifndef USE_SP_WINDOWLIST
168 0, /* _nc_windowlist */
171 #if USE_HOME_TERMINFO
172 NULL, /* home_terminfo */
175 #if !USE_SAFE_SPRINTF
176 0, /* safeprint_cols */
177 0, /* safeprint_rows */
181 PTHREAD_MUTEX_INITIALIZER, /* mutex_curses */
182 PTHREAD_MUTEX_INITIALIZER, /* mutex_prescreen */
183 PTHREAD_MUTEX_INITIALIZER, /* mutex_screen */
184 PTHREAD_MUTEX_INITIALIZER, /* mutex_update */
185 PTHREAD_MUTEX_INITIALIZER, /* mutex_tst_tracef */
186 PTHREAD_MUTEX_INITIALIZER, /* mutex_tracef */
187 0, /* nested_tracef */
188 0, /* use_pthreads */
189 #if USE_PTHREADS_EINTR
193 #if USE_WIDEC_SUPPORT
194 CHARS_0s, /* key_name */
197 FALSE, /* trace_opened */
198 CHARS_0s, /* trace_fname */
203 NULL, /* tracearg_buf */
204 0, /* tracearg_used */
206 NULL, /* tracebuf_ptr */
207 0, /* tracebuf_used */
209 CHARS_0s, /* tracechr_buf */
211 NULL, /* tracedmp_buf */
212 0, /* tracedmp_used */
214 NULL, /* tracetry_buf */
215 0, /* tracetry_used */
217 { CHARS_0s, CHARS_0s }, /* traceatr_color_buf */
218 0, /* traceatr_color_sel */
219 -1, /* traceatr_color_last */
220 #if !defined(USE_PTHREADS) && USE_REENTRANT
221 0, /* nested_tracef */
225 FALSE, /* leak_checking */
229 #define STACK_FRAME_0 { { 0 }, 0 }
230 #define STACK_FRAME_0s { STACK_FRAME_0 }
231 #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 }
233 #define RIPOFF_0 { 0,0,0 }
234 #define RIPOFF_0s { RIPOFF_0 }
236 NCURSES_EXPORT_VAR(NCURSES_PRESCREEN) _nc_prescreen = {
237 NULL, /* allocated */
239 FALSE, /* filter_mode */
240 A_NORMAL, /* previous_attr */
242 NULL, /* tparam_base */
244 STACK_FRAME_0s, /* stack */
254 NUM_VARS_0s, /* dynamic_var */
255 NUM_VARS_0s, /* static_vars */
260 NULL, /* saved_tty */
261 FALSE, /* use_tioctl */
263 #ifndef USE_SP_RIPOFF
264 RIPOFF_0s, /* ripoff */
267 #if NCURSES_NO_PADDING
268 FALSE, /* flag to set if padding disabled */
270 #if BROKEN_LINKER || USE_REENTRANT
271 NULL, /* real_acs_map */
279 #if BROKEN_LINKER || USE_REENTRANT
281 NULL, /* _tputs_trace */
288 * wgetch() and other functions with a WINDOW* parameter may use a SCREEN*
289 * internally, and it is useful to allow those to be invoked without switching
290 * SCREEN's, e.g., for multi-threaded applications.
292 NCURSES_EXPORT(SCREEN *)
293 _nc_screen_of(WINDOW *win)
298 sp = WINDOW_EXT(win, screen);
303 /******************************************************************************/
306 init_global_mutexes(void)
308 static bool initialized = FALSE;
312 _nc_mutex_init(&_nc_globals.mutex_curses);
313 _nc_mutex_init(&_nc_globals.mutex_prescreen);
314 _nc_mutex_init(&_nc_globals.mutex_screen);
315 _nc_mutex_init(&_nc_globals.mutex_update);
316 _nc_mutex_init(&_nc_globals.mutex_tst_tracef);
317 _nc_mutex_init(&_nc_globals.mutex_tracef);
322 _nc_init_pthreads(void)
324 if (_nc_use_pthreads)
326 # if USE_WEAK_SYMBOLS
327 if ((pthread_mutex_init) == 0)
329 if ((pthread_mutex_lock) == 0)
331 if ((pthread_mutex_unlock) == 0)
333 if ((pthread_mutex_trylock) == 0)
335 if ((pthread_mutexattr_settype) == 0)
338 _nc_use_pthreads = 1;
339 init_global_mutexes();
343 * Use recursive mutexes if we have them - they're part of Unix98.
344 * For the cases where we do not, _nc_mutex_trylock() is used to avoid a
345 * deadlock, at the expense of memory leaks and unexpected failures that
346 * may not be handled by typical clients.
348 * FIXME - need configure check for PTHREAD_MUTEX_RECURSIVE, define it to
349 * PTHREAD_MUTEX_NORMAL if not supported.
352 _nc_mutex_init(pthread_mutex_t * obj)
354 pthread_mutexattr_t recattr;
356 if (_nc_use_pthreads) {
357 pthread_mutexattr_init(&recattr);
358 pthread_mutexattr_settype(&recattr, PTHREAD_MUTEX_RECURSIVE);
359 pthread_mutex_init(obj, &recattr);
364 _nc_mutex_lock(pthread_mutex_t * obj)
366 if (_nc_use_pthreads == 0)
368 return pthread_mutex_lock(obj);
372 _nc_mutex_trylock(pthread_mutex_t * obj)
374 if (_nc_use_pthreads == 0)
376 return pthread_mutex_trylock(obj);
380 _nc_mutex_unlock(pthread_mutex_t * obj)
382 if (_nc_use_pthreads == 0)
384 return pthread_mutex_unlock(obj);
386 #endif /* USE_PTHREADS */
388 #if defined(USE_PTHREADS) || USE_PTHREADS_EINTR
391 * NB: sigprocmask(2) is global but pthread_sigmask(3p)
392 * only for the calling thread.
395 _nc_sigprocmask(int how, const sigset_t * newmask, sigset_t * oldmask)
397 if ((pthread_sigmask))
398 return pthread_sigmask(how, newmask, oldmask);
400 return (sigprocmask) (how, newmask, oldmask);
403 #endif /* USE_PTHREADS */