Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module rework #53

Merged
merged 6 commits into from
Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,15 @@ LDSOFLAGS ?= -shared
CFLAGS += -Isrc -DMATRIX_X=$(MATRIX_X) -DMATRIX_Y=$(MATRIX_Y) -DSDL_SCALE_FACTOR=$(SDL_SCALE_FACTOR)
CFLAGS += -DDEFAULT_OUTMOD=\"$(DEFAULT_OUTMOD)\" -DDEFAULT_MODULEDIR=\"$(DEFAULT_MODULEDIR)\"

SOURCES := src/asl.c src/main.c src/matrix.c src/random.c src/timers.c
SOURCES += src/color.c src/graphics.c src/mathey.c src/modloader.c src/util.c
SOURCES := src/asl.c src/main.c src/mod.c src/modloaders/native.c
SOURCES += src/matrix.c src/random.c src/timers.c src/util.c
SOURCES += src/color.c src/graphics.c src/mathey.c
SOURCES += src/taskpool.c src/os/os_$(PLATFORM).c

HEADERS := src/graphics.h src/main.h src/matrix.h src/plugin.h src/timers.h
HEADERS += src/util.h src/asl.h src/loadcore.h src/mathey.h src/modloader.h
HEADERS += src/random.h src/types.h src/oscore.h src/perf.h
HEADERS := src/graphics.h src/main.h src/mod.h src/modloaders/native.h
HEADERS += src/matrix.h src/plugin.h src/timers.h src/util.h
HEADERS += src/asl.h src/loadcore.h src/mathey.h src/modloader.h
HEADERS += src/random.h src/types.h src/oscore.h src/perf.h
HEADERS += src/taskpool.h

# Module libraries.
Expand Down
9 changes: 6 additions & 3 deletions src/dlloadcore.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
#include <stdlib.h>
#include "asl.h"

static char * dirprefix;
static const char * dirprefix;

char ** loadcore_init(char * dir, int * argcno) {
void loadcore_setdir(const char* dir) {
dirprefix = dir;
}

char ** loadcore_init(int * argcno) {
*argcno = 0;
struct dirent * file;
DIR * moduledir = opendir(dir); // for now.
DIR * moduledir = opendir(dirprefix); // for now.
if (!moduledir)
return 0;
char ** list = 0;
Expand Down
6 changes: 3 additions & 3 deletions src/loadcore.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Essentially a wrapper for -ldl when possible, but might actually lead to a weird static linking system.

void loadcore_setdir(char * dir);
void loadcore_setdir(const char * dir);

// Sets the current module directory. This memory is assumed to be kept around
// for as long as modules are being loaded and unloaded.
// It also returns a list of modules in module name form.
// This can be and should be safely freed via the usual method.
char ** loadcore_init(char * dir, int* argcno);
char ** loadcore_init(int* argcno);

// Emulates dlopen with a reasonable set of flags.
void * loadcore_open(const char * modpath);
void * loadcore_open(const char * modname);

// Emulates dlsym.
void * loadcore_sym(void * handle, const char * name);
Expand Down
57 changes: 39 additions & 18 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

#include "types.h"
#include "matrix.h"
#include "modloader.h"
#include "mod.h"
#include "modloaders/native.h"
#include "timers.h"
#include "random.h"
#include "util.h"
Expand All @@ -19,7 +20,7 @@


static int modcount;
struct module *outmod;
module* outmod;

static oscore_mutex rmod_lock;
// Usually -1.
Expand All @@ -37,7 +38,7 @@ static int ci_iteration_count = 0;
static int deinit(void) {
printf("Cleaning up...\n");
int ret;
if ((ret = modules_deinit()) != 0)
if ((ret = mod_deinit()) != 0)
return ret;
if ((ret = matrix_deinit()) != 0)
return ret;
Expand Down Expand Up @@ -69,7 +70,7 @@ static int pick_next_random(int current_modno, ulong in) {
int lastvalidmod = 0;
int usablemodcount = 0;
for (mod = 0; mod < modcount; mod++) {
if (strcmp(modules_get(mod)->type, "gfx") != 0)
if (strcmp(mod_get(mod)->type, "gfx") != 0)
continue;
usablemodcount++;
lastvalidmod = mod;
Expand All @@ -82,7 +83,7 @@ static int pick_next_random(int current_modno, ulong in) {

// Checks after.
if (next_mod == current_modno) next_mod = -1;
module* mod = modules_get(next_mod);
module* mod = mod_get(next_mod);
if (!mod) {
next_mod = -1;
} else if (strcmp(mod->type, "gfx") != 0) {
Expand Down Expand Up @@ -112,7 +113,7 @@ static int pick_next_seq(int current_modno, ulong in) {
int lastvalidmod = 0;
int usablemodcount = 0;
for (mod = 0; mod < modcount; mod++) {
if (strcmp(modules_get(mod)->type, "gfx") != 0)
if (strcmp(mod_get(mod)->type, "gfx") != 0)
continue;
usablemodcount++;
lastvalidmod = mod;
Expand All @@ -137,7 +138,7 @@ static int pick_next_seq(int current_modno, ulong in) {
}

//found a gfx mod, take it
if (strcmp(modules_get(next_mod)->type, "gfx") == 0) done = 1;
if (strcmp(mod_get(next_mod)->type, "gfx") == 0) done = 1;
}
} else if (usablemodcount == 1) {
next_mod = lastvalidmod;
Expand Down Expand Up @@ -270,25 +271,44 @@ int sled_main(int argc, char** argv) {
// Initialize pseudo RNG.
random_seed();

// Load modules
// Prepare for module loading
if (modpath == NULL)
modpath = strdup(default_moduledir);
int* filters = NULL;
if (filterno > 0) {
filters = malloc(filterno * sizeof(int));
if (filterno != 0 && !filters) {
eprintf("Failed to malloc filter list, oops?\n");
return 3;
}
int i;
for (i = 0; i < filterno; ++i)
filters[i] = -1;
}

int outmodno = -1;
if ((ret = modules_loaddir(modpath, outmod_c, &outmodno, filternames, &filterno, filters)) != 0) {
modloader_setdir(modpath);

// Register native module loader.
nativemod_init();

// Load outmod
char outmodname[4 + ARRAY_SIZE(outmod_c)];
snprintf(outmodname, 4 + ARRAY_SIZE(outmod_c), "out_%s", outmod_c);
int outmodno = mod_freeslot();
outmod = mod_get(outmodno);
modloader_load(outmod, outmodname);
if (outmod == NULL) {
eprintf("Didn't load an output module. This isn't good. \n");
deinit();
return 3;
};

// Load remaining modules.
if ((ret = modloader_loaddir(filternames, &filterno, filters)) != 0) {
deinit();
return ret;
}

outmod = modules_get(outmodno);

// Initialize Timers.
ret = timers_init(outmodno);
if (ret) {
Expand All @@ -312,13 +332,13 @@ int sled_main(int argc, char** argv) {
TP_GLOBAL = taskpool_create("taskpool", ncpus, ncpus*8);

// Initialize modules (this can offset outmodno)
ret = modules_init(&outmodno);
ret = mod_init();
if (ret) {
printf("Modules: Init failed.\n");
return ret;
}

modcount = modules_count();
modcount = mod_count();

signal(SIGINT, interrupt_handler);

Expand All @@ -340,17 +360,18 @@ int sled_main(int argc, char** argv) {
continue;
}
if (tnext.moduleno >= 0) {
module* mod = modules_get(tnext.moduleno);
module* mod = mod_get(tnext.moduleno);
mod_gfx *gfx = mod->mod;
if (tnext.moduleno != lastmod) {
printf("\n>> Now drawing %s", mod->name);
fflush(stdout);
if (mod->reset)
mod->reset();
if (gfx->reset)
gfx->reset();
} else {
printf(".");
fflush(stdout);
};
ret = mod->draw(tnext.argc, tnext.argv);
ret = gfx->draw(tnext.argc, tnext.argv);
asl_free_argv(tnext.argc, tnext.argv);
lastmod = tnext.moduleno;
if (ret != 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#ifndef MAIN_H__
#define MAIN_H__

#include "modloader.h"
#include "mod.h"

extern struct module *outmod;
extern module* outmod;

// Changes the outcome of the next random module selection.
// If the outcome has already been changed, waits for that to occur, then tries to grab the random module selection again.
Expand Down
38 changes: 23 additions & 15 deletions src/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@
#include "types.h"
#include <string.h>
#include <assert.h>
#include "modloader.h"
#include "mod.h"
#include "loadcore.h"
#include "main.h"

int* filters;
int filter_amount = 0;
// Filters. Index 0 is the surface layer of the API, unless there are no filters, in which case that responsibility falls to the output module.
// Everything in here, and the output module, is inited and deinited manually.
// A filter is obligated to deinit the module below it in the chain (but not init it), so only the surface-layer module must be deinited from here.
// The last index is directly above the true output module itself.
static int* filters;
static int filter_amount = 0;

// Not to be confused with outmod, this is the contents of filters[0] if it exists, otherwise the output module structure.
static mod_out *out;

int matrix_init(int outmodno, int* filter_list, int filtno, char* outarg, char** filtargs) {
filters = filter_list;
Expand All @@ -22,28 +29,29 @@ int matrix_init(int outmodno, int* filter_list, int filtno, char* outarg, char**
int i = filtno;
int last = outmodno;
for (i = (filtno - 1); i >= 0; --i) {
ret = modules_get(filters[i])->init(last, filtargs[i]);
ret = mod_get(filters[i])->init(last, filtargs[i]);
if (ret != 0) return ret;
last = filters[i];
};
outmod = modules_get(filters[0]);
};
}
outmod = mod_get(filters[0]);
}
out = outmod->mod;
return ret;
}

int matrix_getx(void) {
return outmod->getx();
return out->getx();
}
int matrix_gety(void) {
return outmod->gety();
return out->gety();
}

int matrix_set(int x, int y, RGB color) {
return outmod->set(x, y, color);
return out->set(x, y, color);
}

RGB matrix_get(int x, int y) {
return outmod->get(x, y);
return out->get(x, y);
}

// Fills part of the matrix with jo-- a single color.
Expand All @@ -65,18 +73,18 @@ int matrix_fill(int start_x, int start_y, int end_x, int end_y, RGB color) {

// Zeroes the stuff.
int matrix_clear(void) {
return outmod->clear();
return out->clear();
}

int matrix_render(void) {
return outmod->render();
return out->render();
}

int matrix_deinit(void) {
int ret = 0;
if (outmod != NULL) {
ret = outmod->deinit();
loadcore_close(outmod->lib);
ret = outmod->deinit(mod_getid(outmod));
loadcore_close(out->lib);
}
return ret;
}
2 changes: 1 addition & 1 deletion src/matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#define __INCLUDED_MATRIX__

#include "types.h"
#include "modloader.h"
#include "mod.h"

extern int matrix_init(int outmodno, int* filter_list, int filterno, char* outarg, char** filtargs);
extern int matrix_getx(void);
Expand Down
Loading