~chasedouglas/frame/ubuntu-upstream-xi

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*****************************************************************************
 *
 * utouch-frame - Touch Frame Library
 *
 * Copyright (C) 2011 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 ****************************************************************************/

#include "x11/fixture.h"

#include <X11/Xlib.h>
#include <X11/extensions/XInput2.h>

#include "utouch/frame.h"
#include "utouch/frame_x11.h"

void utouch::frame::x11::testing::Test::SetUp() {
  ASSERT_NO_FATAL_FAILURE(xorg::testing::Test::SetUp());

  Window root = DefaultRootWindow(Display());

  XIEventMask mask;
  mask.mask_len = XIMaskLen(XI_LASTEVENT);
  mask.mask = reinterpret_cast<unsigned char*>(calloc(mask.mask_len,
                                                      sizeof(char)));

  mask.deviceid = XIAllDevices;
  XISetMask(mask.mask, XI_HierarchyChanged);
  ASSERT_EQ(Success, XISelectEvents(Display(), root, &mask, 1));
  XIClearMask(mask.mask, XI_HierarchyChanged);


  mask.deviceid = XIAllMasterDevices;
  XISetMask(mask.mask, XI_TouchBegin);
  XISetMask(mask.mask, XI_TouchUpdate);
  XISetMask(mask.mask, XI_TouchUpdateUnowned);
  XISetMask(mask.mask, XI_TouchEnd);
  XISetMask(mask.mask, XI_TouchOwnership);

  XIGrabModifiers mods = { XIAnyModifier, 0 };

  ASSERT_EQ(Success, XIGrabTouchBegin(Display(), XIAllMasterDevices, root, 0,
                                      &mask, 1, &mods));

  free(mask.mask);

  UFStatus status = frame_x11_new(Display(), &handle_);
  ASSERT_EQ(UFStatusSuccess, status);
  ASSERT_TRUE(handle_ != NULL);
}

void utouch::frame::x11::testing::Test::PumpEvents(uint64_t timeout) {
  fd_set set;
  FD_ZERO(&set);

  int display_fd = ConnectionNumber(Display());
  int frame_fd = frame_get_fd(handle_);
  int nfds = display_fd > frame_fd ? display_fd + 1: frame_fd + 1;

  XSync(Display(), 0);

  while (true) {
    if (!XPending(Display())) {
      FD_SET(display_fd, &set);
      FD_SET(frame_fd, &set);

      struct timeval timeval = { timeout / 1000, timeout % 1000 };

      int ret;
      if (timeout)
        ret = select(nfds, &set, NULL, NULL, &timeval);
      else
        ret = select(nfds, &set, NULL, NULL, NULL);

      ASSERT_GE(ret, 0) << "Failed to select on FDs";

      if (ret == 0)
        return;
    }

    if (XPending(Display()) || FD_ISSET(display_fd, &set)) {
      while (XPending(Display())) {
        XEvent event;

        XNextEvent(Display(), &event);

        if (event.type != GenericEvent)
          continue;

        XGenericEventCookie* xcookie = &event.xcookie;
        ASSERT_NE(0, XGetEventData(Display(), xcookie)) <<
            "Failed to get X generic event data";

        ASSERT_EQ(UFStatusSuccess,
                  frame_x11_process_event(handle_, xcookie)) <<
            "Failed to process X event";

        XFreeEventData(Display(), xcookie);
      }
    }

    if (FD_ISSET(frame_fd, &set)) {
      ProcessFrameEvents();
    }
  }
}

void utouch::frame::x11::testing::Test::TearDown() {
  frame_x11_delete(handle_);
  xorg::testing::Test::TearDown();
}