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 15, 2024
1 parent d810add commit 745bad1
Show file tree
Hide file tree
Showing 9 changed files with 92 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
31 changes: 29 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 d8e1b42a3d436a9e04cc7549ff3eb2861e38242b on Thu Apr 11 05:17:30 PM PDT 2024 (This is not the git hash of this file)
//Generated from files git hash 907aeeb011b451ac72f9c07689eaa15803eda779 on Mon Apr 15 07:11:21 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 @@ -4032,9 +4036,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 @@ -5608,6 +5615,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 +5740,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 +5849,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 @@ -5944,7 +5960,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 745bad1

Please sign in to comment.