Skip to content

Commit

Permalink
added: mouse, keyboard and event functions
Browse files Browse the repository at this point in the history
  • Loading branch information
v1ctor authored and jayrm committed Dec 6, 2020
1 parent 0b8dba4 commit 529c3ec
Show file tree
Hide file tree
Showing 15 changed files with 747 additions and 268 deletions.
2 changes: 1 addition & 1 deletion inc/fbgfx.bi
Expand Up @@ -9,7 +9,7 @@
#inclib "gdi32"
#inclib "winmm"
#inclib "user32"
#elseif defined(__FB_LINUX__)
#elseif defined(__FB_LINUX__) and not defined(__FB_JS__)
#libpath "/usr/X11R6/lib"
#inclib "X11"
#inclib "Xext"
Expand Down
4 changes: 4 additions & 0 deletions makefile
Expand Up @@ -457,6 +457,10 @@ ifneq ($(filter cygwin win32,$(TARGET_OS)),)
ALLFBLFLAGS += -t 2048
endif

ifeq ($(TARGET_OS),js)
DISABLE_MT := YesPlease
endif

# Pass the configuration defines on to the compiler source code
ifdef ENABLE_STANDALONE
ALLFBCFLAGS += -d ENABLE_STANDALONE
Expand Down
42 changes: 0 additions & 42 deletions src/gfxlib2/fb_gfx.h
Expand Up @@ -92,29 +92,6 @@

#define WINDOW_TITLE_SIZE 128

#define EVENT_KEY_PRESS 1
#define EVENT_KEY_RELEASE 2
#define EVENT_KEY_REPEAT 3
#define EVENT_MOUSE_MOVE 4
#define EVENT_MOUSE_BUTTON_PRESS 5
#define EVENT_MOUSE_BUTTON_RELEASE 6
#define EVENT_MOUSE_DOUBLE_CLICK 7
#define EVENT_MOUSE_WHEEL 8
#define EVENT_MOUSE_ENTER 9
#define EVENT_MOUSE_EXIT 10
#define EVENT_WINDOW_GOT_FOCUS 11
#define EVENT_WINDOW_LOST_FOCUS 12
#define EVENT_WINDOW_CLOSE 13
#define EVENT_MOUSE_HWHEEL 14

#define MAX_EVENTS 128

#define BUTTON_LEFT 0x1
#define BUTTON_RIGHT 0x2
#define BUTTON_MIDDLE 0x4
#define BUTTON_X1 0x8
#define BUTTON_X2 0x10

#define GET_WINDOW_POS 0
#define GET_WINDOW_TITLE 1
#define GET_WINDOW_HANDLE 2
Expand Down Expand Up @@ -164,25 +141,6 @@ typedef struct _GFX_CHAR_CELL {
unsigned fg, bg;
} GFX_CHAR_CELL;

struct _EVENT {
int type;
union {
struct { /* keyboard events */
int scancode;
int ascii;
};
struct { /* mouse events */
int x, y;
int dx, dy;
};
int button;
int z;
int w;
};
} FBPACKED;

typedef struct _EVENT EVENT;

typedef struct FBGFX
{
int id; /**< Mode id number for contexts identification */
Expand Down
1 change: 1 addition & 0 deletions src/gfxlib2/gfx_screen.c
Expand Up @@ -231,6 +231,7 @@ static int set_mode
__fb_ctx.hooks.isredirproc = fb_GfxIsRedir;
__fb_ctx.hooks.pagecopyproc = fb_GfxPageCopy;
__fb_ctx.hooks.pagesetproc = fb_GfxPageSet;
__fb_ctx.hooks.posteventproc = NULL;
__fb_gfx = (FBGFX *)calloc(1, sizeof(FBGFX));
}

Expand Down
10 changes: 5 additions & 5 deletions src/gfxlib2/js/fb_gfx_js.h
Expand Up @@ -17,13 +17,13 @@ typedef struct JS_GFXDRIVER_CTX_
SDL_Surface *canvas;
BLITTER *blit;

unsigned long key_buffer[KEY_BUFFER_LEN];
int key_head;
int key_tail;
int doNotCaptureKeyboard;
} JS_GFXDRIVER_CTX;

extern JS_GFXDRIVER_CTX __fb_js_ctx;


void fb_js_events_init(void);
void fb_js_events_check(void);
extern void fb_js_events_init(void);
extern void fb_js_events_exit(void);
extern void fb_js_events_check(void);
extern int fb_js_sdl_buttons_to_fb_buttons(int sdl_buttons);
84 changes: 58 additions & 26 deletions src/gfxlib2/js/gfx_driver.c
Expand Up @@ -3,7 +3,15 @@
#include "../fb_gfx.h"
#include "fb_gfx_js.h"

JS_GFXDRIVER_CTX __fb_js_ctx = { FALSE, FALSE, FALSE, FALSE, NULL };
JS_GFXDRIVER_CTX __fb_js_ctx =
{
.inited = FALSE,
.changingScreen = FALSE,
.updated = FALSE,
.blitting = FALSE,
.canvas = NULL,
.doNotCaptureKeyboard = 0,
};

static void driver_exit(void);

Expand Down Expand Up @@ -93,6 +101,8 @@ static void driver_exit(void)
__fb_js_ctx.canvas = NULL;
}

fb_js_events_exit();

SDL_Quit();
}
}
Expand All @@ -107,63 +117,85 @@ static void driver_unlock(void)
/* !!!WRITEME!!! */
}

static void driver_set_palette(int index, int r, int g, int b)
static void driver_wait_vsync(void)
{
/* !!!WRITEME!!! */
}

static void driver_wait_vsync(void)
int fb_js_sdl_buttons_to_fb_buttons(int sdl_buttons)
{
/* !!!WRITEME!!! */
int fb_buttons = 0;

if( sdl_buttons & SDL_BUTTON_LMASK)
fb_buttons |= BUTTON_LEFT;
if( sdl_buttons & SDL_BUTTON_MMASK)
fb_buttons |= BUTTON_MIDDLE;
if( sdl_buttons & SDL_BUTTON_RMASK)
fb_buttons |= BUTTON_RIGHT;
if( sdl_buttons & SDL_BUTTON_X1MASK)
fb_buttons |= BUTTON_X1;
if( sdl_buttons & SDL_BUTTON_X2MASK)
fb_buttons |= BUTTON_X2;

return fb_buttons;
}

static int driver_get_mouse(int *x, int *y, int *z, int *buttons, int *clip)
{
/* !!!WRITEME!!! */
SDL_PumpEvents();

*buttons = fb_js_sdl_buttons_to_fb_buttons(SDL_GetMouseState(x, y));

*z = 0;
*clip = 0;

return 0;
}

static void driver_set_mouse(int x, int y, int cursor, int clip)
{
/* !!!WRITEME!!! */
//SDL_WarpMouseInWindow(NULL, x, y);
}

static int modes[] = {
SCREENLIST(640, 480),
SCREENLIST(512, 512),
SCREENLIST(320, 240),
SCREENLIST(320, 200),
SCREENLIST(320, 100),
SCREENLIST(256, 256),
SCREENLIST(160, 120),
SCREENLIST(80, 80)
};

static int *driver_fetch_modes(int depth, int *size)
{
int num = 0;
int *modes = NULL, *new_modes;

++num;
new_modes = realloc(modes, sizeof(int) * num);
if (!new_modes) {
*size = num - 1;
return modes;
}

modes = new_modes;
modes[num - 1] = SCREENLIST(320, 200);

*size = num;
return modes;
*size = sizeof(modes) / sizeof(int);
return memcpy((void*)malloc(sizeof(modes)), modes, sizeof(modes));
}

static void driver_poll_events(void)
{
fb_js_events_check( );
}

static void driver_set_window_title(char *title)
{
SDL_WM_SetCaption(title, NULL);
}

static const GFXDRIVER fb_gfxDriverJS =
{
"asmjs", /* char *name; */
driver_init, /* int (*init)(char *title, int w, int h, int depth, int refresh_rate, int flags); */
driver_exit, /* void (*exit)(void); */
driver_lock, /* void (*lock)(void); */
driver_unlock, /* void (*unlock)(void); */
driver_set_palette, /* void (*set_palette)(int index, int r, int g, int b); */
NULL, /* void (*set_palette)(int index, int r, int g, int b); */
driver_wait_vsync, /* void (*wait_vsync)(void); */
driver_get_mouse, /* int (*get_mouse)(int *x, int *y, int *z, int *buttons, int *clip); */
driver_set_mouse, /* void (*set_mouse)(int x, int y, int cursor, int clip); */
NULL, /* void (*set_window_title)(char *title); */
driver_set_window_title, /* void (*set_window_title)(char *title); */
NULL, /* int (*set_window_pos)(int x, int y); */
driver_fetch_modes, /* int *(*fetch_modes)(int depth, int *size); */
NULL, /* void (*flip)(void); */
Expand All @@ -177,9 +209,9 @@ const GFXDRIVER *__fb_gfx_drivers_list[] = {

void fb_hScreenInfo(ssize_t *width, ssize_t *height, ssize_t *depth, ssize_t *refresh)
{
*width = __fb_gfx->w;
*height = __fb_gfx->h;
*depth = __fb_gfx->depth;
*width = 512;
*height = 512;
*depth = 32;
*refresh = GFX_JS_FPS;
}

Expand Down
110 changes: 54 additions & 56 deletions src/gfxlib2/js/gfx_events.c
Expand Up @@ -3,54 +3,6 @@

static int sleep_called = 0;

static int fb_js_gfx_Getkey( void )
{
if( __fb_js_ctx.key_head == __fb_js_ctx.key_tail)
return 0;

int key = __fb_js_ctx.key_buffer[__fb_js_ctx.key_head];
__fb_js_ctx.key_head = (__fb_js_ctx.key_head + 1) % KEY_BUFFER_LEN;

return key;
}

static int fb_js_gfx_KeyHit( void )
{
return __fb_js_ctx.key_head != __fb_js_ctx.key_tail;
}

static FBSTRING *fb_js_gfx_Inkey( void )
{
FBSTRING *res = &__fb_ctx.null_desc;

if( fb_js_gfx_KeyHit( ) != 0 )
{
res = fb_hMakeInkeyStr( fb_js_gfx_Getkey( ) );
}

return res;
}

static void fb_js_key_down(SDL_Event *event)
{
unsigned long key = event->key.keysym.sym;

//emscripten_log(EM_LOG_CONSOLE, "%d, %d, %d", event->key.keysym.sym, event->key.keysym.scancode, key > 0x00FF? ((key & 0x00FF) << 8 | 0x00FF): key);

__fb_js_ctx.key_buffer[__fb_js_ctx.key_tail] = key > 0x00FF? ((key & 0x00FF) << 8 | 0x00FF): key;
__fb_js_ctx.key_tail = (__fb_js_ctx.key_tail + 1) % KEY_BUFFER_LEN;

if( __fb_js_ctx.key_tail == __fb_js_ctx.key_head )
__fb_js_ctx.key_head = (__fb_js_ctx.key_head + 1) % KEY_BUFFER_LEN;

//keys(event.key.keysym.sym) = 1
}

static void fb_js_key_up(SDL_Event *event)
{
//keys(event.key.keysym.sym) = 0
}

void fb_js_events_check(void)
{
SDL_PumpEvents();
Expand All @@ -61,12 +13,44 @@ void fb_js_events_check(void)
{
switch( event.type )
{
case SDL_KEYDOWN:
fb_js_key_down(&event);
case SDL_MOUSEMOTION:
{
EVENT e =
{
.type = EVENT_MOUSE_MOVE,
.x = event.motion.x,
.y = event.motion.y,
.dx = event.motion.xrel,
.dy = event.motion.yrel,
};

fb_hPostEvent(&e);
break;
case SDL_KEYUP:
fb_js_key_up(&event);
}

case SDL_MOUSEBUTTONDOWN:
{
EVENT e =
{
.type = EVENT_MOUSE_BUTTON_PRESS,
.button = fb_js_sdl_buttons_to_fb_buttons(event.button.button),
};

fb_hPostEvent(&e);
break;
}

case SDL_MOUSEBUTTONUP:
{
EVENT e =
{
.type = EVENT_MOUSE_BUTTON_RELEASE,
.button = fb_js_sdl_buttons_to_fb_buttons(event.button.button),
};

fb_hPostEvent(&e);
break;
}
}
}
}
Expand All @@ -82,11 +66,25 @@ static void fb_js_sleep(int msecs)

void fb_js_events_init(void)
{
__fb_ctx.hooks.inkeyproc = fb_js_gfx_Inkey;
__fb_ctx.hooks.getkeyproc = fb_js_gfx_Getkey;
__fb_ctx.hooks.keyhitproc = fb_js_gfx_KeyHit;
__fb_ctx.hooks.multikeyproc = NULL;
__fb_ctx.hooks.sleepproc = fb_js_sleep;

// we can't use SDL's key events because CAPSLOCK isn't handled by emscripten and the printable chars are always lowercase in SDL
__fb_ctx.hooks.inkeyproc = NULL;
__fb_ctx.hooks.getkeyproc = NULL;
__fb_ctx.hooks.keyhitproc = NULL;
__fb_ctx.hooks.multikeyproc = NULL;
__fb_ctx.hooks.posteventproc = fb_hPostEvent;

// don't let SDL capture the keyboard
__fb_js_ctx.doNotCaptureKeyboard = emscripten_run_script_int("Module['doNotCaptureKeyboard']? 1: 0;");
if(!__fb_js_ctx.doNotCaptureKeyboard)
emscripten_run_script("Module['doNotCaptureKeyboard']=1;");

return;
}

void fb_js_events_exit(void)
{
if(!__fb_js_ctx.doNotCaptureKeyboard)
emscripten_run_script("Module['doNotCaptureKeyboard']=null;");
}

0 comments on commit 529c3ec

Please sign in to comment.