global_menu_bar_x11.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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/views/global_menu_bar_x11.h"
  5. #include <X11/Xlib.h>
  6. // This conflicts with mate::Converter,
  7. #undef True
  8. #undef False
  9. // and V8,
  10. #undef None
  11. // and url_request_status.h,
  12. #undef Status
  13. #include <dlfcn.h>
  14. #include <glib-object.h>
  15. #include "atom/browser/native_window_views.h"
  16. #include "atom/browser/ui/atom_menu_model.h"
  17. #include "base/logging.h"
  18. #include "base/strings/stringprintf.h"
  19. #include "base/strings/utf_string_conversions.h"
  20. #include "chrome/browser/ui/views/frame/global_menu_bar_registrar_x11.h"
  21. #include "ui/aura/window.h"
  22. #include "ui/aura/window_tree_host.h"
  23. #include "ui/base/accelerators/menu_label_accelerator_util_linux.h"
  24. #include "ui/events/keycodes/keyboard_code_conversion_x.h"
  25. // libdbusmenu-glib types
  26. typedef struct _DbusmenuMenuitem DbusmenuMenuitem;
  27. typedef DbusmenuMenuitem* (*dbusmenu_menuitem_new_func)();
  28. typedef DbusmenuMenuitem* (*dbusmenu_menuitem_new_with_id_func)(int id);
  29. typedef int (*dbusmenu_menuitem_get_id_func)(DbusmenuMenuitem* item);
  30. typedef GList* (*dbusmenu_menuitem_get_children_func)(DbusmenuMenuitem* item);
  31. typedef DbusmenuMenuitem* (*dbusmenu_menuitem_child_append_func)(
  32. DbusmenuMenuitem* parent,
  33. DbusmenuMenuitem* child);
  34. typedef DbusmenuMenuitem* (*dbusmenu_menuitem_property_set_func)(
  35. DbusmenuMenuitem* item,
  36. const char* property,
  37. const char* value);
  38. typedef DbusmenuMenuitem* (*dbusmenu_menuitem_property_set_variant_func)(
  39. DbusmenuMenuitem* item,
  40. const char* property,
  41. GVariant* value);
  42. typedef DbusmenuMenuitem* (*dbusmenu_menuitem_property_set_bool_func)(
  43. DbusmenuMenuitem* item,
  44. const char* property,
  45. bool value);
  46. typedef DbusmenuMenuitem* (*dbusmenu_menuitem_property_set_int_func)(
  47. DbusmenuMenuitem* item,
  48. const char* property,
  49. int value);
  50. typedef struct _DbusmenuServer DbusmenuServer;
  51. typedef DbusmenuServer* (*dbusmenu_server_new_func)(const char* object);
  52. typedef void (*dbusmenu_server_set_root_func)(DbusmenuServer* self,
  53. DbusmenuMenuitem* root);
  54. namespace atom {
  55. namespace {
  56. // Retrieved functions from libdbusmenu-glib.
  57. // DbusmenuMenuItem methods:
  58. dbusmenu_menuitem_new_func menuitem_new = NULL;
  59. dbusmenu_menuitem_new_with_id_func menuitem_new_with_id = NULL;
  60. dbusmenu_menuitem_get_id_func menuitem_get_id = NULL;
  61. dbusmenu_menuitem_get_children_func menuitem_get_children = NULL;
  62. dbusmenu_menuitem_get_children_func menuitem_take_children = NULL;
  63. dbusmenu_menuitem_child_append_func menuitem_child_append = NULL;
  64. dbusmenu_menuitem_property_set_func menuitem_property_set = NULL;
  65. dbusmenu_menuitem_property_set_variant_func menuitem_property_set_variant =
  66. NULL;
  67. dbusmenu_menuitem_property_set_bool_func menuitem_property_set_bool = NULL;
  68. dbusmenu_menuitem_property_set_int_func menuitem_property_set_int = NULL;
  69. // DbusmenuServer methods:
  70. dbusmenu_server_new_func server_new = NULL;
  71. dbusmenu_server_set_root_func server_set_root = NULL;
  72. // Properties that we set on menu items:
  73. const char kPropertyEnabled[] = "enabled";
  74. const char kPropertyLabel[] = "label";
  75. const char kPropertyShortcut[] = "shortcut";
  76. const char kPropertyType[] = "type";
  77. const char kPropertyToggleType[] = "toggle-type";
  78. const char kPropertyToggleState[] = "toggle-state";
  79. const char kPropertyVisible[] = "visible";
  80. const char kPropertyChildrenDisplay[] = "children-display";
  81. const char kToggleCheck[] = "checkmark";
  82. const char kToggleRadio[] = "radio";
  83. const char kTypeSeparator[] = "separator";
  84. const char kDisplaySubmenu[] = "submenu";
  85. void EnsureMethodsLoaded() {
  86. static bool attempted_load = false;
  87. if (attempted_load)
  88. return;
  89. attempted_load = true;
  90. void* dbusmenu_lib = dlopen("libdbusmenu-glib.so", RTLD_LAZY);
  91. if (!dbusmenu_lib)
  92. dbusmenu_lib = dlopen("libdbusmenu-glib.so.4", RTLD_LAZY);
  93. if (!dbusmenu_lib)
  94. return;
  95. // DbusmenuMenuItem methods.
  96. menuitem_new = reinterpret_cast<dbusmenu_menuitem_new_func>(
  97. dlsym(dbusmenu_lib, "dbusmenu_menuitem_new"));
  98. menuitem_new_with_id = reinterpret_cast<dbusmenu_menuitem_new_with_id_func>(
  99. dlsym(dbusmenu_lib, "dbusmenu_menuitem_new_with_id"));
  100. menuitem_get_id = reinterpret_cast<dbusmenu_menuitem_get_id_func>(
  101. dlsym(dbusmenu_lib, "dbusmenu_menuitem_get_id"));
  102. menuitem_get_children = reinterpret_cast<dbusmenu_menuitem_get_children_func>(
  103. dlsym(dbusmenu_lib, "dbusmenu_menuitem_get_children"));
  104. menuitem_take_children =
  105. reinterpret_cast<dbusmenu_menuitem_get_children_func>(
  106. dlsym(dbusmenu_lib, "dbusmenu_menuitem_take_children"));
  107. menuitem_child_append = reinterpret_cast<dbusmenu_menuitem_child_append_func>(
  108. dlsym(dbusmenu_lib, "dbusmenu_menuitem_child_append"));
  109. menuitem_property_set = reinterpret_cast<dbusmenu_menuitem_property_set_func>(
  110. dlsym(dbusmenu_lib, "dbusmenu_menuitem_property_set"));
  111. menuitem_property_set_variant =
  112. reinterpret_cast<dbusmenu_menuitem_property_set_variant_func>(
  113. dlsym(dbusmenu_lib, "dbusmenu_menuitem_property_set_variant"));
  114. menuitem_property_set_bool =
  115. reinterpret_cast<dbusmenu_menuitem_property_set_bool_func>(
  116. dlsym(dbusmenu_lib, "dbusmenu_menuitem_property_set_bool"));
  117. menuitem_property_set_int =
  118. reinterpret_cast<dbusmenu_menuitem_property_set_int_func>(
  119. dlsym(dbusmenu_lib, "dbusmenu_menuitem_property_set_int"));
  120. // DbusmenuServer methods.
  121. server_new = reinterpret_cast<dbusmenu_server_new_func>(
  122. dlsym(dbusmenu_lib, "dbusmenu_server_new"));
  123. server_set_root = reinterpret_cast<dbusmenu_server_set_root_func>(
  124. dlsym(dbusmenu_lib, "dbusmenu_server_set_root"));
  125. }
  126. AtomMenuModel* ModelForMenuItem(DbusmenuMenuitem* item) {
  127. return reinterpret_cast<AtomMenuModel*>(
  128. g_object_get_data(G_OBJECT(item), "model"));
  129. }
  130. bool GetMenuItemID(DbusmenuMenuitem* item, int* id) {
  131. gpointer id_ptr = g_object_get_data(G_OBJECT(item), "menu-id");
  132. if (id_ptr != NULL) {
  133. *id = GPOINTER_TO_INT(id_ptr) - 1;
  134. return true;
  135. }
  136. return false;
  137. }
  138. void SetMenuItemID(DbusmenuMenuitem* item, int id) {
  139. DCHECK_GE(id, 0);
  140. // Add 1 to the menu_id to avoid setting zero (null) to "menu-id".
  141. g_object_set_data(G_OBJECT(item), "menu-id", GINT_TO_POINTER(id + 1));
  142. }
  143. std::string GetMenuModelStatus(AtomMenuModel* model) {
  144. std::string ret;
  145. for (int i = 0; i < model->GetItemCount(); ++i) {
  146. int status = model->GetTypeAt(i) | (model->IsVisibleAt(i) << 3) |
  147. (model->IsEnabledAt(i) << 4) |
  148. (model->IsItemCheckedAt(i) << 5);
  149. ret += base::StringPrintf(
  150. "%s-%X\n", base::UTF16ToUTF8(model->GetLabelAt(i)).c_str(), status);
  151. }
  152. return ret;
  153. }
  154. } // namespace
  155. GlobalMenuBarX11::GlobalMenuBarX11(NativeWindowViews* window)
  156. : window_(window),
  157. xid_(window_->GetNativeWindow()->GetHost()->GetAcceleratedWidget()) {
  158. EnsureMethodsLoaded();
  159. if (server_new)
  160. InitServer(xid_);
  161. GlobalMenuBarRegistrarX11::GetInstance()->OnWindowMapped(xid_);
  162. }
  163. GlobalMenuBarX11::~GlobalMenuBarX11() {
  164. if (IsServerStarted())
  165. g_object_unref(server_);
  166. GlobalMenuBarRegistrarX11::GetInstance()->OnWindowUnmapped(xid_);
  167. }
  168. // static
  169. std::string GlobalMenuBarX11::GetPathForWindow(gfx::AcceleratedWidget xid) {
  170. return base::StringPrintf("/com/canonical/menu/%lX", xid);
  171. }
  172. void GlobalMenuBarX11::SetMenu(AtomMenuModel* menu_model) {
  173. if (!IsServerStarted())
  174. return;
  175. DbusmenuMenuitem* root_item = menuitem_new();
  176. menuitem_property_set(root_item, kPropertyLabel, "Root");
  177. menuitem_property_set_bool(root_item, kPropertyVisible, true);
  178. if (menu_model != nullptr) {
  179. BuildMenuFromModel(menu_model, root_item);
  180. }
  181. server_set_root(server_, root_item);
  182. g_object_unref(root_item);
  183. }
  184. bool GlobalMenuBarX11::IsServerStarted() const {
  185. return server_;
  186. }
  187. void GlobalMenuBarX11::InitServer(gfx::AcceleratedWidget xid) {
  188. std::string path = GetPathForWindow(xid);
  189. server_ = server_new(path.c_str());
  190. }
  191. void GlobalMenuBarX11::OnWindowMapped() {
  192. GlobalMenuBarRegistrarX11::GetInstance()->OnWindowMapped(xid_);
  193. }
  194. void GlobalMenuBarX11::OnWindowUnmapped() {
  195. GlobalMenuBarRegistrarX11::GetInstance()->OnWindowUnmapped(xid_);
  196. }
  197. void GlobalMenuBarX11::BuildMenuFromModel(AtomMenuModel* model,
  198. DbusmenuMenuitem* parent) {
  199. for (int i = 0; i < model->GetItemCount(); ++i) {
  200. DbusmenuMenuitem* item = menuitem_new();
  201. menuitem_property_set_bool(item, kPropertyVisible, model->IsVisibleAt(i));
  202. AtomMenuModel::ItemType type = model->GetTypeAt(i);
  203. if (type == AtomMenuModel::TYPE_SEPARATOR) {
  204. menuitem_property_set(item, kPropertyType, kTypeSeparator);
  205. } else {
  206. std::string label = ui::ConvertAcceleratorsFromWindowsStyle(
  207. base::UTF16ToUTF8(model->GetLabelAt(i)));
  208. menuitem_property_set(item, kPropertyLabel, label.c_str());
  209. menuitem_property_set_bool(item, kPropertyEnabled, model->IsEnabledAt(i));
  210. g_object_set_data(G_OBJECT(item), "model", model);
  211. SetMenuItemID(item, i);
  212. if (type == AtomMenuModel::TYPE_SUBMENU) {
  213. menuitem_property_set(item, kPropertyChildrenDisplay, kDisplaySubmenu);
  214. g_signal_connect(item, "about-to-show", G_CALLBACK(OnSubMenuShowThunk),
  215. this);
  216. } else {
  217. ui::Accelerator accelerator;
  218. if (model->GetAcceleratorAtWithParams(i, true, &accelerator))
  219. RegisterAccelerator(item, accelerator);
  220. g_signal_connect(item, "item-activated",
  221. G_CALLBACK(OnItemActivatedThunk), this);
  222. if (type == AtomMenuModel::TYPE_CHECK ||
  223. type == AtomMenuModel::TYPE_RADIO) {
  224. menuitem_property_set(
  225. item, kPropertyToggleType,
  226. type == AtomMenuModel::TYPE_CHECK ? kToggleCheck : kToggleRadio);
  227. menuitem_property_set_int(item, kPropertyToggleState,
  228. model->IsItemCheckedAt(i));
  229. }
  230. }
  231. }
  232. menuitem_child_append(parent, item);
  233. g_object_unref(item);
  234. }
  235. }
  236. void GlobalMenuBarX11::RegisterAccelerator(DbusmenuMenuitem* item,
  237. const ui::Accelerator& accelerator) {
  238. // A translation of libdbusmenu-gtk's menuitem_property_set_shortcut()
  239. // translated from GDK types to ui::Accelerator types.
  240. GVariantBuilder builder;
  241. g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY);
  242. if (accelerator.IsCtrlDown())
  243. g_variant_builder_add(&builder, "s", "Control");
  244. if (accelerator.IsAltDown())
  245. g_variant_builder_add(&builder, "s", "Alt");
  246. if (accelerator.IsShiftDown())
  247. g_variant_builder_add(&builder, "s", "Shift");
  248. char* name =
  249. XKeysymToString(XKeysymForWindowsKeyCode(accelerator.key_code(), false));
  250. if (!name) {
  251. NOTIMPLEMENTED();
  252. return;
  253. }
  254. g_variant_builder_add(&builder, "s", name);
  255. GVariant* inside_array = g_variant_builder_end(&builder);
  256. g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY);
  257. g_variant_builder_add_value(&builder, inside_array);
  258. GVariant* outside_array = g_variant_builder_end(&builder);
  259. menuitem_property_set_variant(item, kPropertyShortcut, outside_array);
  260. }
  261. void GlobalMenuBarX11::OnItemActivated(DbusmenuMenuitem* item,
  262. unsigned int timestamp) {
  263. int id;
  264. AtomMenuModel* model = ModelForMenuItem(item);
  265. if (model && GetMenuItemID(item, &id))
  266. model->ActivatedAt(id, 0);
  267. }
  268. void GlobalMenuBarX11::OnSubMenuShow(DbusmenuMenuitem* item) {
  269. int id;
  270. AtomMenuModel* model = ModelForMenuItem(item);
  271. if (!model || !GetMenuItemID(item, &id))
  272. return;
  273. // Do not update menu if the submenu has not been changed.
  274. std::string status = GetMenuModelStatus(model);
  275. char* old = static_cast<char*>(g_object_get_data(G_OBJECT(item), "status"));
  276. if (old && status == old)
  277. return;
  278. // Save the new status.
  279. g_object_set_data_full(G_OBJECT(item), "status", g_strdup(status.c_str()),
  280. g_free);
  281. // Clear children.
  282. GList* children = menuitem_take_children(item);
  283. g_list_foreach(children, reinterpret_cast<GFunc>(g_object_unref), NULL);
  284. g_list_free(children);
  285. // Build children.
  286. BuildMenuFromModel(model->GetSubmenuModelAt(id), item);
  287. }
  288. } // namespace atom