File: xcb_context.cpp

package info (click to toggle)
gfxreconstruct 0.9.18%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 24,636 kB
  • sloc: cpp: 328,961; ansic: 25,454; python: 18,156; xml: 255; sh: 128; makefile: 6
file content (268 lines) | stat: -rw-r--r-- 8,970 bytes parent folder | download
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
/*
** Copyright (c) 2018 Valve Corporation
** Copyright (c) 2018-2021 LunarG, Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and associated documentation files (the "Software"),
** to deal in the Software without restriction, including without limitation
** the rights to use, copy, modify, merge, publish, distribute, sublicense,
** and/or sell copies of the Software, and to permit persons to whom the
** Software is furnished to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice shall be included in
** all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
** FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
** DEALINGS IN THE SOFTWARE.
*/

#include "application/xcb_context.h"
#include "application/application.h"
#include "application/xcb_window.h"
#include "util/logging.h"

#include <cstdlib>

GFXRECON_BEGIN_NAMESPACE(gfxrecon)
GFXRECON_BEGIN_NAMESPACE(application)

XcbContext::XcbContext(Application* application) : WsiContext(application)
{
    xcb_loader_.Initialize();
    if (!xcb_loader_.Initialize())
    {
        GFXRECON_LOG_DEBUG("Failed to initialize XCB loader");
        return;
    }

    auto& xcb          = xcb_loader_.GetFunctionTable();
    int   screen_count = 0;
    connection_        = xcb.connect(nullptr, &screen_count);

    if (xcb.connection_has_error(connection_))
    {
        GFXRECON_LOG_DEBUG("Failed to connect to an X server");
        return;
    }

    const xcb_setup_t*    setup = xcb.get_setup(connection_);
    xcb_screen_iterator_t iter  = xcb.setup_roots_iterator(setup);

    for (int i = 0; i < screen_count; ++i)
    {
        xcb.screen_next(&iter);
    }

    screen_         = iter.data;
    window_factory_ = std::make_unique<XcbWindowFactory>(this);
}

XcbContext::~XcbContext()
{
    if (connection_ != nullptr)
    {
        auto& xcb = xcb_loader_.GetFunctionTable();
        xcb.disconnect(connection_);
    }
}

bool XcbContext::RegisterXcbWindow(XcbWindow* window)
{
    bool success = WsiContext::RegisterWindow(window);

    if (success)
    {
        xcb_window_t window_id = window->GetWindowId();

        if (window_id != 0)
        {
            xcb_windows_.insert(std::make_pair(window_id, window));
        }
    }

    return success;
}

bool XcbContext::UnregisterXcbWindow(XcbWindow* window)
{
    bool success = WsiContext::UnregisterWindow(window);

    if (success)
    {
        xcb_windows_.erase(window->GetWindowId());
    }

    return success;
}

void XcbContext::ProcessEvents(bool wait_for_input)
{
    assert(application_);
    while (application_->IsRunning())
    {
        xcb_generic_event_t* event = nullptr;
        const auto&          xcb   = xcb_loader_.GetFunctionTable();

        if (wait_for_input)
        {
            event = xcb.wait_for_event(connection_);

            // Stop waiting after the first event or this function will never exit.
            wait_for_input = false;
        }
        else
        {
            event = xcb.poll_for_event(connection_);
        }

        if (event != nullptr)
        {
            if (event->response_type == 0)
            {
                // Set error status and break from event processing loop.
                auto error           = reinterpret_cast<xcb_generic_error_t*>(event);
                last_error_sequence_ = error->sequence;
                last_error_code_     = error->error_code;
                break;
            }

            uint8_t event_code = event->response_type & 0x7f;
            switch (event_code)
            {
                case XCB_EXPOSE:
                {
                    // A portion of the window needs to be redrawn.
                    // If playback is paused, the window may no longer contain a valid image, but the only way to fix
                    // this would be to re-draw the current frame.
                    break;
                }
                case XCB_CLIENT_MESSAGE:
                {
                    auto message_event = reinterpret_cast<xcb_client_message_event_t*>(event);
                    auto entry         = xcb_windows_.find(message_event->window);

                    if (entry != xcb_windows_.end())
                    {
                        XcbWindow* xcb_window = entry->second;
                        xcb_atom_t atom       = xcb_window->GetDeleteWindowAtom();

                        if (message_event->data.data32[0] == atom)
                        {
                            application_->StopRunning();
                        }
                    }

                    break;
                }
                case XCB_KEY_RELEASE:
                {
                    auto key = reinterpret_cast<xcb_key_release_event_t*>(event);

                    // TODO: This needs something like XkbKeycodeToKeysym
                    switch (key->detail)
                    {
                        case 0x9: // Escape
                            application_->StopRunning();
                            break;
                        case 0x21: // p
                        case 0x41: // Space
                            application_->SetPaused(!application_->GetPaused());
                            break;
                        default:
                            break;
                    }

                    break;
                }
                case XCB_KEY_PRESS:
                {
                    auto key = reinterpret_cast<xcb_key_press_event_t*>(event);

                    // TODO: This needs something like XkbKeycodeToKeysym
                    switch (key->detail)
                    {
                        // Using XCB_KEY_PRESS for repeat when key is held down.
                        case 0x72: // Right arrow
                        case 0x39: // n
                            if (application_->GetPaused())
                            {
                                application_->PlaySingleFrame();
                            }
                            break;
                        default:
                            break;
                    }

                    break;
                }
                case XCB_CONFIGURE_NOTIFY:
                {
                    auto configure_event = reinterpret_cast<xcb_configure_notify_event_t*>(event);
                    auto entry           = xcb_windows_.find(configure_event->window);

                    if (entry != xcb_windows_.end())
                    {
                        XcbWindow* xcb_window = entry->second;
                        xcb_window->ResizeNotifyReceived(
                            configure_event->sequence, configure_event->width, configure_event->height);
                    }

                    break;
                }
                case XCB_MAP_NOTIFY:
                {
                    auto map_event = reinterpret_cast<xcb_map_notify_event_t*>(event);
                    auto entry     = xcb_windows_.find(map_event->window);

                    if (entry != xcb_windows_.end())
                    {
                        XcbWindow* xcb_window = entry->second;
                        xcb_window->MapNotifyReceived(map_event->sequence, true);
                    }

                    break;
                }
                case XCB_UNMAP_NOTIFY:
                {
                    auto unmap_event = reinterpret_cast<xcb_unmap_notify_event_t*>(event);
                    auto entry       = xcb_windows_.find(unmap_event->window);

                    if (entry != xcb_windows_.end())
                    {
                        XcbWindow* xcb_window = entry->second;
                        xcb_window->MapNotifyReceived(unmap_event->sequence, false);
                    }

                    break;
                }
                case XCB_DESTROY_NOTIFY:
                {
                    auto destroy_event = reinterpret_cast<xcb_destroy_notify_event_t*>(event);
                    auto entry         = xcb_windows_.find(destroy_event->window);

                    if (entry != xcb_windows_.end())
                    {
                        XcbWindow* xcb_window = entry->second;
                        xcb_window->DestroyNotifyReceived(destroy_event->sequence);
                    }

                    break;
                }
            }

            free(event);
        }
        else
        {
            break;
        }
    }
}

GFXRECON_END_NAMESPACE(application)
GFXRECON_END_NAMESPACE(gfxrecon)