xref: /openbsd-src/lib/libcurses/base/lib_driver.c (revision c7ef0cfc17afcba97172c25e1e3a943e893bc632)
1*c7ef0cfcSnicm /****************************************************************************
2*c7ef0cfcSnicm  * Copyright 2018,2020 Thomas E. Dickey                                     *
3*c7ef0cfcSnicm  * Copyright 2009-2012,2014 Free Software Foundation, Inc.                  *
4*c7ef0cfcSnicm  *                                                                          *
5*c7ef0cfcSnicm  * Permission is hereby granted, free of charge, to any person obtaining a  *
6*c7ef0cfcSnicm  * copy of this software and associated documentation files (the            *
7*c7ef0cfcSnicm  * "Software"), to deal in the Software without restriction, including      *
8*c7ef0cfcSnicm  * without limitation the rights to use, copy, modify, merge, publish,      *
9*c7ef0cfcSnicm  * distribute, distribute with modifications, sublicense, and/or sell       *
10*c7ef0cfcSnicm  * copies of the Software, and to permit persons to whom the Software is    *
11*c7ef0cfcSnicm  * furnished to do so, subject to the following conditions:                 *
12*c7ef0cfcSnicm  *                                                                          *
13*c7ef0cfcSnicm  * The above copyright notice and this permission notice shall be included  *
14*c7ef0cfcSnicm  * in all copies or substantial portions of the Software.                   *
15*c7ef0cfcSnicm  *                                                                          *
16*c7ef0cfcSnicm  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17*c7ef0cfcSnicm  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18*c7ef0cfcSnicm  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19*c7ef0cfcSnicm  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20*c7ef0cfcSnicm  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21*c7ef0cfcSnicm  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22*c7ef0cfcSnicm  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23*c7ef0cfcSnicm  *                                                                          *
24*c7ef0cfcSnicm  * Except as contained in this notice, the name(s) of the above copyright   *
25*c7ef0cfcSnicm  * holders shall not be used in advertising or otherwise to promote the     *
26*c7ef0cfcSnicm  * sale, use or other dealings in this Software without prior written       *
27*c7ef0cfcSnicm  * authorization.                                                           *
28*c7ef0cfcSnicm  ****************************************************************************/
29*c7ef0cfcSnicm 
30*c7ef0cfcSnicm /****************************************************************************
31*c7ef0cfcSnicm  *  Author: Juergen Pfeifer                                                 *
32*c7ef0cfcSnicm  *                                                                          *
33*c7ef0cfcSnicm  ****************************************************************************/
34*c7ef0cfcSnicm 
35*c7ef0cfcSnicm #include <curses.priv.h>
36*c7ef0cfcSnicm 
37*c7ef0cfcSnicm MODULE_ID("$Id: lib_driver.c,v 1.1 2023/10/17 09:52:08 nicm Exp $")
38*c7ef0cfcSnicm 
39*c7ef0cfcSnicm #ifndef EXP_WIN32_DRIVER
40*c7ef0cfcSnicm typedef struct DriverEntry {
41*c7ef0cfcSnicm     const char *name;
42*c7ef0cfcSnicm     TERM_DRIVER *driver;
43*c7ef0cfcSnicm } DRIVER_ENTRY;
44*c7ef0cfcSnicm 
45*c7ef0cfcSnicm static DRIVER_ENTRY DriverTable[] =
46*c7ef0cfcSnicm {
47*c7ef0cfcSnicm #ifdef _WIN32
48*c7ef0cfcSnicm     {"win32console", &_nc_WIN_DRIVER},
49*c7ef0cfcSnicm #endif
50*c7ef0cfcSnicm     {"tinfo", &_nc_TINFO_DRIVER}	/* must be last */
51*c7ef0cfcSnicm };
52*c7ef0cfcSnicm 
53*c7ef0cfcSnicm NCURSES_EXPORT(int)
_nc_get_driver(TERMINAL_CONTROL_BLOCK * TCB,const char * name,int * errret)54*c7ef0cfcSnicm _nc_get_driver(TERMINAL_CONTROL_BLOCK * TCB, const char *name, int *errret)
55*c7ef0cfcSnicm {
56*c7ef0cfcSnicm     int code = ERR;
57*c7ef0cfcSnicm     size_t i;
58*c7ef0cfcSnicm     TERM_DRIVER *res = (TERM_DRIVER *) 0;
59*c7ef0cfcSnicm     TERM_DRIVER *use = 0;
60*c7ef0cfcSnicm 
61*c7ef0cfcSnicm     T((T_CALLED("_nc_get_driver(%p, %s, %p)"),
62*c7ef0cfcSnicm        (void *) TCB, NonNull(name), (void *) errret));
63*c7ef0cfcSnicm 
64*c7ef0cfcSnicm     assert(TCB != 0);
65*c7ef0cfcSnicm 
66*c7ef0cfcSnicm     for (i = 0; i < SIZEOF(DriverTable); i++) {
67*c7ef0cfcSnicm 	res = DriverTable[i].driver;
68*c7ef0cfcSnicm 	if (strcmp(DriverTable[i].name, res->td_name(TCB)) == 0) {
69*c7ef0cfcSnicm 	    if (res->td_CanHandle(TCB, name, errret)) {
70*c7ef0cfcSnicm 		use = res;
71*c7ef0cfcSnicm 		break;
72*c7ef0cfcSnicm 	    }
73*c7ef0cfcSnicm 	}
74*c7ef0cfcSnicm     }
75*c7ef0cfcSnicm     if (use != 0) {
76*c7ef0cfcSnicm 	TCB->drv = use;
77*c7ef0cfcSnicm 	code = OK;
78*c7ef0cfcSnicm     }
79*c7ef0cfcSnicm     returnCode(code);
80*c7ef0cfcSnicm }
81*c7ef0cfcSnicm #endif /* !EXP_WIN32_DRIVER */
82*c7ef0cfcSnicm 
83*c7ef0cfcSnicm NCURSES_EXPORT(int)
NCURSES_SP_NAME(has_key)84*c7ef0cfcSnicm NCURSES_SP_NAME(has_key) (SCREEN *sp, int keycode)
85*c7ef0cfcSnicm {
86*c7ef0cfcSnicm     T((T_CALLED("has_key(%p, %d)"), (void *) sp, keycode));
87*c7ef0cfcSnicm     returnCode(IsValidTIScreen(sp) ? CallDriver_1(sp, td_kyExist, keycode) : FALSE);
88*c7ef0cfcSnicm }
89*c7ef0cfcSnicm 
90*c7ef0cfcSnicm NCURSES_EXPORT(int)
has_key(int keycode)91*c7ef0cfcSnicm has_key(int keycode)
92*c7ef0cfcSnicm {
93*c7ef0cfcSnicm     return NCURSES_SP_NAME(has_key) (CURRENT_SCREEN, keycode);
94*c7ef0cfcSnicm }
95*c7ef0cfcSnicm 
96*c7ef0cfcSnicm NCURSES_EXPORT(int)
NCURSES_SP_NAME(_nc_mcprint)97*c7ef0cfcSnicm NCURSES_SP_NAME(_nc_mcprint) (SCREEN *sp, char *data, int len)
98*c7ef0cfcSnicm {
99*c7ef0cfcSnicm     int code = ERR;
100*c7ef0cfcSnicm 
101*c7ef0cfcSnicm     if (0 != TerminalOf(sp))
102*c7ef0cfcSnicm 	code = CallDriver_2(sp, td_print, data, len);
103*c7ef0cfcSnicm     return (code);
104*c7ef0cfcSnicm }
105*c7ef0cfcSnicm 
106*c7ef0cfcSnicm NCURSES_EXPORT(int)
mcprint(char * data,int len)107*c7ef0cfcSnicm mcprint(char *data, int len)
108*c7ef0cfcSnicm {
109*c7ef0cfcSnicm     return NCURSES_SP_NAME(_nc_mcprint) (CURRENT_SCREEN, data, len);
110*c7ef0cfcSnicm }
111*c7ef0cfcSnicm 
112*c7ef0cfcSnicm NCURSES_EXPORT(int)
NCURSES_SP_NAME(doupdate)113*c7ef0cfcSnicm NCURSES_SP_NAME(doupdate) (SCREEN *sp)
114*c7ef0cfcSnicm {
115*c7ef0cfcSnicm     int code = ERR;
116*c7ef0cfcSnicm 
117*c7ef0cfcSnicm     T((T_CALLED("doupdate(%p)"), (void *) sp));
118*c7ef0cfcSnicm 
119*c7ef0cfcSnicm     if (IsValidScreen(sp))
120*c7ef0cfcSnicm 	code = CallDriver(sp, td_update);
121*c7ef0cfcSnicm 
122*c7ef0cfcSnicm     returnCode(code);
123*c7ef0cfcSnicm }
124*c7ef0cfcSnicm 
125*c7ef0cfcSnicm NCURSES_EXPORT(int)
doupdate(void)126*c7ef0cfcSnicm doupdate(void)
127*c7ef0cfcSnicm {
128*c7ef0cfcSnicm     return NCURSES_SP_NAME(doupdate) (CURRENT_SCREEN);
129*c7ef0cfcSnicm }
130*c7ef0cfcSnicm 
131*c7ef0cfcSnicm NCURSES_EXPORT(int)
NCURSES_SP_NAME(mvcur)132*c7ef0cfcSnicm NCURSES_SP_NAME(mvcur) (SCREEN *sp, int yold, int xold, int ynew, int xnew)
133*c7ef0cfcSnicm {
134*c7ef0cfcSnicm     int code = ERR;
135*c7ef0cfcSnicm     TR(TRACE_CALLS | TRACE_MOVE, (T_CALLED("mvcur(%p,%d,%d,%d,%d)"),
136*c7ef0cfcSnicm 				  (void *) sp, yold, xold, ynew, xnew));
137*c7ef0cfcSnicm     if (HasTerminal(sp)) {
138*c7ef0cfcSnicm 	code = CallDriver_4(sp, td_hwcur, yold, xold, ynew, xnew);
139*c7ef0cfcSnicm     }
140*c7ef0cfcSnicm     returnCode(code);
141*c7ef0cfcSnicm }
142*c7ef0cfcSnicm 
143*c7ef0cfcSnicm NCURSES_EXPORT(int)
mvcur(int yold,int xold,int ynew,int xnew)144*c7ef0cfcSnicm mvcur(int yold, int xold, int ynew, int xnew)
145*c7ef0cfcSnicm /* optimized cursor move from (yold, xold) to (ynew, xnew) */
146*c7ef0cfcSnicm {
147*c7ef0cfcSnicm     return NCURSES_SP_NAME(mvcur) (CURRENT_SCREEN, yold, xold, ynew, xnew);
148*c7ef0cfcSnicm }
149