Skip to content

Commit

Permalink
Store scancode and character into variable instead of calling a user …
Browse files Browse the repository at this point in the history
…defined function
  • Loading branch information
Kylogias committed Apr 22, 2024
1 parent 745bad1 commit 97872ac
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 55 deletions.
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
7 changes: 4 additions & 3 deletions CNFG.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +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 );
// 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
10 changes: 8 additions & 2 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,13 +342,18 @@ 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 = newMSG.wParam;
else CNFGLastCharacter = 0;

// fall through
case WM_KEYUP:
HandleScancode( (msg.lParam >> 16) & 0xFF, msg.message==WM_KEYDOWN);
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) );
break;
case WM_CHAR: HandleChar( (int) msg.wParam ); break;
case WM_MOUSEWHEEL:
{
POINT p = { 0 };
Expand Down
11 changes: 6 additions & 5 deletions CNFGXDriver.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,16 +445,17 @@ int CNFGHandleInput()
g_x_global_key_state = report.xkey.state;
g_x_global_shift_key = XLookupKeysym(&report.xkey, 1);

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

HandleScancode( report.xkey.keycode, 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)) HandleChar( *((int*)buf) );
if (Xutf8LookupString(CNFGXIC, &report.xkey, buf, 8, NULL, NULL)) CNFGLastCharacter = *((int*)buf);
else CNFGLastCharacter = 0;
}

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

break;
case ButtonRelease:
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ 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: 0 additions & 10 deletions ogltest.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@ 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: 0 additions & 10 deletions osdtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@ 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: 2 additions & 10 deletions rawdraw.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,8 @@ void HandleKey( int keycode, int bDown )
{
if( keycode == 27 ) exit( 0 );
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 );
printf( "Scancode: %d -> %d\n", CNFGLastScancode, bDown );
printf( "Char: %c\n", CNFGLastCharacter );
}

void HandleButton( int x, int y, int button, int bDown )
Expand Down
34 changes: 23 additions & 11 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 907aeeb011b451ac72f9c07689eaa15803eda779 on Mon Apr 15 07:11:21 PM EDT 2024 (This is not the git hash of this file)
//Generated from files git hash 0ef6ed0fd4ceb1bc5690d9204cc8e24abd70dd44 on Mon Apr 22 07:19:51 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,9 +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 );
// 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 @@ -744,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 @@ -4022,6 +4027,7 @@ int CNFGHandleInput()
while( PeekMessage( &msg, NULL, 0, 0xFFFF, 1 ) )
{
TranslateMessage(&msg);
MSG charMSG;

switch( msg.message )
{
Expand All @@ -4035,13 +4041,18 @@ 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 = newMSG.wParam;
else CNFGLastCharacter = 0;

// fall through
case WM_KEYUP:
HandleScancode( (msg.lParam >> 16) & 0xFF, msg.message==WM_KEYDOWN);
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) );
break;
case WM_CHAR: HandleChar( (int) msg.wParam ); break;
case WM_MOUSEWHEEL:
{
POINT p = { 0 };
Expand Down Expand Up @@ -5961,16 +5972,17 @@ int CNFGHandleInput()
g_x_global_key_state = report.xkey.state;
g_x_global_shift_key = XLookupKeysym(&report.xkey, 1);

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

HandleScancode( report.xkey.keycode, 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)) HandleChar( *((int*)buf) );
if (Xutf8LookupString(CNFGXIC, &report.xkey, buf, 8, NULL, NULL)) CNFGLastCharacter = *((int*)buf);
else CNFGLastCharacter = 0;
}

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

break;
case ButtonRelease:
Expand Down
2 changes: 0 additions & 2 deletions simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#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 97872ac

Please sign in to comment.