<commit_before>/*************** <auto-copyright.pl BEGIN do not edit this line> **************
 *
 * VR Juggler is (C) Copyright 1998-2002 by Iowa State University
 *
 * Original Authors:
 *   <NAME>, <NAME>,
 *   <NAME>, <NAME>,
 *   <NAME>, <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * -----------------------------------------------------------------
 * File:        $RCSfile$
 * Date modified: $Date$
 * Version:      $Revision$
 * -----------------------------------------------------------------
 *
 *************** <auto-copyright.pl END do not edit this line> ***************/

#include <gadget/gadgetConfig.h>

#include <assert.h>
#include <sys/types.h>

#include <jccl/Config/ConfigChunk.h>
#include <gadget/Util/Debug.h>
#include <gadget/Devices/Keyboard/KeyboardWin32.h>

#ifndef GET_X_LPARAM
#define GET_X_LPARAM(lp)   ((int)(short)LOWORD(lp))
#endif
#ifndef GET_Y_LPARAM
#define GET_Y_LPARAM(lp)   ((int)(short)HIWORD(lp))
#endif

namespace gadget
{

bool KeyboardWin32::config( jccl::ConfigChunkPtr c )
{
   vprDEBUG_BEGIN(gadgetDBG_INPUT_MGR, vprDBG_STATE_LVL)
      << "vjKeyboardWin32::config " << std::endl << vprDEBUG_FLUSH;

   // Call base class config function first
   if (!(Input::config( c ) && Keyboard::config( c )))
     return false;

   int i;
   for (i =0; i < 256; i++)
      m_curKeys[i] = m_realkeys[i] = m_keys[i] = 0;
   m_curKeys[0] = m_realkeys[0] = m_keys[0] = 1;

   // Get size and position
   m_width = c->getProperty<int>( "width" );
   m_height = c->getProperty<int>( "height" );
   
   // default to something "sane" if too small
   if (m_width == 0) m_width = 400;
   if (m_height == 0) m_height = 400;

   m_x = c->getProperty<int>( "origin", 0 );
   m_y = c->getProperty<int>( "origin", 1 );

   // Get the lock information
   mLockToggleKey = c->getProperty<int>( "lock_key" );
   std::cout << "[][][][] Lock Key is " << (char)mLockToggleKey << " " << (int)mLockToggleKey << std::endl;
   bool start_locked = c->getProperty<bool>( "start_locked" );
   if (start_locked)
   {
      mLockState = Lock_LockKey;     // Initialize to the locked state
   }
   mLockState = Lock_LockKey;

   m_mouse_sensitivity = c->getProperty<float>( "msens" );

   // HACK: Use a default until config files have defaults
   if (0.0f == m_mouse_sensitivity)
   {
      m_mouse_sensitivity = 0.5f;
   }

   vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_STATE_LVL)
     << "Mouse Sensititivty: " << m_mouse_sensitivity << std::endl
     << vprDEBUG_FLUSH;

   mSleepTimeMS = c->getProperty<int>( "sleep_time" );

   // HACK: Use a default time until config files have defaults
   if (mSleepTimeMS == 0)
      mSleepTimeMS = 50;

   // Default to owning the window
   mWeOwnTheWindow = true;

   vprDEBUG_END(gadgetDBG_INPUT_MGR, vprDBG_STATE_LVL)
      << std::endl << vprDEBUG_FLUSH;

   return true;
}


void KeyboardWin32::controlLoop( void* devPtr )
{
   mControlLoopDone = false;

   // Open the window...
      // The Window has to be created in the same thread that
      // we run the message pump because all window messages
      // dispatched are dispatched to the thread that created
      // the window.  (And we want to receive the messages
      // in the spawned thread)
   this->createWindowWin32();

   // If we have initial locked, then we need to lock the system
   if (mLockState == Lock_LockKey)     // Means that we are in the initially locked state
   {
      vprDEBUG(gadgetDBG_INPUT_MGR,vprDBG_STATE_LVL)
        << "gadget::KeyboardWin32::controlLoop: Mouse set to initial lock. Locking it now.\n"
        << vprDEBUG_FLUSH;
      this->lockMouse();                // Lock the mouse
   }

   // When there are messages, process them all.  Otherwise,
   // sleep for a while...
   while (!mExitFlag)
   {
     this->sample();

     // user-specified sleep time.
     vpr::System::usleep( mSleepTimeMS * 1000 );
   }

   // clean up, delete the window!
   ::CloseWindow( m_hWnd ); // send a message to the window to close
   mControlLoopDone = true;
}

// do not call from any other thread than controlLoop()!!!!
// Peek and GetMessage retrieves messages for any window 
// that belongs to the calling thread...
void KeyboardWin32::handleEvents()
{
   MSG msg;
   bool have_events_to_check = false;
   if (mWeOwnTheWindow)
   {
      // block until message received.
      int retval = ::GetMessage( &msg, m_hWnd, 0, 0 );
      assert( retval != -1 && "invalid m_hWnd window handle or invalid lpMsg pointer" );
      if (retval == -1) return; // for opt mode...
      have_events_to_check = true;
   }
   // check for event, if none, then exit.
   else if (::PeekMessage( &msg, m_hWnd, 0, 0, PM_REMOVE ))
   {
      have_events_to_check = true;
   }
   else
   {
      have_events_to_check = false;
      return; // don't wait on the lock since there is nothing 
              // afterwards that will be called when this is false.
   }

// GUARD m_keys for duration of loop
// Doing it here gives makes sure that we process all events and don't get only part of them for an update
// In order to copy data over to the m_curKeys array
// Lock access to the m_keys array for the duration of this function
vpr::Guard<vpr::Mutex> guard( mKeysLock );      <commit_msg>Use vprASSERT instead of assert as suggested by patrick.<commit_after>/*************** <auto-copyright.pl BEGIN do not edit this line> **************
 *
 * VR Juggler is (C) Copyright 1998-2002 by Iowa State University
 *
 * Original Authors:
 *   <NAME>, <NAME>,
 *   <NAME>, <NAME>,
 *   <NAME>, <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * -----------------------------------------------------------------
 * File:        $RCSfile$
 * Date modified: $Date$
 * Version:      $Revision$
 * -----------------------------------------------------------------
 *
 *************** <auto-copyright.pl END do not edit this line> ***************/

#include <gadget/gadgetConfig.h>

#include <sys/types.h>

#include <jccl/Config/ConfigChunk.h>
#include <gadget/Util/Debug.h>
#include <gadget/Devices/Keyboard/KeyboardWin32.h>

#ifndef GET_X_LPARAM
#define GET_X_LPARAM(lp)   ((int)(short)LOWORD(lp))
#endif
#ifndef GET_Y_LPARAM
#define GET_Y_LPARAM(lp)   ((int)(short)HIWORD(lp))
#endif

namespace gadget
{

bool KeyboardWin32::config( jccl::ConfigChunkPtr c )
{
   vprDEBUG_BEGIN(gadgetDBG_INPUT_MGR, vprDBG_STATE_LVL)
      << "vjKeyboardWin32::config " << std::endl << vprDEBUG_FLUSH;

   // Call base class config function first
   if (!(Input::config( c ) && Keyboard::config( c )))
     return false;

   int i;
   for (i =0; i < 256; i++)
      m_curKeys[i] = m_realkeys[i] = m_keys[i] = 0;
   m_curKeys[0] = m_realkeys[0] = m_keys[0] = 1;

   // Get size and position
   m_width = c->getProperty<int>( "width" );
   m_height = c->getProperty<int>( "height" );
   
   // default to something "sane" if too small
   if (m_width == 0) m_width = 400;
   if (m_height == 0) m_height = 400;

   m_x = c->getProperty<int>( "origin", 0 );
   m_y = c->getProperty<int>( "origin", 1 );

   // Get the lock information
   mLockToggleKey = c->getProperty<int>( "lock_key" );
   std::cout << "[][][][] Lock Key is " << (char)mLockToggleKey << " " << (int)mLockToggleKey << std::endl;
   bool start_locked = c->getProperty<bool>( "start_locked" );
   if (start_locked)
   {
      mLockState = Lock_LockKey;     // Initialize to the locked state
   }
   mLockState = Lock_LockKey;

   m_mouse_sensitivity = c->getProperty<float>( "msens" );

   // HACK: Use a default until config files have defaults
   if (0.0f == m_mouse_sensitivity)
   {
      m_mouse_sensitivity = 0.5f;
   }

   vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_STATE_LVL)
     << "Mouse Sensititivty: " << m_mouse_sensitivity << std::endl
     << vprDEBUG_FLUSH;

   mSleepTimeMS = c->getProperty<int>( "sleep_time" );

   // HACK: Use a default time until config files have defaults
   if (mSleepTimeMS == 0)
      mSleepTimeMS = 50;

   // Default to owning the window
   mWeOwnTheWindow = true;

   vprDEBUG_END(gadgetDBG_INPUT_MGR, vprDBG_STATE_LVL)
      << std::endl << vprDEBUG_FLUSH;

   return true;
}


void KeyboardWin32::controlLoop( void* devPtr )
{
   mControlLoopDone = false;

   // Open the window...
      // The Window has to be created in the same thread that
      // we run the message pump because all window messages
      // dispatched are dispatched to the thread that created
      // the window.  (And we want to receive the messages
      // in the spawned thread)
   this->createWindowWin32();

   // If we have initial locked, then we need to lock the system
   if (mLockState == Lock_LockKey)     // Means that we are in the initially locked state
   {
      vprDEBUG(gadgetDBG_INPUT_MGR,vprDBG_STATE_LVL)
        << "gadget::KeyboardWin32::controlLoop: Mouse set to initial lock. Locking it now.\n"
        << vprDEBUG_FLUSH;
      this->lockMouse();                // Lock the mouse
   }

   // When there are messages, process them all.  Otherwise,
   // sleep for a while...
   while (!mExitFlag)
   {
     this->sample();

     // user-specified sleep time.
     vpr::System::usleep( mSleepTimeMS * 1000 );
   }

   // clean up, delete the window!
   ::CloseWindow( m_hWnd ); // send a message to the window to close
   mControlLoopDone = true;
}

// do not call from any other thread than controlLoop()!!!!
// Peek and GetMessage retrieves messages for any window 
// that belongs to the calling thread...
void KeyboardWin32::handleEvents()
{
   MSG msg;
   bool have_events_to_check = false;
   if (mWeOwnTheWindow)
   {
      // block until message received.
      int retval = ::GetMessage( &msg, m_hWnd, 0, 0 );
      vprASSERT( retval != -1 && "invalid m_hWnd window handle or invalid lpMsg pointer" );
      if (retval == -1) return; // for opt mode...
      have_events_to_check = true;
   }
   // check for event, if none, then exit.
   else if (::PeekMessage( &msg, m_hWnd, 0, 0, PM_REMOVE ))
   {
      have_events_to_check = true;
   }
   else
   {
      have_events_to_check = false;
      return; // don't wait on the lock since there is nothing 
              // afterwards that will be called when this is false.
   }

// GUARD m_keys for duration of loop
// Doing it here gives makes sure that we process all events and don't get only part of them for an update
// In order to copy data over to the m_curKeys array
// Lock access to the m_keys array for the duration of this function
vpr::Guard<vpr::Mutex> guard( mKeysLock );      
{'fraction_non_alphanumeric': 0.14070139942674084, 'fraction_numerical': 0.012308211094250548, 'mean_word_length': 2.8354348528936306, 'pattern_counts': {'":': 0, '<': 114, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2494182', 'n_tokens_mistral': 3862, 'n_tokens_neox': 3719, 'n_words': 1248}