Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Simple GEIS demo

By: a guest on Apr 11th, 2011  |  syntax: C++  |  size: 2.92 KB  |  views: 474  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <geis/geis.h>
  2. #include <iostream>
  3. #include <stdexcept>
  4. #include <X11/Xutil.h>
  5.  
  6. using namespace std;
  7.  
  8.  
  9.  
  10. class GeisTest
  11. {
  12. public:
  13.  
  14.   // Sets up a GEIS instance
  15.   GeisTest()
  16.   {
  17.     GeisXcbWinInfo xcbWinInfo;
  18.     xcbWinInfo.display_name = NULL;
  19.     xcbWinInfo.screenp      = NULL;
  20.     xcbWinInfo.window_id    = XRootWindow(XOpenDisplay(NULL), 0);
  21.  
  22.     GeisWinInfo  winInfo;
  23.     winInfo.win_type = GEIS_XCB_FULL_WINDOW;
  24.     winInfo.win_info = &xcbWinInfo;
  25.  
  26.     if (geis_init(&winInfo, &geisInstance) != GEIS_STATUS_SUCCESS)
  27.     {
  28.       throw runtime_error("error initializing geis");
  29.     }
  30.  
  31.     gestureFuncs.added   = GeisTest::gestureAdded;
  32.     gestureFuncs.removed = GeisTest::gestureRemoved;
  33.     gestureFuncs.start   = GeisTest::gestureStart;
  34.     gestureFuncs.update  = GeisTest::gestureUpdate;
  35.     gestureFuncs.finish  = GeisTest::gestureFinish;
  36.  
  37.     if (geis_subscribe(geisInstance,
  38.                        GEIS_ALL_INPUT_DEVICES,
  39.                        GEIS_ALL_GESTURES,
  40.                        &gestureFuncs,
  41.                        this) != GEIS_STATUS_SUCCESS)
  42.     {
  43.       throw runtime_error("error creating geis subscription");
  44.     }
  45.   }
  46.  
  47.   // Tears down a GEIS instance
  48.   ~GeisTest()
  49.   {
  50.       geis_finish(geisInstance);
  51.   }
  52.  
  53.   // an example of how to integrate GEIS into an event loop
  54.   int
  55.   run()
  56.   {
  57.       int fd = -1;
  58.       geis_configuration_get_value(geisInstance, GEIS_CONFIG_UNIX_FD, &fd);
  59.  
  60.       for(;;)
  61.       {
  62.         fd_set read_fds;
  63.         FD_ZERO(&read_fds);
  64.         FD_SET(0, &read_fds);
  65.         FD_SET(fd, &read_fds);
  66.         int sstat = select(fd+1, &read_fds, NULL, NULL, NULL);
  67.  
  68.         if (sstat < 0)
  69.           throw runtime_error("error in select");
  70.  
  71.         if (FD_ISSET(0, &read_fds))
  72.           break;
  73.  
  74.         if (FD_ISSET(fd, &read_fds))
  75.           geis_event_dispatch(geisInstance);
  76.       }
  77.       return 0;
  78.   }
  79.  
  80.   static void
  81.   gestureAdded(void* cookie, GeisGestureType type,
  82.                GeisGestureId id, GeisSize numAttrs, GeisGestureAttr* attrs)
  83.   {
  84.     cout << "Gesture added\n";
  85.   }
  86.  
  87.   static void
  88.   gestureRemoved(void* cookie, GeisGestureType type,
  89.           GeisGestureId id, GeisSize numAttrs, GeisGestureAttr* attrs)
  90.   {
  91.       cout << "Gesture removed\n";
  92.   }
  93.  
  94.   static void
  95.   gestureStart(void* cookie, GeisGestureType type,
  96.           GeisGestureId id, GeisSize numAttrs, GeisGestureAttr* attrs)
  97.   {
  98.     cout << "Gesture start\n";
  99.   }
  100.  
  101.   static void
  102.   gestureUpdate(void* cookie, GeisGestureType type,
  103.           GeisGestureId id, GeisSize numAttrs, GeisGestureAttr* attrs)
  104.   {
  105.     cout << "Gesture update\n";
  106.   }
  107.  
  108.   static void
  109.   gestureFinish(void* cookie, GeisGestureType type,
  110.           GeisGestureId id, GeisSize numAttrs, GeisGestureAttr* attrs)
  111.   {
  112.     cout << "Gesture finish\n";
  113.   }
  114.  
  115. private:
  116.   GeisInstance     geisInstance;
  117.   GeisGestureFuncs gestureFuncs;
  118. };
  119.  
  120.  
  121. int
  122. main(int, char*[])
  123. {
  124.   try
  125.   {
  126.     return GeisTest().run();
  127.   }
  128.   catch (exception& ex)
  129.   {
  130.     cerr << ex.what() << "\n";
  131.     return 1;
  132.   }
  133. }