Mailbox support for texture layers.
[chromium-blink-merge.git] / ui / ui_controls / ui_controls_mac.mm
blob71bd272d7cbf2c4c8cf893902011a633c49ad690
1 // Copyright (c) 2012 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 "ui/ui_controls/ui_controls.h"
7 #import <Cocoa/Cocoa.h>
8 #include <mach/mach_time.h>
9 #include <vector>
11 #include "base/bind.h"
12 #include "base/callback.h"
13 #include "base/message_loop.h"
14 #include "ui/base/keycodes/keyboard_code_conversion_mac.h"
17 // Implementation details: We use [NSApplication sendEvent:] instead
18 // of [NSApplication postEvent:atStart:] so that the event gets sent
19 // immediately.  This lets us run the post-event task right
20 // immediately as well.  Unfortunately I cannot subclass NSEvent (it's
21 // probably a class cluster) to allow other easy answers.  For
22 // example, if I could subclass NSEvent, I could run the Task in it's
23 // dealloc routine (which necessarily happens after the event is
24 // dispatched).  Unlike Linux, Mac does not have message loop
25 // observer/notification.  Unlike windows, I cannot post non-events
26 // into the event queue.  (I can post other kinds of tasks but can't
27 // guarantee their order with regards to events).
29 // But [NSApplication sendEvent:] causes a problem when sending mouse click
30 // events. Because in order to handle mouse drag, when processing a mouse
31 // click event, the application may want to retrieve the next event
32 // synchronously by calling NSApplication's nextEventMatchingMask method.
33 // In this case, [NSApplication sendEvent:] causes deadlock.
34 // So we need to use [NSApplication postEvent:atStart:] for mouse click
35 // events. In order to notify the caller correctly after all events has been
36 // processed, we setup a task to watch for the event queue time to time and
37 // notify the caller as soon as there is no event in the queue.
39 // TODO(suzhe):
40 // 1. Investigate why using [NSApplication postEvent:atStart:] for keyboard
41 //    events causes BrowserKeyEventsTest.CommandKeyEvents to fail.
42 //    See http://crbug.com/49270
43 // 2. On OSX 10.6, [NSEvent addLocalMonitorForEventsMatchingMask:handler:] may
44 //    be used, so that we don't need to poll the event queue time to time.
46 namespace {
48 // From
49 // http://stackoverflow.com/questions/1597383/cgeventtimestamp-to-nsdate
50 // Which credits Apple sample code for this routine.
51 uint64_t UpTimeInNanoseconds(void) {
52   uint64_t time;
53   uint64_t timeNano;
54   static mach_timebase_info_data_t sTimebaseInfo;
56   time = mach_absolute_time();
58   // Convert to nanoseconds.
60   // If this is the first time we've run, get the timebase.
61   // We can use denom == 0 to indicate that sTimebaseInfo is
62   // uninitialised because it makes no sense to have a zero
63   // denominator is a fraction.
64   if (sTimebaseInfo.denom == 0) {
65     (void) mach_timebase_info(&sTimebaseInfo);
66   }
68   // This could overflow; for testing needs we probably don't care.
69   timeNano = time * sTimebaseInfo.numer / sTimebaseInfo.denom;
70   return timeNano;
73 NSTimeInterval TimeIntervalSinceSystemStartup() {
74   return UpTimeInNanoseconds() / 1000000000.0;
77 // Creates and returns an autoreleased key event.
78 NSEvent* SynthesizeKeyEvent(NSWindow* window,
79                             bool keyDown,
80                             ui::KeyboardCode keycode,
81                             NSUInteger flags) {
82   unichar character;
83   unichar characterIgnoringModifiers;
84   int macKeycode = ui::MacKeyCodeForWindowsKeyCode(
85       keycode, flags, &character, &characterIgnoringModifiers);
87   if (macKeycode < 0)
88     return nil;
90   NSString* charactersIgnoringModifiers =
91       [[[NSString alloc] initWithCharacters:&characterIgnoringModifiers
92                                      length:1]
93         autorelease];
94   NSString* characters =
95       [[[NSString alloc] initWithCharacters:&character length:1] autorelease];
97   NSEventType type = (keyDown ? NSKeyDown : NSKeyUp);
99   // Modifier keys generate NSFlagsChanged event rather than
100   // NSKeyDown/NSKeyUp events.
101   if (keycode == ui::VKEY_CONTROL || keycode == ui::VKEY_SHIFT ||
102       keycode == ui::VKEY_MENU || keycode == ui::VKEY_COMMAND)
103     type = NSFlagsChanged;
105   // For events other than mouse moved, [event locationInWindow] is
106   // UNDEFINED if the event is not NSMouseMoved.  Thus, the (0,0)
107   // location should be fine.
108   NSEvent* event =
109       [NSEvent keyEventWithType:type
110                        location:NSMakePoint(0, 0)
111                   modifierFlags:flags
112                       timestamp:TimeIntervalSinceSystemStartup()
113                    windowNumber:[window windowNumber]
114                         context:nil
115                      characters:characters
116     charactersIgnoringModifiers:charactersIgnoringModifiers
117                       isARepeat:NO
118                         keyCode:(unsigned short)macKeycode];
120   return event;
123 // Creates the proper sequence of autoreleased key events for a key down + up.
124 void SynthesizeKeyEventsSequence(NSWindow* window,
125                                  ui::KeyboardCode keycode,
126                                  bool control,
127                                  bool shift,
128                                  bool alt,
129                                  bool command,
130                                  std::vector<NSEvent*>* events) {
131   NSEvent* event = nil;
132   NSUInteger flags = 0;
133   if (control) {
134     flags |= NSControlKeyMask;
135     event = SynthesizeKeyEvent(window, true, ui::VKEY_CONTROL, flags);
136     DCHECK(event);
137     events->push_back(event);
138   }
139   if (shift) {
140     flags |= NSShiftKeyMask;
141     event = SynthesizeKeyEvent(window, true, ui::VKEY_SHIFT, flags);
142     DCHECK(event);
143     events->push_back(event);
144   }
145   if (alt) {
146     flags |= NSAlternateKeyMask;
147     event = SynthesizeKeyEvent(window, true, ui::VKEY_MENU, flags);
148     DCHECK(event);
149     events->push_back(event);
150   }
151   if (command) {
152     flags |= NSCommandKeyMask;
153     event = SynthesizeKeyEvent(window, true, ui::VKEY_COMMAND, flags);
154     DCHECK(event);
155     events->push_back(event);
156   }
158   event = SynthesizeKeyEvent(window, true, keycode, flags);
159   DCHECK(event);
160   events->push_back(event);
161   event = SynthesizeKeyEvent(window, false, keycode, flags);
162   DCHECK(event);
163   events->push_back(event);
165   if (command) {
166     flags &= ~NSCommandKeyMask;
167     event = SynthesizeKeyEvent(window, false, ui::VKEY_COMMAND, flags);
168     DCHECK(event);
169     events->push_back(event);
170   }
171   if (alt) {
172     flags &= ~NSAlternateKeyMask;
173     event = SynthesizeKeyEvent(window, false, ui::VKEY_MENU, flags);
174     DCHECK(event);
175     events->push_back(event);
176   }
177   if (shift) {
178     flags &= ~NSShiftKeyMask;
179     event = SynthesizeKeyEvent(window, false, ui::VKEY_SHIFT, flags);
180     DCHECK(event);
181     events->push_back(event);
182   }
183   if (control) {
184     flags &= ~NSControlKeyMask;
185     event = SynthesizeKeyEvent(window, false, ui::VKEY_CONTROL, flags);
186     DCHECK(event);
187     events->push_back(event);
188   }
191 // A helper function to watch for the event queue. The specific task will be
192 // fired when there is no more event in the queue.
193 void EventQueueWatcher(const base::Closure& task) {
194   NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask
195                                       untilDate:nil
196                                          inMode:NSDefaultRunLoopMode
197                                         dequeue:NO];
198   // If there is still event in the queue, then we need to check again.
199   if (event) {
200     MessageLoop::current()->PostTask(
201         FROM_HERE,
202         base::Bind(&EventQueueWatcher, task));
203   } else {
204     MessageLoop::current()->PostTask(FROM_HERE, task);
205   }
208 // Stores the current mouse location on the screen. So that we can use it
209 // when firing keyboard and mouse click events.
210 NSPoint g_mouse_location = { 0, 0 };
212 }  // namespace
214 namespace ui_controls {
216 bool SendKeyPress(gfx::NativeWindow window,
217                   ui::KeyboardCode key,
218                   bool control,
219                   bool shift,
220                   bool alt,
221                   bool command) {
222   return SendKeyPressNotifyWhenDone(window, key,
223                                     control, shift, alt, command,
224                                     base::Closure());
227 // Win and Linux implement a SendKeyPress() this as a
228 // SendKeyPressAndRelease(), so we should as well (despite the name).
229 bool SendKeyPressNotifyWhenDone(gfx::NativeWindow window,
230                                 ui::KeyboardCode key,
231                                 bool control,
232                                 bool shift,
233                                 bool alt,
234                                 bool command,
235                                 const base::Closure& task) {
236   DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type());
238   std::vector<NSEvent*> events;
239   SynthesizeKeyEventsSequence(
240       window, key, control, shift, alt, command, &events);
242   // TODO(suzhe): Using [NSApplication postEvent:atStart:] here causes
243   // BrowserKeyEventsTest.CommandKeyEvents to fail. See http://crbug.com/49270
244   // But using [NSApplication sendEvent:] should be safe for keyboard events,
245   // because until now, no code wants to retrieve the next event when handling
246   // a keyboard event.
247   for (std::vector<NSEvent*>::iterator iter = events.begin();
248        iter != events.end(); ++iter)
249     [[NSApplication sharedApplication] sendEvent:*iter];
251   if (!task.is_null()) {
252     MessageLoop::current()->PostTask(
253         FROM_HERE, base::Bind(&EventQueueWatcher, task));
254   }
256   return true;
259 bool SendMouseMove(long x, long y) {
260   return SendMouseMoveNotifyWhenDone(x, y, base::Closure());
263 // Input position is in screen coordinates.  However, NSMouseMoved
264 // events require them window-relative, so we adjust.  We *DO* flip
265 // the coordinate space, so input events can be the same for all
266 // platforms.  E.g. (0,0) is upper-left.
267 bool SendMouseMoveNotifyWhenDone(long x, long y, const base::Closure& task) {
268   NSWindow* window = [[NSApplication sharedApplication] keyWindow];
269   CGFloat screenHeight =
270     [[[NSScreen screens] objectAtIndex:0] frame].size.height;
271   g_mouse_location = NSMakePoint(x, screenHeight - y);  // flip!
272   NSPoint pointInWindow = g_mouse_location;
273   if (window)
274     pointInWindow = [window convertScreenToBase:pointInWindow];
275   NSTimeInterval timestamp = TimeIntervalSinceSystemStartup();
277   NSEvent* event =
278       [NSEvent mouseEventWithType:NSMouseMoved
279                          location:pointInWindow
280                     modifierFlags:0
281                         timestamp:timestamp
282                      windowNumber:[window windowNumber]
283                           context:nil
284                       eventNumber:0
285                        clickCount:0
286                          pressure:0.0];
287   [[NSApplication sharedApplication] postEvent:event atStart:NO];
289   if (!task.is_null()) {
290     MessageLoop::current()->PostTask(
291         FROM_HERE, base::Bind(&EventQueueWatcher, task));
292   }
294   return true;
297 bool SendMouseEvents(MouseButton type, int state) {
298   return SendMouseEventsNotifyWhenDone(type, state, base::Closure());
301 bool SendMouseEventsNotifyWhenDone(MouseButton type, int state,
302                                    const base::Closure& task) {
303   // On windows it appears state can be (UP|DOWN).  It is unclear if
304   // that'll happen here but prepare for it just in case.
305   if (state == (UP|DOWN)) {
306     return (SendMouseEventsNotifyWhenDone(type, DOWN, base::Closure()) &&
307             SendMouseEventsNotifyWhenDone(type, UP, task));
308   }
309   NSEventType etype = 0;
310   if (type == LEFT) {
311     if (state == UP) {
312       etype = NSLeftMouseUp;
313     } else {
314       etype = NSLeftMouseDown;
315     }
316   } else if (type == MIDDLE) {
317     if (state == UP) {
318       etype = NSOtherMouseUp;
319     } else {
320       etype = NSOtherMouseDown;
321     }
322   } else if (type == RIGHT) {
323     if (state == UP) {
324       etype = NSRightMouseUp;
325     } else {
326       etype = NSRightMouseDown;
327     }
328   } else {
329     return false;
330   }
331   NSWindow* window = [[NSApplication sharedApplication] keyWindow];
332   NSPoint pointInWindow = g_mouse_location;
333   if (window)
334     pointInWindow = [window convertScreenToBase:pointInWindow];
336   NSEvent* event =
337       [NSEvent mouseEventWithType:etype
338                          location:pointInWindow
339                     modifierFlags:0
340                         timestamp:TimeIntervalSinceSystemStartup()
341                      windowNumber:[window windowNumber]
342                           context:nil
343                       eventNumber:0
344                        clickCount:1
345                          pressure:(state == DOWN ? 1.0 : 0.0 )];
346   [[NSApplication sharedApplication] postEvent:event atStart:NO];
348   if (!task.is_null()) {
349     MessageLoop::current()->PostTask(
350         FROM_HERE, base::Bind(&EventQueueWatcher, task));
351   }
353   return true;
356 bool SendMouseClick(MouseButton type) {
357  return SendMouseEventsNotifyWhenDone(type, UP|DOWN, base::Closure());
360 }  // namespace ui_controls