~chasedouglas/frame/ubuntu-upstream-xi

« back to all changes in this revision

Viewing changes to test/x11/fixture.cpp

  • Committer: Chase Douglas
  • Date: 2011-12-09 01:36:45 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: chase.douglas@ubuntu.com-20111209013645-n24l4myiumblzsfu
* New upstream release.
  - Version 2 adds a new API built on top of XInput multitouch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 *
 
3
 * utouch-frame - Touch Frame Library
 
4
 *
 
5
 * Copyright (C) 2011 Canonical Ltd.
 
6
 *
 
7
 * This program is free software: you can redistribute it and/or modify it
 
8
 * under the terms of the GNU General Public License as published by the
 
9
 * Free Software Foundation, either version 3 of the License, or (at your
 
10
 * option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful, but
 
13
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License along
 
18
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 *
 
20
 ****************************************************************************/
 
21
 
 
22
#include "x11/fixture.h"
 
23
 
 
24
#include <X11/Xlib.h>
 
25
#include <X11/extensions/XInput2.h>
 
26
 
 
27
#include "utouch/frame.h"
 
28
#include "utouch/frame_x11.h"
 
29
 
 
30
void utouch::frame::x11::testing::Test::SetUp() {
 
31
  ASSERT_NO_FATAL_FAILURE(xorg::testing::Test::SetUp());
 
32
 
 
33
  Window root = DefaultRootWindow(Display());
 
34
 
 
35
  XIEventMask mask;
 
36
  mask.mask_len = XIMaskLen(XI_LASTEVENT);
 
37
  mask.mask = reinterpret_cast<unsigned char*>(calloc(mask.mask_len,
 
38
                                                      sizeof(char)));
 
39
 
 
40
  mask.deviceid = XIAllDevices;
 
41
  XISetMask(mask.mask, XI_HierarchyChanged);
 
42
  ASSERT_EQ(Success, XISelectEvents(Display(), root, &mask, 1));
 
43
  XIClearMask(mask.mask, XI_HierarchyChanged);
 
44
 
 
45
 
 
46
  mask.deviceid = XIAllMasterDevices;
 
47
  XISetMask(mask.mask, XI_TouchBegin);
 
48
  XISetMask(mask.mask, XI_TouchUpdate);
 
49
  XISetMask(mask.mask, XI_TouchUpdateUnowned);
 
50
  XISetMask(mask.mask, XI_TouchEnd);
 
51
  XISetMask(mask.mask, XI_TouchOwnership);
 
52
 
 
53
  XIGrabModifiers mods = { XIAnyModifier, 0 };
 
54
 
 
55
  ASSERT_EQ(Success, XIGrabTouchBegin(Display(), XIAllMasterDevices, root, 0,
 
56
                                      &mask, 1, &mods));
 
57
 
 
58
  free(mask.mask);
 
59
 
 
60
  UFStatus status = frame_x11_new(Display(), &handle_);
 
61
  ASSERT_EQ(UFStatusSuccess, status);
 
62
  ASSERT_TRUE(handle_ != NULL);
 
63
}
 
64
 
 
65
void utouch::frame::x11::testing::Test::PumpEvents(uint64_t timeout) {
 
66
  fd_set set;
 
67
  FD_ZERO(&set);
 
68
 
 
69
  int display_fd = ConnectionNumber(Display());
 
70
  int frame_fd = frame_get_fd(handle_);
 
71
  int nfds = display_fd > frame_fd ? display_fd + 1: frame_fd + 1;
 
72
 
 
73
  XSync(Display(), 0);
 
74
 
 
75
  while (true) {
 
76
    if (!XPending(Display())) {
 
77
      FD_SET(display_fd, &set);
 
78
      FD_SET(frame_fd, &set);
 
79
 
 
80
      struct timeval timeval = { timeout / 1000, timeout % 1000 };
 
81
 
 
82
      int ret;
 
83
      if (timeout)
 
84
        ret = select(nfds, &set, NULL, NULL, &timeval);
 
85
      else
 
86
        ret = select(nfds, &set, NULL, NULL, NULL);
 
87
 
 
88
      ASSERT_GE(ret, 0) << "Failed to select on FDs";
 
89
 
 
90
      if (ret == 0)
 
91
        return;
 
92
    }
 
93
 
 
94
    if (XPending(Display()) || FD_ISSET(display_fd, &set)) {
 
95
      while (XPending(Display())) {
 
96
        XEvent event;
 
97
 
 
98
        XNextEvent(Display(), &event);
 
99
 
 
100
        if (event.type != GenericEvent)
 
101
          continue;
 
102
 
 
103
        XGenericEventCookie* xcookie = &event.xcookie;
 
104
        ASSERT_NE(0, XGetEventData(Display(), xcookie)) <<
 
105
            "Failed to get X generic event data";
 
106
 
 
107
        ASSERT_EQ(UFStatusSuccess,
 
108
                  frame_x11_process_event(handle_, xcookie)) <<
 
109
            "Failed to process X event";
 
110
 
 
111
        XFreeEventData(Display(), xcookie);
 
112
      }
 
113
    }
 
114
 
 
115
    if (FD_ISSET(frame_fd, &set)) {
 
116
      ProcessFrameEvents();
 
117
    }
 
118
  }
 
119
}
 
120
 
 
121
void utouch::frame::x11::testing::Test::TearDown() {
 
122
  frame_x11_delete(handle_);
 
123
  xorg::testing::Test::TearDown();
 
124
}