mudgangster

Log | Files | Refs

commit f0398bf6ec8fe694b495433e124b5943fa66df0d
parent 979777afad4a71cac739f0340bdd4244b844e391
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Thu,  6 Sep 2018 11:52:28 +0300

Macros and intervals on Windows

Diffstat:
src/main.cc | 3+++
src/script.cc | 3+++
src/textbox.cc | 2--
src/win32.cc | 153+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------
4 files changed, 124 insertions(+), 37 deletions(-)

diff --git a/src/main.cc b/src/main.cc @@ -1,8 +1,10 @@ #include "script.h" #include "input.h" #include "ui.h" +#include "platform_network.h" int main() { + net_init(); ui_init(); input_init(); script_init(); @@ -12,6 +14,7 @@ int main() { script_term(); input_term(); ui_term(); + net_term(); return 0; } diff --git a/src/script.cc b/src/script.cc @@ -45,6 +45,9 @@ void script_handleInput( const char * buffer, int len ) { } void script_doMacro( const char * key, int len, bool shift, bool ctrl, bool alt ) { + // printf( "macro %s%s%s%.*s\n", shift ? "s" : "", ctrl ? "c" : "", alt ? "a" : "", len, key ); + // fflush( stdout ); + assert( macroHandlerIdx != LUA_NOREF ); lua_rawgeti( lua, LUA_REGISTRYINDEX, macroHandlerIdx ); diff --git a/src/textbox.cc b/src/textbox.cc @@ -71,8 +71,6 @@ void textbox_scroll( TextBox * tb, int offset ) { else { tb->scroll_offset = min( tb->scroll_offset + offset, tb->num_lines - 1 ); } - - textbox_draw( tb ); } static size_t num_rows( size_t h ) { diff --git a/src/win32.cc b/src/win32.cc @@ -317,9 +317,6 @@ void ui_draw_char( int left, int top, char c, Colour colour, bool bold, bool for TextOutA( UI.hdc, left, top + SPACING, &c, 1 ); } -void ui_dirty( int left, int top, int width, int height ) { -} - typedef struct { char c; @@ -360,8 +357,6 @@ void ui_draw_status() { int y = UI.height - ( PADDING * 3 ) - Style.font.height * 2 - SPACING; ui_draw_char( x, y, sc.c, sc.fg, sc.bold ); } - - ui_dirty( 0, UI.height - ( PADDING * 4 ) - ( Style.font.height * 2 ), UI.width, Style.font.height + ( PADDING * 2 ) ); } void draw_input() { @@ -378,8 +373,6 @@ void draw_input() { if( input.cursor_pos < input.len ) { ui_draw_char( PADDING + input.cursor_pos * Style.font.width, top - SPACING, input.buf[ input.cursor_pos ], COLOUR_BG, false ); } - - ui_dirty( PADDING, UI.height - ( PADDING + Style.font.height ), UI.width - PADDING * 2, Style.font.height ); } void ui_draw() { @@ -393,8 +386,6 @@ void ui_draw() { int spacerY = ( 2 * PADDING ) + ( Style.font.height + SPACING ) * CHAT_ROWS; ui_fill_rect( 0, spacerY, UI.width, 1, COLOUR_STATUSBG, false ); - - ui_dirty( 0, 0, UI.width, UI.height ); } void ui_handleXEvents() { } @@ -527,11 +518,7 @@ LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ) { break; case WM_TIMER: { - // SelectObject( dc, c % 2 == 0 ? font : bold_font ); - // char buf[ 64 ]; - // int l = snprintf( buf, sizeof( buf ), "timer %d", c ); - // TextOutA( dc, 10, 40, buf, l ); - // c++; + script_fire_intervals(); } break; case WM_CHAR: { @@ -542,37 +529,111 @@ LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ) { } } break; - case WM_KEYDOWN: { + case WM_KEYDOWN: + case WM_SYSKEYDOWN: { + #define ADD_MACRO( key, name ) \ + case key: \ + script_doMacro( name, sizeof( name ) - 1, shift, ctrl, alt ); \ + break + + bool shift = ( GetKeyState( VK_SHIFT ) & 0x8000 ) != 0; + bool ctrl = ( GetKeyState( VK_CONTROL ) & 0x8000 ) != 0; + bool alt = ( GetKeyState( VK_MENU ) & 0x8000 ) != 0; + switch( wParam ) { case VK_BACK: input_backspace(); + ui_draw(); break; case VK_DELETE: input_delete(); + ui_draw(); break; case VK_RETURN: input_return(); + ui_draw(); break; case VK_LEFT: input_left(); + ui_draw(); break; case VK_RIGHT: input_right(); + ui_draw(); break; case VK_UP: input_up(); + ui_draw(); break; case VK_DOWN: input_down(); + ui_draw(); break; + + case VK_PRIOR: + if( shift ) + textbox_scroll( &UI.main_text, 1 ); + else + textbox_page_up( &UI.main_text ); + textbox_draw( &UI.main_text ); + break; + + case VK_NEXT: + if( shift ) + textbox_scroll( &UI.main_text, -1 ); + else + textbox_page_down( &UI.main_text ); + textbox_draw( &UI.main_text ); + break; + + ADD_MACRO( VK_NUMPAD0, "kp0" ); + ADD_MACRO( VK_NUMPAD1, "kp1" ); + ADD_MACRO( VK_NUMPAD2, "kp2" ); + ADD_MACRO( VK_NUMPAD3, "kp3" ); + ADD_MACRO( VK_NUMPAD4, "kp4" ); + ADD_MACRO( VK_NUMPAD5, "kp5" ); + ADD_MACRO( VK_NUMPAD6, "kp6" ); + ADD_MACRO( VK_NUMPAD7, "kp7" ); + ADD_MACRO( VK_NUMPAD8, "kp8" ); + ADD_MACRO( VK_NUMPAD9, "kp9" ); + + ADD_MACRO( VK_MULTIPLY, "kp*" ); + ADD_MACRO( VK_DIVIDE, "kp/" ); + ADD_MACRO( VK_SUBTRACT, "kp-" ); + ADD_MACRO( VK_ADD, "kp+" ); + ADD_MACRO( VK_DECIMAL, "kp." ); + + ADD_MACRO( VK_F1, "f1" ); + ADD_MACRO( VK_F2, "f2" ); + ADD_MACRO( VK_F3, "f3" ); + ADD_MACRO( VK_F4, "f4" ); + ADD_MACRO( VK_F5, "f5" ); + ADD_MACRO( VK_F6, "f6" ); + ADD_MACRO( VK_F7, "f7" ); + ADD_MACRO( VK_F8, "f8" ); + ADD_MACRO( VK_F9, "f9" ); + ADD_MACRO( VK_F10, "f10" ); + ADD_MACRO( VK_F11, "f11" ); + ADD_MACRO( VK_F12, "f12" ); + + default: { + char c = MapVirtualKeyA( wParam, MAPVK_VK_TO_CHAR ); + if( c == 0 ) + break; + c = tolower( c ); + if( ctrl || alt ) { + script_doMacro( &c, 1, shift, ctrl, alt ); + } + } break; } - ui_draw(); + + #undef ADD_MACRO } break; case 12345: { @@ -609,9 +670,41 @@ LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ) { return 0; } -int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { - net_init(); +static bool is_macro( const MSG * msg ) { + if( msg->message != WM_KEYDOWN && msg->message != WM_KEYUP + && msg->message != WM_SYSKEYDOWN && msg->message != WM_SYSKEYUP ) + return false; + + WPARAM macro_vks[] = { + VK_NUMPAD0, + VK_NUMPAD1, + VK_NUMPAD2, + VK_NUMPAD3, + VK_NUMPAD4, + VK_NUMPAD5, + VK_NUMPAD6, + VK_NUMPAD7, + VK_NUMPAD8, + VK_NUMPAD9, + VK_MULTIPLY, + VK_DIVIDE, + VK_SUBTRACT, + VK_ADD, + VK_DECIMAL, + }; + + for( WPARAM vk : macro_vks ) { + if( msg->wParam == vk ) + return true; + } + + bool ctrl = ( GetKeyState( VK_CONTROL ) & 0x8000 ) != 0; + bool alt = ( GetKeyState( VK_MENU ) & 0x8000 ) != 0; + return ctrl || alt; +} + +int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { for( Socket & s : sockets ) { s.in_use = false; } @@ -657,32 +750,22 @@ int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLin UpdateWindow( UI.hwnd ); SetTimer( UI.hwnd, 1, 500, NULL ); + net_init(); input_init(); script_init(); - // NetAddress addr; - // dns_first( "mikejsavage.co.uk", &addr ); - // TCPSocket sock; - // bool ok = net_new_tcp( &sock, addr, NET_BLOCKING ); - // if( !ok ) return 1; - // printf( "%lld\n", sock.fd ); - // - // WSAAsyncSelect( sock.fd, UI.hwnd, 12345, FD_READ | FD_CLOSE ); - // - // const char buf[] = "GET / HTTP/1.1\r\n" - // "Host: mikejsavage.co.uk\r\n" - // "Connection: close\r\n" - // "\r\n\r\n"; - // if( !net_send( sock, buf, strlen( buf ) ) ) - // FATAL( "net_send" ); - MSG msg; while( GetMessage( &msg, NULL, 0, 0 ) > 0 ) { - TranslateMessage( &msg ); + if( !is_macro( &msg ) ) + TranslateMessage( &msg ); DispatchMessage( &msg ); } + script_term(); + input_term(); net_term(); + // TODO: clean up UI stuff + return 0; }