OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/message_pump_glib.h" | 5 #include "base/message_pump_glib.h" |
6 | 6 |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <math.h> | 8 #include <math.h> |
9 | 9 |
10 #include <gtk/gtk.h> | 10 #include <gtk/gtk.h> |
11 #include <glib.h> | 11 #include <glib.h> |
12 | 12 |
| 13 #include <string> |
| 14 #include <vector> |
| 15 |
13 #include "base/eintr_wrapper.h" | 16 #include "base/eintr_wrapper.h" |
14 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/command_line.h" |
15 #include "base/threading/platform_thread.h" | 19 #include "base/threading/platform_thread.h" |
16 | 20 |
17 namespace { | 21 namespace { |
18 | 22 |
| 23 // TODO(jamiewalch): This is pretty much a direct copy of |
| 24 // gfx::GtkInitFromCommandLine. The fact that it calls gdk_init instead of |
| 25 // gtk_init is probably unimportant as the latter calls the former, but |
| 26 // we can't have base depending on ui so we can't use the function directly. |
| 27 void GdkInitFromCommandLine(const CommandLine& command_line) { |
| 28 const std::vector<std::string>& args = command_line.argv(); |
| 29 int argc = args.size(); |
| 30 scoped_array<char *> argv(new char *[argc + 1]); |
| 31 for (size_t i = 0; i < args.size(); ++i) { |
| 32 // gdk_init makes no const-ness guarantees so best to be safe here. If it |
| 33 // doesn't delete the arguments it consumes, then there is a small memory |
| 34 // leak, but the alternative is far more severe. |
| 35 argv[i] = strdup(args[i].c_str()); |
| 36 } |
| 37 argv[argc] = NULL; |
| 38 char **argv_pointer = argv.get(); |
| 39 |
| 40 gdk_init(&argc, &argv_pointer); |
| 41 // gfx::GtkInitFromCommandLine uses args.size() instead of argc for the loop |
| 42 // limit, which I believe is wrong, though probably benign. |
| 43 for (int i = 0; i < argc; ++i) { |
| 44 free(argv[i]); |
| 45 } |
| 46 } |
| 47 |
19 // We send a byte across a pipe to wakeup the event loop. | 48 // We send a byte across a pipe to wakeup the event loop. |
20 const char kWorkScheduled = '\0'; | 49 const char kWorkScheduled = '\0'; |
21 | 50 |
22 // Return a timeout suitable for the glib loop, -1 to block forever, | 51 // Return a timeout suitable for the glib loop, -1 to block forever, |
23 // 0 to return right away, or a timeout in milliseconds from now. | 52 // 0 to return right away, or a timeout in milliseconds from now. |
24 int GetTimeIntervalMilliseconds(const base::TimeTicks& from) { | 53 int GetTimeIntervalMilliseconds(const base::TimeTicks& from) { |
25 if (from.is_null()) | 54 if (from.is_null()) |
26 return -1; | 55 return -1; |
27 | 56 |
28 // Be careful here. TimeDelta has a precision of microseconds, but we want a | 57 // Be careful here. TimeDelta has a precision of microseconds, but we want a |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 // This keeps the state of whether the pump got signaled that there was new | 166 // This keeps the state of whether the pump got signaled that there was new |
138 // work to be done. Since we eat the message on the wake up pipe as soon as | 167 // work to be done. Since we eat the message on the wake up pipe as soon as |
139 // we get it, we keep that state here to stay consistent. | 168 // we get it, we keep that state here to stay consistent. |
140 bool has_work; | 169 bool has_work; |
141 }; | 170 }; |
142 | 171 |
143 MessagePumpForUI::MessagePumpForUI() | 172 MessagePumpForUI::MessagePumpForUI() |
144 : state_(NULL), | 173 : state_(NULL), |
145 context_(g_main_context_default()), | 174 context_(g_main_context_default()), |
146 wakeup_gpollfd_(new GPollFD) { | 175 wakeup_gpollfd_(new GPollFD) { |
| 176 // Initialize gdk. |
| 177 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 178 GdkInitFromCommandLine(*command_line); |
147 // Create our wakeup pipe, which is used to flag when work was scheduled. | 179 // Create our wakeup pipe, which is used to flag when work was scheduled. |
148 int fds[2]; | 180 int fds[2]; |
149 CHECK_EQ(pipe(fds), 0); | 181 CHECK_EQ(pipe(fds), 0); |
150 wakeup_pipe_read_ = fds[0]; | 182 wakeup_pipe_read_ = fds[0]; |
151 wakeup_pipe_write_ = fds[1]; | 183 wakeup_pipe_write_ = fds[1]; |
152 wakeup_gpollfd_->fd = wakeup_pipe_read_; | 184 wakeup_gpollfd_->fd = wakeup_pipe_read_; |
153 wakeup_gpollfd_->events = G_IO_IN; | 185 wakeup_gpollfd_->events = G_IO_IN; |
154 | 186 |
155 work_source_ = g_source_new(&WorkSourceFuncs, sizeof(WorkSource)); | 187 work_source_ = g_source_new(&WorkSourceFuncs, sizeof(WorkSource)); |
156 static_cast<WorkSource*>(work_source_)->pump = this; | 188 static_cast<WorkSource*>(work_source_)->pump = this; |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 FOR_EACH_OBSERVER(Observer, observers_, DidProcessEvent(event)); | 387 FOR_EACH_OBSERVER(Observer, observers_, DidProcessEvent(event)); |
356 } | 388 } |
357 | 389 |
358 // static | 390 // static |
359 void MessagePumpForUI::EventDispatcher(GdkEvent* event, gpointer data) { | 391 void MessagePumpForUI::EventDispatcher(GdkEvent* event, gpointer data) { |
360 MessagePumpForUI* message_pump = reinterpret_cast<MessagePumpForUI*>(data); | 392 MessagePumpForUI* message_pump = reinterpret_cast<MessagePumpForUI*>(data); |
361 message_pump->DispatchEvents(event); | 393 message_pump->DispatchEvents(event); |
362 } | 394 } |
363 | 395 |
364 } // namespace base | 396 } // namespace base |
OLD | NEW |