⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comtest.cpp

📁 Synaptics触摸板应用开发包(SDK)
💻 CPP
字号:
// Console based test program for excercising the COM API.

#include <stdio.h>

// All programs using the Synaptics COM SDK include SynKit.h
#include "SynKit.h"

// A help function for drawing the SDK logo on a cPad display.
void DrawLogo(ISynDisplay *pDisplay, int iInstance = 1)
{
  if (!pDisplay)
    return;

  HDC hdc = 0;
  HRESULT result = pDisplay->GetDC(&hdc);

  RECT rect;

  rect.top = 0; rect.bottom = 160; rect.left = 0; rect.right = 240;
  FillRect(hdc, &rect, (HBRUSH) GetStockObject(WHITE_BRUSH));

  rect.top = 0; rect.bottom = 20; rect.left = 0; rect.right = 240;
  FillRect(hdc, &rect, (HBRUSH) GetStockObject(BLACK_BRUSH));

  rect.top = 140; rect.bottom = 160; rect.left = 0; rect.right = 240;
  FillRect(hdc, &rect, (HBRUSH) GetStockObject(BLACK_BRUSH));

  rect.top = 0; rect.bottom = 160; rect.left = 0; rect.right = 20;
  FillRect(hdc, &rect, (HBRUSH) GetStockObject(BLACK_BRUSH));

  rect.top = 0; rect.bottom = 160; rect.left = 220; rect.right = 240;
  FillRect(hdc, &rect, (HBRUSH) GetStockObject(BLACK_BRUSH));

  rect.top = 40; rect.bottom = 120; rect.left = 40; rect.right = 200;
  char buf[256];
  sprintf(buf, "Synaptics COMSDK\nTest Instance: %d", iInstance);
  DrawText(hdc, buf, -1, &rect, 
    DT_CENTER | DT_VCENTER);

  rect.top = 100; rect.bottom = 120; rect.left = 40; rect.right = 200;
  DrawText(hdc, "Tap on the cPad to Exit.", -1, &rect, 
    DT_CENTER | DT_VCENTER);
}

int main(int argc, char *argv[])
{
  ISynAPI *pAPI = 0;
  HRESULT result = SynCreateAPI(&pAPI);
  if (result)
  {
    printf("Cannot obtain an API object, error: %.8x.\n", result);
    exit(-1);
  }

  // Test some API properties.
  long lValue = 0;
  result = pAPI->GetProperty(SP_Version, &lValue);

  TCHAR tBuf[256];
  long lBufLen = sizeof(tBuf);
  result = pAPI->GetStringProperty(SP_VersionString, tBuf, &lBufLen);

  printf("API version: %.8x.\n", lValue);
  printf("Synaptics Driver version: %s.\n", tBuf);

  // Find a device, preferentially a TouchPad or Styk.
  ISynDevice *pDevice = 0;
  long lHandle = -1;
  if ((pAPI->FindDevice(SE_ConnectionAny, SE_DeviceTouchPad, &lHandle) &&
       pAPI->FindDevice(SE_ConnectionAny, SE_DeviceStyk, &lHandle) &&
       pAPI->FindDevice(SE_ConnectionAny, SE_DeviceAny, &lHandle)) || 
      pAPI->CreateDevice(lHandle, &pDevice))
  {
    printf("Unable to find a Synaptics Device.\n");
    exit(-1);
  }

  // Test some device properties.
  result = pDevice->GetProperty(SP_Handle, &lValue);
  result = pDevice->GetProperty(SP_DeviceType, &lValue);
  printf("Found device type: %d,", lValue);
  result = pDevice->GetProperty(SP_ConnectionType, &lValue);
  printf("Connection type: %d.\n", lValue);


  lBufLen = sizeof(tBuf);
  result = pDevice->GetStringProperty(SP_ModelString, tBuf, &lBufLen);
  printf("Device Model ID: %s.\n", tBuf);
  lBufLen = sizeof(tBuf);
  result = pDevice->GetStringProperty(SP_PnPID, tBuf, &lBufLen);
  printf("Device PnP ID: %s.\n", tBuf);
  lBufLen = sizeof(tBuf);
  result = pDevice->GetStringProperty(SP_ShortName, tBuf, &lBufLen);
  printf("Device Config Key: %s.\n", tBuf);

  result = pDevice->GetProperty(SP_DisplayFlags, &lValue);
  if (!(lValue & SF_DisplayPresent))
  {
    printf("Synaptics cPad display not present.\n");
  }

  // Create a unique global named event.
  HANDLE hEvent = 0;
  for (int iInstance = 0; ; iInstance++) 
  {
    char EventName[32];
    sprintf(EventName, "SynCom%d", iInstance);
    hEvent = CreateEvent(0, 0, 0, EventName);

    if (GetLastError() != ERROR_ALREADY_EXISTS)
      break;
  }

  // Find a cPad display if one exists.
  ISynDisplay *pDisplay = 0;
  pDevice->CreateDisplay(&pDisplay);

  if (pDisplay) // If a display was found.
  {
    // Acquire the cPad display, has the side effect of turning on 
    // the backlight.
    result = pDisplay->Acquire(SE_AcquireCooperative); 
    if (result)
    {
      printf("Unable to acquire the cPad.\n");
      exit(-1);
    }

    // Test the backlight.
    result = pDisplay->GetProperty(SP_BackLightState, &lValue);
    printf("cPad backlight state: %d.\n", lValue);
    Sleep(500);

    printf("Turning cPad backlight off.\n");
    result = pDisplay->SetProperty(SP_BackLightState, 0);
    Sleep(500);
    printf("Turning cPad backlight on.\n");
    result = pDisplay->SetProperty(SP_BackLightState, 1); 
  
    // Draw the SDK logo.
    printf("Drawing SDK logo on the cPad display.\n");
    DrawLogo(pDisplay, iInstance);
    pDisplay->FlushDC(SE_FlushSynchronous);
  }

  // Setup notification of API, device, and display events.
  pAPI->SetEventNotification(hEvent);
  pDevice->SetEventNotification(hEvent);
  if (pDisplay)
    pDisplay->SetEventNotification(hEvent);


  printf("Entering packet processing loop.\n");
  printf("Tap on the device or press a device button to exit the program.\n");

  // Loop until a device packet that contains a button or tap event occurs.
  SynPacket Packet;
  for (; ; )
  {
    WaitForSingleObject(hEvent, INFINITE);
    
    for ( ; ; )
    {
      long lParameter = 0;
      result = pAPI->GetEventParameter(&lParameter);
      if (result != S_OK)
        break;

      printf("API event parameter: %d.\n", lParameter);
    }

    for (; pDisplay; )
    {
      long lParameter = 0;
      result = pDisplay->GetEventParameter(&lParameter);
      if (result != S_OK)
        break;

      printf("Display event parameter: %d.\n", lParameter);

      if (lParameter == SE_AcquisitionGained)
      {
        DrawLogo(pDisplay, iInstance);
        pDisplay->FlushDC(SE_FlushSynchronous);
        // Wait for taps or buttons on the terminating application 
        // to complete before restoring notification.
        Sleep(100);
        pDevice->SetEventNotification(hEvent);
      }
      else
      {
        // Another application has acquired the display, turn off notification 
        // so we don't terminate due to a tap on that app.
        pDevice->SetEventNotification(0);
      }
    }

    for ( ; ; )
    {
      result = pDevice->LoadPacket(Packet);
      if (result == SYNE_FAIL)
        break;

      printf("Sequence number: %d, Finger state: %.8x.\n", 
        Packet.SequenceNumber(), Packet.FingerState());


      if ((Packet.FingerState() & SF_FingerTap) || Packet.ButtonState())
      {
        exit(0);
      }
    }
  }

  if (pDisplay)
  {
    pDisplay->Unacquire(); 
    pDisplay->Release();
  }

  pDevice->Release();
  pAPI->Release();

  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -