Skip to content

Commit

Permalink
Scancode and Character handling for Windows and Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
Kylogias committed Apr 14, 2024
1 parent 2fd72a3 commit 2da6270
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CNFG.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ int HandleDestroy(); // Return nonzero if you want to cancel destroy.
void HandleWindowTermination();
#endif

// Only implemented in Windows and X11 drivers
void HandleScancode( int scancode, int bDown );
void HandleChar( int character );

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

Expand Down
3 changes: 3 additions & 0 deletions CNFGWinDriver.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,12 @@ int CNFGHandleInput()
case WM_MBUTTONUP: HandleButton( (msg.lParam & 0xFFFF), (msg.lParam>>16) & 0xFFFF, 3, 0 ); break;
case WM_KEYDOWN:
case WM_KEYUP:
HandleScancode( (msg.lParam >> 16) & 0xFF, msg.message==WM_KEYDOWN);

if (msg.lParam & 0x01000000) HandleKey( (int) msg.wParam + 0x7C , (msg.message==WM_KEYDOWN) );
else HandleKey( (int) msg.wParam, (msg.message==WM_KEYDOWN) );
break;
case WM_CHAR: HandleChar( (int) msg.wParam ); break;
case WM_MOUSEWHEEL:
{
POINT p = { 0 };
Expand Down
22 changes: 21 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 @@ -435,7 +444,18 @@ 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 );

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

HandleScancode( report.xkey.keycode, bKeyDirection );

// 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)) HandleChar( *((int*)buf) );
}

break;
case ButtonRelease:
bKeyDirection = 0;
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ is as follows:
#include "rawdraw_sf.h"

void HandleKey( int keycode, int bDown ) { }
void HandleScancode( int scancode, int bDown ) { }
void HandleChar( int character ) { }
void HandleButton( int x, int y, int button, int bDown ) { }
void HandleMotion( int x, int y, int mask ) { }
int HandleDestroy() { return 0; }
Expand Down
10 changes: 10 additions & 0 deletions ogltest.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ void HandleKey( int keycode, int bDown )
printf( "Key: %d %d\n", keycode, bDown );
}

void HandleScancode( int scancode, int bDown )
{
printf( "Scancode: %d -> %d\n", scancode, bDown );
}

void HandleChar( int character )
{
printf( "Char: %c\n", character );
}

void HandleButton( int x, int y, int button, int bDown )
{
}
Expand Down
10 changes: 10 additions & 0 deletions osdtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ void HandleKey( int keycode, int bDown )
printf( "Key: %d -> %d\n", keycode, bDown );
}

void HandleScancode( int scancode, int bDown )
{
printf( "Scancode: %d -> %d\n", scancode, bDown );
}

void HandleChar( int character )
{
printf( "Char: %c\n", character );
}

void HandleButton( int x, int y, int button, int bDown )
{
printf( "Button: %d,%d (%d) -> %d\n", x, y, button, bDown );
Expand Down
12 changes: 11 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 @@ -22,6 +22,16 @@ void HandleKey( int keycode, int bDown )
printf( "Key: %d -> %d\n", keycode, bDown );
}

void HandleScancode( int scancode, int bDown )
{
printf( "Scancode: %d -> %d\n", scancode, bDown );
}

void HandleChar( int character )
{
printf( "Char: %c\n", character );
}

void HandleButton( int x, int y, int button, int bDown )
{
printf( "Button: %d,%d (%d) -> %d\n", x, y, button, bDown );
Expand Down
32 changes: 30 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 ba4b30ccfa84456792ff70a812e1662ea3f09f88 on Fri Sep 8 21:14:25 UTC 2023 (This is not the git hash of this file)
//Generated from files git hash fea0c14c8007a2e30f6109719f8c27a636b32116 on Sat Apr 13 06:23:49 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,10 @@ int HandleDestroy(); // Return nonzero if you want to cancel destroy.
void HandleWindowTermination();
#endif

// Only implemented in Windows and X11 drivers
void HandleScancode( int scancode, int bDown );
void HandleChar( int character );

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

Expand Down Expand Up @@ -3830,9 +3834,12 @@ int CNFGHandleInput()
case WM_MBUTTONUP: HandleButton( (msg.lParam & 0xFFFF), (msg.lParam>>16) & 0xFFFF, 3, 0 ); break;
case WM_KEYDOWN:
case WM_KEYUP:
HandleScancode( (msg.lParam >> 16) & 0xFF, msg.message==WM_KEYDOWN);

if (msg.lParam & 0x01000000) HandleKey( (int) msg.wParam + 0x7C , (msg.message==WM_KEYDOWN) );
else HandleKey( (int) msg.wParam, (msg.message==WM_KEYDOWN) );
break;
case WM_CHAR: HandleChar( (int) msg.wParam ); break;
case WM_MOUSEWHEEL:
{
POINT p = { 0 };
Expand Down Expand Up @@ -5406,6 +5413,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 @@ -5528,6 +5538,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 @@ -5634,10 +5647,14 @@ 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;
#endif

CNFGDisplay = NULL;
CNFGWindowGC = CNFGGC = NULL;
CNFGClassHint = NULL;
Expand Down Expand Up @@ -5742,7 +5759,18 @@ 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 );

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

HandleScancode( report.xkey.keycode, bKeyDirection );

// 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)) HandleChar( *((int*)buf) );
}

break;
case ButtonRelease:
bKeyDirection = 0;
Expand Down
2 changes: 2 additions & 0 deletions simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "CNFG.h"

void HandleKey( int keycode, int bDown ) { }
void HandleScancode( int scancode, int bDown ) { }
void HandleChar( int character ) { }
void HandleButton( int x, int y, int button, int bDown ) { }
void HandleMotion( int x, int y, int mask ) { }
int HandleDestroy() { return 0; }
Expand Down

0 comments on commit 2da6270

Please sign in to comment.