forked from icculus/mojosetup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.c
258 lines (213 loc) · 6.28 KB
/
gui.c
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
* MojoSetup; a portable, flexible installation application.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
#include "gui.h"
#include "platform.h"
#include "fileio.h"
typedef struct S_PLUGINLIST
{
void *lib;
const MojoGui *gui;
MojoGuiPluginPriority priority;
uint32 imglen;
uint8 *img;
struct S_PLUGINLIST *next;
} PluginList;
const MojoGui *GGui = NULL;
static PluginList *pluginDetails = NULL;
typedef struct StaticGuiPlugin
{
const char* name;
const MojoGuiEntryPoint entry;
} StaticGuiPlugin;
#define STATIC_GUI_PLUGIN(name) {#name, MojoGuiPlugin_##name}
static StaticGuiPlugin staticGui[] =
{
#if GUI_STATIC_LINK_STDIO
STATIC_GUI_PLUGIN(stdio),
#endif
#if GUI_STATIC_LINK_COCOA
STATIC_GUI_PLUGIN(cocoa),
#endif
#if GUI_STATIC_LINK_GTKPLUS3
STATIC_GUI_PLUGIN(gtkplus3),
#endif
#if GUI_STATIC_LINK_GTKPLUS2
STATIC_GUI_PLUGIN(gtkplus2),
#endif
#if GUI_STATIC_LINK_WWW
STATIC_GUI_PLUGIN(www),
#endif
#if GUI_STATIC_LINK_NCURSES
STATIC_GUI_PLUGIN(ncurses),
#endif
{NULL, NULL}
};
#undef STATIC_GUI_PLUGIN
static MojoGuiPluginPriority calcGuiPriority(const MojoGui *gui)
{
MojoGuiPluginPriority retval;
retval = gui->priority(MojoPlatform_istty());
// If the plugin isn't saying "don't try me at all" then see if the
// user explicitly wants this one.
if (retval != MOJOGUI_PRIORITY_NEVER_TRY)
{
static const char *envr = NULL;
if (envr == NULL)
envr = cmdlinestr("ui", "MOJOSETUP_UI", NULL);
if ((envr != NULL) && (strcasecmp(envr, gui->name()) == 0))
retval = MOJOGUI_PRIORITY_USER_REQUESTED;
} // if
return retval;
} // calcGuiPriority
const MojoGui *MojoSetup_loadGuiPlugin(const uint8 *img, uint32 len, void **lib)
{
*lib = MojoPlatform_dlopen(img, len);
if (*lib != NULL)
{
void *addr = MojoPlatform_dlsym(*lib, MOJOGUI_ENTRY_POINT_STR);
MojoGuiEntryPoint entry = (MojoGuiEntryPoint) addr;
if (entry != NULL)
return entry(MOJOGUI_INTERFACE_REVISION, &GEntryPoints);
} // if
return NULL;
} // MojoSetup_loadGuiPlugin
static PluginList *initGuiPluginsByPriority(PluginList *plugins)
{
MojoGuiPluginPriority p;
for (p = MOJOGUI_PRIORITY_USER_REQUESTED; p < MOJOGUI_PRIORITY_TOTAL; p++)
{
PluginList *i;
for (i = plugins->next; i != NULL; i = i->next)
{
if (i->priority != p)
continue;
if (i->img != NULL)
i->gui = MojoSetup_loadGuiPlugin(i->img, i->imglen, &i->lib);
if (i->gui && i->gui->init())
{
logInfo("Selected '%0' UI.", i->gui->name());
return i;
} // if
if (i->lib)
{
MojoPlatform_dlclose(i->lib);
i->lib = NULL;
i->gui = NULL;
} // if
} // for
} // for
return NULL;
} // initGuiPluginsByPriority
static void deleteGuiPlugin(PluginList *plugin)
{
if (plugin != NULL)
{
if (plugin->gui)
plugin->gui->deinit();
if (plugin->lib)
MojoPlatform_dlclose(plugin->lib);
free(plugin->img);
free(plugin);
} // if
} // deleteGuiPlugin
static PluginList *addGuiPlugin(PluginList *plugins, const MojoGui *gui, MojoGuiPluginPriority priority)
{
PluginList *plug = xmalloc(sizeof (PluginList));
plug->lib = NULL;
plug->gui = gui;
plug->priority = priority;
plug->next = plugins->next;
plugins->next = plug;
return plug;
} // addGuiPlugin
static void loadStaticGuiPlugins(PluginList *plugins)
{
int i;
for (i = 0; staticGui[i].entry != NULL; i++)
{
const MojoGui *gui;
logInfo("Testing static GUI plugin %0", staticGui[i].name);
gui = staticGui[i].entry(MOJOGUI_INTERFACE_REVISION, &GEntryPoints);
if (gui != NULL)
addGuiPlugin(plugins, gui, calcGuiPriority(gui));
}
} // loadStaticGuiPlugins
static boolean loadDynamicGuiPlugin(PluginList *plugins, MojoArchive *ar)
{
PluginList *plug = NULL;
MojoInput *io = ar->openCurrentEntry(ar);
if (io != NULL)
{
const uint32 imglen = (uint32) io->length(io);
uint8 *img = (uint8 *) xmalloc(imglen);
const uint32 br = (uint32) io->read(io, img, imglen);
io->close(io);
if (br == imglen)
{
MojoGuiPluginPriority priority;
priority = MojoPlatform_getGuiPriority(img, imglen);
if (priority != MOJOGUI_PRIORITY_NEVER_TRY)
{
plug = addGuiPlugin(plugins, NULL, priority);
plug->img = img;
plug->imglen = imglen;
}
} // if
} // if
return plug != NULL;
} // loadDynamicGuiPlugin
static void loadDynamicGuiPlugins(PluginList *plugins)
{
if (GBaseArchive->enumerate(GBaseArchive))
{
const MojoArchiveEntry *entinfo;
while ((entinfo = GBaseArchive->enumNext(GBaseArchive)) != NULL)
{
if (entinfo->type != MOJOARCHIVE_ENTRY_FILE)
continue;
else if (strncmp(entinfo->filename, "guis/", 5) != 0)
continue;
logInfo("Testing dynamic GUI plugin %0", entinfo->filename);
loadDynamicGuiPlugin(plugins, GBaseArchive);
} // while
} // if
} // loadDynamicGuiPlugins
const MojoGui *MojoGui_initGuiPlugin(void)
{
PluginList plugins;
PluginList *i = NULL;
if (pluginDetails != NULL)
return pluginDetails->gui;
memset(&plugins, '\0', sizeof (plugins));
assert(GGui == NULL);
loadDynamicGuiPlugins(&plugins);
loadStaticGuiPlugins(&plugins);
pluginDetails = initGuiPluginsByPriority(&plugins);
// cleanout unused plugins...
i = plugins.next;
while (i != NULL)
{
PluginList *next = i->next;
if (i != pluginDetails)
deleteGuiPlugin(i);
i = next;
} // while
if (pluginDetails != NULL)
{
GGui = pluginDetails->gui;
pluginDetails->next = NULL;
} // if
return GGui;
} // MojoGui_findGuiPlugin
void MojoGui_deinitGuiPlugin(void)
{
GGui = NULL;
deleteGuiPlugin(pluginDetails);
pluginDetails = NULL;
} // MojoGui_deinitGuiPlugin
// end of gui.c ...