summaryrefslogtreecommitdiffstats
path: root/ui/views/widget/x11_desktop_window_move_client.cc
blob: 7d5e535cdce7a9da2e4b73f8d507891c6bd67e24 (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
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
// Copyright (c) 2012 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 "ui/views/widget/x11_desktop_window_move_client.h"

#include <X11/Xlib.h>
// Get rid of a macro from Xlib.h that conflicts with Aura's RootWindow class.
#undef RootWindow

#include "base/message_loop.h"
#include "base/message_pump_aurax11.h"
#include "base/run_loop.h"
#include "ui/aura/env.h"
#include "ui/aura/root_window.h"
#include "ui/base/event.h"
#include "ui/base/x/x11_util.h"
#include "ui/gfx/screen.h"

namespace views {

X11DesktopWindowMoveClient::X11DesktopWindowMoveClient()
    : in_move_loop_(false) {
}

X11DesktopWindowMoveClient::~X11DesktopWindowMoveClient() {}

bool X11DesktopWindowMoveClient::PreHandleKeyEvent(aura::Window* target,
                                                   ui::KeyEvent* event) {
  return false;
}

bool X11DesktopWindowMoveClient::PreHandleMouseEvent(aura::Window* target,
                                                     ui::MouseEvent* event) {
  if (in_move_loop_) {
    switch (event->type()) {
      case ui::ET_MOUSE_DRAGGED:
      case ui::ET_MOUSE_MOVED: {
        DCHECK(event->valid_system_location());
        gfx::Point system_loc =
            event->system_location().Subtract(window_offset_);
        aura::RootWindow* root_window = target->GetRootWindow();
        root_window->SetHostBounds(gfx::Rect(
            system_loc, root_window->GetHostSize()));
        return true;
      }
      case ui::ET_MOUSE_CAPTURE_CHANGED:
      case ui::ET_MOUSE_RELEASED: {
        EndMoveLoop();
        return true;
      }
      default:
        break;
    }
  }

  return false;
}

ui::TouchStatus X11DesktopWindowMoveClient::PreHandleTouchEvent(
    aura::Window* target,
    ui::TouchEvent* event) {
  return ui::TOUCH_STATUS_UNKNOWN;
}

ui::GestureStatus X11DesktopWindowMoveClient::PreHandleGestureEvent(
    aura::Window* target,
    ui::GestureEvent* event) {
  return ui::GESTURE_STATUS_UNKNOWN;
}

void X11DesktopWindowMoveClient::RunMoveLoop(aura::Window* source,
                                             const gfx::Point& drag_offset) {
  DCHECK(!in_move_loop_);  // Can only handle one nested loop at a time.
  in_move_loop_ = true;
  window_offset_ = drag_offset;

  source->GetRootWindow()->ShowRootWindow();

  Display* display = base::MessagePumpAuraX11::GetDefaultXDisplay();
  XGrabServer(display);
  XUngrabPointer(display, CurrentTime);

  aura::RootWindow* root_window = source->GetRootWindow();
  int ret = XGrabPointer(display,
                         root_window->GetAcceleratedWidget(),
                         False,
                         ButtonReleaseMask | PointerMotionMask,
                         GrabModeAsync,
                         GrabModeAsync,
                         None,
                         None,
                         CurrentTime);
  XUngrabServer(display);
  if (ret != GrabSuccess) {
    DLOG(ERROR) << "Grabbing new tab for dragging failed: " << ret;
    return;
  }

  MessageLoopForUI* loop = MessageLoopForUI::current();
  MessageLoop::ScopedNestableTaskAllower allow_nested(loop);
  base::RunLoop run_loop(aura::Env::GetInstance()->GetDispatcher());
  quit_closure_ = run_loop.QuitClosure();
  run_loop.Run();
}

void X11DesktopWindowMoveClient::EndMoveLoop() {
  if (!in_move_loop_)
    return;

  // TODO(erg): Is this ungrab the cause of having to click to give input focus
  // on drawn out windows? Not ungrabbing here screws the X server until I kill
  // the chrome process.

  // Ungrab before we let go of the window.
  Display* display = base::MessagePumpAuraX11::GetDefaultXDisplay();
  XUngrabPointer(display, CurrentTime);

  in_move_loop_ = false;
  quit_closure_.Run();
}

}  // namespace views