]> git.tdb.fi Git - libs/gui.git/blobdiff - source/input/keyboard.cpp
Discard the EventSource abstraction
[libs/gui.git] / source / input / keyboard.cpp
index 02ff12eb2c0c2e7d02f739e776ddc6433bc9c815..ea66cf28ab11aa9d74f55680293336f4e5efae83 100644 (file)
@@ -2,8 +2,12 @@
 #include <windows.h>
 #else
 #include <X11/Xlib.h>
+#include <X11/Xutil.h>
 #endif
 #include <msp/graphics/display.h>
+#include <msp/graphics/window.h>
+#include <msp/graphics/display_priv.h>
+#include <msp/graphics/window_priv.h>
 #include <msp/strings/format.h>
 #include "keyboard.h"
 #include "keys.h"
 namespace Msp {
 namespace Input {
 
-Keyboard::Keyboard(Graphics::EventSource &s):
-       source(s)
+Keyboard::Keyboard(Graphics::Window &w):
+       window(w)
 {
        name = "Keyboard";
 
        buttons.resize(N_KEYS_, false);
 
-       source.signal_key_press.connect(sigc::mem_fun(this, &Keyboard::key_press));
-       source.signal_key_release.connect(sigc::mem_fun(this, &Keyboard::key_release));
+       window.signal_input_event.connect(sigc::mem_fun(this, &Keyboard::input_event));
 }
 
 std::string Keyboard::get_button_name(unsigned btn) const
@@ -42,14 +45,39 @@ std::string Keyboard::get_button_name(unsigned btn) const
 #endif
 }
 
-void Keyboard::key_press(unsigned key, unsigned, unsigned)
+void Keyboard::input_event(const Graphics::Window::Event &event)
 {
-       set_button_state(key_from_sys(key), true, true);
-}
-
-void Keyboard::key_release(unsigned key, unsigned)
-{
-       set_button_state(key_from_sys(key), false, true);
+#ifdef WIN32
+       switch(event.msg)
+       {
+       case WM_KEYDOWN:
+       case WM_KEYUP:
+               set_button_state(key_from_sys(event.wparam), event.msg==WM_KEYDOWN, true);
+               break;
+       case WM_CHAR:
+               signal_character.emit(event.wparam);
+               break;
+       }
+#else
+       switch(event.xevent.type)
+       {
+       case KeyPress:
+       case KeyRelease:
+               {
+                       KeySym keysym = XKeycodeToKeysym(window.get_display().get_private().display, event.xevent.xkey.keycode, 0);
+                       if(keysym!=NoSymbol)
+                               set_button_state(key_from_sys(keysym), event.xevent.type==KeyPress, true);
+                       if(event.xevent.type==KeyPress)
+                       {
+                               char ch;
+                               if(XLookupString(const_cast<XKeyEvent *>(&event.xevent.xkey), &ch, 1, 0, 0))
+                                       // XLookupString always returns Latin-1
+                                       signal_character.emit(static_cast<unsigned char>(ch));
+                       }
+               }
+               break;
+       }
+#endif
 }
 
 } // namespace Input