All pastes #517173 Raw Edit

Miscellany

public text v1 · immutable
#517173 ·published 2007-05-28 23:58 UTC
rendered paste body
static int key_dinput_init(void)
{
   HRESULT hr;
   DIPROPDWORD property_buf_size =
   {
      /* the header */
      {
	  sizeof(DIPROPDWORD),   // diph.dwSize
	  sizeof(DIPROPHEADER),  // diph.dwHeaderSize
	  0,                     // diph.dwObj
	  DIPH_DEVICE,           // diph.dwHow
      },

      /* the data */
      DINPUT_BUFFERSIZE,         // dwData
   };

   /* Get DirectInput interface */
   hr = DirectInputCreate(allegro_inst, DIRECTINPUT_VERSION, &key_dinput, NULL);
   if (FAILED(hr)) {
      TRACE("DirectInputCreate failed.\n");
      goto Error;
   }

   /* Create the keyboard device */
   hr = IDirectInput_CreateDevice(key_dinput, &GUID_SysKeyboard, &key_dinput_device, NULL);
   if (FAILED(hr)) {
      TRACE("IDirectInput_CreateDevice failed.\n");
      goto Error;
   }

   /* Set data format */
   hr = IDirectInputDevice_SetDataFormat(key_dinput_device, &c_dfDIKeyboard);
   if (FAILED(hr)) {
      TRACE("IDirectInputDevice_SetDataFormat failed.\n");
      goto Error;
   }

   /* Set buffer size */
   hr = IDirectInputDevice_SetProperty(key_dinput_device, DIPROP_BUFFERSIZE, &property_buf_size.diph);
   if (FAILED(hr)) {
      TRACE("IDirectInputDevice_SetProperty failed.\n");
      goto Error;
   }

   /* Enable event notification */
   key_input_event = CreateEvent(NULL, FALSE, FALSE, NULL);
   hr = IDirectInputDevice_SetEventNotification(key_dinput_device, key_input_event);
   if (FAILED(hr)) {
      TRACE("IDirectInputDevice_SetEventNotification failed.\n");
      goto Error;
   }

   /* Set up the timer for autorepeat emulation */
   key_autorepeat_timer = CreateWaitableTimer(NULL, FALSE, NULL);
   if (!key_autorepeat_timer) {
      TRACE("CreateWaitableTimer failed.\n");
      goto Error;
   }

   /* Register event handlers */
   if (_win_input_register_event(key_input_event, key_dinput_handle) != 0 ||
       _win_input_register_event(key_autorepeat_timer, key_dinput_repeat) != 0) {
      TRACE("Registering dinput keyboard event handlers failed.\n");
      goto Error;
   }

   return 0;

 Error:
   key_dinput_exit();
   return -1;
}