gbm: factor out gbm_window_{init,teardown}
[mesa-waffle.git] / src / waffle / wgl / wgl_platform.c
blobf4649b44d4839282a7e8f3b8d28c979b5b6e60fb
1 // Copyright 2014 Emil Velikov
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // - Redistributions of source code must retain the above copyright notice, this
9 // list of conditions and the following disclaimer.
11 // - Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <stdlib.h>
27 #include <windows.h>
29 #include "wcore_error.h"
31 #include "wgl_config.h"
32 #include "wgl_context.h"
33 #include "wgl_display.h"
34 #include "wgl_dl.h"
35 #include "wgl_platform.h"
36 #include "wgl_window.h"
38 static const struct wcore_platform_vtbl wgl_platform_vtbl;
40 const char* wfl_class_name = "waffle";
42 static bool
43 wgl_platform_destroy(struct wcore_platform *wc_self)
45 struct wgl_platform *self = wgl_platform(wc_self);
46 bool ok = true;
48 if (!self)
49 return true;
51 if (self->dl_gl)
52 ok &= wgl_dl_close(wc_self);
54 if (self->class_name)
55 ok &= UnregisterClass(self->class_name, GetModuleHandle(NULL));
57 ok &= wcore_platform_teardown(wc_self);
58 free(self);
59 return ok;
62 static bool
63 wgl_platform_register_class(const char* class_name)
65 WNDCLASS wc;
66 bool ok;
68 memset(&wc, 0, sizeof(wc));
69 wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
70 // XXX: Use a non-default window_proc ?
71 wc.lpfnWndProc = DefWindowProc;
72 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
73 wc.hInstance = GetModuleHandle(NULL);
74 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
75 wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);;
76 wc.lpszClassName = class_name;
78 ok = !!RegisterClass(&wc);
80 if (!ok) {
81 int error = GetLastError();
83 if (error) {
84 wcore_errorf(WAFFLE_ERROR_UNKNOWN,
85 "RegisterClass() failed: %d",
86 error);
90 return ok;
93 struct wcore_platform*
94 wgl_platform_create(void)
96 struct wgl_platform *self;
97 bool ok;
99 self = wcore_calloc(sizeof(*self));
100 if (!self)
101 return NULL;
103 ok = wcore_platform_init(&self->wcore);
104 if (!ok)
105 goto error;
107 ok = wgl_platform_register_class(wfl_class_name);
108 if (!ok)
109 goto error;
110 self->class_name = wfl_class_name;
112 self->wcore.vtbl = &wgl_platform_vtbl;
113 return &self->wcore;
115 error:
116 wgl_platform_destroy(&self->wcore);
117 return NULL;
120 static bool
121 wgl_make_current(struct wcore_platform *wc_self,
122 struct wcore_display *wc_dpy,
123 struct wcore_window *wc_window,
124 struct wcore_context *wc_ctx)
126 HDC hDC = wc_window ? wgl_window(wc_window)->hDC : NULL;
127 HGLRC hglrc = wc_ctx ? wgl_context(wc_ctx)->hglrc : NULL;
129 return wglMakeCurrent(hDC, hglrc);
132 static void*
133 wgl_get_proc_address(struct wcore_platform *wc_self, const char *name)
135 return wglGetProcAddress(name);
138 static const struct wcore_platform_vtbl wgl_platform_vtbl = {
139 .destroy = wgl_platform_destroy,
141 .make_current = wgl_make_current,
142 .get_proc_address = wgl_get_proc_address,
143 .dl_can_open = wgl_dl_can_open,
144 .dl_sym = wgl_dl_sym,
146 .display = {
147 .connect = wgl_display_connect,
148 .destroy = wgl_display_destroy,
149 .supports_context_api = wgl_display_supports_context_api,
150 .get_native = NULL,
153 .config = {
154 .choose = wgl_config_choose,
155 .destroy = wgl_config_destroy,
156 .get_native = NULL,
159 .context = {
160 .create = wgl_context_create,
161 .destroy = wgl_context_destroy,
162 .get_native = NULL,
165 .window = {
166 .create = wgl_window_create,
167 .destroy = wgl_window_destroy,
168 .show = wgl_window_show,
169 .resize = wgl_window_resize,
170 .swap_buffers = wgl_window_swap_buffers,
171 .get_native = NULL,