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
|
|
|
|
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"
|
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"
|
2022-11-17 19:59:23 +00:00
|
|
|
#include "shell/common/thread_restrictions.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"
|
2020-09-21 08:00:36 +00:00
|
|
|
#include "ui/gfx/x/xproto.h"
|
|
|
|
#include "ui/gfx/x/xproto_util.h"
|
2014-08-26 05:37:37 +00:00
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
2020-06-22 17:35:10 +00:00
|
|
|
void SetWMSpecState(x11::Window window, bool enabled, x11::Atom state) {
|
2021-06-29 20:02:27 +00:00
|
|
|
ui::SendClientMessage(
|
|
|
|
window, ui::GetX11RootWindow(), x11::GetAtom("_NET_WM_STATE"),
|
|
|
|
{static_cast<uint32_t>(enabled ? 1 : 0), static_cast<uint32_t>(state),
|
|
|
|
static_cast<uint32_t>(x11::Window::None), 1, 0});
|
2014-08-26 05:37:37 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 17:35:10 +00:00
|
|
|
void SetWindowType(x11::Window window, const std::string& type) {
|
2015-02-11 04:12:22 +00:00
|
|
|
std::string type_prefix = "_NET_WM_WINDOW_TYPE_";
|
2020-12-22 22:14:44 +00:00
|
|
|
x11::Atom window_type = x11::GetAtom(type_prefix + base::ToUpperASCII(type));
|
|
|
|
x11::SetProperty(window, x11::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() {
|
2022-11-17 19:59:23 +00:00
|
|
|
ScopedAllowBlockingForElectron allow_blocking;
|
2021-06-04 00:23:06 +00:00
|
|
|
auto 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;
|
2021-06-08 02:00:05 +00:00
|
|
|
auto bus = base::MakeRefCounted<dbus::Bus>(options);
|
2015-08-05 05:16:03 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-06-22 17:35:10 +00:00
|
|
|
void MoveWindowToForeground(x11::Window window) {
|
|
|
|
MoveWindowAbove(window, static_cast<x11::Window>(0));
|
2019-08-15 06:51:15 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 17:35:10 +00:00
|
|
|
void MoveWindowAbove(x11::Window window, x11::Window other_window) {
|
2020-09-21 08:00:36 +00:00
|
|
|
ui::SendClientMessage(window, ui::GetX11RootWindow(),
|
2020-12-22 22:14:44 +00:00
|
|
|
x11::GetAtom("_NET_RESTACK_WINDOW"),
|
2020-09-21 08:00:36 +00:00
|
|
|
{2, static_cast<uint32_t>(other_window),
|
|
|
|
static_cast<uint32_t>(x11::StackMode::Above), 0, 0});
|
2019-02-07 20:48:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 17:35:10 +00:00
|
|
|
bool IsWindowValid(x11::Window window) {
|
2020-10-16 01:30:41 +00:00
|
|
|
auto* conn = x11::Connection::Get();
|
|
|
|
return conn->GetWindowAttributes({window}).Sync();
|
2019-08-15 06:51:15 +00:00
|
|
|
}
|
|
|
|
|
2014-08-26 05:37:37 +00:00
|
|
|
} // namespace electron
|