| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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/base/x/x11_window_cache_event_source.h" |
| 6 |
| 7 #include <glib.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "ui/base/x/x11_window_cache.h" |
| 11 |
| 12 namespace ui { |
| 13 |
| 14 namespace { |
| 15 |
| 16 struct GLibXcbSource : public GSource { |
| 17 GPollFD* poll_fd; |
| 18 }; |
| 19 |
| 20 gboolean callback(gpointer data) { |
| 21 XWindowCache* cache = reinterpret_cast<XWindowCache*>(data); |
| 22 cache->OnConnectionReadable(); |
| 23 return !cache->ConnectionHasError(); |
| 24 } |
| 25 |
| 26 gboolean prepare(GSource* source, gint* timeout) { |
| 27 *timeout = -1; |
| 28 return FALSE; |
| 29 } |
| 30 |
| 31 gboolean check(GSource* source) { |
| 32 return static_cast<GLibXcbSource*>(source)->poll_fd->revents & G_IO_IN; |
| 33 } |
| 34 |
| 35 gboolean dispatch(GSource* source, GSourceFunc callback, gpointer data) { |
| 36 return callback(data); |
| 37 } |
| 38 |
| 39 GSourceFuncs source_funcs {prepare, check, dispatch, nullptr}; |
| 40 |
| 41 } // anonymous namespace |
| 42 |
| 43 XWindowCacheEventSource::XWindowCacheEventSource(XWindowCache* cache) { |
| 44 DCHECK(!cache->ConnectionHasError()); |
| 45 int fd = cache->ConnectionFd(); |
| 46 |
| 47 GPollFD* poll = new GPollFD(); |
| 48 g_poll_.reset(poll); |
| 49 poll->fd = fd; |
| 50 poll->events = G_IO_IN; |
| 51 poll->revents = 0; |
| 52 |
| 53 GLibXcbSource* g_source = static_cast<GLibXcbSource*>( |
| 54 g_source_new(&source_funcs, sizeof(GLibXcbSource))); |
| 55 g_source_ = g_source; |
| 56 g_source->poll_fd = poll; |
| 57 g_source_add_poll(g_source, poll); |
| 58 g_source_set_can_recurse(g_source, TRUE); |
| 59 g_source_set_callback(g_source, callback, cache, nullptr); |
| 60 g_source_attach(g_source, g_main_context_default()); |
| 61 } |
| 62 |
| 63 XWindowCacheEventSource::~XWindowCacheEventSource() { |
| 64 g_source_destroy(g_source_); |
| 65 g_source_unref(g_source_); |
| 66 } |
| 67 |
| 68 } // namespace ui |
| OLD | NEW |