mudgangster

Log | Files | Refs

commit 90ba5e85aa2d02c4bdd20307546d61c2fbf61b04
parent 879068ae65ccf4ee6f1d44cfb6cfc1e550023d72
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Mon,  3 Sep 2018 18:17:37 +0300

Style

Diffstat:
src/input.cc | 81+++++++++++++++++++++++++++-----------------------------------------------------
src/input.h | 2+-
src/main.cc | 16++--------------
src/script.cc | 9++++-----
src/ui.cc | 29++++++++++++++---------------
5 files changed, 48 insertions(+), 89 deletions(-)

diff --git a/src/input.cc b/src/input.cc @@ -5,9 +5,8 @@ #include "input.h" #include "script.h" -typedef struct -{ - char* text; +typedef struct { + char * text; int len; } InputHistory; @@ -16,32 +15,27 @@ static int inputHistoryHead = 0; static int inputHistoryCount = 0; static int inputHistoryDelta = 0; -static char* inputBuffer = NULL; -static char* starsBuffer = NULL; +static char * inputBuffer = NULL; +static char * starsBuffer = NULL; static int inputBufferSize = 256; static int inputLen = 0; static int inputPos = 0; -void input_send() -{ - if( inputLen > 0 ) - { - InputHistory* lastCmd = &inputHistory[ ( inputHistoryHead + inputHistoryCount - 1 ) % MAX_INPUT_HISTORY ]; +void input_send() { + if( inputLen > 0 ) { + InputHistory * lastCmd = &inputHistory[ ( inputHistoryHead + inputHistoryCount - 1 ) % MAX_INPUT_HISTORY ]; - if( inputLen != lastCmd->len || strncmp( inputBuffer, lastCmd->text, inputLen ) != 0 ) - { + if( inputLen != lastCmd->len || strncmp( inputBuffer, lastCmd->text, inputLen ) != 0 ) { int pos = ( inputHistoryHead + inputHistoryCount ) % MAX_INPUT_HISTORY; - if( inputHistoryCount == MAX_INPUT_HISTORY ) - { + if( inputHistoryCount == MAX_INPUT_HISTORY ) { free( inputHistory[ pos ].text ); inputHistoryHead = ( inputHistoryHead + 1 ) % MAX_INPUT_HISTORY; } - else - { + else { inputHistoryCount++; } @@ -62,10 +56,8 @@ void input_send() input_draw(); } -void input_backspace() -{ - if( inputPos > 0 ) - { +void input_backspace() { + if( inputPos > 0 ) { memmove( inputBuffer + inputPos - 1, inputBuffer + inputPos, inputLen - inputPos ); inputLen--; @@ -75,10 +67,8 @@ void input_backspace() input_draw(); } -void input_delete() -{ - if( inputPos < inputLen ) - { +void input_delete() { + if( inputPos < inputLen ) { memmove( inputBuffer + inputPos, inputBuffer + inputPos + 1, inputLen - inputPos ); inputLen--; @@ -87,12 +77,9 @@ void input_delete() input_draw(); } -void input_up() -{ +void input_up() { if( inputHistoryDelta >= inputHistoryCount ) - { return; - } inputHistoryDelta++; int pos = ( inputHistoryHead + inputHistoryCount - inputHistoryDelta ) % MAX_INPUT_HISTORY; @@ -107,17 +94,13 @@ void input_up() input_draw(); } -void input_down() -{ +void input_down() { if( inputHistoryDelta == 0 ) - { return; - } inputHistoryDelta--; - if( inputHistoryDelta != 0 ) - { + if( inputHistoryDelta != 0 ) { int pos = ( inputHistoryHead + inputHistoryCount - inputHistoryDelta ) % MAX_INPUT_HISTORY; InputHistory cmd = inputHistory[ pos ]; @@ -127,8 +110,7 @@ void input_down() inputLen = cmd.len; inputPos = cmd.len; } - else - { + else { inputLen = 0; inputPos = 0; } @@ -136,24 +118,20 @@ void input_down() input_draw(); } -void input_left() -{ +void input_left() { inputPos = max( inputPos - 1, 0 ); input_draw(); } -void input_right() -{ +void input_right() { inputPos = min( inputPos + 1, inputLen ); input_draw(); } -void input_add( char* buffer, int len ) -{ - if( inputLen + len >= inputBufferSize ) - { +void input_add( const char * buffer, int len ) { + if( inputLen + len >= inputBufferSize ) { inputBufferSize *= 2; inputBuffer = ( char * ) realloc( inputBuffer, inputBufferSize ); @@ -162,8 +140,7 @@ void input_add( char* buffer, int len ) memset( starsBuffer + inputBufferSize / 2, '*', inputBufferSize / 2 ); } - if( inputPos < inputLen ) - { + if( inputPos < inputLen ) { memmove( inputBuffer + inputPos + len, inputBuffer + inputPos, inputLen - inputPos ); } @@ -175,8 +152,7 @@ void input_add( char* buffer, int len ) input_draw(); } -void input_draw() -{ +void input_draw() { XSetFont( UI.display, UI.gc, Style.font.font->fid ); XSetForeground( UI.display, UI.gc, Style.bg ); @@ -188,23 +164,20 @@ void input_draw() XSetForeground( UI.display, UI.gc, Style.cursor ); XFillRectangle( UI.display, UI.window, UI.gc, PADDING + Style.font.width * inputPos, UI.height - ( PADDING + Style.font.height ), Style.font.width, Style.font.height ); - if( inputPos < inputLen ) - { + if( inputPos < inputLen ) { XSetForeground( UI.display, UI.gc, Style.bg ); XDrawString( UI.display, UI.window, UI.gc, PADDING + Style.font.width * inputPos, UI.height - ( PADDING + Style.font.descent ), inputBuffer + inputPos, 1 ); } } -void input_init() -{ +void input_init() { inputBuffer = ( char * ) malloc( inputBufferSize ); starsBuffer = ( char * ) malloc( inputBufferSize ); memset( starsBuffer, '*', inputBufferSize ); } -void input_end() -{ +void input_end() { free( inputBuffer ); free( starsBuffer ); } diff --git a/src/input.h b/src/input.h @@ -11,7 +11,7 @@ void input_down(); void input_left(); void input_right(); -void input_add( char* buffer, int len ); +void input_add( const char * buffer, int len ); void input_draw(); diff --git a/src/main.cc b/src/main.cc @@ -1,20 +1,8 @@ -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <string.h> - -#include <X11/Xutil.h> -#include <X11/XKBlib.h> -#include <X11/cursorfont.h> -#include <X11/keysym.h> - -#include "common.h" #include "script.h" #include "input.h" #include "ui.h" -int main() -{ +int main() { ui_init(); input_init(); script_init(); @@ -25,5 +13,5 @@ int main() input_end(); ui_end(); - return EXIT_SUCCESS; + return 0; } diff --git a/src/script.cc b/src/script.cc @@ -56,7 +56,7 @@ extern "C" int mud_handleXEvents( lua_State * ) { } static void generic_print( TextBox * tb, lua_State * L ) { - const char* str = luaL_checkstring( L, 1 ); + const char * str = luaL_checkstring( L, 1 ); size_t len = luaL_len( L, 1 ); Colour fg = Colour( luaL_checkinteger( L, 2 ) ); @@ -103,15 +103,14 @@ extern "C" int mud_setStatus( lua_State * L ) { ui_statusClear(); - for( size_t i = 0; i < len; i++ ) - { + for( size_t i = 0; i < len; i++ ) { lua_pushnumber( L, i + 1 ); lua_gettable( L, 1 ); lua_pushliteral( L, "text" ); lua_gettable( L, 2 ); size_t seglen; - const char* str = lua_tolstring( L, -1, &seglen ); + const char * str = lua_tolstring( L, -1, &seglen ); lua_pushliteral( L, "fg" ); lua_gettable( L, 2 ); @@ -147,7 +146,7 @@ extern "C" int mud_setHandlers( lua_State * L ) { extern "C" int mud_urgent( lua_State * L ) { if( !UI.hasFocus ) { - XWMHints* hints = XGetWMHints( UI.display, UI.window ); + XWMHints * hints = XGetWMHints( UI.display, UI.window ); hints->flags |= XUrgencyHint; XSetWMHints( UI.display, UI.window, hints ); XFree( hints ); diff --git a/src/ui.cc b/src/ui.cc @@ -23,7 +23,7 @@ typedef struct { bool bold; } StatusChar; -static StatusChar* statusContents = NULL; +static StatusChar * statusContents = NULL; static size_t statusCapacity = 256; static size_t statusLen = 0; @@ -31,8 +31,7 @@ void ui_statusDraw() { XSetForeground( UI.display, UI.gc, Style.statusBG ); XFillRectangle( UI.display, UI.window, UI.gc, 0, UI.height - ( PADDING * 4 ) - ( Style.font.height * 2 ), UI.width, Style.font.height + ( PADDING * 2 ) ); - for( size_t i = 0; i < statusLen; i++ ) - { + for( size_t i = 0; i < statusLen; i++ ) { StatusChar sc = statusContents[ i ]; XSetFont( UI.display, UI.gc, ( sc.bold ? Style.fontBold : Style.font ).font->fid ); @@ -78,17 +77,17 @@ void ui_draw() { XFillRectangle( UI.display, UI.window, UI.gc, 0, spacerY, UI.width, 1 ); } -void eventButtonPress( XEvent* event ) { } +void eventButtonPress( XEvent * event ) { } -void eventButtonRelease( XEvent* event ) { } +void eventButtonRelease( XEvent * event ) { } -void eventMessage( XEvent* event ) { +void eventMessage( XEvent * event ) { if( ( Atom ) event->xclient.data.l[ 0 ] == wmDeleteWindow ) { script_handleClose(); } } -void eventResize( XEvent* event ) { +void eventResize( XEvent * event ) { int newWidth = event->xconfigure.width; int newHeight = event->xconfigure.height; @@ -111,17 +110,17 @@ void eventResize( XEvent* event ) { ); } -void eventExpose( XEvent* event ) { +void eventExpose( XEvent * event ) { ui_draw(); } -void eventKeyPress( XEvent* event ) { +void eventKeyPress( XEvent * event ) { #define ADD_MACRO( key, name ) \ case key: \ script_doMacro( name, sizeof( name ) - 1, shift, ctrl, alt ); \ break - XKeyEvent* keyEvent = &event->xkey; + XKeyEvent * keyEvent = &event->xkey; char keyBuffer[ 32 ]; KeySym key; @@ -238,21 +237,21 @@ void eventKeyPress( XEvent* event ) { #undef ADD_MACRO } -void eventFocusOut( XEvent* event ) { +void eventFocusOut( XEvent * event ) { UI.hasFocus = 0; } -void eventFocusIn( XEvent* event ) { +void eventFocusIn( XEvent * event ) { UI.hasFocus = 1; - XWMHints* hints = XGetWMHints( UI.display, UI.window ); + XWMHints * hints = XGetWMHints( UI.display, UI.window ); hints->flags &= ~XUrgencyHint; XSetWMHints( UI.display, UI.window, hints ); XFree( hints ); } void ui_handleXEvents() { - void ( *EventHandler[ LASTEvent ] )( XEvent* ) = { }; + void ( *EventHandler[ LASTEvent ] )( XEvent * ) = { }; EventHandler[ ButtonPress ] = eventButtonPress; EventHandler[ ButtonRelease ] = eventButtonRelease; EventHandler[ ClientMessage ] = eventMessage; @@ -372,7 +371,7 @@ void ui_init() { UI.window = XCreateWindow( UI.display, root, 0, 0, 800, 600, 0, UI.depth, InputOutput, visual, CWBackPixel | CWEventMask | CWColormap, &attr ); UI.gc = XCreateGC( UI.display, UI.window, 0, NULL ); - XWMHints* hints = XAllocWMHints(); + XWMHints * hints = XAllocWMHints(); XSetWMHints( UI.display, UI.window, hints ); XFree( hints );