Skip to content

Commit

Permalink
Split code out of mojosetup.c into fileio.c and gui.c, and did some p…
Browse files Browse the repository at this point in the history
…olish

 work on the GUI plugin stuff.
  • Loading branch information
icculus committed Mar 25, 2006
1 parent 4070388 commit a112f49
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 169 deletions.
50 changes: 50 additions & 0 deletions fileio.c
@@ -0,0 +1,50 @@
#include "fileio.h"

// !!! FIXME: don't have this here. (need unlink for now).
#include <unistd.h>

boolean mojoInputToPhysicalFile(MojoInput *in, const char *fname)
{
FILE *out = NULL;
boolean iofailure = false;
char buf[1024];
size_t br;

STUBBED("mkdir first?");

if (in == NULL)
return false;

STUBBED("fopen?");
unlink(fname);
out = fopen(fname, "wb");
if (out == NULL)
return false;

while (!iofailure)
{
br = in->read(in, buf, sizeof (buf));
STUBBED("how to detect read failures?");
if (br == 0) // we're done!
break;
else if (br < 0)
iofailure = true;
else
{
if (fwrite(buf, br, 1, out) != 1)
iofailure = true;
} // else
} // while

fclose(out);
if (iofailure)
{
unlink(fname);
return false;
} // if

return true;
} // mojoInputToPhysicalFile

// end of fileio.c ...

2 changes: 2 additions & 0 deletions fileio.h
Expand Up @@ -70,6 +70,8 @@ struct MojoArchive
MojoArchive *MojoArchive_newFromDirectory(const char *dirname);
MojoArchive *MojoArchive_newFromInput(MojoInput *io, const char *origfname);

boolean mojoInputToPhysicalFile(MojoInput *in, const char *fname);

#ifdef __cplusplus
}
#endif
Expand Down
187 changes: 187 additions & 0 deletions gui.c
@@ -0,0 +1,187 @@
#include "gui.h"
#include "fileio.h"

// !!! FIXME: None of these should be here.
#include <dlfcn.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>

typedef struct S_PLUGINLIST
{
char fname[1024];
void *lib;
MojoGui *gui;
MojoGuiPluginPriority priority;
struct S_PLUGINLIST *next;
} PluginList;

MojoGui *GGui = NULL;
PluginList *pluginDetails = NULL;


static PluginList *loadGuiPlugin(MojoArchive *ar)
{
char fname[1024] = { 0 };
PluginList *retval = NULL;
void *lib = NULL;
MojoGuiEntryType entry = NULL;
boolean rc;
MojoInput *io = ar->openCurrentEntry(ar);
if (io == NULL)
return false;

STUBBED("Don't copy if it's a physical file already?");

STUBBED("Filename creation has to change");
{
struct timeval tv;
gettimeofday(&tv, NULL);
snprintf(fname, sizeof (fname), "/tmp/mojosetup-gui-%ld.so", tv.tv_sec);
}

rc = mojoInputToPhysicalFile(io, fname);
io->close(io);

if (rc)
{
lib = dlopen(fname, RTLD_NOW | RTLD_GLOBAL);
STUBBED("abstract out dlopen!");
if (lib != NULL)
{
MojoGui *gui = NULL;
entry = (MojoGuiEntryType) dlsym(lib, MOJOGUI_ENTRY_POINT_STR);
if ( (entry != NULL) && ((gui = entry()) != NULL) )
{
retval = malloc(sizeof (PluginList));
if (retval != NULL)
{
strcpy(retval->fname, fname);
retval->lib = lib;
retval->gui = gui;
retval->priority = gui->priority(gui);
retval->next = NULL;
} // if
} // if
} // if
} // if

if (!retval)
{
if (lib != NULL)
dlclose(lib);
if (fname[0])
unlink(fname);
} // if

return retval;
} // loadGuiPlugin


static PluginList *initGuiPluginsByPriority(PluginList *plugins)
{
MojoGuiPluginPriority p;
for (p = MOJOGUI_PRIORITY_TRY_FIRST; p < MOJOGUI_PRIORITY_TOTAL; p++)
{
PluginList *i;
for (i = plugins->next; i != NULL; i = i->next)
{
if ( (i->priority == p) && (i->gui->init(i->gui)) )
return i;
} // for
} // for

return NULL;
} // initGuiPluginsByPriority


static void deleteGuiPlugin(PluginList *plugin)
{
if (plugin != NULL)
{
if (plugin->gui)
plugin->gui->deinit(plugin->gui);
if (plugin->lib)
dlclose(plugin->lib);
if (plugin->fname[0])
unlink(plugin->fname);
free(plugin);
} // if
} // deleteGuiPlugin


MojoGui *MojoGui_initGuiPlugin(void)
{
const MojoArchiveEntryInfo *entinfo = NULL;
PluginList plugins;

if (pluginDetails != NULL)
return pluginDetails->gui;

assert(GGui == NULL);

memset(&plugins, '\0', sizeof (plugins));

// !!! FIXME: Have a global MojoArchive that represents the install
// !!! FIXME: (either an archive, a physical dir, etc.)
STUBBED("use a global MojoArchive for basedir");
MojoArchive *dir = MojoArchive_newFromDirectory(".");
if (dir == NULL)
return false;

dir->restartEnumeration(dir);
while ((entinfo = dir->enumEntry(dir)) != NULL)
{
PluginList *item;

if (entinfo->type != MOJOARCHIVE_ENTRY_FILE)
continue;

// not in the gui dir?
if (strncmp(entinfo->filename, "guiplugins/", 4) != 0)
continue;

item = loadGuiPlugin(dir);
if (item == NULL)
continue;

item->next = plugins.next;
plugins.next = item;
} // while

STUBBED("Add static plugins to the list?");

pluginDetails = initGuiPluginsByPriority(&plugins);

// cleanout unused plugins...
PluginList *i = plugins.next;
while (i != NULL)
{
PluginList *next = i->next;
if (i != pluginDetails)
deleteGuiPlugin(i);
i = next;
} // while

STUBBED("Don't close this when it's a global.");
dir->close(dir);

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 ...

13 changes: 10 additions & 3 deletions gui.h
Expand Up @@ -14,8 +14,9 @@ typedef enum
{
MOJOGUI_PRIORITY_NEVER_TRY = 0,
MOJOGUI_PRIORITY_TRY_FIRST,
MOJOGUI_PRIORITY_TRY_LAST,
MOJOGUI_PRIORITY_TRY_NORMAL,
MOJOGUI_PRIORITY_TRY_LAST,
MOJOGUI_PRIORITY_TOTAL
} MojoGuiPluginPriority;

/*
Expand All @@ -30,6 +31,7 @@ struct MojoGui_rev1
{
// public
uint8 (*priority)(MojoGui_rev1 *gui);
const char* (*name)(MojoGui_rev1 *gui);
boolean (*init)(MojoGui_rev1 *gui);
void (*deinit)(MojoGui_rev1 *gui);
void (*msgbox)(MojoGui_rev1 *gui, const char *title, const char *text);
Expand Down Expand Up @@ -59,9 +61,8 @@ struct MojoGui_rev1
#define MOJOGUI_ENTRY_POINT_STR MOJOGUI_ENTRY_POINT_STR_VER(MOJOGUI_INTERFACE_REVISION)

typedef MOJOGUI_STRUCT MojoGui;

typedef MOJOGUI_STRUCT * (*MojoGuiEntryType)(void);
__EXPORT__ MOJOGUI_STRUCT *MOJOGUI_ENTRY_POINT(void);
extern MojoGui *GGui;

/*
* We do this as a macro so we only have to update one place, and it
Expand All @@ -73,6 +74,7 @@ MOJOGUI_STRUCT *MOJOGUI_ENTRY_POINT(void) \
{ \
static MOJOGUI_STRUCT retval; \
retval.priority = MojoGui_##module##_priority; \
retval.name = MojoGui_##module##_name; \
retval.init = MojoGui_##module##_init; \
retval.deinit = MojoGui_##module##_deinit; \
retval.msgbox = MojoGui_##module##_msgbox; \
Expand All @@ -81,6 +83,11 @@ MOJOGUI_STRUCT *MOJOGUI_ENTRY_POINT(void) \
return &retval; \
} \


extern MojoGui *GGui;
MojoGui *MojoGui_initGuiPlugin(void);
void MojoGui_deinitGuiPlugin(void);

#ifdef __cplusplus
}
#endif
Expand Down
5 changes: 5 additions & 0 deletions gui/gui_stdio.c
Expand Up @@ -6,6 +6,11 @@ static uint8 MojoGui_stdio_priority(MojoGui_rev1 *gui)
return MOJOGUI_PRIORITY_TRY_LAST;
}

static const char* MojoGui_stdio_name(MojoGui_rev1 *gui)
{
return "stdio";
}

static boolean MojoGui_stdio_init(MojoGui_rev1 *gui)
{
return true;
Expand Down
2 changes: 2 additions & 0 deletions makefile
Expand Up @@ -107,6 +107,8 @@ INCLUDES := \

SRCS := \
mojosetup.c \
gui.c \
fileio.c \


ifeq ($(isunix),true)
Expand Down

0 comments on commit a112f49

Please sign in to comment.