WebClient.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. #include <SDL/include/SDL.h>
  2. #include <ThirdParty/SDL/include/SDL_syswm.h>
  3. #include <include/cef_app.h>
  4. #include <include/cef_client.h>
  5. #include <include/cef_browser.h>
  6. #include <include/wrapper/cef_helpers.h>
  7. #include <include/base/cef_bind.h>
  8. #include <include/wrapper/cef_closure_task.h>
  9. #include <Atomic/Core/ProcessUtils.h>
  10. #include <Atomic/Core/CoreEvents.h>
  11. #include <Atomic/IO/Log.h>
  12. #include <Atomic/Input/Input.h>
  13. #include <Atomic/Graphics/Graphics.h>
  14. #include "WebBrowserHost.h"
  15. #include "WebClient.h"
  16. #include "WebKeyboardSDL.h"
  17. namespace Atomic
  18. {
  19. #ifdef ATOMIC_PLATFORM_OSX
  20. void* GetNSWindowContentView(void* window);
  21. #endif
  22. class WebClientPrivate : public CefClient, public CefLifeSpanHandler
  23. {
  24. friend class WebClient;
  25. public:
  26. WebClientPrivate(WebClient* client)
  27. {
  28. webClient_ = client;
  29. webBrowserHost_ = webClient_->GetSubsystem<WebBrowserHost>();
  30. }
  31. CefRefPtr<CefRenderHandler> GetRenderHandler() OVERRIDE
  32. {
  33. if (webClient_->renderHandler_.Null())
  34. return nullptr;
  35. return webClient_->renderHandler_->GetCEFRenderHandler();
  36. }
  37. virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE
  38. {
  39. return this;
  40. }
  41. bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
  42. CefProcessId source_process,
  43. CefRefPtr<CefProcessMessage> message) OVERRIDE
  44. {
  45. return false;
  46. }
  47. bool CreateBrowser(const String& initialURL, int width, int height)
  48. {
  49. if (webClient_->renderHandler_.Null())
  50. {
  51. LOGERROR("WebClient::CreateBrowser - No render handler specified");
  52. return false;
  53. }
  54. CefWindowInfo windowInfo;
  55. CefBrowserSettings browserSettings;
  56. //browserSettings.webgl = STATE_ENABLED;
  57. windowInfo.width = width;
  58. windowInfo.height = height;
  59. Graphics* graphics = webClient_->GetSubsystem<Graphics>();
  60. SDL_Window* sdlWindow = static_cast<SDL_Window*>(graphics->GetSDLWindow());
  61. SDL_SysWMinfo info;
  62. SDL_VERSION(&info.version);
  63. if(SDL_GetWindowWMInfo(sdlWindow, &info))
  64. {
  65. NSView* view = (NSView*) GetNSWindowContentView(info.info.cocoa.window);
  66. windowInfo.SetAsWindowless(view, false);
  67. webClient_->renderHandler_->SetSize(width, height);
  68. CefRefPtr<CefBrowser> browser = CefBrowserHost::CreateBrowserSync(windowInfo, this,
  69. initialURL.CString(), browserSettings, nullptr);
  70. if (!browser.get())
  71. return false;
  72. browser_ = browser;
  73. return true;
  74. }
  75. return false;
  76. }
  77. // CefLifeSpanHandler methods:
  78. virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE
  79. {
  80. CEF_REQUIRE_UI_THREAD();
  81. }
  82. virtual bool DoClose(CefRefPtr<CefBrowser> browser) OVERRIDE
  83. {
  84. return false;
  85. }
  86. virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) OVERRIDE
  87. {
  88. CEF_REQUIRE_UI_THREAD();
  89. if (browser->IsSame(browser_))
  90. browser_ = nullptr;
  91. }
  92. void CloseBrowser(bool force_close)
  93. {
  94. if (!CefCurrentlyOn(TID_UI))
  95. {
  96. // Execute on the UI thread.
  97. CefPostTask(TID_UI,
  98. base::Bind(&WebClientPrivate::CloseBrowser, this, force_close));
  99. return;
  100. }
  101. if (!browser_.get())
  102. return;
  103. browser_->GetHost()->CloseBrowser(force_close);
  104. }
  105. IMPLEMENT_REFCOUNTING(WebClientPrivate);
  106. private:
  107. CefRefPtr<CefBrowser> browser_;
  108. WeakPtr<WebBrowserHost> webBrowserHost_;
  109. WeakPtr<WebClient> webClient_;
  110. };
  111. WebClient::WebClient(Context* context) : Object(context)
  112. {
  113. d_ = new WebClientPrivate(this);
  114. }
  115. WebClient::~WebClient()
  116. {
  117. renderHandler_ = 0;
  118. //d_->Release();
  119. }
  120. void WebClient::SendMouseClickEvent(int x, int y, unsigned button, bool mouseUp, unsigned modifier) const
  121. {
  122. if (!d_->browser_.get())
  123. return;
  124. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  125. CefMouseEvent mevent;
  126. mevent.x = x;
  127. mevent.y = y;
  128. mevent.modifiers = 0;
  129. //MBT_LEFT = 0,
  130. //MBT_MIDDLE,
  131. //MBT_RIGHT,
  132. host->SendMouseClickEvent(mevent, (CefBrowserHost::MouseButtonType) button, mouseUp, 1);
  133. }
  134. void WebClient::SendMouseMoveEvent(int x, int y, unsigned modifier, bool mouseLeave) const
  135. {
  136. if (!d_->browser_.get())
  137. return;
  138. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  139. CefMouseEvent mevent;
  140. mevent.x = x;
  141. mevent.y = y;
  142. mevent.modifiers = 0;
  143. Input* input = GetSubsystem<Input>();
  144. if (input->GetMouseButtonDown(MOUSEB_LEFT))
  145. mevent.modifiers |= EVENTFLAG_LEFT_MOUSE_BUTTON;
  146. if (input->GetMouseButtonDown(MOUSEB_MIDDLE))
  147. mevent.modifiers |= EVENTFLAG_MIDDLE_MOUSE_BUTTON;
  148. if (input->GetMouseButtonDown(MOUSEB_RIGHT))
  149. mevent.modifiers |= EVENTFLAG_RIGHT_MOUSE_BUTTON;
  150. host->SendMouseMoveEvent(mevent, mouseLeave);
  151. }
  152. void WebClient::SendMouseWheelEvent(int x, int y, unsigned modifier,int deltaX, int deltaY) const
  153. {
  154. if (!d_->browser_.get())
  155. return;
  156. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  157. CefMouseEvent mevent;
  158. mevent.x = x;
  159. mevent.y = y;
  160. mevent.modifiers = 0;
  161. #ifdef ATOMIC_PLATFORM_OSX
  162. deltaY = -deltaY;
  163. #endif
  164. host->SendMouseWheelEvent(mevent, deltaX, deltaY * 5);
  165. }
  166. /*
  167. EVENTFLAG_CAPS_LOCK_ON = 1 << 0,
  168. EVENTFLAG_SHIFT_DOWN = 1 << 1,
  169. EVENTFLAG_CONTROL_DOWN = 1 << 2,
  170. EVENTFLAG_ALT_DOWN = 1 << 3,
  171. EVENTFLAG_LEFT_MOUSE_BUTTON = 1 << 4,
  172. EVENTFLAG_MIDDLE_MOUSE_BUTTON = 1 << 5,
  173. EVENTFLAG_RIGHT_MOUSE_BUTTON = 1 << 6,
  174. // Mac OS-X command key.
  175. EVENTFLAG_COMMAND_DOWN = 1 << 7,
  176. EVENTFLAG_NUM_LOCK_ON = 1 << 8,
  177. EVENTFLAG_IS_KEY_PAD = 1 << 9,
  178. EVENTFLAG_IS_LEFT = 1 << 10,
  179. EVENTFLAG_IS_RIGHT = 1 << 11,
  180. } cef_event_flags_t;
  181. */
  182. void WebClient::SendKeyEvent(int scanCode, int qual, bool keyUp)
  183. {
  184. if (!d_->browser_.get())
  185. return;
  186. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  187. CefKeyEvent keyEvent;
  188. if (keyUp)
  189. return;
  190. // handle return special
  191. if (scanCode == SDL_SCANCODE_RETURN)
  192. {
  193. keyEvent.type = KEYEVENT_CHAR;
  194. keyEvent.character = 13;
  195. host->SendKeyEvent(keyEvent);
  196. return;
  197. }
  198. unsigned modifiers = EVENTFLAG_NONE;
  199. if (qual & QUAL_SHIFT)
  200. modifiers |= EVENTFLAG_SHIFT_DOWN;
  201. if (qual & QUAL_ALT)
  202. modifiers |= EVENTFLAG_ALT_DOWN;
  203. if (qual & QUAL_CTRL)
  204. modifiers |= EVENTFLAG_CONTROL_DOWN;
  205. #ifdef ATOMIC_PLATFORM_OSX
  206. Input* input = GetSubsystem<Input>();
  207. if (input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI))
  208. {
  209. modifiers |= EVENTFLAG_COMMAND_DOWN;
  210. }
  211. #endif
  212. keyEvent.modifiers = modifiers;
  213. int nativeKeyCode = GetNativeKeyFromSDLScanCode(scanCode);
  214. if (nativeKeyCode == -1)
  215. return;
  216. /*
  217. target->type = src->type;
  218. target->modifiers = src->modifiers;
  219. target->windows_key_code = src->windows_key_code;
  220. target->native_key_code = src->native_key_code;
  221. target->is_system_key = src->is_system_key;
  222. target->character = src->character;
  223. target->unmodified_character = src->unmodified_character;
  224. */
  225. keyEvent.type = keyUp ? KEYEVENT_KEYUP : KEYEVENT_KEYDOWN;
  226. keyEvent.native_key_code = nativeKeyCode;
  227. host->SendKeyEvent(keyEvent);
  228. #ifdef ATOMIC_PLATFORM_OSX
  229. // Send an empty key event on OSX, which seems to fix
  230. // keyboard problems on OSX with cefclient
  231. // ./cefclient --off-screen-rendering-enabled
  232. // return does not work at all on cef client with offscreen
  233. // bad interaction with arrow keys (for example here, after
  234. // hitting arrow keys, return/text takes a couple presses to register
  235. keyEvent.type = keyUp ? KEYEVENT_KEYUP : KEYEVENT_KEYDOWN;
  236. keyEvent.modifiers = 0;
  237. keyEvent.native_key_code = 0;
  238. host->SendKeyEvent(keyEvent);
  239. #endif
  240. }
  241. void WebClient::SendTextEvent(const String& text, unsigned modifiers)
  242. {
  243. if (!d_->browser_.get())
  244. return;
  245. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  246. CefKeyEvent keyEvent;
  247. keyEvent.type = KEYEVENT_CHAR;
  248. keyEvent.character = text[0];
  249. host->SendKeyEvent(keyEvent);
  250. }
  251. void WebClient::ShortcutCut()
  252. {
  253. if (!d_->browser_.get())
  254. return;
  255. d_->browser_->GetFocusedFrame()->Cut();
  256. }
  257. void WebClient::ShortcutCopy()
  258. {
  259. if (!d_->browser_.get())
  260. return;
  261. d_->browser_->GetFocusedFrame()->Copy();
  262. }
  263. void WebClient::ShortcutPaste()
  264. {
  265. if (!d_->browser_.get())
  266. return;
  267. d_->browser_->GetFocusedFrame()->Paste();
  268. }
  269. void WebClient::ShortcutSelectAll()
  270. {
  271. if (!d_->browser_.get())
  272. return;
  273. d_->browser_->GetFocusedFrame()->SelectAll();
  274. }
  275. void WebClient::ShortcutUndo()
  276. {
  277. if (!d_->browser_.get())
  278. return;
  279. d_->browser_->GetFocusedFrame()->Undo();
  280. }
  281. void WebClient::ShortcutRedo()
  282. {
  283. if (!d_->browser_.get())
  284. return;
  285. d_->browser_->GetFocusedFrame()->Redo();
  286. }
  287. void WebClient::ShortcutDelete()
  288. {
  289. if (!d_->browser_.get())
  290. return;
  291. d_->browser_->GetFocusedFrame()->Delete();
  292. }
  293. void WebClient::WasResized()
  294. {
  295. if (!d_->browser_.get())
  296. return;
  297. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  298. host->WasResized();;
  299. }
  300. bool WebClient::CreateBrowser(const String& initialURL, int width, int height)
  301. {
  302. return d_->CreateBrowser(initialURL, width, height);
  303. }
  304. void WebClient::SetSize(int width, int height)
  305. {
  306. if (renderHandler_.Null())
  307. return;
  308. if (renderHandler_->GetWidth() == width && renderHandler_->GetHeight() == height)
  309. return;
  310. renderHandler_->SetSize(width, height);
  311. WasResized();
  312. }
  313. void WebClient::SetWebRenderHandler(WebRenderHandler* handler)
  314. {
  315. handler->SetWebClient(this);
  316. renderHandler_ = handler;
  317. }
  318. CefClient* WebClient::GetCefClient()
  319. {
  320. return d_;
  321. }
  322. }