payredu

[WIP] Cross-platform ledger GUI written in c99

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 
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <signal.h>
#include <unistd.h>

#ifdef ENABLE_GUI
#include <GLFW/glfw3.h>
#define NK_INCLUDE_FIXED_TYPES
#define NK_INCLUDE_STANDARD_IO
#define NK_INCLUDE_STANDARD_VARARGS
#define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
#define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT
#define NK_IMPLEMENTATION
#define NK_GLFW_GL2_IMPLEMENTATION
#define NK_KEYSTATE_BASED_INPUT
#include <nuklear.h>
#include <nuklear_glfw_gl2.h>
#endif

#include "common.h"

#define MAX_MEMORY 4064
#define WINDOW_WIDTH 512
#define WINDOW_HEIGHT 512
#define BUFFER_SIZE 256

void* state = NULL;
int should_exit = 0;
// init gui state
int width,height;

#ifdef ENABLE_GUI
struct nk_context* ctx;
GLFWwindow* win;
#endif

void sig_handle() {
	printf("Reloaded\n");
	system("date");
	should_exit = 1;
}

typedef void* (*module_main_func)(void*, int);

static void error_callback(int e, const char *d)
{printf("Error %d: %s\n", e, d);}

int
main(int argc, char* argv[]) {
	signal(SIGQUIT, sig_handle);
#ifdef ENABLE_GUI
	glfwSetErrorCallback(error_callback);
	if (!glfwInit()) {
		fprintf(stdout, "[GFLW] failed to init!\n");
		exit(1);
	}
	/***********************/
	win = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Balance", NULL, NULL);
	glfwMakeContextCurrent(win);
	glfwGetWindowSize(win, &width, &height);

	/* GUI */
	ctx = nk_glfw3_init(win, NK_GLFW3_INSTALL_CALLBACKS);
	/***********************/
	//nk_init_fixed(ctx, calloc(1, MAX_MEMORY), MAX_MEMORY, &font);
	{
		struct nk_font_atlas *atlas;
		nk_glfw3_font_stash_begin(&atlas);
		struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "./Ubuntu-Medium.ttf", 14, 0);
		nk_glfw3_font_stash_end();
		nk_style_set_font(ctx, &droid->handle);
	}
	while(1) {
		void* module = dlopen("./libbook.so", RTLD_NOW);
		while(module == NULL) {
			fprintf(stderr, "Failed to load module. (%s)\n", dlerror());
			fprintf(stderr, "Press return to try again.\n");
			getchar();
			module = dlopen("./libbook.so", RTLD_NOW);
		}
		module_main_func module_main = dlsym(module, "module_main");
		while (!glfwWindowShouldClose(win) && !should_exit) {
			glfwPollEvents();
			nk_glfw3_new_frame();
			state = module_main((void*)ctx, width, height);
			glfwGetWindowSize(win, &width, &height);
			glViewport(0, 0, width, height);
			glClearColor(0.10f, 0.18f, 1.0f, 1.0f);
			glClear(GL_COLOR_BUFFER_BIT);
			/* IMPORTANT: `nk_glfw_render` modifies some global OpenGL state
			 * with blending, scissor, face culling and depth test and defaults everything
			 * back into a default state. Make sure to either save and restore or
			 * reset your own state after drawing rendering the UI. */
			nk_glfw3_render(NK_ANTI_ALIASING_ON);
			glfwSwapBuffers(win);
		}
		dlclose(module);
		should_exit = 0;
	}
#else
	//while(1) {
		void* module = dlopen("./libbook.so", RTLD_NOW);
		while(module == NULL){
			fprintf(stderr, "Failed to load module. (%s)\n", dlerror());
			fprintf(stderr, "Press return to try again.\n");
			getchar();
			module = dlopen("./libbook.so", RTLD_NOW);
		}
		module_main_func module_main = dlsym(module, "module_main");
		FILE* in = fopen("october-2023.txt", "r");
		char* data = (char*)malloc(2048 * sizeof(char));
		size_t data_size = 0;
		size_t c_read =  0;
		while((c_read = fread(data + data_size + 0, 1, BUFFER_SIZE, in)) != 0) {
			data_size += c_read;
		}
		if (ferror(in)) fprintf(stderr, "Error reading file\n");
		fprintf(stdout, "Startig loop\n");
		module_main(data, data_size);

		while(should_exit == 0) {
			sleep(1);
		}
		should_exit = 0;
		dlclose(module);
		fprintf(stderr, "Continue?\n");
	//}
#endif
	return 0;
}