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.
5 #include "base/time/default_tick_clock.h"
6 #include "ui/display/chromeos/x11/native_display_event_dispatcher_x11.h"
7 #include "ui/display/chromeos/x11/display_mode_x11.h"
8 #include "ui/display/chromeos/x11/display_snapshot_x11.h"
10 #include <X11/extensions/Xrandr.h>
15 const int NativeDisplayEventDispatcherX11::kUseCacheAfterStartupMs = 7000;
17 NativeDisplayEventDispatcherX11::NativeDisplayEventDispatcherX11(
18 NativeDisplayDelegateX11::HelperDelegate* delegate,
19 int xrandr_event_base)
20 : delegate_(delegate),
21 xrandr_event_base_(xrandr_event_base),
22 tick_clock_(new base::DefaultTickClock) {
23 startup_time_ = tick_clock_->NowTicks();
26 NativeDisplayEventDispatcherX11::~NativeDisplayEventDispatcherX11() {}
28 uint32_t NativeDisplayEventDispatcherX11::Dispatch(
29 const base::NativeEvent& event) {
30 if (event->type - xrandr_event_base_ == RRScreenChangeNotify) {
31 VLOG(1) << "Received RRScreenChangeNotify event";
32 delegate_->UpdateXRandRConfiguration(event);
33 return POST_DISPATCH_PERFORM_DEFAULT;
36 // Bail out early for everything except RRNotify_OutputChange events
37 // about an output getting connected or disconnected.
38 if (event->type - xrandr_event_base_ != RRNotify)
39 return POST_DISPATCH_PERFORM_DEFAULT;
40 const XRRNotifyEvent* notify_event = reinterpret_cast<XRRNotifyEvent*>(event);
41 if (notify_event->subtype != RRNotify_OutputChange)
42 return POST_DISPATCH_PERFORM_DEFAULT;
43 const XRROutputChangeNotifyEvent* output_change_event =
44 reinterpret_cast<XRROutputChangeNotifyEvent*>(event);
45 const int action = output_change_event->connection;
46 if (action != RR_Connected && action != RR_Disconnected)
47 return POST_DISPATCH_PERFORM_DEFAULT;
49 const bool connected = (action == RR_Connected);
50 VLOG(1) << "Received RRNotify_OutputChange event:"
51 << " output=" << output_change_event->output
52 << " crtc=" << output_change_event->crtc
53 << " mode=" << output_change_event->mode
54 << " action=" << (connected ? "connected" : "disconnected");
56 bool check_cache = (tick_clock_->NowTicks() - startup_time_)
57 .InMilliseconds() <= kUseCacheAfterStartupMs;
60 bool found_changed_output = false;
61 const std::vector<DisplaySnapshot*>& cached_outputs =
62 delegate_->GetCachedOutputs();
63 for (std::vector<DisplaySnapshot*>::const_iterator it =
64 cached_outputs.begin();
65 it != cached_outputs.end();
67 const DisplaySnapshotX11* x11_output =
68 static_cast<const DisplaySnapshotX11*>(*it);
69 const DisplayModeX11* x11_mode =
70 static_cast<const DisplayModeX11*>(x11_output->current_mode());
72 if (x11_output->output() == output_change_event->output) {
73 if (connected && x11_output->crtc() == output_change_event->crtc &&
74 x11_mode->mode_id() == output_change_event->mode) {
75 VLOG(1) << "Ignoring event describing already-cached state";
76 return POST_DISPATCH_PERFORM_DEFAULT;
78 found_changed_output = true;
83 if (!connected && !found_changed_output) {
84 VLOG(1) << "Ignoring event describing already-disconnected output";
85 return POST_DISPATCH_PERFORM_DEFAULT;
89 delegate_->NotifyDisplayObservers();
91 return POST_DISPATCH_PERFORM_DEFAULT;
94 void NativeDisplayEventDispatcherX11::SetTickClockForTest(
95 scoped_ptr<base::TickClock> tick_clock) {
96 tick_clock_ = tick_clock.Pass();
97 startup_time_ = tick_clock_->NowTicks();