summaryrefslogtreecommitdiffstats
path: root/WebKitTools/DumpRenderTree/gtk/EventSender.cpp
blob: 6268b5b0fa8fbbed99bb38384641500efef5226e (plain)
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/*
 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
 * Copyright (C) 2009 Zan Dobersek <zandobersek@gmail.com>
 * Copyright (C) 2009 Holger Hans Peter Freyther
 *
 * 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.
 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "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 OR ITS 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.
 */

#include "config.h"
#include "EventSender.h"

#include "DumpRenderTree.h"

#include <JavaScriptCore/JSObjectRef.h>
#include <JavaScriptCore/JSRetainPtr.h>
#include <JavaScriptCore/JSStringRef.h>
#include <webkit/webkitwebframe.h>
#include <webkit/webkitwebview.h>
#include <wtf/ASCIICType.h>
#include <wtf/Platform.h>

#include <gdk/gdk.h>
#include <gdk/gdkkeysyms.h>
#include <string.h>

// TODO: Currently drag and drop related code is left out and
// should be merged once we have drag and drop support in WebCore.

extern "C" {
    extern void webkit_web_frame_layout(WebKitWebFrame* frame);
}

static bool down = false;
static bool currentEventButton = 1;
static bool dragMode = true;
static bool replayingSavedEvents = false;
static int lastMousePositionX;
static int lastMousePositionY;

static int lastClickPositionX;
static int lastClickPositionY;
static int clickCount = 0;

struct DelayedMessage {
    GdkEvent event;
    gulong delay;
    gboolean isDragEvent;
};

static DelayedMessage msgQueue[1024];

static unsigned endOfQueue;
static unsigned startOfQueue;

static const float zoomMultiplierRatio = 1.2f;

static JSValueRef getDragModeCallback(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
{
    return JSValueMakeBoolean(context, dragMode);
}

static bool setDragModeCallback(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
{
    dragMode = JSValueToBoolean(context, value);
    return true;
}

static JSValueRef leapForwardCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
    // FIXME: Add proper support for forward leaps
    return JSValueMakeUndefined(context);
}

static JSValueRef contextClickCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
    webkit_web_frame_layout(mainFrame);

    WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
    if (!view)
        return JSValueMakeUndefined(context);

    GdkEvent event;
    memset(&event, 0, sizeof(event));
    event.button.button = 3;
    event.button.x = lastMousePositionX;
    event.button.y = lastMousePositionY;
    event.button.window = GTK_WIDGET(view)->window;

    gboolean return_val;
    down = true;
    event.type = GDK_BUTTON_PRESS;
    g_signal_emit_by_name(view, "button_press_event", &event, &return_val);

    down = false;
    event.type = GDK_BUTTON_RELEASE;
    g_signal_emit_by_name(view, "button_release_event", &event, &return_val);

    return JSValueMakeUndefined(context);
}

static void updateClickCount(int /* button */)
{
    // FIXME: take the last clicked button number and the time of last click into account.
    if (lastClickPositionX != lastMousePositionX || lastClickPositionY != lastMousePositionY)
        clickCount = 1;
    else
        clickCount++;
}

#if !GTK_CHECK_VERSION(2,17,3)
static void getRootCoords(GtkWidget* view, int* rootX, int* rootY)
{
    GtkWidget* window = gtk_widget_get_toplevel(GTK_WIDGET(view));
    int tmpX, tmpY;

    gtk_widget_translate_coordinates(view, window, lastMousePositionX, lastMousePositionY, &tmpX, &tmpY);

    gdk_window_get_origin(window->window, rootX, rootY);

    *rootX += tmpX;
    *rootY += tmpY;
}
#endif

static JSValueRef mouseDownCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
    WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
    if (!view)
        return JSValueMakeUndefined(context);

    down = true;

    GdkEvent event;
    memset(&event, 0, sizeof(event));
    event.type = GDK_BUTTON_PRESS;
    event.button.button = 1;

    if (argumentCount == 1) {
        event.button.button = (int)JSValueToNumber(context, arguments[0], exception) + 1;
        g_return_val_if_fail((!exception || !*exception), JSValueMakeUndefined(context));
    }

    currentEventButton = event.button.button;

    event.button.x = lastMousePositionX;
    event.button.y = lastMousePositionY;
    event.button.window = GTK_WIDGET(view)->window;
    event.button.time = GDK_CURRENT_TIME;
    event.button.device = gdk_device_get_core_pointer();

    int x_root, y_root;
#if GTK_CHECK_VERSION(2,17,3)
    gdk_window_get_root_coords(GTK_WIDGET(view)->window, lastMousePositionX, lastMousePositionY, &x_root, &y_root);
#else
    getRootCoords(GTK_WIDGET(view), &x_root, &y_root);
#endif

    event.button.x_root = x_root;
    event.button.y_root = y_root;

    updateClickCount(1);

    if (!msgQueue[endOfQueue].delay) {
        webkit_web_frame_layout(mainFrame);

        gboolean return_val;
        g_signal_emit_by_name(view, "button_press_event", &event, &return_val);
        if (clickCount == 2) {
            event.type = GDK_2BUTTON_PRESS;
            g_signal_emit_by_name(view, "button_press_event", &event, &return_val);
        }
    } else {
        // replaySavedEvents should have the required logic to make leapForward delays work
        msgQueue[endOfQueue++].event = event;
        replaySavedEvents();
    }

    return JSValueMakeUndefined(context);
}

static guint getStateFlags()
{
    guint state = 0;

    if (down) {
        if (currentEventButton == 1)
            state = GDK_BUTTON1_MASK;
        else if (currentEventButton == 2)
            state = GDK_BUTTON2_MASK;
        else if (currentEventButton == 3)
            state = GDK_BUTTON3_MASK;
    } else
        state = 0;

    return state;
}

static JSValueRef mouseUpCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{

    WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
    if (!view)
        return JSValueMakeUndefined(context);

    GdkEvent event;
    memset(&event, 0, sizeof(event));
    event.type = GDK_BUTTON_RELEASE;
    event.button.button = 1;

    if (argumentCount == 1) {
        event.button.button = (int)JSValueToNumber(context, arguments[0], exception) + 1;
        g_return_val_if_fail((!exception || !*exception), JSValueMakeUndefined(context));
    }

    currentEventButton = event.button.button;

    event.button.x = lastMousePositionX;
    event.button.y = lastMousePositionY;
    event.button.window = GTK_WIDGET(view)->window;
    event.button.time = GDK_CURRENT_TIME;
    event.button.device = gdk_device_get_core_pointer();
    event.button.state = getStateFlags();

    down = false;

    int x_root, y_root;
#if GTK_CHECK_VERSION(2,17,3)
    gdk_window_get_root_coords(GTK_WIDGET(view)->window, lastMousePositionX, lastMousePositionY, &x_root, &y_root);
#else
    getRootCoords(GTK_WIDGET(view), &x_root, &y_root);
#endif

    event.button.x_root = x_root;
    event.button.y_root = y_root;

    if ((dragMode && !replayingSavedEvents) || msgQueue[endOfQueue].delay) {
        msgQueue[endOfQueue].event = event;
        msgQueue[endOfQueue++].isDragEvent = true;
        replaySavedEvents();
    } else {
        webkit_web_frame_layout(mainFrame);

        gboolean return_val;
        g_signal_emit_by_name(view, "button_release_event", &event, &return_val);
    }

    lastClickPositionX = lastMousePositionX;
    lastClickPositionY = lastMousePositionY;

    return JSValueMakeUndefined(context);
}

static JSValueRef mouseMoveToCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
    WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
    if (!view)
        return JSValueMakeUndefined(context);

    if (argumentCount < 2)
        return JSValueMakeUndefined(context);

    lastMousePositionX = (int)JSValueToNumber(context, arguments[0], exception);
    g_return_val_if_fail((!exception || !*exception), JSValueMakeUndefined(context));
    lastMousePositionY = (int)JSValueToNumber(context, arguments[1], exception);
    g_return_val_if_fail((!exception || !*exception), JSValueMakeUndefined(context));

    GdkEvent event;
    memset(&event, 0, sizeof(event));
    event.type = GDK_MOTION_NOTIFY;
    event.motion.x = lastMousePositionX;
    event.motion.y = lastMousePositionY;
    event.motion.time = GDK_CURRENT_TIME;
    event.motion.window = GTK_WIDGET(view)->window;
    event.motion.device = gdk_device_get_core_pointer();

    int x_root, y_root;
#if GTK_CHECK_VERSION(2,17,3)
    gdk_window_get_root_coords(GTK_WIDGET(view)->window, lastMousePositionX, lastMousePositionY, &x_root, &y_root);
#else
    getRootCoords(GTK_WIDGET(view), &x_root, &y_root);
#endif

    event.motion.x_root = x_root;
    event.motion.y_root = y_root;
    
    event.motion.state = getStateFlags();

    if (dragMode && down && !replayingSavedEvents) {
        msgQueue[endOfQueue].event = event;
        msgQueue[endOfQueue++].isDragEvent = true;
    } else {
        webkit_web_frame_layout(mainFrame);

        gboolean return_val;
        g_signal_emit_by_name(view, "motion_notify_event", &event, &return_val);
    }

    return JSValueMakeUndefined(context);
}

static JSValueRef mouseWheelToCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
    WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
    if (!view)
        return JSValueMakeUndefined(context);

    if (argumentCount < 2)
        return JSValueMakeUndefined(context);

    int horizontal = (int)JSValueToNumber(context, arguments[0], exception);
    g_return_val_if_fail((!exception || !*exception), JSValueMakeUndefined(context));
    int vertical = (int)JSValueToNumber(context, arguments[1], exception);
    g_return_val_if_fail((!exception || !*exception), JSValueMakeUndefined(context));

    // GTK+ doesn't support multiple direction scrolls in the same event!
    g_return_val_if_fail((!vertical || !horizontal), JSValueMakeUndefined(context));

    GdkEvent event;
    event.type = GDK_SCROLL;
    event.scroll.x = lastMousePositionX;
    event.scroll.y = lastMousePositionY;
    event.scroll.time = GDK_CURRENT_TIME;
    event.scroll.window = GTK_WIDGET(view)->window;

    if (horizontal < 0)
        event.scroll.direction = GDK_SCROLL_LEFT;
    else if (horizontal > 0)
        event.scroll.direction = GDK_SCROLL_RIGHT;
    else if (vertical < 0)
        event.scroll.direction = GDK_SCROLL_UP;
    else if (vertical > 0)
        event.scroll.direction = GDK_SCROLL_DOWN;
    else
        g_assert_not_reached();

    if (dragMode && down && !replayingSavedEvents) {
        msgQueue[endOfQueue].event = event;
        msgQueue[endOfQueue++].isDragEvent = true;
    } else {
        webkit_web_frame_layout(mainFrame);
        gtk_main_do_event(&event);
    }

    return JSValueMakeUndefined(context);
}

static JSValueRef beginDragWithFilesCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
    if (argumentCount < 1)
        return JSValueMakeUndefined(context);

    // FIXME: Implement this completely once WebCore has complete drag and drop support
    return JSValueMakeUndefined(context);
}

void replaySavedEvents()
{
    // FIXME: This doesn't deal with forward leaps, but it should.

    WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
    if (!view)
        return;

    replayingSavedEvents = true;

    for (unsigned queuePos = 0; queuePos < endOfQueue; queuePos++) {
        GdkEvent event = msgQueue[queuePos].event;
        gboolean return_val;

        switch (event.type) {
        case GDK_BUTTON_RELEASE:
            g_signal_emit_by_name(view, "button_release_event", &event, &return_val);
            break;
        case GDK_BUTTON_PRESS:
            g_signal_emit_by_name(view, "button_press_event", &event, &return_val);
            break;
        case GDK_MOTION_NOTIFY:
            g_signal_emit_by_name(view, "motion_notify_event", &event, &return_val);
            break;
        default:
            continue;
        }

        startOfQueue++;
    }

    int numQueuedMessages = endOfQueue - startOfQueue;
    if (!numQueuedMessages) {
        startOfQueue = 0;
        endOfQueue = 0;
        replayingSavedEvents = false;
        return;
    }

    startOfQueue = 0;
    endOfQueue = 0;

    replayingSavedEvents = false;
}

static JSValueRef keyDownCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
    if (argumentCount < 1)
        return JSValueMakeUndefined(context);

    static const JSStringRef lengthProperty = JSStringCreateWithUTF8CString("length");

    webkit_web_frame_layout(mainFrame);

    // handle modifier keys.
    int state = 0;
    if (argumentCount > 1) {
        JSObjectRef modifiersArray = JSValueToObject(context, arguments[1], exception);
        if (modifiersArray) {
            for (int i = 0; i < JSValueToNumber(context, JSObjectGetProperty(context, modifiersArray, lengthProperty, 0), 0); ++i) {
                JSValueRef value = JSObjectGetPropertyAtIndex(context, modifiersArray, i, 0);
                JSStringRef string = JSValueToStringCopy(context, value, 0);
                if (JSStringIsEqualToUTF8CString(string, "ctrlKey"))
                    state |= GDK_CONTROL_MASK;
                else if (JSStringIsEqualToUTF8CString(string, "shiftKey"))
                    state |= GDK_SHIFT_MASK;
                else if (JSStringIsEqualToUTF8CString(string, "altKey"))
                    state |= GDK_MOD1_MASK;

                JSStringRelease(string);
            }
        }
    }

    JSStringRef character = JSValueToStringCopy(context, arguments[0], exception);
    g_return_val_if_fail((!exception || !*exception), JSValueMakeUndefined(context));
    int gdkKeySym;
    if (JSStringIsEqualToUTF8CString(character, "leftArrow"))
        gdkKeySym = GDK_Left;
    else if (JSStringIsEqualToUTF8CString(character, "rightArrow"))
        gdkKeySym = GDK_Right;
    else if (JSStringIsEqualToUTF8CString(character, "upArrow"))
        gdkKeySym = GDK_Up;
    else if (JSStringIsEqualToUTF8CString(character, "downArrow"))
        gdkKeySym = GDK_Down;
    else if (JSStringIsEqualToUTF8CString(character, "pageUp"))
        gdkKeySym = GDK_Page_Up;
    else if (JSStringIsEqualToUTF8CString(character, "pageDown"))
        gdkKeySym = GDK_Page_Down;
    else if (JSStringIsEqualToUTF8CString(character, "home"))
        gdkKeySym = GDK_Home;
    else if (JSStringIsEqualToUTF8CString(character, "end"))
        gdkKeySym = GDK_End;
    else if (JSStringIsEqualToUTF8CString(character, "delete"))
        gdkKeySym = GDK_BackSpace;
    else if (JSStringIsEqualToUTF8CString(character, "F1"))
        gdkKeySym = GDK_F1;
    else if (JSStringIsEqualToUTF8CString(character, "F2"))
        gdkKeySym = GDK_F2;
    else if (JSStringIsEqualToUTF8CString(character, "F3"))
        gdkKeySym = GDK_F3;
    else if (JSStringIsEqualToUTF8CString(character, "F4"))
        gdkKeySym = GDK_F4;
    else if (JSStringIsEqualToUTF8CString(character, "F5"))
        gdkKeySym = GDK_F5;
    else if (JSStringIsEqualToUTF8CString(character, "F6"))
        gdkKeySym = GDK_F6;
    else if (JSStringIsEqualToUTF8CString(character, "F7"))
        gdkKeySym = GDK_F7;
    else if (JSStringIsEqualToUTF8CString(character, "F8"))
        gdkKeySym = GDK_F8;
    else if (JSStringIsEqualToUTF8CString(character, "F9"))
        gdkKeySym = GDK_F9;
    else if (JSStringIsEqualToUTF8CString(character, "F10"))
        gdkKeySym = GDK_F10;
    else if (JSStringIsEqualToUTF8CString(character, "F11"))
        gdkKeySym = GDK_F11;
    else if (JSStringIsEqualToUTF8CString(character, "F12"))
        gdkKeySym = GDK_F12;
    else {
        int charCode = JSStringGetCharactersPtr(character)[0];
        if (charCode == '\n' || charCode == '\r')
            gdkKeySym = GDK_Return;
        else if (charCode == '\t')
            gdkKeySym = GDK_Tab;
        else if (charCode == '\x8')
            gdkKeySym = GDK_BackSpace;
        else {
            gdkKeySym = gdk_unicode_to_keyval(charCode);
            if (WTF::isASCIIUpper(charCode))
                state |= GDK_SHIFT_MASK;
        }
    }

    JSStringRelease(character);

    WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
    if (!view)
        return JSValueMakeUndefined(context);

    // create and send the event
    GdkEvent event;
    memset(&event, 0, sizeof(event));
    event.key.keyval = gdkKeySym;
    event.key.state = state;
    event.key.window = GTK_WIDGET(view)->window;

    gboolean return_val;
    event.key.type = GDK_KEY_PRESS;
    g_signal_emit_by_name(view, "key-press-event", &event.key, &return_val);

    event.key.type = GDK_KEY_RELEASE;
    g_signal_emit_by_name(view, "key-release-event", &event.key, &return_val);

    return JSValueMakeUndefined(context);
}

static JSValueRef textZoomInCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
    WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
    if (!view)
        return JSValueMakeUndefined(context);

    gfloat currentZoom = webkit_web_view_get_zoom_level(view);
    webkit_web_view_set_zoom_level(view, currentZoom * zoomMultiplierRatio);

    return JSValueMakeUndefined(context);
}

static JSValueRef textZoomOutCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
    WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
    if (!view)
        return JSValueMakeUndefined(context);

    gfloat currentZoom = webkit_web_view_get_zoom_level(view);
    webkit_web_view_set_zoom_level(view, currentZoom / zoomMultiplierRatio);

    return JSValueMakeUndefined(context);
}

static JSValueRef zoomPageInCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
    WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
    if (!view)
        return JSValueMakeUndefined(context);

    webkit_web_view_zoom_in(view);
    return JSValueMakeUndefined(context);
}

static JSValueRef zoomPageOutCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
    WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
    if (!view)
        return JSValueMakeUndefined(context);

    webkit_web_view_zoom_out(view);
    return JSValueMakeUndefined(context);
}

static JSStaticFunction staticFunctions[] = {
    { "mouseWheelTo", mouseWheelToCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
    { "contextClick", contextClickCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
    { "mouseDown", mouseDownCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
    { "mouseUp", mouseUpCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
    { "mouseMoveTo", mouseMoveToCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
    { "beginDragWithFiles", beginDragWithFilesCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
    { "leapForward", leapForwardCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
    { "keyDown", keyDownCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
    { "textZoomIn", textZoomInCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
    { "textZoomOut", textZoomOutCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
    { "zoomPageIn", zoomPageInCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
    { "zoomPageOut", zoomPageOutCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
    { 0, 0, 0 }
};

static JSStaticValue staticValues[] = {
    { "dragMode", getDragModeCallback, setDragModeCallback, kJSPropertyAttributeNone },
    { 0, 0, 0, 0 }
};

static JSClassRef getClass(JSContextRef context)
{
    static JSClassRef eventSenderClass = 0;

    if (!eventSenderClass) {
        JSClassDefinition classDefinition = {
                0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        classDefinition.staticFunctions = staticFunctions;
        classDefinition.staticValues = staticValues;

        eventSenderClass = JSClassCreate(&classDefinition);
    }

    return eventSenderClass;
}

JSObjectRef makeEventSender(JSContextRef context)
{
    down = false;
    dragMode = true;
    lastMousePositionX = lastMousePositionY = 0;
    lastClickPositionX = lastClickPositionY = 0;

    if (!replayingSavedEvents) {
        // This function can be called in the middle of a test, even
        // while replaying saved events. Resetting these while doing that
        // can break things.
        endOfQueue = 0;
        startOfQueue = 0;
    }

    return JSObjectMake(context, getClass(context), 0);
}