changeset 6:1b73e60d53b4

Now with keyboard support
author Eris Caffee <discordia@eldalin.com>
date Tue, 17 Apr 2012 01:56:49 -0500
parents 008e28de2870
children 713e3c5db1e1
files CMakeLists.txt include/App.h src/App.cpp
diffstat 3 files changed, 48 insertions(+), 20 deletions(-) [+]
line diff
     1.1 --- a/CMakeLists.txt	Mon Apr 16 10:32:59 2012 -0500
     1.2 +++ b/CMakeLists.txt	Tue Apr 17 01:56:49 2012 -0500
     1.3 @@ -113,11 +113,11 @@
     1.4  ################################################################################
     1.5  # When using GCC turn on lots of warnings.
     1.6  # Some other options to consider:
     1.7 -#   C++  -Weffc++
     1.8 +#   C++  -Weffc++ -std=c++0x or -std=gnu++0x
     1.9  #   C    -std=gnu99 -std=c99
    1.10  
    1.11  if (CMAKE_COMPILER_IS_GNUCXX)
    1.12 -  add_definitions(-pedantic -Wall)
    1.13 +  add_definitions(-pedantic -Wall -std=gnu++0x -Weffc++)
    1.14  endif ()
    1.15  
    1.16  
     2.1 --- a/include/App.h	Mon Apr 16 10:32:59 2012 -0500
     2.2 +++ b/include/App.h	Tue Apr 17 01:56:49 2012 -0500
     2.3 @@ -77,6 +77,7 @@
     2.4  
     2.5     Display * dpy;
     2.6     WindowMap windows;
     2.7 +   long event_mask;
     2.8     Atom wm_delete_window;  // Need this in contructor and run().
     2.9  
    2.10     };
     3.1 --- a/src/App.cpp	Mon Apr 16 10:32:59 2012 -0500
     3.2 +++ b/src/App.cpp	Tue Apr 17 01:56:49 2012 -0500
     3.3 @@ -5,6 +5,7 @@
     3.4  #include <typeinfo>
     3.5  #include <X11/Xutil.h>
     3.6  
     3.7 +
     3.8  ////////////////////////////////////////////////////////////////////////////////
     3.9  App::App(const std::string & appname)
    3.10     {
    3.11 @@ -86,7 +87,8 @@
    3.12  
    3.13     // Select the event types we want to receive.
    3.14     //Other interesting events include KeyReleaseMask and ButtonReleaseMask
    3.15 -   XSelectInput(dpy, wd->win, ExposureMask | KeyPressMask | ButtonPressMask | StructureNotifyMask);
    3.16 +   event_mask = ExposureMask | KeyPressMask | ButtonPressMask | StructureNotifyMask ;
    3.17 +   XSelectInput(dpy, wd->win, event_mask);
    3.18  
    3.19  
    3.20  
    3.21 @@ -182,29 +184,54 @@
    3.22  void App::run(void)
    3.23     {
    3.24     XEvent e;
    3.25 +   KeySym k;
    3.26 +   char key_buffer[8];
    3.27 +   key_buffer[7] = 0;
    3.28 +   XComposeStatus compose_status;
    3.29 +
    3.30     int done = 0;
    3.31 +
    3.32     while (!done)
    3.33        {
    3.34 -      XNextEvent(dpy, &e);
    3.35 -      switch (e.type)
    3.36 +      while (XCheckIfEvent(dpy, &e, 
    3.37 +      			   // *sniff*  My very first lambda function in C++
    3.38 +      			   [&](Display * d, XEvent * e, XPointer a)->Bool { return True; }, 
    3.39 +      			   NULL))
    3.40  	 {
    3.41 -	 case Expose:
    3.42 -	    // We'll only redraw the entire window at a time, so unless this is 
    3.43 -	    // the last contiguous expose, don't draw the window.
    3.44 -	    if (e.xexpose.count != 0)
    3.45 +	 switch (e.type)
    3.46 +	    {
    3.47 +	    case Expose:
    3.48 +	       // We'll only redraw the entire window at a time, so unless this is 
    3.49 +	       // the last contiguous expose, don't draw the window.
    3.50 +	       if (e.xexpose.count != 0)
    3.51 +		  break;
    3.52 +	       // Draw the window contents here
    3.53  	       break;
    3.54 -	    // Draw the window contents here
    3.55 -	    break;
    3.56 -	 case ClientMessage:
    3.57 -	    if ((Atom)e.xclient.data.l[0] == wm_delete_window)
    3.58 -	       done = 1;
    3.59 -	    break;
    3.60 -	 case ButtonPress:
    3.61 -	 case KeyPress:
    3.62 -	    done = 1;
    3.63 -	 default:
    3.64 -	    break;
    3.65 +	    case ClientMessage:
    3.66 +	       if ((Atom)e.xclient.data.l[0] == wm_delete_window)
    3.67 +		  done = 1;
    3.68 +	       break;
    3.69 +	    case ButtonPress:
    3.70 +	    case KeyPress:
    3.71 +	       XLookupString(&(e.xkey), key_buffer, 7, &k, &compose_status);
    3.72 +	       switch (k)
    3.73 +		  {
    3.74 +		  case XK_Escape:
    3.75 +		  case XK_q:
    3.76 +		     std::cout << "Goodbye!" << std::endl;
    3.77 +		     done = 1;
    3.78 +		     break;
    3.79 +		  default:
    3.80 +		     std::cout << "You typed " << key_buffer << std::endl;
    3.81 +		     break;
    3.82 +		  }
    3.83 +	    default:
    3.84 +	       break;
    3.85 +	    }
    3.86  	 }
    3.87 +
    3.88 +      // Do idle time things here
    3.89 +
    3.90        }
    3.91     }
    3.92