summaryrefslogtreecommitdiff
path: root/Kernel/ProcessGUI.cpp
blob: 01477a12b4797e4a6a5b0b7e72b8cdddf321539b (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
#include "Process.h"
#include "MemoryManager.h"
#include <LibC/errno_numbers.h>
#include <SharedGraphics/Font.h>
#include <WindowServer/WSScreen.h>
#include <WindowServer/WSEventLoop.h>
#include <WindowServer/WSWindow.h>
#include <WindowServer/WSWindowManager.h>

//#define LOG_GUI_SYSCALLS

void Process::initialize_gui_statics()
{
    Font::initialize();
    WSEventLoop::initialize();
    WSWindowManager::initialize();
    WSScreen::initialize();

    new WSEventLoop;
}

int Process::make_window_id()
{
    int new_id = m_next_window_id++;
    while (!new_id || m_windows.contains(new_id)) {
        new_id = m_next_window_id++;
        if (new_id < 0)
            new_id = 1;
    }
    return new_id;
}

static void wait_for_gui_server()
{
    // FIXME: Time out after a while and return an error.
    while (!WSEventLoop::the().running())
        sleep(10);
}

int Process::gui$create_window(const GUI_WindowParameters* user_params)
{
    wait_for_gui_server();

    if (!validate_read_typed(user_params))
        return -EFAULT;

    auto params = *user_params;
    Rect rect = params.rect;

    if (rect.is_empty())
        return -EINVAL;

    ProcessPagingScope scope(WSEventLoop::the().server_process());

    int window_id = make_window_id();
    if (!window_id)
        return -ENOMEM;

    auto window = make<WSWindow>(*this, window_id);
    if (!window)
        return -ENOMEM;

    window->set_title(params.title);
    window->set_rect(rect);

    m_windows.set(window_id, move(window));
#ifdef LOG_GUI_SYSCALLS
    dbgprintf("%s<%u> gui$create_window: %d with rect {%d,%d %dx%d}\n", name().characters(), pid(), window_id, rect.x(), rect.y(), rect.width(), rect.height());
#endif
    return window_id;
}

int Process::gui$destroy_window(int window_id)
{
#ifdef LOG_GUI_SYSCALLS
    dbgprintf("%s<%u> gui$destroy_window (window_id=%d)\n", name().characters(), pid(), window_id);
#endif
    if (window_id < 0)
        return -EINVAL;
    auto it = m_windows.find(window_id);
    if (it == m_windows.end())
        return -EBADWINDOW;
    m_windows.remove(window_id);
    return 0;
}

int Process::gui$get_window_backing_store(int window_id, GUI_WindowBackingStoreInfo* info)
{
#ifdef LOG_GUI_SYSCALLS
    dbgprintf("%s<%u> gui$get_window_backing_store (window_id=%d, info=%p)\n", name().characters(), pid(), window_id, info);
#endif
    if (!validate_write_typed(info))
        return -EFAULT;
    if (window_id < 0)
        return -EINVAL;
    auto it = m_windows.find(window_id);
    if (it == m_windows.end())
        return -EBADWINDOW;
    auto& window = *(*it).value;
    WSWindowLocker locker(window);
    auto* backing_store = window.backing();
#ifdef BACKING_STORE_DEBUG
    dbgprintf("%s<%u> +++ %p[%d] (%dx%d)\n", name().characters(), pid(), backing_store, backing_store->width(), backing_store->height());
#endif
    m_retained_backing_stores.append(backing_store);
    info->backing_store_id = backing_store;
    info->bpp = sizeof(RGBA32);
    info->pitch = backing_store->pitch();
    info->size = backing_store->size();
    info->pixels = reinterpret_cast<RGBA32*>(backing_store->client_region()->laddr().as_ptr());
    return 0;
}

int Process::gui$release_window_backing_store(void* backing_store_id)
{
    for (size_t i = 0; i < m_retained_backing_stores.size(); ++i) {
        if (m_retained_backing_stores[i].ptr() == backing_store_id) {
#ifdef BACKING_STORE_DEBUG
            auto* backing_store = m_retained_backing_stores[i].ptr();
            dbgprintf("%s<%u> --- %p (%dx%d)\n", name().characters(), pid(), backing_store, backing_store->width(), backing_store->height());
#endif
            m_retained_backing_stores.remove(i);
            return 0;
        }
    }
    return -EBADBACKING;
}

int Process::gui$invalidate_window(int window_id, const GUI_Rect* rect)
{
    if (window_id < 0)
        return -EINVAL;
    if (rect && !validate_read_typed(rect))
        return -EFAULT;
    auto it = m_windows.find(window_id);
    if (it == m_windows.end())
        return -EBADWINDOW;
#ifdef LOG_GUI_SYSCALLS
    if (!rect)
        dbgprintf("%s<%u> gui$invalidate_window (window_id=%d, rect=(entire))\n", name().characters(), pid(), window_id);
    else
        dbgprintf("%s<%u> gui$invalidate_window (window_id=%d, rect={%d,%d %dx%d})\n", name().characters(), pid(), window_id, rect->location.x, rect->location.y, rect->size.width, rect->size.height);
#endif
    auto& window = *(*it).value;
    Rect invalidation_rect;
    if (rect) {
        WSWindowLocker locker(window);
        invalidation_rect = *rect;
    }
    WSEventLoop::the().post_event(&window, make<WSWindowInvalidationEvent>(invalidation_rect));
    WSEventLoop::the().server_process().request_wakeup();
    return 0;
}

int Process::gui$get_window_title(int window_id, char* buffer, size_t size)
{
    if (window_id < 0)
        return -EINVAL;
    if (!validate_write(buffer, size))
        return -EFAULT;
    auto it = m_windows.find(window_id);
    if (it == m_windows.end())
        return -EBADWINDOW;
    auto& window = *(*it).value;
    String title;
    {
        WSWindowLocker locker(window);
        title = window.title();
    }
    if (title.length() > size)
        return -ERANGE;
    memcpy(buffer, title.characters(), title.length());
    return title.length();

}

int Process::gui$set_window_title(int window_id, const char* title, size_t size)
{
    if (window_id < 0)
        return -EINVAL;
    if (!validate_read(title, size))
        return -EFAULT;
    auto it = m_windows.find(window_id);
    if (it == m_windows.end())
        return -EBADWINDOW;
    auto& window = *(*it).value;
    String new_title(title, size);
    WSEventLoop::the().post_event(&window, make<WSSetWindowTitle>(move(new_title)));
    WSEventLoop::the().server_process().request_wakeup();
    return 0;
}

int Process::gui$get_window_rect(int window_id, GUI_Rect* rect)
{
    if (window_id < 0)
        return -EINVAL;
    if (!validate_write_typed(rect))
        return -EFAULT;
    auto it = m_windows.find(window_id);
    if (it == m_windows.end())
        return -EBADWINDOW;
    auto& window = *(*it).value;
    {
        WSWindowLocker locker(window);
        *rect = window.rect();
    }
    return 0;
}

int Process::gui$set_window_rect(int window_id, const GUI_Rect* rect)
{
    if (window_id < 0)
        return -EINVAL;
    if (!validate_read_typed(rect))
        return -EFAULT;
    auto it = m_windows.find(window_id);
    if (it == m_windows.end())
        return -EBADWINDOW;
    auto& window = *(*it).value;
    Rect new_rect = *rect;
    WSEventLoop::the().post_event(&window, make<WSSetWindowRect>(new_rect));
    WSEventLoop::the().server_process().request_wakeup();
    return 0;
}