gitlab-ci: enable sanitizers for the meson builds
[mesa-waffle.git] / src / waffle / wgl / wgl_platform.c
blob5c78b0f18efa8dbebb3879b65d36dc066ef4683e
1 // SPDX-FileCopyrightText: Copyright 2014 Emil Velikov
2 // SPDX-License-Identifier: BSD-2-Clause
4 #include <stdlib.h>
5 #include <windows.h>
7 #include "wcore_error.h"
9 #include "win_platform.h"
11 #include "wgl_config.h"
12 #include "wgl_context.h"
13 #include "wgl_display.h"
14 #include "wgl_platform.h"
15 #include "wgl_window.h"
17 static const struct wcore_platform_vtbl wgl_platform_vtbl;
19 const char* wfl_class_name = "waffle";
21 static bool
22 wgl_platform_destroy(struct wcore_platform *wc_self)
24 struct wgl_platform *self = wgl_platform(wc_self);
25 bool ok = true;
27 if (self->class_name)
28 ok = UnregisterClass(self->class_name, GetModuleHandle(NULL));
30 win_platform_teardown(wc_self);
31 free(self);
32 return ok;
35 static bool
36 wgl_platform_register_class(const char* class_name)
38 WNDCLASS wc;
39 bool ok;
41 memset(&wc, 0, sizeof(wc));
42 wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
43 // XXX: Use a non-default window_proc ?
44 wc.lpfnWndProc = DefWindowProc;
45 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
46 wc.hInstance = GetModuleHandle(NULL);
47 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
48 wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);;
49 wc.lpszClassName = class_name;
51 ok = !!RegisterClass(&wc);
53 if (!ok) {
54 int error = GetLastError();
56 if (error) {
57 wcore_errorf(WAFFLE_ERROR_UNKNOWN,
58 "RegisterClass() failed: %d",
59 error);
63 return ok;
66 struct wcore_platform*
67 wgl_platform_create(void)
69 struct wgl_platform *self;
71 self = wcore_calloc(sizeof(*self));
72 if (!self)
73 return NULL;
75 wcore_platform_init(&self->wcore);
76 win_platform_init(&self->wcore);
78 if (!wgl_platform_register_class(wfl_class_name))
79 goto error;
81 self->class_name = wfl_class_name;
83 self->wcore.vtbl = &wgl_platform_vtbl;
84 return &self->wcore;
86 error:
87 wgl_platform_destroy(&self->wcore);
88 return NULL;
91 static bool
92 wgl_make_current(struct wcore_platform *wc_self,
93 struct wcore_display *wc_dpy,
94 struct wcore_window *wc_window,
95 struct wcore_context *wc_ctx)
97 HDC hDC = wc_window ? wgl_window(wc_window)->hDC : NULL;
98 HGLRC hglrc = wc_ctx ? wgl_context(wc_ctx)->hglrc : NULL;
100 return wglMakeCurrent(hDC, hglrc);
103 static void*
104 wgl_get_proc_address(struct wcore_platform *wc_self, const char *name)
106 return wglGetProcAddress(name);
109 static const struct wcore_platform_vtbl wgl_platform_vtbl = {
110 .destroy = wgl_platform_destroy,
112 .make_current = wgl_make_current,
113 .get_proc_address = wgl_get_proc_address,
114 .dl_can_open = win_platform_can_dlopen,
115 .dl_sym = win_platform_dlsym,
117 .display = {
118 .connect = wgl_display_connect,
119 .destroy = wgl_display_destroy,
120 .supports_context_api = wgl_display_supports_context_api,
121 .get_native = NULL,
124 .config = {
125 .choose = wgl_config_choose,
126 .destroy = wgl_config_destroy,
127 .get_native = NULL,
130 .context = {
131 .create = wgl_context_create,
132 .destroy = wgl_context_destroy,
133 .get_native = NULL,
136 .window = {
137 .create = wgl_window_create,
138 .destroy = wgl_window_destroy,
139 .show = wgl_window_show,
140 .resize = wgl_window_resize,
141 .swap_buffers = wgl_window_swap_buffers,
142 .get_native = NULL,