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 "ash/wm/system_gesture_event_filter.h" 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/accelerators/accelerator_controller.h" 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/accelerators/accelerator_table.h" 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/ash_switches.h" 10d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)#include "ash/metrics/user_metrics_recorder.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/root_window_controller.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/shell.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/shell_window_ids.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/gestures/long_press_affordance_handler.h" 154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "ash/wm/gestures/overview_gesture_handler.h" 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/gestures/system_pinch_handler.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/gestures/two_finger_drag_handler.h" 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/window_util.h" 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h" 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/root_window.h" 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/ui_base_switches.h" 22d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "ui/events/event.h" 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_CHROMEOS) 2568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "ui/events/x/touch_factory_x11.h" 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace { 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)aura::Window* GetTargetForSystemGestureEvent(aura::Window* target) { 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) aura::Window* system_target = target; 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!system_target || system_target == target->GetRootWindow()) 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) system_target = ash::wm::GetActiveWindow(); 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (system_target) 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) system_target = system_target->GetToplevelWindow(); 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return system_target; 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace ash { 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace internal { 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SystemGestureEventFilter::SystemGestureEventFilter() 452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) : system_gestures_enabled_(CommandLine::ForCurrentProcess()-> 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) HasSwitch(ash::switches::kAshEnableAdvancedGestures)), 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) long_press_affordance_(new LongPressAffordanceHandler), 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) two_finger_drag_(new TwoFingerDragHandler) { 494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) if (switches::UseOverviewMode()) 504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) overview_gesture_handler_.reset(new OverviewGestureHandler); 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SystemGestureEventFilter::~SystemGestureEventFilter() { 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void SystemGestureEventFilter::OnMouseEvent(ui::MouseEvent* event) { 57eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_CHROMEOS) && !defined(USE_OZONE) 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (event->type() == ui::ET_MOUSE_PRESSED && event->native_event() && 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ui::TouchFactory::GetInstance()->IsTouchDevicePresent() && 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Shell::GetInstance()->delegate()) { 61d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles) Shell::GetInstance()->metrics()->RecordUserMetricsAction(UMA_MOUSE_DOWN); 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void SystemGestureEventFilter::OnScrollEvent(ui::ScrollEvent* event) { 670f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) if (overview_gesture_handler_ && 680f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) overview_gesture_handler_->ProcessScrollEvent(*event)) { 690f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) event->StopPropagation(); 700f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) return; 710f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) } 720f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)} 730f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void SystemGestureEventFilter::OnTouchEvent(ui::TouchEvent* event) { 752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) aura::Window* target = static_cast<aura::Window*>(event->target()); 76eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch ash::TouchUMA::GetInstance()->RecordTouchEvent(target, *event); 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void SystemGestureEventFilter::OnGestureEvent(ui::GestureEvent* event) { 802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) aura::Window* target = static_cast<aura::Window*>(event->target()); 81eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch ash::TouchUMA::GetInstance()->RecordGestureEvent(target, *event); 82f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) long_press_affordance_->ProcessEvent(target, event); 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (two_finger_drag_->ProcessGestureEvent(target, *event)) { 852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) event->StopPropagation(); 862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return; 872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) if (overview_gesture_handler_ && 904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) overview_gesture_handler_->ProcessGestureEvent(*event)) { 914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) event->StopPropagation(); 924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) return; 934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!system_gestures_enabled_) 962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return; 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) aura::Window* system_target = GetTargetForSystemGestureEvent(target); 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!system_target) 1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return; 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) RootWindowController* root_controller = 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GetRootWindowController(system_target->GetRootWindow()); 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) CHECK(root_controller); 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) aura::Window* desktop_container = root_controller->GetContainer( 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ash::internal::kShellWindowId_DesktopBackgroundContainer); 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (desktop_container->Contains(system_target)) { 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The gesture was on the desktop window. 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (event->type() == ui::ET_GESTURE_MULTIFINGER_SWIPE && 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) event->details().swipe_up() && 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) event->details().touch_points() == 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) SystemPinchHandler::kSystemGesturePoints) { 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ash::AcceleratorController* accelerator = 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ash::Shell::GetInstance()->accelerator_controller(); 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (accelerator->PerformAction(CYCLE_FORWARD_MRU, ui::Accelerator())) 1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) event->StopPropagation(); 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return; 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) WindowPinchHandlerMap::iterator find = pinch_handlers_.find(system_target); 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (find != pinch_handlers_.end()) { 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) SystemGestureStatus status = 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) (*find).second->ProcessGestureEvent(*event); 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (status == SYSTEM_GESTURE_END) 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ClearGestureHandlerForWindow(system_target); 1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) event->StopPropagation(); 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } else { 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (event->type() == ui::ET_GESTURE_BEGIN && 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) event->details().touch_points() >= 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) SystemPinchHandler::kSystemGesturePoints) { 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) pinch_handlers_[system_target] = new SystemPinchHandler(system_target); 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) system_target->AddObserver(this); 1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) event->StopPropagation(); 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void SystemGestureEventFilter::OnWindowVisibilityChanged(aura::Window* window, 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool visible) { 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!visible) 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ClearGestureHandlerForWindow(window); 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void SystemGestureEventFilter::OnWindowDestroying(aura::Window* window) { 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ClearGestureHandlerForWindow(window); 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void SystemGestureEventFilter::ClearGestureHandlerForWindow( 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) aura::Window* window) { 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) WindowPinchHandlerMap::iterator find = pinch_handlers_.find(window); 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (find == pinch_handlers_.end()) { 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The handler may have already been removed. 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) delete (*find).second; 1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) pinch_handlers_.erase(find); 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) window->RemoveObserver(this); 1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace internal 1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace ash 162