OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 "base/message_pump_glib_x.h" |
| 6 |
| 7 #include <gdk/gdkx.h> |
| 8 #include <X11/Xlib.h> |
| 9 |
| 10 namespace { |
| 11 |
| 12 gboolean PlaceholderDispatch(GSource* source, |
| 13 GSourceFunc cb, |
| 14 gpointer data) { |
| 15 return TRUE; |
| 16 } |
| 17 |
| 18 } // namespace |
| 19 |
| 20 namespace base { |
| 21 |
| 22 MessagePumpGlibX::MessagePumpGlibX() : base::MessagePumpForUI(), |
| 23 gdksource_(NULL), |
| 24 dispatching_event_(false), |
| 25 capture_x_events_(0), |
| 26 capture_gdk_events_(0) { |
| 27 gdk_event_handler_set(&EventDispatcherX, this, NULL); |
| 28 |
| 29 InitializeEventsToCapture(); |
| 30 } |
| 31 |
| 32 MessagePumpGlibX::~MessagePumpGlibX() { |
| 33 } |
| 34 |
| 35 bool MessagePumpGlibX::RunOnce(GMainContext* context, bool block) { |
| 36 GdkDisplay* gdisp = gdk_display_get_default(); |
| 37 Display* display = GDK_DISPLAY_XDISPLAY(gdisp); |
| 38 if (XPending(display)) { |
| 39 XEvent xev; |
| 40 XPeekEvent(display, &xev); |
| 41 if (capture_x_events_[xev.type]) { |
| 42 XNextEvent(display, &xev); |
| 43 |
| 44 DLOG(INFO) << "nom noming event"; |
| 45 |
| 46 // TODO(sad): Create a GdkEvent from |xev| and pass it on to |
| 47 // EventDispatcherX. The ultimate goal is to create a views::Event from |
| 48 // |xev| and send it to a rootview. When done, the preceding DLOG will be |
| 49 // removed. |
| 50 } else { |
| 51 // TODO(sad): A couple of extra events can still sneak in during this |
| 52 g_main_context_iteration(context, FALSE); |
| 53 } |
| 54 } |
| 55 |
| 56 bool retvalue; |
| 57 if (gdksource_) { |
| 58 // Replace the dispatch callback of the GDK event source temporarily so that |
| 59 // it doesn't read events from X. |
| 60 gboolean (*cb)(GSource*, GSourceFunc, void*) = |
| 61 gdksource_->source_funcs->dispatch; |
| 62 gdksource_->source_funcs->dispatch = PlaceholderDispatch; |
| 63 |
| 64 dispatching_event_ = true; |
| 65 retvalue = g_main_context_iteration(context, block); |
| 66 dispatching_event_ = false; |
| 67 |
| 68 gdksource_->source_funcs->dispatch = cb; |
| 69 } else { |
| 70 retvalue = g_main_context_iteration(context, block); |
| 71 } |
| 72 |
| 73 return retvalue; |
| 74 } |
| 75 |
| 76 void MessagePumpGlibX::InitializeEventsToCapture(void) { |
| 77 // TODO(sad): Decide which events we want to capture and update the tables |
| 78 // accordingly. |
| 79 capture_x_events_[KeyPress] = true; |
| 80 capture_gdk_events_[GDK_KEY_PRESS] = true; |
| 81 |
| 82 capture_x_events_[KeyRelease] = true; |
| 83 capture_gdk_events_[GDK_KEY_RELEASE] = true; |
| 84 } |
| 85 |
| 86 void MessagePumpGlibX::EventDispatcherX(GdkEvent* event, gpointer data) { |
| 87 MessagePumpGlibX* pump_x = reinterpret_cast<MessagePumpGlibX*>(data); |
| 88 |
| 89 if (!pump_x->gdksource_) { |
| 90 pump_x->gdksource_ = g_main_current_source(); |
| 91 } else if (!pump_x->IsDispatchingEvent()) { |
| 92 if (event->type != GDK_NOTHING && |
| 93 pump_x->capture_gdk_events_[event->type]) { |
| 94 // TODO(sad): An X event is caught by the GDK handler. Put it back in the |
| 95 // X queue so that we catch it in the next iteration. When done, the |
| 96 // following DLOG statement will be removed. |
| 97 DLOG(INFO) << "GDK ruined it!!"; |
| 98 } |
| 99 } |
| 100 |
| 101 pump_x->DispatchEvents(event); |
| 102 } |
| 103 |
| 104 } // namespace base |
OLD | NEW |