libgoatvr
diff src/vr_modules.c @ 0:ded3d0a74e19
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 29 Aug 2014 03:45:25 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/vr_modules.c Fri Aug 29 03:45:25 2014 +0300 1.3 @@ -0,0 +1,94 @@ 1.4 +/* XXX this might become partly auto-generated in the future */ 1.5 +#include <stdio.h> 1.6 +#include <stdlib.h> 1.7 +#include "vr_impl.h" 1.8 + 1.9 +struct vr_module *vr_module_libovr(void); 1.10 +struct vr_module *vr_module_openhmd(void); 1.11 +struct vr_module *vr_module_null(void); 1.12 + 1.13 +static struct vr_module *modules; 1.14 +static int num_modules, modules_max_size; 1.15 + 1.16 +static int *active_modules; 1.17 +static int num_act_modules, act_modules_max_size; 1.18 + 1.19 + 1.20 +void vr_init_modules(void) 1.21 +{ 1.22 + struct vr_module *m; 1.23 + 1.24 + vr_clear_modules(); 1.25 + 1.26 + if((m = vr_module_libovr())) { 1.27 + vr_register_module(m); 1.28 + } 1.29 + 1.30 + if((m = vr_module_openhmd())) { 1.31 + vr_register_module(m); 1.32 + } 1.33 + 1.34 + if((m = vr_module_null())) { 1.35 + vr_register_module(m); 1.36 + } 1.37 +} 1.38 + 1.39 +void vr_clear_modules(void) 1.40 +{ 1.41 + free(modules); 1.42 + free(active_modules); 1.43 + modules = 0; 1.44 + num_modules = modules_max_size = 0; 1.45 + active_modules = 0; 1.46 + num_act_modules = act_modules_max_size = 0; 1.47 +} 1.48 + 1.49 +void vr_register_module(struct vr_module *mod) 1.50 +{ 1.51 + if(num_modules >= modules_max_size) { 1.52 + int newsz = modules_max_size ? modules_max_size * 2 : 2; 1.53 + struct vr_module *newmods = realloc(modules, newsz * sizeof *newmods); 1.54 + if(!newmods) { 1.55 + fprintf(stderr, "failed to resize modules array up to %d\n", newsz); 1.56 + return; 1.57 + } 1.58 + modules = newmods; 1.59 + modules_max_size = newsz; 1.60 + } 1.61 + modules[num_modules++] = *mod; 1.62 +} 1.63 + 1.64 +int vr_get_num_modules(void) 1.65 +{ 1.66 + return num_modules; 1.67 +} 1.68 + 1.69 +struct vr_module *vr_get_module(int idx) 1.70 +{ 1.71 + return modules + idx; 1.72 +} 1.73 + 1.74 +void vr_activate_module(int idx) 1.75 +{ 1.76 + if(num_act_modules >= act_modules_max_size) { 1.77 + int newsz = act_modules_max_size ? act_modules_max_size * 2 : 2; 1.78 + int *newact = realloc(active_modules, newsz * sizeof *newact); 1.79 + if(!newact) { 1.80 + fprintf(stderr, "failed to resize active modules array up to %d\n", newsz); 1.81 + return; 1.82 + } 1.83 + active_modules = newact; 1.84 + act_modules_max_size = newsz; 1.85 + } 1.86 + active_modules[num_act_modules++] = idx; 1.87 +} 1.88 + 1.89 +int vr_get_num_active_modules(void) 1.90 +{ 1.91 + return num_act_modules; 1.92 +} 1.93 + 1.94 +struct vr_module *vr_get_active_module(int idx) 1.95 +{ 1.96 + return modules + active_modules[idx]; 1.97 +}