1/****************************************************************************
2 * Copyright (c) 2008-2010,2012 Free Software Foundation, Inc.              *
3 *                                                                          *
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: Juergen Pfeifer                                                 *
31 *                                                                          *
32 ****************************************************************************/
33
34#include <curses.priv.h>
35
36MODULE_ID("$Id: lib_driver.c,v 1.4 2012/09/22 19:32:46 tom Exp $")
37
38typedef struct DriverEntry {
39    const char *name;
40    TERM_DRIVER *driver;
41} DRIVER_ENTRY;
42
43static DRIVER_ENTRY DriverTable[] =
44{
45#ifdef __MINGW32__
46    {"win32con", &_nc_WIN_DRIVER},
47#endif
48    {"tinfo", &_nc_TINFO_DRIVER}	/* must be last */
49};
50
51NCURSES_EXPORT(int)
52_nc_get_driver(TERMINAL_CONTROL_BLOCK * TCB, const char *name, int *errret)
53{
54    int code = ERR;
55    size_t i;
56    TERM_DRIVER *res = (TERM_DRIVER *) 0;
57    TERM_DRIVER *use = 0;
58
59    T((T_CALLED("_nc_get_driver(%p, %s, %p)"),
60       (void *) TCB, NonNull(name), (void *) errret));
61
62    assert(TCB != 0);
63
64    for (i = 0; i < SIZEOF(DriverTable); i++) {
65	res = DriverTable[i].driver;
66	/*
67	 * Use "#" (a character which cannot begin a terminal's name) to
68	 * select specific driver from the table.
69	 *
70	 * In principle, we could have more than one non-terminfo driver,
71	 * e.g., "win32gui".
72	 */
73	if (name != 0 && *name == '#') {
74	    size_t n = strlen(name + 1);
75	    if (n != 0
76		&& strncmp(name + 1, DriverTable[i].name, n)) {
77		continue;
78	    }
79	}
80	if (res->CanHandle(TCB, name, errret)) {
81	    use = res;
82	    break;
83	}
84    }
85    if (use != 0) {
86	TCB->drv = use;
87	code = OK;
88    }
89    returnCode(code);
90}
91
92NCURSES_EXPORT(int)
93NCURSES_SP_NAME(has_key) (SCREEN *sp, int keycode)
94{
95    T((T_CALLED("has_key(%p, %d)"), (void *) sp, keycode));
96    returnCode(IsValidTIScreen(sp) ? CallDriver_1(sp, kyExist, keycode) : FALSE);
97}
98
99NCURSES_EXPORT(int)
100has_key(int keycode)
101{
102    return NCURSES_SP_NAME(has_key) (CURRENT_SCREEN, keycode);
103}
104
105NCURSES_EXPORT(int)
106NCURSES_SP_NAME(_nc_mcprint) (SCREEN *sp, char *data, int len)
107{
108    int code = ERR;
109
110    if (0 != TerminalOf(sp))
111	code = CallDriver_2(sp, print, data, len);
112    return (code);
113}
114
115NCURSES_EXPORT(int)
116mcprint(char *data, int len)
117{
118    return NCURSES_SP_NAME(_nc_mcprint) (CURRENT_SCREEN, data, len);
119}
120
121NCURSES_EXPORT(int)
122NCURSES_SP_NAME(doupdate) (SCREEN *sp)
123{
124    int code = ERR;
125
126    T((T_CALLED("doupdate(%p)"), (void *) sp));
127
128    if (IsValidScreen(sp))
129	code = CallDriver(sp, update);
130
131    returnCode(code);
132}
133
134NCURSES_EXPORT(int)
135doupdate(void)
136{
137    return NCURSES_SP_NAME(doupdate) (CURRENT_SCREEN);
138}
139
140NCURSES_EXPORT(int)
141NCURSES_SP_NAME(mvcur) (SCREEN *sp, int yold, int xold, int ynew, int xnew)
142{
143    int code = ERR;
144    TR(TRACE_CALLS | TRACE_MOVE, (T_CALLED("mvcur(%p,%d,%d,%d,%d)"),
145				  (void *) sp, yold, xold, ynew, xnew));
146    if (HasTerminal(sp)) {
147	code = CallDriver_4(sp, hwcur, yold, xold, ynew, xnew);
148    }
149    returnCode(code);
150}
151
152NCURSES_EXPORT(int)
153mvcur(int yold, int xold, int ynew, int xnew)
154/* optimized cursor move from (yold, xold) to (ynew, xnew) */
155{
156    return NCURSES_SP_NAME(mvcur) (CURRENT_SCREEN, yold, xold, ynew, xnew);
157}
158