~sircmpwn/xrgears

ref: fb9d492b99b8bb61f3c60bd3c976bccb800df57d xrgears/vkc/vkcWindowXCB.hpp -rw-r--r-- 5.6 KiB
fb9d492b — Lubosz Sarnecki move surface and format to swapchain. 4 years ago
                                                                                
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
#pragma once

#include <xcb/xcb.h>
#include <string.h>
#include <stdio.h>

#include "vkcWindow.hpp"
#include "vkcApplication.hpp"
#include "vkcRenderer.hpp"

#include "../vks/vksLog.hpp"

static xcb_atom_t
get_atom(struct xcb_connection_t *conn, const char *name)
{
  xcb_intern_atom_cookie_t cookie;
  xcb_intern_atom_reply_t *reply;
  xcb_atom_t atom;

  cookie = xcb_intern_atom(conn, 0, strlen(name), name);
  reply = xcb_intern_atom_reply(conn, cookie, NULL);
  if (reply)
    atom = reply->atom;
  else
    atom = XCB_NONE;

  free(reply);
  return atom;
}

namespace vkc {
class WindowXCB : public Window {

  xcb_connection_t *conn;
  xcb_window_t window;
  xcb_atom_t atom_wm_protocols;
  xcb_atom_t atom_wm_delete_window;

  bool repaint = false;

public:
  WindowXCB() {
    window = XCB_NONE;
    name = "xcb";
  }

  ~WindowXCB() {}

  // Return -1 on failure.
  int init(Renderer* r) {
    xcb_screen_iterator_t iter;
    static const char title[] = "Vulkan Cube";

    conn = xcb_connect(0, 0);
    if (xcb_connection_has_error(conn))
      return -1;

    window = xcb_generate_id(conn);

    uint32_t window_values[] = {
      XCB_EVENT_MASK_EXPOSURE |
      XCB_EVENT_MASK_STRUCTURE_NOTIFY |
      XCB_EVENT_MASK_KEY_PRESS
    };

    iter = xcb_setup_roots_iterator(xcb_get_setup(conn));

    xcb_create_window(conn,
                      XCB_COPY_FROM_PARENT,
                      window,
                      iter.data->root,
                      0, 0,
                      r->width,
                      r->height,
                      0,
                      XCB_WINDOW_CLASS_INPUT_OUTPUT,
                      iter.data->root_visual,
                      XCB_CW_EVENT_MASK, window_values);

    atom_wm_protocols = get_atom(conn, "WM_PROTOCOLS");
    atom_wm_delete_window = get_atom(conn, "WM_DELETE_WINDOW");
    xcb_change_property(conn,
                        XCB_PROP_MODE_REPLACE,
                        window,
                        atom_wm_protocols,
                        XCB_ATOM_ATOM,
                        32,
                        1, &atom_wm_delete_window);

    xcb_change_property(conn,
                        XCB_PROP_MODE_REPLACE,
                        window,
                        get_atom(conn, "_NET_WM_NAME"),
                        get_atom(conn, "UTF8_STRING"),
                        8, // sizeof(char),
                        strlen(title), title);

    xcb_map_window(conn, window);

    xcb_flush(conn);

    r->init_vk(VK_KHR_XCB_SURFACE_EXTENSION_NAME);

    VkBool32 ret = vkGetPhysicalDeviceXcbPresentationSupportKHR(
          r->physical_device, 0, conn, iter.data->root_visual);
    vik_log_f_if(!ret, "Vulkan not supported on given X window");

    init_surface(r);
    init_cb();

    vik_log_d("init xcb done");

    return 0;
  }

  void init_surface(Renderer *r) {
    VkXcbSurfaceCreateInfoKHR surfaceInfo = {};

    surfaceInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
    surfaceInfo.connection = conn;
    surfaceInfo.window = window;
    if (r->swap_chain == nullptr)
      r->swap_chain = new SwapChainVK();

    SwapChainVK *sc = (SwapChainVK*) r->swap_chain;

    vkCreateXcbSurfaceKHR(r->instance, &surfaceInfo, NULL, &sc->surface);

    sc->choose_surface_format(r->physical_device);
  }

  void schedule_repaint() {
    xcb_client_message_event_t client_message;
    client_message.response_type = XCB_CLIENT_MESSAGE;
    client_message.format = 32;
    client_message.window = window;
    client_message.type = XCB_ATOM_NOTICE;
    xcb_send_event(conn, 0, window, 0, (char *) &client_message);
  }


  void poll_events(Renderer *r) {
    xcb_generic_event_t *event;
    xcb_key_press_event_t *key_press;
    xcb_client_message_event_t *client_message;
    xcb_configure_notify_event_t *configure;

    event = xcb_wait_for_event(conn);
    while (event) {
      switch (event->response_type & 0x7f) {
        case XCB_CLIENT_MESSAGE:
          client_message = (xcb_client_message_event_t *) event;
          if (client_message->window != window)
            break;

          if (client_message->type == atom_wm_protocols &&
              client_message->data.data32[0] == atom_wm_delete_window) {
            exit(0);
          }

          if (client_message->type == XCB_ATOM_NOTICE)
            repaint = true;
          break;

        case XCB_CONFIGURE_NOTIFY:
          configure = (xcb_configure_notify_event_t *) event;
          if (r->width != configure->width ||
              r->height != configure->height) {

            vik_log_d("XCB_CONFIGURE_NOTIFY %dx%d", r->width, r->height);

            SwapChainVK *sc = (SwapChainVK*) r->swap_chain;

            if (sc != nullptr)
              sc->destroy(r->device);

            r->width = configure->width;
            r->height = configure->height;
          }
          break;
        case XCB_EXPOSE:
          vik_log_d("XCB_EXPOSE");
          //if (r->swap_chain == nullptr) {
        {
          if (r->swap_chain == nullptr)
            r->swap_chain = new SwapChainVK();
          SwapChainVK *sc = (SwapChainVK*) r->swap_chain;
          sc->create(r->device, r->physical_device, r->width, r->height);
          sc->update_images(r->device);
          r->create_frame_buffers();
        }
          schedule_repaint();
          break;
        case XCB_KEY_PRESS:
          key_press = (xcb_key_press_event_t *) event;
          if (key_press->detail == 9)
            exit(0);
          break;
      }
      free(event);

      event = xcb_poll_for_event(conn);
    }
  }

  void iterate(Renderer *r) {
    poll_events(r);

    if (repaint) {

      update_cb();
      r->render_swapchain_vk();
      schedule_repaint();
    }
    xcb_flush(conn);
  }
};
}