mirror of
https://github.com/buserror/mii_emu.git
synced 2024-11-04 09:05:57 +00:00
f7a56ebc01
Cleaned up for release at last! Signed-off-by: Michel Pollet <buserror@gmail.com>
323 lines
11 KiB
C
323 lines
11 KiB
C
/*
|
|
* mii_emu.c
|
|
*
|
|
* Copyright (C) 2023 Michel Pollet <buserror@gmail.com>
|
|
*
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdint.h>
|
|
#include <stdarg.h>
|
|
#include <string.h>
|
|
#include <math.h>
|
|
#include <assert.h>
|
|
#include <time.h>
|
|
#include <limits.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_BUTTON_TRIGGER_ON_RELEASE
|
|
#include "nuklear.h"
|
|
|
|
#define NK_XLIB_GL3_IMPLEMENTATION
|
|
#define NK_XLIB_LOAD_OPENGL_EXTENSIONS
|
|
#include "nuklear_xlib_gl3.h"
|
|
|
|
#include "mii.h"
|
|
#include "mish.h"
|
|
|
|
#define WINDOW_WIDTH 1280
|
|
#define WINDOW_HEIGHT 720 // (10 + (192) * 3)
|
|
|
|
#define MAX_VERTEX_BUFFER 512 * 1024
|
|
#define MAX_ELEMENT_BUFFER 128 * 1024
|
|
|
|
void
|
|
mii_nuklear(
|
|
mii_t *mii,
|
|
struct nk_context *ctx);
|
|
void
|
|
mii_nuklear_init(
|
|
mii_t *mii,
|
|
struct nk_context *ctx);
|
|
/* ===============================================================
|
|
*
|
|
* DEMO
|
|
*
|
|
* ===============================================================*/
|
|
struct XWindow {
|
|
Display *dpy;
|
|
Window win;
|
|
XVisualInfo *vis;
|
|
Colormap cmap;
|
|
XSetWindowAttributes swa;
|
|
XWindowAttributes attr;
|
|
GLXFBConfig fbc;
|
|
Atom wm_delete_window;
|
|
int width, height;
|
|
};
|
|
static int gl_err = nk_false;
|
|
static int gl_error_handler(Display *dpy, XErrorEvent *ev)
|
|
{NK_UNUSED(dpy); NK_UNUSED(ev); gl_err = nk_true;return 0;}
|
|
|
|
static void
|
|
die(const char *fmt, ...)
|
|
{
|
|
va_list ap;
|
|
va_start(ap, fmt);
|
|
vfprintf(stderr, fmt, ap);
|
|
va_end(ap);
|
|
fputs("\n", stderr);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
static int
|
|
has_extension(const char *string, const char *ext)
|
|
{
|
|
const char *start, *where, *term;
|
|
where = strchr(ext, ' ');
|
|
if (where || *ext == '\0')
|
|
return nk_false;
|
|
|
|
for (start = string;;) {
|
|
where = strstr((const char*)start, ext);
|
|
if (!where) break;
|
|
term = where + strlen(ext);
|
|
if (where == start || *(where - 1) == ' ') {
|
|
if (*term == ' ' || *term == '\0')
|
|
return nk_true;
|
|
}
|
|
start = term;
|
|
}
|
|
return nk_false;
|
|
}
|
|
|
|
struct nk_font *nk_main_font = NULL;
|
|
|
|
/* global now, mish commands use it */
|
|
mii_t g_mii = {};
|
|
|
|
int
|
|
main(
|
|
int argc,
|
|
const char *argv[])
|
|
{
|
|
/* Platform */
|
|
int running = 1;
|
|
struct XWindow win;
|
|
GLXContext glContext;
|
|
struct nk_context *ctx;
|
|
struct nk_colorf bg;
|
|
|
|
mii_init(&g_mii);
|
|
int idx = 0;
|
|
uint32_t flags = MII_INIT_DEFAULT;
|
|
int r = mii_argv_parse(&g_mii, argc, argv, &idx, &flags);
|
|
if (r == 0) {
|
|
printf("mii: Invalid argument %s, skipped\n", argv[idx]);
|
|
} else if (r == -1)
|
|
exit(1);
|
|
mii_prepare(&g_mii, flags);
|
|
mish_prepare(1);
|
|
printf("MISH_TELNET_PORT = %s\n", getenv("MISH_TELNET_PORT"));
|
|
|
|
memset(&win, 0, sizeof(win));
|
|
win.dpy = XOpenDisplay(NULL);
|
|
if (!win.dpy) die("Failed to open X display\n");
|
|
{
|
|
/* check glx version */
|
|
int glx_major, glx_minor;
|
|
if (!glXQueryVersion(win.dpy, &glx_major, &glx_minor))
|
|
die("[X11]: Error: Failed to query OpenGL version\n");
|
|
if ((glx_major == 1 && glx_minor < 3) || (glx_major < 1))
|
|
die("[X11]: Error: Invalid GLX version!\n");
|
|
}
|
|
{
|
|
/* find and pick matching framebuffer visual */
|
|
int fb_count;
|
|
static GLint attr[] = {
|
|
GLX_X_RENDERABLE, True,
|
|
GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
|
|
GLX_RENDER_TYPE, GLX_RGBA_BIT,
|
|
GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR,
|
|
GLX_RED_SIZE, 8,
|
|
GLX_GREEN_SIZE, 8,
|
|
GLX_BLUE_SIZE, 8,
|
|
GLX_ALPHA_SIZE, 8,
|
|
GLX_DEPTH_SIZE, 24,
|
|
GLX_STENCIL_SIZE, 8,
|
|
GLX_DOUBLEBUFFER, True,
|
|
None
|
|
};
|
|
GLXFBConfig *fbc;
|
|
fbc = glXChooseFBConfig(win.dpy, DefaultScreen(win.dpy), attr, &fb_count);
|
|
if (!fbc) die("[X11]: Error: failed to retrieve framebuffer configuration\n");
|
|
{
|
|
/* pick framebuffer with most samples per pixel */
|
|
int i;
|
|
int fb_best = -1, best_num_samples = -1;
|
|
for (i = 0; i < fb_count; ++i) {
|
|
XVisualInfo *vi = glXGetVisualFromFBConfig(win.dpy, fbc[i]);
|
|
if (vi) {
|
|
int sample_buffer, samples;
|
|
glXGetFBConfigAttrib(win.dpy, fbc[i], GLX_SAMPLE_BUFFERS, &sample_buffer);
|
|
glXGetFBConfigAttrib(win.dpy, fbc[i], GLX_SAMPLES, &samples);
|
|
if ((fb_best < 0) || (sample_buffer && samples > best_num_samples))
|
|
fb_best = i, best_num_samples = samples;
|
|
XFree(vi);
|
|
}
|
|
}
|
|
win.fbc = fbc[fb_best];
|
|
XFree(fbc);
|
|
win.vis = glXGetVisualFromFBConfig(win.dpy, win.fbc);
|
|
}
|
|
}
|
|
{
|
|
/* create window */
|
|
win.cmap = XCreateColormap(win.dpy, RootWindow(win.dpy, win.vis->screen), win.vis->visual, AllocNone);
|
|
win.swa.colormap = win.cmap;
|
|
win.swa.background_pixmap = None;
|
|
win.swa.border_pixel = 0;
|
|
win.swa.event_mask =
|
|
ExposureMask | KeyPressMask | KeyReleaseMask |
|
|
ButtonPress | ButtonReleaseMask| ButtonMotionMask |
|
|
Button1MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask|
|
|
PointerMotionMask| StructureNotifyMask;
|
|
win.win = XCreateWindow(win.dpy, RootWindow(win.dpy, win.vis->screen), 0, 0,
|
|
WINDOW_WIDTH, WINDOW_HEIGHT, 0, win.vis->depth, InputOutput,
|
|
win.vis->visual, CWBorderPixel|CWColormap|CWEventMask, &win.swa);
|
|
if (!win.win) die("[X11]: Failed to create window\n");
|
|
XFree(win.vis);
|
|
|
|
char title[128];
|
|
sprintf(title, "MII //e Emulator");
|
|
char *telnet = getenv("MISH_TELNET_PORT");
|
|
if (telnet)
|
|
sprintf(title + strlen(title), " (telnet port %s)", telnet);
|
|
else
|
|
sprintf(title + strlen(title), " (telnet disabled)");
|
|
XStoreName(win.dpy, win.win, title);
|
|
XMapWindow(win.dpy, win.win);
|
|
win.wm_delete_window = XInternAtom(win.dpy, "WM_DELETE_WINDOW", False);
|
|
XSetWMProtocols(win.dpy, win.win, &win.wm_delete_window, 1);
|
|
}
|
|
{
|
|
/* create opengl context */
|
|
typedef GLXContext(*glxCreateContext)(Display*, GLXFBConfig, GLXContext, Bool, const int*);
|
|
int(*old_handler)(Display*, XErrorEvent*) = XSetErrorHandler(gl_error_handler);
|
|
const char *extensions_str = glXQueryExtensionsString(win.dpy, DefaultScreen(win.dpy));
|
|
glxCreateContext create_context = (glxCreateContext)
|
|
glXGetProcAddressARB((const GLubyte*)"glXCreateContextAttribsARB");
|
|
|
|
gl_err = nk_false;
|
|
if (!has_extension(extensions_str, "GLX_ARB_create_context") || !create_context) {
|
|
fprintf(stdout, "[X11]: glXCreateContextAttribARB() not found...\n");
|
|
fprintf(stdout, "[X11]: ... using old-style GLX context\n");
|
|
glContext = glXCreateNewContext(win.dpy, win.fbc, GLX_RGBA_TYPE, 0, True);
|
|
} else {
|
|
GLint attr[] = {
|
|
GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
|
|
GLX_CONTEXT_MINOR_VERSION_ARB, 0,
|
|
None
|
|
};
|
|
glContext = create_context(win.dpy, win.fbc, 0, True, attr);
|
|
XSync(win.dpy, False);
|
|
if (gl_err || !glContext) {
|
|
/* Could not create GL 3.0 context. Fallback to old 2.x context.
|
|
* If a version below 3.0 is requested, implementations will
|
|
* return the newest context version compatible with OpenGL
|
|
* version less than version 3.0.*/
|
|
attr[1] = 1; attr[3] = 0;
|
|
gl_err = nk_false;
|
|
fprintf(stdout, "[X11] Failed to create OpenGL 3.0 context\n");
|
|
fprintf(stdout, "[X11] ... using old-style GLX context!\n");
|
|
glContext = create_context(win.dpy, win.fbc, 0, True, attr);
|
|
}
|
|
}
|
|
XSync(win.dpy, False);
|
|
XSetErrorHandler(old_handler);
|
|
if (gl_err || !glContext)
|
|
die("[X11]: Failed to create an OpenGL context\n");
|
|
glXMakeCurrent(win.dpy, win.win, glContext);
|
|
}
|
|
//extern const unsigned char mii_proggy_data[];
|
|
//extern const unsigned int mii_proggy_size;
|
|
extern const unsigned char mii_droid_data[];
|
|
extern const unsigned int mii_droid_size;
|
|
|
|
ctx = nk_x11_init(win.dpy, win.win);
|
|
/* Load Fonts: if none of these are loaded a default font will be used */
|
|
{
|
|
struct nk_font_atlas *atlas;
|
|
nk_x11_font_stash_begin(&atlas);
|
|
struct nk_font_config cfg = nk_font_config(0);
|
|
#if 0
|
|
nk_rune ranges[] = {
|
|
0x0020, 0x007E, /* Ascii */
|
|
0x00A1, 0x00FF, /* Symbols + Umlaute */
|
|
0
|
|
};
|
|
#endif
|
|
cfg.range = nk_font_default_glyph_ranges();
|
|
cfg.oversample_h = cfg.oversample_v = 1;
|
|
cfg.pixel_snap = true;
|
|
nk_main_font = nk_font_atlas_add_from_memory(atlas,
|
|
(void*)mii_droid_data, mii_droid_size, 20, &cfg);
|
|
nk_x11_font_stash_end();
|
|
/*nk_style_load_all_cursors(ctx, atlas->cursors);*/
|
|
if (nk_main_font)
|
|
nk_style_set_font(ctx, &nk_main_font->handle);
|
|
}
|
|
|
|
mii_nuklear_init(&g_mii, ctx);
|
|
|
|
bg.r = 0.0f; bg.g = 0.0f; bg.b = 0.0f; bg.a = 1.0f;
|
|
// bg.r = 0.8f; bg.g = 0.8f; bg.b = 0.8f; bg.a = 1.0f;
|
|
while (running) {
|
|
/* Input */
|
|
XEvent evt;
|
|
nk_input_begin(ctx);
|
|
while (XPending(win.dpy)) {
|
|
XNextEvent(win.dpy, &evt);
|
|
if (evt.type == ClientMessage) goto cleanup;
|
|
if (XFilterEvent(&evt, win.win)) continue;
|
|
nk_x11_handle_event(&evt);
|
|
}
|
|
nk_input_end(ctx);
|
|
|
|
/* GUI */
|
|
mii_nuklear(&g_mii, ctx);
|
|
|
|
/* Draw */
|
|
XGetWindowAttributes(win.dpy, win.win, &win.attr);
|
|
glViewport(0, 0, win.width, win.height);
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
glClearColor(bg.r, bg.g, bg.b, bg.a);
|
|
/* IMPORTANT: `nk_x11_render` modifies some global OpenGL state
|
|
* with blending, scissor, face culling, depth test and viewport and
|
|
* defaults everything back into a default state.
|
|
* Make sure to either a.) save and restore or b.) reset your own state after
|
|
* rendering the UI. */
|
|
nk_x11_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER);
|
|
glXSwapBuffers(win.dpy, win.win);
|
|
}
|
|
|
|
cleanup:
|
|
nk_x11_shutdown();
|
|
glXMakeCurrent(win.dpy, 0, 0);
|
|
glXDestroyContext(win.dpy, glContext);
|
|
XUnmapWindow(win.dpy, win.win);
|
|
XFreeColormap(win.dpy, win.cmap);
|
|
XDestroyWindow(win.dpy, win.win);
|
|
XCloseDisplay(win.dpy);
|
|
return 0;
|
|
|
|
}
|