cnping

Minimal Graphical Ping Tool
git clone https://git.sinitax.com/cnlohr/cnping
Log | Files | Refs | Submodules | README | LICENSE | sfeed.txt

commit 8cb62c2dd61a8bb65af1e3af27980f4a0256234b
parent 933806fc806b9052110e0342ef8fc6823ee278de
Author: CNLohr <charles@cnlohr.com>
Date:   Tue, 18 Oct 2016 09:35:48 -0400

Merge pull request #12 from brookst/x11-fix

X11 Improvements
Diffstat:
MDrawFunctions.h | 1+
MWinDriver.c | 7++++++-
MXDriver.c | 43+++++++++++++++++++++++++++++++++++++------
3 files changed, 44 insertions(+), 7 deletions(-)

diff --git a/DrawFunctions.h b/DrawFunctions.h @@ -34,6 +34,7 @@ void CNFGClearFrame(); void CNFGSwapBuffers(); void CNFGGetDimensions( short * x, short * y ); +void CNFGTearDown(); void CNFGSetup( const char * WindowName, int w, int h ); void CNFGSetupFullscreen( const char * WindowName, int screen_number ); void CNFGHandleInput(); diff --git a/WinDriver.c b/WinDriver.c @@ -123,12 +123,17 @@ LRESULT CALLBACK MyWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) switch(msg) { case WM_DESTROY: - PostQuitMessage(0); + CNFGTearDown(); return 0; } return DefWindowProc(hwnd, msg, wParam, lParam); } +void CNFGTearDown() +{ + PostQuitMessage(0); +} + //This was from the article, too... well, mostly. void CNFGSetup( const char * name_of_window, int width, int height ) { diff --git a/XDriver.c b/XDriver.c @@ -19,6 +19,7 @@ #include <stdlib.h> XWindowAttributes CNFGWinAtt; +XClassHint *CNFGClassHint; Display *CNFGDisplay; Window CNFGWindow; Pixmap CNFGPixmap; @@ -36,6 +37,20 @@ static void InternalLinkScreenAndGo( const char * WindowName ) { XGetWindowAttributes( CNFGDisplay, CNFGWindow, &CNFGWinAtt ); + XGetClassHint( CNFGDisplay, CNFGWindow, CNFGClassHint ); + if (!CNFGClassHint) { + CNFGClassHint = XAllocClassHint(); + if (CNFGClassHint) { + CNFGClassHint->res_name = "cnping"; + CNFGClassHint->res_class = "cnping"; + XSetClassHint( CNFGDisplay, CNFGWindow, CNFGClassHint ); + } else { + fprintf( stderr, "Failed to allocate XClassHint!\n" ); + } + } else { + fprintf( stderr, "Pre-existing XClassHint\n" ); + } + XSelectInput (CNFGDisplay, CNFGWindow, KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | ExposureMask | PointerMotionMask ); XSetStandardProperties( CNFGDisplay, CNFGWindow, WindowName, WindowName, None, NULL, 0, NULL ); @@ -112,9 +127,21 @@ void CNFGSetupFullscreen( const char * WindowName, int screen_no ) } +void CNFGTearDown() +{ + if ( CNFGClassHint ) XFree( CNFGClassHint ); + if ( CNFGGC ) XFreeGC( CNFGDisplay, CNFGGC ); + if ( CNFGWindowGC ) XFreeGC( CNFGDisplay, CNFGWindowGC ); + if ( CNFGDisplay ) XCloseDisplay( CNFGDisplay ); + CNFGDisplay = NULL; + CNFGWindowGC = CNFGGC = NULL; + CNFGClassHint = NULL; +} + void CNFGSetup( const char * WindowName, int w, int h ) { CNFGDisplay = XOpenDisplay(NULL); + atexit( CNFGTearDown ); XGetWindowAttributes( CNFGDisplay, RootWindow(CNFGDisplay, 0), &CNFGWinAtt ); int depth = CNFGWinAtt.depth; @@ -123,6 +150,10 @@ void CNFGSetup( const char * WindowName, int w, int h ) XFlush(CNFGDisplay); InternalLinkScreenAndGo( WindowName ); + + Atom WM_DELETE_WINDOW = XInternAtom( CNFGDisplay, "WM_DELETE_WINDOW", False ); + XSetWMProtocols( CNFGDisplay, CNFGWindow, &WM_DELETE_WINDOW, 1 ); + XSelectInput( CNFGDisplay, CNFGWindow, KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | ExposureMask | PointerMotionMask ); } void CNFGHandleInput() @@ -132,13 +163,9 @@ void CNFGHandleInput() int bKeyDirection = 1; int r; - while( (r=XCheckMaskEvent( CNFGDisplay, KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | ExposureMask | PointerMotionMask , &report )) ) + while( XPending( CNFGDisplay ) ) { -// XEvent nev; -// XPeekEvent(CNFGDisplay, &nev); - - //printf( "EVENT %d\n", report.type ); - //XMaskEvent(CNFGDisplay, KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | ExposureMask, &report); + r=XNextEvent( CNFGDisplay, &report ); bKeyDirection = 1; switch (report.type) @@ -167,6 +194,10 @@ void CNFGHandleInput() case MotionNotify: HandleMotion( report.xmotion.x, report.xmotion.y, ButtonsDown>>1 ); break; + case ClientMessage: + // Only subscribed to WM_DELETE_WINDOW, so just exit + exit( 0 ); + break; default: printf( "Event: %d\n", report.type ); }