2014-10-31 12:17:05 -06:00
|
|
|
// Copyright (c) 2014 GitHub, Inc.
|
2014-08-25 23:37:37 -06:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 14:46:59 -06:00
|
|
|
#include "shell/browser/ui/x/x_window_utils.h"
|
2014-08-25 23:37:37 -06:00
|
|
|
|
2018-09-12 18:25:56 -06:00
|
|
|
#include <memory>
|
2015-02-10 21:12:22 -07:00
|
|
|
|
2015-10-04 02:33:03 -06:00
|
|
|
#include "base/environment.h"
|
2015-02-10 21:12:22 -07:00
|
|
|
#include "base/strings/string_util.h"
|
2015-08-04 23:16:03 -06:00
|
|
|
#include "dbus/bus.h"
|
|
|
|
#include "dbus/message.h"
|
2016-08-26 16:30:02 -06:00
|
|
|
#include "dbus/object_proxy.h"
|
2022-11-17 12:59:23 -07:00
|
|
|
#include "shell/common/thread_restrictions.h"
|
2014-08-25 23:37:37 -06:00
|
|
|
#include "ui/base/x/x11_util.h"
|
2023-12-11 13:58:26 -07:00
|
|
|
#include "ui/gfx/x/atom_cache.h"
|
2023-11-01 17:01:01 -06:00
|
|
|
#include "ui/gfx/x/connection.h"
|
2020-09-21 02:00:36 -06:00
|
|
|
#include "ui/gfx/x/xproto.h"
|
2014-08-25 23:37:37 -06:00
|
|
|
|
2019-06-19 15:23:04 -06:00
|
|
|
namespace electron {
|
2014-08-25 23:37:37 -06:00
|
|
|
|
2020-06-22 11:35:10 -06:00
|
|
|
void SetWMSpecState(x11::Window window, bool enabled, x11::Atom state) {
|
2021-06-29 14:02:27 -06: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-25 23:37:37 -06:00
|
|
|
}
|
|
|
|
|
2020-06-22 11:35:10 -06:00
|
|
|
void SetWindowType(x11::Window window, const std::string& type) {
|
2015-02-10 21:12:22 -07:00
|
|
|
std::string type_prefix = "_NET_WM_WINDOW_TYPE_";
|
2023-12-11 13:58:26 -07:00
|
|
|
std::string window_type_str = type_prefix + base::ToUpperASCII(type);
|
|
|
|
x11::Atom window_type = x11::GetAtom(window_type_str.c_str());
|
2023-11-01 17:01:01 -06:00
|
|
|
auto* connection = x11::Connection::Get();
|
|
|
|
connection->SetProperty(window, x11::GetAtom("_NET_WM_WINDOW_TYPE"),
|
|
|
|
x11::Atom::ATOM, window_type);
|
2015-02-10 21:12:22 -07:00
|
|
|
}
|
|
|
|
|
2015-08-04 23:16:03 -06:00
|
|
|
bool ShouldUseGlobalMenuBar() {
|
2022-11-17 12:59:23 -07:00
|
|
|
ScopedAllowBlockingForElectron allow_blocking;
|
2021-06-03 18:23:06 -06:00
|
|
|
auto env = base::Environment::Create();
|
2015-10-04 02:33:03 -06:00
|
|
|
if (env->HasVar("ELECTRON_FORCE_WINDOW_MENU_BAR"))
|
|
|
|
return false;
|
|
|
|
|
2015-08-04 23:16:03 -06:00
|
|
|
dbus::Bus::Options options;
|
2021-06-07 20:00:05 -06:00
|
|
|
auto bus = base::MakeRefCounted<dbus::Bus>(options);
|
2015-08-04 23:16:03 -06:00
|
|
|
|
|
|
|
dbus::ObjectProxy* object_proxy =
|
|
|
|
bus->GetObjectProxy(DBUS_SERVICE_DBUS, dbus::ObjectPath(DBUS_PATH_DBUS));
|
|
|
|
dbus::MethodCall method_call(DBUS_INTERFACE_DBUS, "ListNames");
|
2023-07-18 16:26:27 -06:00
|
|
|
std::unique_ptr<dbus::Response> response =
|
|
|
|
object_proxy
|
|
|
|
->CallMethodAndBlock(&method_call,
|
|
|
|
dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)
|
|
|
|
.value_or(nullptr);
|
2015-08-04 23:16:03 -06:00
|
|
|
if (!response) {
|
|
|
|
bus->ShutdownAndBlock();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbus::MessageReader reader(response.get());
|
2019-09-16 16:12:00 -06:00
|
|
|
dbus::MessageReader array_reader(nullptr);
|
2015-08-04 23:16:03 -06: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 11:35:10 -06:00
|
|
|
void MoveWindowToForeground(x11::Window window) {
|
|
|
|
MoveWindowAbove(window, static_cast<x11::Window>(0));
|
2019-08-15 00:51:15 -06:00
|
|
|
}
|
|
|
|
|
2020-06-22 11:35:10 -06:00
|
|
|
void MoveWindowAbove(x11::Window window, x11::Window other_window) {
|
2020-09-21 02:00:36 -06:00
|
|
|
ui::SendClientMessage(window, ui::GetX11RootWindow(),
|
2020-12-22 15:14:44 -07:00
|
|
|
x11::GetAtom("_NET_RESTACK_WINDOW"),
|
2020-09-21 02:00:36 -06:00
|
|
|
{2, static_cast<uint32_t>(other_window),
|
|
|
|
static_cast<uint32_t>(x11::StackMode::Above), 0, 0});
|
2019-02-07 13:48:19 -07:00
|
|
|
}
|
|
|
|
|
2020-06-22 11:35:10 -06:00
|
|
|
bool IsWindowValid(x11::Window window) {
|
2020-10-15 19:30:41 -06:00
|
|
|
auto* conn = x11::Connection::Get();
|
|
|
|
return conn->GetWindowAttributes({window}).Sync();
|
2019-08-15 00:51:15 -06:00
|
|
|
}
|
|
|
|
|
2019-06-19 15:23:04 -06:00
|
|
|
} // namespace electron
|