Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: ui/display/chromeos/x11/native_display_event_dispatcher_x11.cc

Issue 2540313002: Split //ui/display and create //ui/display/manager. (Closed)
Patch Set: Cleanup export header. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 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/display/chromeos/x11/native_display_event_dispatcher_x11.h"
6
7 #include <X11/extensions/Xrandr.h>
8 #include <utility>
9
10 #include "base/time/default_tick_clock.h"
11 #include "ui/display/chromeos/x11/display_mode_x11.h"
12 #include "ui/display/chromeos/x11/display_snapshot_x11.h"
13 #include "ui/events/platform/platform_event_source.h"
14
15 namespace ui {
16
17 // static
18 const int NativeDisplayEventDispatcherX11::kUseCacheAfterStartupMs = 7000;
19
20 NativeDisplayEventDispatcherX11::NativeDisplayEventDispatcherX11(
21 NativeDisplayDelegateX11::HelperDelegate* delegate,
22 int xrandr_event_base)
23 : delegate_(delegate),
24 xrandr_event_base_(xrandr_event_base),
25 tick_clock_(new base::DefaultTickClock) {
26 startup_time_ = tick_clock_->NowTicks();
27 }
28
29 NativeDisplayEventDispatcherX11::~NativeDisplayEventDispatcherX11() {}
30
31 bool NativeDisplayEventDispatcherX11::CanDispatchEvent(
32 const PlatformEvent& event) {
33 return (event->type - xrandr_event_base_ == RRScreenChangeNotify) ||
34 (event->type - xrandr_event_base_ == RRNotify);
35 }
36
37 uint32_t NativeDisplayEventDispatcherX11::DispatchEvent(
38 const PlatformEvent& event) {
39 if (event->type - xrandr_event_base_ == RRScreenChangeNotify) {
40 VLOG(1) << "Received RRScreenChangeNotify event";
41 delegate_->UpdateXRandRConfiguration(event);
42 return ui::POST_DISPATCH_PERFORM_DEFAULT;
43 }
44
45 // Bail out early for everything except RRNotify_OutputChange events
46 // about an output getting connected or disconnected.
47 if (event->type - xrandr_event_base_ != RRNotify)
48 return ui::POST_DISPATCH_PERFORM_DEFAULT;
49 const XRRNotifyEvent* notify_event = reinterpret_cast<XRRNotifyEvent*>(event);
50 if (notify_event->subtype != RRNotify_OutputChange)
51 return ui::POST_DISPATCH_PERFORM_DEFAULT;
52 const XRROutputChangeNotifyEvent* output_change_event =
53 reinterpret_cast<XRROutputChangeNotifyEvent*>(event);
54 const int action = output_change_event->connection;
55 if (action != RR_Connected && action != RR_Disconnected)
56 return ui::POST_DISPATCH_PERFORM_DEFAULT;
57
58 const bool connected = (action == RR_Connected);
59 VLOG(1) << "Received RRNotify_OutputChange event:"
60 << " output=" << output_change_event->output
61 << " crtc=" << output_change_event->crtc
62 << " mode=" << output_change_event->mode
63 << " action=" << (connected ? "connected" : "disconnected");
64
65 bool check_cache = (tick_clock_->NowTicks() - startup_time_)
66 .InMilliseconds() <= kUseCacheAfterStartupMs;
67
68 if (check_cache) {
69 bool found_changed_output = false;
70 const std::vector<DisplaySnapshot*>& cached_outputs =
71 delegate_->GetCachedDisplays();
72 for (std::vector<DisplaySnapshot*>::const_iterator it =
73 cached_outputs.begin();
74 it != cached_outputs.end();
75 ++it) {
76 const DisplaySnapshotX11* x11_output =
77 static_cast<const DisplaySnapshotX11*>(*it);
78 const DisplayModeX11* x11_mode =
79 static_cast<const DisplayModeX11*>(x11_output->current_mode());
80 RRMode mode_id = x11_mode ? x11_mode->mode_id() : None;
81
82 // Update if we failed to fetch the external display's ID before.
83 // Internal display's EDID should always be available.
84 bool display_id_needs_update =
85 x11_output->type() != ui::DISPLAY_CONNECTION_TYPE_INTERNAL &&
86 !x11_output->display_id();
87
88 if (x11_output->output() == output_change_event->output) {
89 if (connected && x11_output->crtc() == output_change_event->crtc &&
90 mode_id == output_change_event->mode &&
91 !display_id_needs_update) {
92 VLOG(1) << "Ignoring event describing already-cached state";
93 return POST_DISPATCH_PERFORM_DEFAULT;
94 }
95 found_changed_output = true;
96 break;
97 }
98 }
99
100 if (!connected && !found_changed_output) {
101 VLOG(1) << "Ignoring event describing already-disconnected output";
102 return ui::POST_DISPATCH_PERFORM_DEFAULT;
103 }
104 }
105
106 delegate_->NotifyDisplayObservers();
107
108 return ui::POST_DISPATCH_PERFORM_DEFAULT;
109 }
110
111 void NativeDisplayEventDispatcherX11::SetTickClockForTest(
112 std::unique_ptr<base::TickClock> tick_clock) {
113 tick_clock_ = std::move(tick_clock);
114 startup_time_ = tick_clock_->NowTicks();
115 }
116
117 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698