File: x11_window_manager.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (102 lines) | stat: -rw-r--r-- 3,148 bytes parent folder | download | duplicates (3)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/ozone/platform/x11/x11_window_manager.h"

#include "base/containers/contains.h"
#include "ui/ozone/platform/x11/x11_window.h"

namespace ui {

namespace {

X11WindowManager* g_instance = nullptr;

}  // namespace

X11WindowManager::X11WindowManager() {
  DCHECK(!g_instance) << "There should only be a single X11WindowManager";
  g_instance = this;
}

X11WindowManager::~X11WindowManager() = default;

// static
X11WindowManager* X11WindowManager::GetInstance() {
  if (!g_instance) {
    auto manager = std::make_unique<X11WindowManager>();
    X11WindowManager* manager_ptr = manager.release();
    DCHECK_EQ(g_instance, manager_ptr);
  }
  return g_instance;
}

void X11WindowManager::GrabEvents(X11Window* window) {
  DCHECK_NE(located_events_grabber_, window);

  // Grabbing the mouse is asynchronous. However, we synchronously start
  // forwarding all mouse events received by Chrome to the
  // aura::WindowEventDispatcher which has capture. This makes capture
  // synchronous for all intents and purposes if either:
  // - |located_events_grabber_| is set to have capture.
  // OR
  // - The topmost window underneath the mouse is managed by Chrome.
  auto* old_grabber = located_events_grabber_.get();

  // Update |located_events_grabber_| prior to calling OnXWindowLostCapture() to
  // avoid releasing pointer grab.
  located_events_grabber_ = window;
  if (old_grabber)
    old_grabber->OnXWindowLostCapture();

  // the X11Window calls GrabPointer by itself.
}

void X11WindowManager::UngrabEvents(X11Window* window) {
  DCHECK_EQ(located_events_grabber_, window);
  // Release mouse grab asynchronously. A window managed by Chrome is likely
  // the topmost window underneath the mouse so the capture release being
  // asynchronous is likely inconsequential.
  auto* old_grabber = located_events_grabber_.get();
  located_events_grabber_ = nullptr;
  old_grabber->OnXWindowLostCapture();
}

void X11WindowManager::AddWindow(X11Window* window) {
  DCHECK(window);
  auto widget = window->GetWidget();
  DCHECK_NE(gfx::kNullAcceleratedWidget, widget);
  DCHECK(!base::Contains(windows_, widget));
  windows_.emplace(widget, window);
}

void X11WindowManager::RemoveWindow(X11Window* window) {
  DCHECK(window);
  auto widget = window->GetWidget();
  auto it = windows_.find(widget);
  // The XWindow might not have been initialized due to some errors.
  if (widget == gfx::kNullAcceleratedWidget) {
    DCHECK(it == windows_.end());
  } else {
    CHECK(it != windows_.end());
    if (window_mouse_currently_on_ == it->second)
      window_mouse_currently_on_ = nullptr;
    windows_.erase(it);
  }
}

X11Window* X11WindowManager::GetWindow(gfx::AcceleratedWidget widget) const {
  auto it = windows_.find(widget);
  return it != windows_.end() ? it->second : nullptr;
}

void X11WindowManager::MouseOnWindow(X11Window* window) {
  if (window_mouse_currently_on_ == window)
    return;

  window_mouse_currently_on_ = window;
  window->OnMouseEnter();
}

}  // namespace ui