blob: 21dc226e9ff32af8a85e0feff36d4eb888a918e5 [file] [log] [blame]
lanwei2574b1b2015-04-29 17:42:551// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ui/events/base_event_utils.h"
6
7#include "base/atomic_sequence_num.h"
afakhrye8eac602015-11-10 02:31:228#include "base/command_line.h"
majidvpc9cc06af2016-06-24 21:58:309#include "base/lazy_instance.h"
lanwei2574b1b2015-04-29 17:42:5510#include "base/logging.h"
majidvp9b3bda82016-06-09 18:12:0911#include "base/time/time.h"
avidf8474b2015-12-24 03:49:5012#include "build/build_config.h"
afakhry8518ee72015-09-04 22:50:0413#include "ui/events/event_constants.h"
afakhrye8eac602015-11-10 02:31:2214#include "ui/events/event_switches.h"
lanwei2574b1b2015-04-29 17:42:5515
16namespace ui {
17
afakhry8518ee72015-09-04 22:50:0418namespace {
19
20#if defined(OS_CHROMEOS)
21const int kSystemKeyModifierMask = EF_ALT_DOWN | EF_COMMAND_DOWN;
mblshae405f4e2016-11-25 05:32:3122#elif defined(OS_MACOSX)
23// Alt modifier is used to input extended characters on Mac.
24const int kSystemKeyModifierMask = EF_COMMAND_DOWN;
afakhry8518ee72015-09-04 22:50:0425#else
26const int kSystemKeyModifierMask = EF_ALT_DOWN;
mblshae405f4e2016-11-25 05:32:3127#endif // !defined(OS_CHROMEOS) && !defined(OS_MACOSX)
afakhry8518ee72015-09-04 22:50:0428
29} // namespace
30
tzik96977a152017-07-13 05:03:4431base::AtomicSequenceNumber g_next_event_id;
lanwei2574b1b2015-04-29 17:42:5532
avidf8474b2015-12-24 03:49:5033uint32_t GetNextTouchEventId() {
lanwei2574b1b2015-04-29 17:42:5534 // Set the first touch event ID to 1 because we set id to 0 for other types
35 // of events.
avidf8474b2015-12-24 03:49:5036 uint32_t id = g_next_event_id.GetNext();
lanwei2574b1b2015-04-29 17:42:5537 if (id == 0)
38 id = g_next_event_id.GetNext();
39 DCHECK_NE(0U, id);
40 return id;
41}
42
afakhry8518ee72015-09-04 22:50:0443bool IsSystemKeyModifier(int flags) {
afakhry3d96df642015-09-18 16:00:2544 // AltGr modifier is used to type alternative keys on certain keyboard layouts
45 // so we don't consider keys with the AltGr modifier as a system key.
46 return (kSystemKeyModifierMask & flags) != 0 &&
47 (EF_ALTGR_DOWN & flags) == 0;
afakhry8518ee72015-09-04 22:50:0448}
49
Greg Thompsonaa48ce8d2018-04-03 06:11:4350base::LazyInstance<const base::TickClock*>::Leaky g_tick_clock =
majidvpc9cc06af2016-06-24 21:58:3051 LAZY_INSTANCE_INITIALIZER;
52
53base::TimeTicks EventTimeForNow() {
54 return g_tick_clock.Get() ? g_tick_clock.Get()->NowTicks()
55 : base::TimeTicks::Now();
56}
57
Greg Thompsonaa48ce8d2018-04-03 06:11:4358void SetEventTickClockForTesting(const base::TickClock* tick_clock) {
tzik8529bdd82018-02-28 05:47:2159 g_tick_clock.Get() = tick_clock;
majidvpc9cc06af2016-06-24 21:58:3060}
61
majidvp9b3bda82016-06-09 18:12:0962double EventTimeStampToSeconds(base::TimeTicks time_stamp) {
63 return (time_stamp - base::TimeTicks()).InSecondsF();
64}
65
66base::TimeTicks EventTimeStampFromSeconds(double time_stamp_seconds) {
67 return base::TimeTicks() + base::TimeDelta::FromSecondsD(time_stamp_seconds);
68}
69
lanwei2574b1b2015-04-29 17:42:5570} // namespace ui