~3v1n0/unity/scale-window-cast-protection

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
125
126
127
128
129
130
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
/*
* 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 version 3 as
* published by the Free Software Foundation.
*
* 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/>.
*
* Authored by: Andrea Azzarone <andrea.azzarone@canonical.com>
*/

#include "XdndCollectionWindowImp.h"
#include "unity-shared/UScreen.h"
#include "unity-shared/WindowManager.h"

namespace unity {
namespace {

class PrivateWindow : public nux::BaseWindow
{
public:
  PrivateWindow(XdndCollectionWindowImp* parent)
    : nux::BaseWindow("")
    , parent_(parent)
  {
    // Make it invisible...
    SetBackgroundColor(nux::color::Transparent);
    SetOpacity(0.0f);
    // ... and as big as the whole screen.
    auto uscreen = UScreen::GetDefault();
    SetGeometry(uscreen->GetScreenGeometry());

    // We are not calling ShowWindow () as this window
    // isn't really visible
    PushToBack();

    if (nux::GetWindowThread()->IsEmbeddedWindow())
    {
      // Hack to create the X Window as soon as possible.
      EnableInputWindow(true, "XdndCollectionWindowImp");
      EnableInputWindow(false, "XdndCollectionWindowImp");
    }

    SetDndEnabled(false, true);

    uscreen->changed.connect(sigc::mem_fun(this, &PrivateWindow::OnScreenChanged));
    WindowManager::Default().window_moved.connect(sigc::mem_fun(this, &PrivateWindow::OnWindowMoved));
  }

  void OnScreenChanged(int /*primary*/, std::vector<nux::Geometry> const& /*monitors*/)
  {
    auto uscreen = UScreen::GetDefault();
    SetGeometry(uscreen->GetScreenGeometry());
  }

  /**
   * EnableInputWindow doesn't show the window immediately.
   * Since nux::EnableInputWindow uses XMoveResizeWindow the best way to know if
   * the X Window is really on/off screen is receiving WindowManager::window_moved
   * signal. Please don't hate me!
   **/
  void OnWindowMoved(Window window_id)
  {
    if (G_LIKELY(window_id != GetInputWindowId()))
      return;

    // Create a fake mouse move because sometimes an extra one is required.
    auto display = nux::GetGraphicsDisplay()->GetX11Display();
    XWarpPointer(display, None, None, 0, 0, 0, 0, 0, 0);
    XFlush(display);
  }

  void ProcessDndMove(int x, int y, std::list<char*> mimes)
  {
    // Hide the window as soon as possible.
    PushToBack();
    EnableInputWindow(false, "XdndCollectionWindowImp");

    std::vector<std::string> data;
    for (auto mime : mimes)
      if (mime) data.push_back(mime);

    parent_->collected.emit(data);
  }

  XdndCollectionWindowImp* parent_;
};

}

XdndCollectionWindowImp::XdndCollectionWindowImp()
  : window_(new PrivateWindow(this))
{}

void XdndCollectionWindowImp::Collect()
{
  // Using PushToFront we're sure that the window is shown over the panel window,
  // the launcher window and the dash window. Don't forget to call PushToBack as
  // soon as possible.
  window_->ShowWindow(true);
  window_->PushToFront();

  if (nux::GetWindowThread()->IsEmbeddedWindow())
    window_->EnableInputWindow(true, "XdndCollectionWindowImp");
}

void XdndCollectionWindowImp::Deactivate()
{
  window_->ShowWindow(false);
  window_->PushToBack();

  if (nux::GetWindowThread()->IsEmbeddedWindow())
    window_->EnableInputWindow(false, "XdndCollectionWindowImp");
}

std::string XdndCollectionWindowImp::GetData(std::string const& type)
{
  auto& gp_display = nux::GetWindowThread()->GetGraphicsDisplay();
  return glib::String(gp_display.GetDndData(const_cast<char*>(type.c_str()))).Str();
}

}