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

Add Scancode and Character handling #104

Merged
merged 1 commit into from
Apr 30, 2024
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
4 changes: 4 additions & 0 deletions CNFG.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
//want to include this in your build, but instead, #include "CNFG.h"
//after #define CNFG_IMPLEMENTATION in one of your C files.

// Defined here for universal definition
int CNFGLastCharacter = 0;
int CNFGLastScancode = 0;

#if defined( CNFGHTTP )
#include "CNFGHTTP.c"
#elif defined( __wasm__ )
Expand Down
5 changes: 5 additions & 0 deletions CNFG.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ int HandleDestroy(); // Return nonzero if you want to cancel destroy.
void HandleWindowTermination();
#endif

// Guaranteed to be for the current key inside HandleKey for drivers that support it
// If drivers don't support it, it is 0
extern int CNFGLastCharacter;
extern int CNFGLastScancode;

//Internal function for resizing rasterizer for rasterizer-mode.
void CNFGInternalResize( short x, short y ); //don't call this.

Expand Down
13 changes: 13 additions & 0 deletions CNFGWinDriver.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ int CNFGHandleInput()
while( PeekMessage( &msg, NULL, 0, 0xFFFF, 1 ) )
{
TranslateMessage(&msg);
MSG charMSG;

switch( msg.message )
{
Expand All @@ -341,9 +342,21 @@ int CNFGHandleInput()
case WM_RBUTTONUP: HandleButton( (msg.lParam & 0xFFFF), (msg.lParam>>16) & 0xFFFF, 2, 0 ); break;
case WM_MBUTTONUP: HandleButton( (msg.lParam & 0xFFFF), (msg.lParam>>16) & 0xFFFF, 3, 0 ); break;
case WM_KEYDOWN:
// Check if there is a WM_CHAR message in the queue. If there is one, put it into CNFGLastCharacter
// Otherwise, we don't want HandleKey to handle the wrong character, so set to 0
if (PeekMessage(&charMSG, NULL, WM_CHAR, WM_CHAR, PM_REMOVE)) CNFGLastCharacter = charMSG.wParam;
else CNFGLastCharacter = 0;

// fall through
case WM_KEYUP:
CNFGLastScancode = (msg.lParam >> 16) & 0xFF;

if (msg.lParam & 0x01000000) HandleKey( (int) msg.wParam + 0x7C , (msg.message==WM_KEYDOWN) );
else HandleKey( (int) msg.wParam, (msg.message==WM_KEYDOWN) );

// Don't confuse the program when CNFGLastCharacter is set on WM_KEYUP.
// Since we shouldn't be using CNFGLastCharacter outside HandleKey anyways, just clear it here.
CNFGLastCharacter = 0;
break;
case WM_MOUSEWHEEL:
{
Expand Down
29 changes: 28 additions & 1 deletion CNFGXDriver.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ void CNFGGLXSetup( )
int CNFGX11ForceNoDecoration;
XImage *xi;

XIM CNFGXIM = NULL;
XIC CNFGXIC = NULL;

int g_x_global_key_state;
int g_x_global_shift_key;

Expand Down Expand Up @@ -221,6 +224,9 @@ static void InternalLinkScreenAndGo( const char * WindowName )
if( !CNFGWindowInvisible )
XMapWindow(CNFGDisplay, CNFGWindow);

CNFGXIM = XOpenIM(CNFGDisplay, NULL, wm_res_name, wm_res_class);
CNFGXIC = XCreateIC(CNFGXIM, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, NULL);

#ifdef CNFG_HAS_XSHAPE
if( prepare_xshape )
{
Expand Down Expand Up @@ -327,6 +333,9 @@ void CNFGTearDown()
if ( CNFGGC ) XFreeGC( CNFGDisplay, CNFGGC );
if ( CNFGWindowGC ) XFreeGC( CNFGDisplay, CNFGWindowGC );
if ( CNFGDisplay ) XCloseDisplay( CNFGDisplay );
if ( CNFGXIC ) XDestroyIC( CNFGXIC );
if ( CNFGXIM ) XCloseIM( CNFGXIM );

#ifdef CNFGOGL
if ( CNFGGLXFBConfigs ) XFree( CNFGGLXFBConfigs );
CNFGGLXFBConfigs = NULL;
Expand Down Expand Up @@ -418,6 +427,8 @@ int CNFGHandleInput()
CNFGPixmap = XCreatePixmap( CNFGDisplay, CNFGWindow, CNFGWinAtt.width, CNFGWinAtt.height, CNFGWinAtt.depth );
if( CNFGGC ) XFreeGC( CNFGDisplay, CNFGGC );
CNFGGC = XCreateGC(CNFGDisplay, CNFGPixmap, 0, 0);

CNFGLastScancode = 0;
HandleKey( CNFG_X11_EXPOSE, 0 );
break;
case KeyRelease:
Expand All @@ -435,7 +446,23 @@ int CNFGHandleInput()
case KeyPress:
g_x_global_key_state = report.xkey.state;
g_x_global_shift_key = XLookupKeysym(&report.xkey, 1);
HandleKey( XLookupKeysym(&report.xkey, 0), bKeyDirection );

CNFGLastScancode = report.xkey.keycode;

// Chars should ONLY be handled on Key Press
if (report.type == KeyPress) {
char buf[8] = {0};
if (Xutf8LookupString(CNFGXIC, &report.xkey, buf, 8, NULL, NULL)) CNFGLastCharacter = *((int*)buf);
else CNFGLastCharacter = 0;
}

KeySym sym = XLookupKeysym(&report.xkey, 0);
HandleKey( sym, bKeyDirection );

// Don't confuse the program when CNFGLastCharacter is set on KeyRelease.
// Since we shouldn't be using CNFGLastCharacter outside HandleKey anyways, just clear it here.
CNFGLastCharacter = 0;

break;
case ButtonRelease:
bKeyDirection = 0;
Expand Down
4 changes: 3 additions & 1 deletion rawdraw.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#define CNFG3D
#define CNFG_IMPLEMENTATION
#define CNFGOGL
//#define CNFGOGL
//#define CNFGRASTERIZER
//#define CNFG_WINDOWS_DISABLE_BATCH

Expand All @@ -20,6 +20,8 @@ void HandleKey( int keycode, int bDown )
{
if( keycode == 27 ) exit( 0 );
printf( "Key: %d -> %d\n", keycode, bDown );
printf( "Scancode: %d -> %d\n", CNFGLastScancode, bDown );
printf( "Char: %c\n", CNFGLastCharacter );
}

void HandleButton( int x, int y, int button, int bDown )
Expand Down
53 changes: 51 additions & 2 deletions rawdraw_sf.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//This file was automatically generated by Makefile at https://github.com/cntools/rawdraw
//Generated from files git hash 3267604549307ef6bc34f2ba970700321f79e09b on Mon Apr 29 08:58:40 AM EDT 2024 (This is not the git hash of this file)
//Generated from files git hash d810add50f84bb8dd81b456d81c68c483d58aa70 on Mon Apr 29 05:11:01 PM EDT 2024 (This is not the git hash of this file)
// Copyright 2010-2021 <>< CNLohr, et. al. (Several other authors, many but not all mentioned)
// Licensed under the MIT/x11 or NewBSD License you choose.
//
Expand Down Expand Up @@ -139,6 +139,11 @@ int HandleDestroy(); // Return nonzero if you want to cancel destroy.
void HandleWindowTermination();
#endif

// Guaranteed to be for the current key inside HandleKey for drivers that support it
// If drivers don't support it, it is 0
extern int CNFGLastCharacter;
extern int CNFGLastScancode;

//Internal function for resizing rasterizer for rasterizer-mode.
void CNFGInternalResize( short x, short y ); //don't call this.

Expand Down Expand Up @@ -740,6 +745,10 @@ static inline void DumpObjectClassProperties( jobject objToDump )
//want to include this in your build, but instead, #include "CNFG.h"
//after #define CNFG_IMPLEMENTATION in one of your C files.

// Defined here for universal definition
int CNFGLastCharacter = 0;
int CNFGLastScancode = 0;

#if defined( CNFGHTTP )
//Copyright 2015-2021 <>< Charles Lohr Under the MIT/x11 License, NewBSD License or
// ColorChord License. You Choose. This file mostly based on `cnhttp` from cntools.
Expand Down Expand Up @@ -4018,6 +4027,7 @@ int CNFGHandleInput()
while( PeekMessage( &msg, NULL, 0, 0xFFFF, 1 ) )
{
TranslateMessage(&msg);
MSG charMSG;

switch( msg.message )
{
Expand All @@ -4031,9 +4041,21 @@ int CNFGHandleInput()
case WM_RBUTTONUP: HandleButton( (msg.lParam & 0xFFFF), (msg.lParam>>16) & 0xFFFF, 2, 0 ); break;
case WM_MBUTTONUP: HandleButton( (msg.lParam & 0xFFFF), (msg.lParam>>16) & 0xFFFF, 3, 0 ); break;
case WM_KEYDOWN:
// Check if there is a WM_CHAR message in the queue. If there is one, put it into CNFGLastCharacter
// Otherwise, we don't want HandleKey to handle the wrong character, so set to 0
if (PeekMessage(&charMSG, NULL, WM_CHAR, WM_CHAR, PM_REMOVE)) CNFGLastCharacter = charMSG.wParam;
else CNFGLastCharacter = 0;

// fall through
case WM_KEYUP:
CNFGLastScancode = (msg.lParam >> 16) & 0xFF;

if (msg.lParam & 0x01000000) HandleKey( (int) msg.wParam + 0x7C , (msg.message==WM_KEYDOWN) );
else HandleKey( (int) msg.wParam, (msg.message==WM_KEYDOWN) );

// Don't confuse the program when CNFGLastCharacter is set on WM_KEYUP.
// Since we shouldn't be using CNFGLastCharacter outside HandleKey anyways, just clear it here.
CNFGLastCharacter = 0;
break;
case WM_MOUSEWHEEL:
{
Expand Down Expand Up @@ -5608,6 +5630,9 @@ void CNFGGLXSetup( )
int CNFGX11ForceNoDecoration;
XImage *xi;

XIM CNFGXIM = NULL;
XIC CNFGXIC = NULL;

int g_x_global_key_state;
int g_x_global_shift_key;

Expand Down Expand Up @@ -5730,6 +5755,9 @@ static void InternalLinkScreenAndGo( const char * WindowName )
if( !CNFGWindowInvisible )
XMapWindow(CNFGDisplay, CNFGWindow);

CNFGXIM = XOpenIM(CNFGDisplay, NULL, wm_res_name, wm_res_class);
CNFGXIC = XCreateIC(CNFGXIM, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, NULL);

#ifdef CNFG_HAS_XSHAPE
if( prepare_xshape )
{
Expand Down Expand Up @@ -5836,6 +5864,9 @@ void CNFGTearDown()
if ( CNFGGC ) XFreeGC( CNFGDisplay, CNFGGC );
if ( CNFGWindowGC ) XFreeGC( CNFGDisplay, CNFGWindowGC );
if ( CNFGDisplay ) XCloseDisplay( CNFGDisplay );
if ( CNFGXIC ) XDestroyIC( CNFGXIC );
if ( CNFGXIM ) XCloseIM( CNFGXIM );

#ifdef CNFGOGL
if ( CNFGGLXFBConfigs ) XFree( CNFGGLXFBConfigs );
CNFGGLXFBConfigs = NULL;
Expand Down Expand Up @@ -5927,6 +5958,8 @@ int CNFGHandleInput()
CNFGPixmap = XCreatePixmap( CNFGDisplay, CNFGWindow, CNFGWinAtt.width, CNFGWinAtt.height, CNFGWinAtt.depth );
if( CNFGGC ) XFreeGC( CNFGDisplay, CNFGGC );
CNFGGC = XCreateGC(CNFGDisplay, CNFGPixmap, 0, 0);

CNFGLastScancode = 0;
HandleKey( CNFG_X11_EXPOSE, 0 );
break;
case KeyRelease:
Expand All @@ -5944,7 +5977,23 @@ int CNFGHandleInput()
case KeyPress:
g_x_global_key_state = report.xkey.state;
g_x_global_shift_key = XLookupKeysym(&report.xkey, 1);
HandleKey( XLookupKeysym(&report.xkey, 0), bKeyDirection );

CNFGLastScancode = report.xkey.keycode;

// Chars should ONLY be handled on Key Press
if (report.type == KeyPress) {
char buf[8] = {0};
if (Xutf8LookupString(CNFGXIC, &report.xkey, buf, 8, NULL, NULL)) CNFGLastCharacter = *((int*)buf);
else CNFGLastCharacter = 0;
}

KeySym sym = XLookupKeysym(&report.xkey, 0);
HandleKey( sym, bKeyDirection );

// Don't confuse the program when CNFGLastCharacter is set on KeyRelease.
// Since we shouldn't be using CNFGLastCharacter outside HandleKey anyways, just clear it here.
CNFGLastCharacter = 0;

break;
case ButtonRelease:
bKeyDirection = 0;
Expand Down
Loading