// Copyright 2020 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. #ifndef UI_GFX_X_EVENT_H_ #define UI_GFX_X_EVENT_H_ #include #include #include "base/component_export.h" #include "base/memory/ref_counted_memory.h" #include "base/memory/scoped_refptr.h" #include "ui/gfx/x/xproto.h" namespace x11 { class Connection; class Event; struct ReadBuffer; COMPONENT_EXPORT(X11) void ReadEvent(Event* event, Connection* connection, ReadBuffer* buffer); class COMPONENT_EXPORT(X11) Event { public: template explicit Event(T&& xproto_event, bool sequence_valid = true) { using DecayT = std::decay_t; sequence_valid_ = true; sequence_ = xproto_event.sequence; type_id_ = DecayT::type_id; deleter_ = [](void* event) { delete reinterpret_cast(event); }; auto* event = new DecayT(std::forward(xproto_event)); event_ = event; window_ = event->GetWindow(); sequence_valid_ = sequence_valid; } Event(); // |event_bytes| is modified and will not be valid after this call. // A copy is necessary if the original data is still needed. Event(scoped_refptr event_bytes, Connection* connection, bool sequence_valid = true); Event(const Event&) = delete; Event& operator=(const Event&) = delete; Event(Event&& event); Event& operator=(Event&& event); ~Event(); template T* As() { if (type_id_ == T::type_id) return reinterpret_cast(event_); return nullptr; } template const T* As() const { return const_cast(this)->As(); } bool sequence_valid() const { return sequence_valid_; } uint32_t sequence() const { return sequence_; } x11::Window window() const { return window_ ? *window_ : x11::Window::None; } void set_window(x11::Window window) { if (window_) *window_ = window; } bool Initialized() const { return deleter_; } private: friend void ReadEvent(Event* event, Connection* connection, ReadBuffer* buffer); void Dealloc(); bool sequence_valid_ = false; uint16_t sequence_ = 0; // XProto event state. int type_id_ = 0; void (*deleter_)(void*) = nullptr; void* event_ = nullptr; // This member points to a field in |event_|, or may be nullptr if there's no // associated window for the event. It's owned by |event_|, not us. x11::Window* window_ = nullptr; }; } // namespace x11 #endif // UI_GFX_X_EVENT_H_