summaryrefslogtreecommitdiff
path: root/chromium/components/exo/wayland/serial_tracker.cc
blob: 80f32170baefaafb2e699ad3db01d9d7bf831e1b (plain)
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
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/exo/wayland/serial_tracker.h"

#include <wayland-server-core.h>

namespace exo {
namespace wayland {

namespace {

// Number of previous events to retain information about.
constexpr uint32_t kMaxEventsTracked = 1024;

}  // namespace

SerialTracker::SerialTracker(struct wl_display* display)
    : display_(display), events_(kMaxEventsTracked) {}

SerialTracker::~SerialTracker() {}

uint32_t SerialTracker::GetNextSerial(EventType type) {
  uint32_t serial = wl_display_next_serial(display_);
  events_[serial % kMaxEventsTracked] = type;
  max_event_ = serial + 1;
  if ((max_event_ - min_event_) > kMaxEventsTracked)
    min_event_ = max_event_ - kMaxEventsTracked;

  switch (type) {
    case EventType::POINTER_BUTTON_DOWN:
      pointer_down_serial_ = serial;
      break;
    case EventType::POINTER_BUTTON_UP:
      pointer_down_serial_ = base::nullopt;
      break;
    case EventType::TOUCH_DOWN:
      touch_down_serial_ = serial;
      break;
    case EventType::TOUCH_UP:
      touch_down_serial_ = base::nullopt;
      break;
    default:
      break;
  }

  return serial;
}

base::Optional<SerialTracker::EventType> SerialTracker::GetEventType(
    uint32_t serial) const {
  if (max_event_ < min_event_) {
    // The valid range has partially overflowed the 32 bit space, so we should
    // only reject if the serial number is in neither the upper nor lower parts
    // of the space.
    if (!((serial < max_event_) || (serial >= min_event_)))
      return base::nullopt;
  } else {
    // Normal, non-overflowed case. Reject the serial number if it isn't in the
    // interval.
    if (!((serial < max_event_) && (serial >= min_event_)))
      return base::nullopt;
  }

  return events_[serial % kMaxEventsTracked];
}

base::Optional<uint32_t> SerialTracker::GetPointerDownSerial() {
  return pointer_down_serial_;
}

base::Optional<uint32_t> SerialTracker::GetTouchDownSerial() {
  return touch_down_serial_;
}

void SerialTracker::ResetTouchDownSerial() {
  touch_down_serial_ = base::nullopt;
}

}  // namespace wayland
}  // namespace exo