~mmach/netext73/webkit2gtk

« back to all changes in this revision

Viewing changes to Source/WebKit/UIProcess/gtk/PointerLockManager.cpp

  • Committer: mmach
  • Date: 2023-06-16 17:21:37 UTC
  • Revision ID: netbit73@gmail.com-20230616172137-2rqx6yr96ga9g3kp
1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2019 Igalia S.L.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions
 
6
 * are met:
 
7
 * 1. Redistributions of source code must retain the above copyright
 
8
 *    notice, this list of conditions and the following disclaimer.
 
9
 * 2. Redistributions in binary form must reproduce the above copyright
 
10
 *    notice, this list of conditions and the following disclaimer in the
 
11
 *    documentation and/or other materials provided with the distribution.
 
12
 *
 
13
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 
14
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
15
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
16
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 
17
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
18
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
19
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
20
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 
21
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
22
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
23
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
24
 */
 
25
 
 
26
#include "config.h"
 
27
#include "PointerLockManager.h"
 
28
 
 
29
#include "NativeWebMouseEvent.h"
 
30
#include "WebPageProxy.h"
 
31
#include <WebCore/PlatformDisplay.h>
 
32
#include <gtk/gtk.h>
 
33
 
 
34
#if PLATFORM(WAYLAND)
 
35
#include "PointerLockManagerWayland.h"
 
36
#endif
 
37
 
 
38
#if PLATFORM(X11)
 
39
#include "PointerLockManagerX11.h"
 
40
#endif
 
41
 
 
42
namespace WebKit {
 
43
using namespace WebCore;
 
44
 
 
45
std::unique_ptr<PointerLockManager> PointerLockManager::create(WebPageProxy& webPage, const GdkEvent* event)
 
46
{
 
47
#if PLATFORM(WAYLAND)
 
48
    if (PlatformDisplay::sharedDisplay().type() == PlatformDisplay::Type::Wayland)
 
49
        return makeUnique<PointerLockManagerWayland>(webPage, event);
 
50
#endif
 
51
#if PLATFORM(X11)
 
52
    if (PlatformDisplay::sharedDisplay().type() == PlatformDisplay::Type::X11)
 
53
        return makeUnique<PointerLockManagerX11>(webPage, event);
 
54
#endif
 
55
    RELEASE_ASSERT_NOT_REACHED();
 
56
    return nullptr;
 
57
}
 
58
 
 
59
PointerLockManager::PointerLockManager(WebPageProxy& webPage, const GdkEvent* event)
 
60
    : m_webPage(webPage)
 
61
    , m_event(event)
 
62
{
 
63
}
 
64
 
 
65
PointerLockManager::~PointerLockManager()
 
66
{
 
67
    RELEASE_ASSERT(!m_device);
 
68
}
 
69
 
 
70
bool PointerLockManager::lock()
 
71
{
 
72
    RELEASE_ASSERT(!m_device);
 
73
 
 
74
    auto* viewWidget = m_webPage.viewWidget();
 
75
    m_device = gdk_seat_get_pointer(gdk_display_get_default_seat(gtk_widget_get_display(viewWidget)));
 
76
    GRefPtr<GdkCursor> cursor = adoptGRef(gdk_cursor_new_from_name(gtk_widget_get_display(viewWidget), "none"));
 
77
    auto grabResult = gdk_seat_grab(gdk_device_get_seat(m_device), gtk_widget_get_window(viewWidget), GDK_SEAT_CAPABILITY_ALL_POINTING, TRUE,
 
78
        cursor.get(), nullptr, nullptr, nullptr);
 
79
    if (grabResult != GDK_GRAB_SUCCESS) {
 
80
        m_device = nullptr;
 
81
        return false;
 
82
    }
 
83
 
 
84
    return true;
 
85
}
 
86
 
 
87
bool PointerLockManager::unlock()
 
88
{
 
89
    if (!m_device)
 
90
        return false;
 
91
 
 
92
    gdk_seat_ungrab(gdk_device_get_seat(m_device));
 
93
    m_device = nullptr;
 
94
    return true;
 
95
}
 
96
 
 
97
void PointerLockManager::handleMotion(IntPoint&& delta)
 
98
{
 
99
    m_webPage.handleMouseEvent(NativeWebMouseEvent(const_cast<GdkEvent*>(m_event), 0, delta));
 
100
}
 
101
 
 
102
} // namespace WebKit