From 54a83fb3523e2fb8a9e226fce41133432cbce463 Mon Sep 17 00:00:00 2001
From: Ingo Ruhnke <grumbel@gmx.de>
Date: Tue, 25 Jan 2011 01:52:33 +0100
Subject: [PATCH] Added mutex lock for UInput

---
 src/default_message_processor.cpp |  2 ++
 src/ui_event.cpp                  | 16 ++++++++++++++--
 src/ui_event.hpp                  |  4 ++++
 src/uinput.cpp                    | 14 ++------------
 src/uinput.hpp                    |  7 ++++---
 5 files changed, 26 insertions(+), 17 deletions(-)

diff --git a/src/default_message_processor.cpp b/src/default_message_processor.cpp
index 066454b..81c2f09 100644
--- a/src/default_message_processor.cpp
+++ b/src/default_message_processor.cpp
@@ -40,6 +40,8 @@ DefaultMessageProcessor::send(const XboxGenericMsg& msg_in, int msec_delta)
 {
   if (!m_config->empty())
   {
+    boost::mutex::scoped_lock lock(m_uinput.get_mutex());
+
     XboxGenericMsg msg = msg_in; 
 
     // handling switching of configurations
diff --git a/src/ui_event.cpp b/src/ui_event.cpp
index 0bff185..f60f327 100644
--- a/src/ui_event.cpp
+++ b/src/ui_event.cpp
@@ -20,6 +20,18 @@
 
 #include "uinput.hpp"
 
+bool
+UIEvent::is_mouse_button(int ev_code)
+{
+  return  (ev_code >= BTN_MOUSE && ev_code <= BTN_TASK);
+}
+
+bool
+UIEvent::is_keyboard_button(int ev_code)
+{
+  return (ev_code < 256);
+}
+
 UIEvent
 UIEvent::create(int device_id, int type, int code) 
 {
@@ -96,11 +108,11 @@ UIEvent::resolve_device_id(int slot, bool extra_devices)
     switch(type)
     {
       case EV_KEY:
-        if (uInput::is_mouse_button(code))
+        if (is_mouse_button(code))
         {
           m_device_id = DEVICEID_MOUSE;
         }
-        else if (uInput::is_keyboard_button(code))
+        else if (is_keyboard_button(code))
         {
           m_device_id = DEVICEID_KEYBOARD;
         }
diff --git a/src/ui_event.hpp b/src/ui_event.hpp
index 35e3189..da5d477 100644
--- a/src/ui_event.hpp
+++ b/src/ui_event.hpp
@@ -40,6 +40,10 @@ public:
   static UIEvent create(int device_id, int type, int code);
   static UIEvent invalid();
 
+public:
+  static bool is_mouse_button(int ev_code);
+  static bool is_keyboard_button(int ev_code);
+
 public:
   void resolve_device_id(int slot, bool extra_devices);
   bool is_valid() const;
diff --git a/src/uinput.cpp b/src/uinput.cpp
index 2c7409a..3623455 100644
--- a/src/uinput.cpp
+++ b/src/uinput.cpp
@@ -20,20 +20,10 @@
 
 #include "log.hpp"
 
-bool
-uInput::is_mouse_button(int ev_code)
-{
-  return  (ev_code >= BTN_MOUSE && ev_code <= BTN_TASK);
-}
-
-bool
-uInput::is_keyboard_button(int ev_code)
-{
-  return (ev_code < 256);
-}
 uInput::uInput() :
   uinput_devs(),
-  rel_repeat_lst()
+  rel_repeat_lst(),
+  m_mutex()
 {
 #ifdef FIXME
   if (cfg.force_feedback)
diff --git a/src/uinput.hpp b/src/uinput.hpp
index d57c1d8..aae045b 100644
--- a/src/uinput.hpp
+++ b/src/uinput.hpp
@@ -24,6 +24,7 @@
 #include <memory>
 #include <stdexcept>
 #include <boost/shared_ptr.hpp>
+#include <boost/thread/mutex.hpp>
 
 #include "axis_event.hpp"
 #include "button_event.hpp"
@@ -53,9 +54,7 @@ private:
 
   std::map<UIEvent, RelRepeat> rel_repeat_lst;
 
-public:
-  static bool is_mouse_button(int ev_code);
-  static bool is_keyboard_button(int ev_code);
+  boost::mutex m_mutex;
 
 public:
   uInput();
@@ -90,6 +89,8 @@ public:
 
   LinuxUinput* get_force_feedback_uinput() const;
 
+  boost::mutex& get_mutex() { return m_mutex; }
+
 private:
   /** create a LinuxUinput with the given device_id, if some already
       exist return a pointer to it */