2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2014 GitHub, Inc.
|
2014-08-26 05:37:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/ui/x/x_window_utils.h"
|
2014-08-26 05:37:37 +00:00
|
|
|
|
2015-02-11 04:12:22 +00:00
|
|
|
#include <X11/Xatom.h>
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <memory>
|
2015-02-11 04:12:22 +00:00
|
|
|
|
2015-10-04 08:33:03 +00:00
|
|
|
#include "base/environment.h"
|
2015-02-11 04:12:22 +00:00
|
|
|
#include "base/strings/string_util.h"
|
2017-12-16 09:25:15 +00:00
|
|
|
#include "base/threading/thread_restrictions.h"
|
2015-08-05 05:16:03 +00:00
|
|
|
#include "dbus/bus.h"
|
|
|
|
#include "dbus/message.h"
|
2016-08-26 22:30:02 +00:00
|
|
|
#include "dbus/object_proxy.h"
|
2014-08-26 05:37:37 +00:00
|
|
|
#include "ui/base/x/x11_util.h"
|
2020-05-26 20:06:26 +00:00
|
|
|
#include "ui/gfx/x/x11_atom_cache.h"
|
2014-08-26 05:37:37 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2014-08-26 05:37:37 +00:00
|
|
|
|
2020-05-26 20:06:26 +00:00
|
|
|
void SetWMSpecState(::Window xwindow, bool enabled, x11::Atom state) {
|
2014-08-26 05:37:37 +00:00
|
|
|
XEvent xclient;
|
|
|
|
memset(&xclient, 0, sizeof(xclient));
|
|
|
|
xclient.type = ClientMessage;
|
|
|
|
xclient.xclient.window = xwindow;
|
2020-05-26 20:06:26 +00:00
|
|
|
xclient.xclient.message_type =
|
|
|
|
static_cast<uint32_t>(gfx::GetAtom("_NET_WM_STATE"));
|
2014-08-26 05:37:37 +00:00
|
|
|
xclient.xclient.format = 32;
|
|
|
|
xclient.xclient.data.l[0] = enabled ? 1 : 0;
|
2020-05-26 20:06:26 +00:00
|
|
|
xclient.xclient.data.l[1] = static_cast<uint32_t>(state);
|
2018-04-18 16:52:58 +00:00
|
|
|
xclient.xclient.data.l[2] = x11::None;
|
2014-08-26 05:37:37 +00:00
|
|
|
xclient.xclient.data.l[3] = 1;
|
|
|
|
xclient.xclient.data.l[4] = 0;
|
|
|
|
|
|
|
|
XDisplay* xdisplay = gfx::GetXDisplay();
|
2018-04-18 16:52:58 +00:00
|
|
|
XSendEvent(xdisplay, DefaultRootWindow(xdisplay), x11::False,
|
2018-04-18 01:55:30 +00:00
|
|
|
SubstructureRedirectMask | SubstructureNotifyMask, &xclient);
|
2014-08-26 05:37:37 +00:00
|
|
|
}
|
|
|
|
|
2015-02-11 04:12:22 +00:00
|
|
|
void SetWindowType(::Window xwindow, const std::string& type) {
|
|
|
|
std::string type_prefix = "_NET_WM_WINDOW_TYPE_";
|
2020-05-26 20:06:26 +00:00
|
|
|
x11::Atom window_type = gfx::GetAtom(type_prefix + base::ToUpperASCII(type));
|
|
|
|
ui::SetProperty(xwindow, gfx::GetAtom("_NET_WM_WINDOW_TYPE"), x11::Atom::ATOM,
|
|
|
|
window_type);
|
2015-02-11 04:12:22 +00:00
|
|
|
}
|
|
|
|
|
2015-08-05 05:16:03 +00:00
|
|
|
bool ShouldUseGlobalMenuBar() {
|
2017-12-16 09:25:15 +00:00
|
|
|
base::ThreadRestrictions::ScopedAllowIO allow_io;
|
2016-05-23 01:59:39 +00:00
|
|
|
std::unique_ptr<base::Environment> env(base::Environment::Create());
|
2015-10-04 08:33:03 +00:00
|
|
|
if (env->HasVar("ELECTRON_FORCE_WINDOW_MENU_BAR"))
|
|
|
|
return false;
|
|
|
|
|
2015-08-05 05:16:03 +00:00
|
|
|
dbus::Bus::Options options;
|
|
|
|
scoped_refptr<dbus::Bus> bus(new dbus::Bus(options));
|
|
|
|
|
|
|
|
dbus::ObjectProxy* object_proxy =
|
|
|
|
bus->GetObjectProxy(DBUS_SERVICE_DBUS, dbus::ObjectPath(DBUS_PATH_DBUS));
|
|
|
|
dbus::MethodCall method_call(DBUS_INTERFACE_DBUS, "ListNames");
|
2016-05-23 01:59:39 +00:00
|
|
|
std::unique_ptr<dbus::Response> response(object_proxy->CallMethodAndBlock(
|
2015-08-05 05:16:03 +00:00
|
|
|
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT));
|
|
|
|
if (!response) {
|
|
|
|
bus->ShutdownAndBlock();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbus::MessageReader reader(response.get());
|
2019-09-16 22:12:00 +00:00
|
|
|
dbus::MessageReader array_reader(nullptr);
|
2015-08-05 05:16:03 +00:00
|
|
|
if (!reader.PopArray(&array_reader)) {
|
|
|
|
bus->ShutdownAndBlock();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
while (array_reader.HasMoreData()) {
|
|
|
|
std::string name;
|
|
|
|
if (array_reader.PopString(&name) &&
|
|
|
|
name == "com.canonical.AppMenu.Registrar") {
|
|
|
|
bus->ShutdownAndBlock();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bus->ShutdownAndBlock();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-07 20:48:19 +00:00
|
|
|
void MoveWindowToForeground(::Window xwindow) {
|
2019-08-15 06:51:15 +00:00
|
|
|
MoveWindowAbove(xwindow, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MoveWindowAbove(::Window xwindow, ::Window other_xwindow) {
|
2019-02-07 20:48:19 +00:00
|
|
|
XDisplay* xdisplay = gfx::GetXDisplay();
|
|
|
|
XEvent xclient;
|
|
|
|
memset(&xclient, 0, sizeof(xclient));
|
|
|
|
|
|
|
|
xclient.type = ClientMessage;
|
|
|
|
xclient.xclient.display = xdisplay;
|
|
|
|
xclient.xclient.window = xwindow;
|
2020-05-26 20:06:26 +00:00
|
|
|
xclient.xclient.message_type =
|
|
|
|
static_cast<uint32_t>(gfx::GetAtom("_NET_RESTACK_WINDOW"));
|
2019-02-07 20:48:19 +00:00
|
|
|
xclient.xclient.format = 32;
|
|
|
|
xclient.xclient.data.l[0] = 2;
|
2019-08-15 06:51:15 +00:00
|
|
|
xclient.xclient.data.l[1] = other_xwindow;
|
2020-06-01 20:34:34 +00:00
|
|
|
xclient.xclient.data.l[2] = static_cast<uint32_t>(x11::StackMode::Above);
|
2019-02-07 20:48:19 +00:00
|
|
|
xclient.xclient.data.l[3] = 0;
|
|
|
|
xclient.xclient.data.l[4] = 0;
|
|
|
|
|
|
|
|
XSendEvent(xdisplay, DefaultRootWindow(xdisplay), x11::False,
|
|
|
|
SubstructureRedirectMask | SubstructureNotifyMask, &xclient);
|
|
|
|
XFlush(xdisplay);
|
|
|
|
}
|
|
|
|
|
2019-08-15 06:51:15 +00:00
|
|
|
bool IsWindowValid(::Window xwindow) {
|
|
|
|
XWindowAttributes attrs;
|
|
|
|
return XGetWindowAttributes(gfx::GetXDisplay(), xwindow, &attrs);
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|