1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
|
/*
* Copyright (C) 2008 Apple Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#if ENABLE(NETSCAPE_PLUGIN_API) && !defined(__LP64__)
#import "WebNetscapePluginEventHandlerCarbon.h"
#import "WebNetscapePluginView.h"
#import "WebKitLogging.h"
#import "WebKitSystemInterface.h"
// Send null events 50 times a second when active, so plug-ins like Flash get high frame rates.
#define NullEventIntervalActive 0.02
#define NullEventIntervalNotActive 0.25
WebNetscapePluginEventHandlerCarbon::WebNetscapePluginEventHandlerCarbon(WebNetscapePluginView* pluginView)
: WebNetscapePluginEventHandler(pluginView)
, m_keyEventHandler(0)
, m_suspendKeyUpEvents(false)
{
}
static void getCarbonEvent(EventRecord* carbonEvent)
{
carbonEvent->what = nullEvent;
carbonEvent->message = 0;
carbonEvent->when = TickCount();
GetGlobalMouse(&carbonEvent->where);
carbonEvent->modifiers = GetCurrentKeyModifiers();
if (!Button())
carbonEvent->modifiers |= btnState;
}
static EventModifiers modifiersForEvent(NSEvent *event)
{
EventModifiers modifiers;
unsigned int modifierFlags = [event modifierFlags];
NSEventType eventType = [event type];
modifiers = 0;
if (eventType != NSLeftMouseDown && eventType != NSRightMouseDown)
modifiers |= btnState;
if (modifierFlags & NSCommandKeyMask)
modifiers |= cmdKey;
if (modifierFlags & NSShiftKeyMask)
modifiers |= shiftKey;
if (modifierFlags & NSAlphaShiftKeyMask)
modifiers |= alphaLock;
if (modifierFlags & NSAlternateKeyMask)
modifiers |= optionKey;
if (modifierFlags & NSControlKeyMask || eventType == NSRightMouseDown)
modifiers |= controlKey;
return modifiers;
}
static void getCarbonEvent(EventRecord *carbonEvent, NSEvent *cocoaEvent)
{
if (WKConvertNSEventToCarbonEvent(carbonEvent, cocoaEvent))
return;
NSPoint where = [[cocoaEvent window] convertBaseToScreen:[cocoaEvent locationInWindow]];
carbonEvent->what = nullEvent;
carbonEvent->message = 0;
carbonEvent->when = (UInt32)([cocoaEvent timestamp] * 60); // seconds to ticks
carbonEvent->where.h = (short)where.x;
carbonEvent->where.v = (short)(NSMaxY([(NSScreen *)[[NSScreen screens] objectAtIndex:0] frame]) - where.y);
carbonEvent->modifiers = modifiersForEvent(cocoaEvent);
}
void WebNetscapePluginEventHandlerCarbon::sendNullEvent()
{
EventRecord event;
getCarbonEvent(&event);
// Plug-in should not react to cursor position when not active or when a menu is down.
MenuTrackingData trackingData;
OSStatus error = GetMenuTrackingData(NULL, &trackingData);
// Plug-in should not react to cursor position when the actual window is not key.
if (![[m_pluginView window] isKeyWindow] || (error == noErr && trackingData.menu)) {
// FIXME: Does passing a v and h of -1 really prevent it from reacting to the cursor position?
event.where.v = -1;
event.where.h = -1;
}
sendEvent(&event);
}
void WebNetscapePluginEventHandlerCarbon::drawRect(CGContextRef, const NSRect&)
{
EventRecord event;
getCarbonEvent(&event);
event.what = updateEvt;
WindowRef windowRef = (WindowRef)[[m_pluginView window] windowRef];
event.message = (unsigned long)windowRef;
BOOL acceptedEvent;
acceptedEvent = sendEvent(&event);
LOG(PluginEvents, "NPP_HandleEvent(updateEvt): %d", acceptedEvent);
}
void WebNetscapePluginEventHandlerCarbon::mouseDown(NSEvent* theEvent)
{
EventRecord event;
getCarbonEvent(&event, theEvent);
event.what = ::mouseDown;
BOOL acceptedEvent;
acceptedEvent = sendEvent(&event);
LOG(PluginEvents, "NPP_HandleEvent(mouseDown): %d pt.v=%d, pt.h=%d", acceptedEvent, event.where.v, event.where.h);
}
void WebNetscapePluginEventHandlerCarbon::mouseUp(NSEvent* theEvent)
{
EventRecord event;
getCarbonEvent(&event, theEvent);
event.what = ::mouseUp;
BOOL acceptedEvent;
acceptedEvent = sendEvent(&event);
LOG(PluginEvents, "NPP_HandleEvent(mouseUp): %d pt.v=%d, pt.h=%d", acceptedEvent, event.where.v, event.where.h);
}
bool WebNetscapePluginEventHandlerCarbon::scrollWheel(NSEvent* theEvent)
{
return false;
}
void WebNetscapePluginEventHandlerCarbon::mouseEntered(NSEvent* theEvent)
{
EventRecord event;
getCarbonEvent(&event, theEvent);
event.what = NPEventType_AdjustCursorEvent;
BOOL acceptedEvent;
acceptedEvent = sendEvent(&event);
LOG(PluginEvents, "NPP_HandleEvent(mouseEntered): %d", acceptedEvent);
}
void WebNetscapePluginEventHandlerCarbon::mouseExited(NSEvent* theEvent)
{
EventRecord event;
getCarbonEvent(&event, theEvent);
event.what = NPEventType_AdjustCursorEvent;
BOOL acceptedEvent;
acceptedEvent = sendEvent(&event);
LOG(PluginEvents, "NPP_HandleEvent(mouseExited): %d", acceptedEvent);
}
void WebNetscapePluginEventHandlerCarbon::mouseDragged(NSEvent*)
{
}
void WebNetscapePluginEventHandlerCarbon::mouseMoved(NSEvent* theEvent)
{
EventRecord event;
getCarbonEvent(&event, theEvent);
event.what = NPEventType_AdjustCursorEvent;
BOOL acceptedEvent;
acceptedEvent = sendEvent(&event);
LOG(PluginEvents, "NPP_HandleEvent(mouseMoved): %d", acceptedEvent);
}
void WebNetscapePluginEventHandlerCarbon::keyDown(NSEvent *theEvent)
{
m_suspendKeyUpEvents = true;
WKSendKeyEventToTSM(theEvent);
}
void WebNetscapePluginEventHandlerCarbon::syntheticKeyDownWithCommandModifier(int keyCode, char character)
{
EventRecord event;
getCarbonEvent(&event);
event.what = ::keyDown;
event.modifiers |= cmdKey;
event.message = keyCode << 8 | character;
sendEvent(&event);
}
static UInt32 keyMessageForEvent(NSEvent *event)
{
NSData *data = [[event characters] dataUsingEncoding:CFStringConvertEncodingToNSStringEncoding(CFStringGetSystemEncoding())];
if (!data)
return 0;
UInt8 characterCode;
[data getBytes:&characterCode length:1];
UInt16 keyCode = [event keyCode];
return keyCode << 8 | characterCode;
}
void WebNetscapePluginEventHandlerCarbon::keyUp(NSEvent* theEvent)
{
WKSendKeyEventToTSM(theEvent);
// TSM won't send keyUp events so we have to send them ourselves.
// Only send keyUp events after we receive the TSM callback because this is what plug-in expect from OS 9.
if (!m_suspendKeyUpEvents) {
EventRecord event;
getCarbonEvent(&event, theEvent);
event.what = ::keyUp;
if (event.message == 0)
event.message = keyMessageForEvent(theEvent);
sendEvent(&event);
}
}
void WebNetscapePluginEventHandlerCarbon::flagsChanged(NSEvent*)
{
}
void WebNetscapePluginEventHandlerCarbon::focusChanged(bool hasFocus)
{
EventRecord event;
getCarbonEvent(&event);
bool acceptedEvent;
if (hasFocus) {
event.what = NPEventType_GetFocusEvent;
acceptedEvent = sendEvent(&event);
LOG(PluginEvents, "NPP_HandleEvent(NPEventType_GetFocusEvent): %d", acceptedEvent);
installKeyEventHandler();
} else {
event.what = NPEventType_LoseFocusEvent;
acceptedEvent = sendEvent(&event);
LOG(PluginEvents, "NPP_HandleEvent(NPEventType_LoseFocusEvent): %d", acceptedEvent);
removeKeyEventHandler();
}
}
void WebNetscapePluginEventHandlerCarbon::windowFocusChanged(bool hasFocus)
{
WindowRef windowRef = (WindowRef)[[m_pluginView window] windowRef];
SetUserFocusWindow(windowRef);
EventRecord event;
getCarbonEvent(&event);
event.what = activateEvt;
event.message = (unsigned long)windowRef;
if (hasFocus)
event.modifiers |= activeFlag;
BOOL acceptedEvent;
acceptedEvent = sendEvent(&event);
LOG(PluginEvents, "NPP_HandleEvent(activateEvent): %d isActive: %d", acceptedEvent, hasFocus);
}
OSStatus WebNetscapePluginEventHandlerCarbon::TSMEventHandler(EventHandlerCallRef inHandlerRef, EventRef inEvent, void *eventHandler)
{
EventRef rawKeyEventRef;
OSStatus status = GetEventParameter(inEvent, kEventParamTextInputSendKeyboardEvent, typeEventRef, NULL, sizeof(EventRef), NULL, &rawKeyEventRef);
if (status != noErr) {
LOG_ERROR("GetEventParameter failed with error: %d", status);
return noErr;
}
// Two-pass read to allocate/extract Mac charCodes
ByteCount numBytes;
status = GetEventParameter(rawKeyEventRef, kEventParamKeyMacCharCodes, typeChar, NULL, 0, &numBytes, NULL);
if (status != noErr) {
LOG_ERROR("GetEventParameter failed with error: %d", status);
return noErr;
}
char *buffer = (char *)malloc(numBytes);
status = GetEventParameter(rawKeyEventRef, kEventParamKeyMacCharCodes, typeChar, NULL, numBytes, NULL, buffer);
if (status != noErr) {
LOG_ERROR("GetEventParameter failed with error: %d", status);
free(buffer);
return noErr;
}
EventRef cloneEvent = CopyEvent(rawKeyEventRef);
unsigned i;
for (i = 0; i < numBytes; i++) {
status = SetEventParameter(cloneEvent, kEventParamKeyMacCharCodes, typeChar, 1 /* one char code */, &buffer[i]);
if (status != noErr) {
LOG_ERROR("SetEventParameter failed with error: %d", status);
free(buffer);
return noErr;
}
EventRecord eventRec;
if (ConvertEventRefToEventRecord(cloneEvent, &eventRec)) {
BOOL acceptedEvent;
acceptedEvent = static_cast<WebNetscapePluginEventHandlerCarbon*>(eventHandler)->sendEvent(&eventRec);
LOG(PluginEvents, "NPP_HandleEvent(keyDown): %d charCode:%c keyCode:%lu",
acceptedEvent, (char) (eventRec.message & charCodeMask), (eventRec.message & keyCodeMask));
// We originally thought that if the plug-in didn't accept this event,
// we should pass it along so that keyboard scrolling, for example, will work.
// In practice, this is not a good idea, because plug-ins tend to eat the event but return false.
// MacIE handles each key event twice because of this, but we will emulate the other browsers instead.
}
}
ReleaseEvent(cloneEvent);
free(buffer);
return noErr;
}
void WebNetscapePluginEventHandlerCarbon::installKeyEventHandler()
{
static const EventTypeSpec sTSMEvents[] =
{
{ kEventClassTextInput, kEventTextInputUnicodeForKeyEvent }
};
if (!m_keyEventHandler) {
InstallEventHandler(GetWindowEventTarget((WindowRef)[[m_pluginView window] windowRef]),
NewEventHandlerUPP(TSMEventHandler),
GetEventTypeCount(sTSMEvents),
sTSMEvents,
this,
&m_keyEventHandler);
}
}
void WebNetscapePluginEventHandlerCarbon::removeKeyEventHandler()
{
if (m_keyEventHandler) {
RemoveEventHandler(m_keyEventHandler);
m_keyEventHandler = 0;
}
}
void WebNetscapePluginEventHandlerCarbon::nullEventTimerFired(CFRunLoopTimerRef timerRef, void *context)
{
static_cast<WebNetscapePluginEventHandlerCarbon*>(context)->sendNullEvent();
}
void WebNetscapePluginEventHandlerCarbon::startTimers(bool throttleTimers)
{
ASSERT(!m_nullEventTimer);
CFTimeInterval interval = !throttleTimers ? NullEventIntervalActive : NullEventIntervalNotActive;
CFRunLoopTimerContext context = { 0, this, NULL, NULL, NULL };
m_nullEventTimer = adoptCF(CFRunLoopTimerCreate(0, CFAbsoluteTimeGetCurrent() + interval, interval,
0, 0, nullEventTimerFired, &context));
CFRunLoopAddTimer(CFRunLoopGetCurrent(), m_nullEventTimer.get(), kCFRunLoopDefaultMode);
}
void WebNetscapePluginEventHandlerCarbon::stopTimers()
{
if (!m_nullEventTimer)
return;
CFRunLoopTimerInvalidate(m_nullEventTimer.get());
m_nullEventTimer = 0;
}
void* WebNetscapePluginEventHandlerCarbon::platformWindow(NSWindow* window)
{
return [window windowRef];
}
bool WebNetscapePluginEventHandlerCarbon::sendEvent(EventRecord* event)
{
// If at any point the user clicks or presses a key from within a plugin, set the
// currentEventIsUserGesture flag to true. This is important to differentiate legitimate
// window.open() calls; we still want to allow those. See rdar://problem/4010765
if (event->what == ::mouseDown || event->what == ::keyDown || event->what == ::mouseUp || event->what == ::autoKey)
m_currentEventIsUserGesture = true;
m_suspendKeyUpEvents = false;
bool result = [m_pluginView sendEvent:event isDrawRect:event->what == updateEvt];
m_currentEventIsUserGesture = false;
return result;
}
#endif // ENABLE(NETSCAPE_PLUGIN_API) && !defined(__LP64__)
|