-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathnc_wrapper.c
More file actions
168 lines (124 loc) · 3.45 KB
/
Copy pathnc_wrapper.c
File metadata and controls
168 lines (124 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <stdlib.h>
#include <dlfcn.h>
#ifdef __linux__
#include <ncursesw/curses.h>
#endif
#ifdef __FreeBSD__
#include <ncurses/ncurses.h>
#endif
#if defined(__APPLE__) && defined(__MACH__)
#include <ncursesw/ncurses.h>
#endif
#include "nc_wrapper.h"
#include "macros.h"
struct _nc_wrapper_s
{
void *dh;
int (*init_pair) (int, int, int);
int (*init_color) (int, int, int, int);
int (*pair_content) (int, int *, int *);
int (*color_content) (int, int *, int *, int *);
};
typedef struct _nc_wrapper_s nc_wrapper_t;
nc_wrapper_t* ncw_get_handle(void);
nc_wrapper_t *
ncw_get_handle(void)
{
static nc_wrapper_t *ncw = NULL;
char *lib_path[] =
{
"libncursesw.so.6.1",
"libncursesw.so.6",
"libncursesw.so.5",
};
int array_sz = 0;
int i;
if(ncw != NULL) return ncw;
ncw = CALLOC_PTR(ncw);
array_sz = ARRAY_SZ(lib_path);
for(i = 0; i < array_sz; i++)
{
ncw->dh = dlopen(lib_path[i], RTLD_NOW);
if(ncw->dh != NULL) break;
}
// unable to load ncurses extensions
if(ncw->dh == NULL) return ncw;
// clear any vestigial errors
dlerror();
*(void **)(&ncw->init_pair) = dlsym(ncw->dh, "init_extended_pair");
// ncw->init_pair = dlsym(ncw->dh, "init_extended_pair");
if(dlerror() != NULL) ncw->init_pair = NULL;
*(void **)(&ncw->init_color) = dlsym(ncw->dh, "init_extended_color");
if(dlerror() != NULL) ncw->init_color = NULL;
*(void **)(&ncw->pair_content) = dlsym(ncw->dh, "extended_pair_content");
if(dlerror() != NULL) ncw->pair_content = NULL;
*(void **)(&ncw->color_content) = dlsym(ncw->dh, "extended_color_content");
if(dlerror() != NULL) ncw->color_content = NULL;
return ncw;
}
int
ncw_init_pair(int pair, int f, int b)
{
nc_wrapper_t *ncw;
int retval;
ncw = ncw_get_handle();
if(ncw->init_pair == NULL)
{
retval = init_pair(pair, (short)f, (short)b);
return retval;
}
retval = ncw->init_pair(pair, f, b);
return retval;
}
int
ncw_init_color(int color, int r, int g, int b)
{
nc_wrapper_t *ncw;
int retval;
ncw = ncw_get_handle();
if(ncw->init_color == NULL)
{
retval = init_color(color, r, g, b);
return retval;
}
retval = ncw->init_color(color, r, g, b);
return retval;
}
int
ncw_pair_content(int pair, int *f, int *b)
{
nc_wrapper_t *ncw;
int retval;
ncw = ncw_get_handle();
#ifndef __FreeBSD__
if(ncw->pair_content == NULL)
#endif
{
retval = pair_content((short)pair, (short *)f, (short *)b);
return retval;
}
retval = ncw->pair_content(pair, f, b);
return retval;
}
int
ncw_color_content(int color, int *r, int *g, int *b)
{
nc_wrapper_t *ncw;
short sred;
short sgreen;
short sblue;
int retval;
ncw = ncw_get_handle();
#ifndef __FreeBSD__
if(ncw->color_content == NULL)
#endif
{
retval = color_content((short)color, &sred, &sgreen, &sblue);
*r = sred;
*g = sgreen;
*b = sblue;
return retval;
}
retval = ncw->color_content(color, r, g, b);
return retval;
}