env.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/env.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/env_observer.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/root_window_host.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/window.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/compositor/compositor.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/compositor/compositor_switches.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(USE_X11)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/message_pump_aurax11.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace aura {
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Env* Env::instance_ = NULL;
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Env, public:
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Env::Env()
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : mouse_button_flags_(0),
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      is_touch_down_(false),
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      render_white_bg_(true) {
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Env::~Env() {
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(USE_X11)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::MessagePumpAuraX11::Current()->RemoveObserver(
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &device_list_updater_aurax11_);
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FOR_EACH_OBSERVER(EnvObserver, observers_, OnWillDestroyEnv());
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ui::Compositor::Terminate();
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Env* Env::GetInstance() {
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!instance_) {
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    instance_ = new Env;
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    instance_->Init();
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return instance_;
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Env::DeleteInstance() {
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  delete instance_;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  instance_ = NULL;
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Env::AddObserver(EnvObserver* observer) {
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  observers_.AddObserver(observer);
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Env::RemoveObserver(EnvObserver* observer) {
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  observers_.RemoveObserver(observer);
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if !defined(OS_MACOSX)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)MessageLoop::Dispatcher* Env::GetDispatcher() {
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(USE_X11)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return base::MessagePumpAuraX11::Current();
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return dispatcher_.get();
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void Env::RootWindowActivated(RootWindow* root_window) {
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FOR_EACH_OBSERVER(EnvObserver, observers_,
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                    OnRootWindowActivated(root_window));
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Env, private:
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Env::Init() {
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if !defined(USE_X11)
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  dispatcher_.reset(CreateDispatcher());
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(USE_X11)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We can't do this with a root window listener because XI_HierarchyChanged
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // messages don't have a target window.
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::MessagePumpAuraX11::Current()->AddObserver(
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &device_list_updater_aurax11_);
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ui::Compositor::Initialize(
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CommandLine::ForCurrentProcess()->HasSwitch(
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          switches::kUIEnableThreadedCompositing));
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Env::NotifyWindowInitialized(Window* window) {
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FOR_EACH_OBSERVER(EnvObserver, observers_, OnWindowInitialized(window));
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void Env::NotifyRootWindowInitialized(RootWindow* root_window) {
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FOR_EACH_OBSERVER(EnvObserver,
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                    observers_,
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                    OnRootWindowInitialized(root_window));
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Env, ui::EventTarget implementation:
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool Env::CanAcceptEvent(const ui::Event& event) {
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ui::EventTarget* Env::GetParentTarget() {
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NULL;
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace aura
121