x_window_utils.cc 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (c) 2014 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/ui/x/x_window_utils.h"
  5. #include <X11/Xatom.h>
  6. #include "base/environment.h"
  7. #include "base/strings/string_util.h"
  8. #include "base/threading/thread_restrictions.h"
  9. #include "dbus/bus.h"
  10. #include "dbus/message.h"
  11. #include "dbus/object_proxy.h"
  12. #include "ui/base/x/x11_util.h"
  13. namespace atom {
  14. ::Atom GetAtom(const char* name) {
  15. return XInternAtom(gfx::GetXDisplay(), name, false);
  16. }
  17. void SetWMSpecState(::Window xwindow, bool enabled, ::Atom state) {
  18. XEvent xclient;
  19. memset(&xclient, 0, sizeof(xclient));
  20. xclient.type = ClientMessage;
  21. xclient.xclient.window = xwindow;
  22. xclient.xclient.message_type = GetAtom("_NET_WM_STATE");
  23. xclient.xclient.format = 32;
  24. xclient.xclient.data.l[0] = enabled ? 1 : 0;
  25. xclient.xclient.data.l[1] = state;
  26. xclient.xclient.data.l[2] = None;
  27. xclient.xclient.data.l[3] = 1;
  28. xclient.xclient.data.l[4] = 0;
  29. XDisplay* xdisplay = gfx::GetXDisplay();
  30. XSendEvent(xdisplay, DefaultRootWindow(xdisplay), False,
  31. SubstructureRedirectMask | SubstructureNotifyMask, &xclient);
  32. }
  33. void SetWindowType(::Window xwindow, const std::string& type) {
  34. XDisplay* xdisplay = gfx::GetXDisplay();
  35. std::string type_prefix = "_NET_WM_WINDOW_TYPE_";
  36. ::Atom window_type = XInternAtom(
  37. xdisplay, (type_prefix + base::ToUpperASCII(type)).c_str(), False);
  38. XChangeProperty(xdisplay, xwindow,
  39. XInternAtom(xdisplay, "_NET_WM_WINDOW_TYPE", False), XA_ATOM,
  40. 32, PropModeReplace,
  41. reinterpret_cast<unsigned char*>(&window_type), 1);
  42. }
  43. bool ShouldUseGlobalMenuBar() {
  44. base::ThreadRestrictions::ScopedAllowIO allow_io;
  45. std::unique_ptr<base::Environment> env(base::Environment::Create());
  46. if (env->HasVar("ELECTRON_FORCE_WINDOW_MENU_BAR"))
  47. return false;
  48. dbus::Bus::Options options;
  49. scoped_refptr<dbus::Bus> bus(new dbus::Bus(options));
  50. dbus::ObjectProxy* object_proxy =
  51. bus->GetObjectProxy(DBUS_SERVICE_DBUS, dbus::ObjectPath(DBUS_PATH_DBUS));
  52. dbus::MethodCall method_call(DBUS_INTERFACE_DBUS, "ListNames");
  53. std::unique_ptr<dbus::Response> response(object_proxy->CallMethodAndBlock(
  54. &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT));
  55. if (!response) {
  56. bus->ShutdownAndBlock();
  57. return false;
  58. }
  59. dbus::MessageReader reader(response.get());
  60. dbus::MessageReader array_reader(NULL);
  61. if (!reader.PopArray(&array_reader)) {
  62. bus->ShutdownAndBlock();
  63. return false;
  64. }
  65. while (array_reader.HasMoreData()) {
  66. std::string name;
  67. if (array_reader.PopString(&name) &&
  68. name == "com.canonical.AppMenu.Registrar") {
  69. bus->ShutdownAndBlock();
  70. return true;
  71. }
  72. }
  73. bus->ShutdownAndBlock();
  74. return false;
  75. }
  76. } // namespace atom