-
Notifications
You must be signed in to change notification settings - Fork 1
/
gui.c
235 lines (195 loc) · 5.53 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
/**
* 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;
struct S_PLUGINLIST *next;
} PluginList;
const MojoGui *GGui = NULL;
static PluginList *pluginDetails = NULL;
static const MojoGuiEntryPoint staticGui[] =
{
#if GUI_STATIC_LINK_STDIO
MojoGuiPlugin_stdio,
#endif
#if GUI_STATIC_LINK_COCOA
MojoGuiPlugin_cocoa,
#endif
#if GUI_STATIC_LINK_GTKPLUS2
MojoGuiPlugin_gtkplus2,
#endif
#if GUI_STATIC_LINK_WWW
MojoGuiPlugin_www,
#endif
#if GUI_STATIC_LINK_NCURSES
MojoGuiPlugin_ncurses,
#endif
NULL
};
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
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) && (i->gui->init()) )
{
logInfo("Selected '%0' UI.", i->gui->name());
return i;
} // 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);
} // if
} // deleteGuiPlugin
static boolean tryGuiPlugin(PluginList *plugins, MojoGuiEntryPoint entry)
{
boolean retval = false;
const MojoGui *gui = entry(MOJOGUI_INTERFACE_REVISION, &GEntryPoints);
if (gui != NULL)
{
PluginList *plug = xmalloc(sizeof (PluginList));
plug->lib = NULL;
plug->gui = gui;
plug->priority = calcGuiPriority(gui);
plug->next = plugins->next;
plugins->next = plug;
retval = true;
} // if
return retval;
} // tryGuiPlugin
static void loadStaticGuiPlugins(PluginList *plugins)
{
int i;
for (i = 0; staticGui[i] != NULL; i++)
tryGuiPlugin(plugins, staticGui[i]);
} // loadStaticGuiPlugins
static boolean loadDynamicGuiPlugin(PluginList *plugins, MojoArchive *ar)
{
boolean rc = false;
void *lib = 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)
lib = MojoPlatform_dlopen(img, imglen);
free(img);
} // if
if (lib != NULL)
{
void *addr = MojoPlatform_dlsym(lib, MOJOGUI_ENTRY_POINT_STR);
MojoGuiEntryPoint entry = (MojoGuiEntryPoint) addr;
if (entry != NULL)
{
if ((rc = tryGuiPlugin(plugins, entry)) == false)
MojoPlatform_dlclose(lib);
} // if
} // if
return rc;
} // loadDynamicGuiPlugin
static void loadDynamicGuiPlugins(MojoArchive* archive, PluginList *plugins)
{
if (archive->enumerate(archive))
{
const MojoArchiveEntry *entinfo;
while ((entinfo = archive->enumNext(archive)) != NULL)
{
if (entinfo->type != MOJOARCHIVE_ENTRY_FILE)
continue;
if (strncmp(entinfo->filename, "guis/", 5) != 0)
continue;
loadDynamicGuiPlugin(plugins, archive);
} // while
} // if
} // loadDynamicGuiPlugins
static void loadGuiPathGuiPlugins(PluginList *plugins)
{
const char *guipath;
MojoArchive *ar;
if ((guipath = cmdlinestr("guipath", "MOJOSETUP_GUIPATH", NULL))!=NULL)
{
ar = MojoArchive_newFromDirectory(guipath);
if (ar) {
loadDynamicGuiPlugins(ar, plugins);
}
ar->close(ar);
ar = NULL;
}
} // loadGuiPathGuiPlugins
const MojoGui *MojoGui_initGuiPlugin(void)
{
PluginList plugins;
PluginList *i = NULL;
if (pluginDetails != NULL)
return pluginDetails->gui;
memset(&plugins, '\0', sizeof (plugins));
assert(GGui == NULL);
loadGuiPathGuiPlugins(&plugins);
loadDynamicGuiPlugins(GBaseArchive, &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 ...