1/*
2 * Copyright © 2013 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#include <stdio.h>
25#include <epoxy/wgl.h>
26#include "wgl_common.h"
27
28static int (*test_callback)(HDC hdc);
29
30static void
31setup_pixel_format(HDC hdc)
32{
33    PIXELFORMATDESCRIPTOR pfd = {
34        sizeof(PIXELFORMATDESCRIPTOR),
35        1,
36        PFD_SUPPORT_OPENGL |
37        PFD_DRAW_TO_WINDOW |
38        PFD_DOUBLEBUFFER,
39        PFD_TYPE_RGBA,
40        32,
41        0, 0, 0, 0, 0, 0,
42        0,
43        0,
44        0,
45        0, 0, 0, 0,
46        16,
47        0,
48        0,
49        PFD_MAIN_PLANE,
50        0,
51        0, 0, 0,
52    };
53    int pixel_format;
54
55    pixel_format = ChoosePixelFormat(hdc, &pfd);
56    if (!pixel_format) {
57        fputs("ChoosePixelFormat failed.\n", stderr);
58        exit(1);
59    }
60
61    if (SetPixelFormat(hdc, pixel_format, &pfd) != TRUE) {
62        fputs("SetPixelFormat() failed.\n", stderr);
63        exit(1);
64    }
65}
66
67static LRESULT CALLBACK
68window_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
69{
70    HDC hdc = GetDC(hwnd);
71    int ret;
72
73    switch (message) {
74    case WM_CREATE:
75        setup_pixel_format(hdc);
76        ret = test_callback(hdc);
77        ReleaseDC(hwnd, hdc);
78        exit(ret);
79        return 0;
80    default:
81        return DefWindowProc(hwnd, message, wparam, lparam);
82    }
83}
84
85void
86make_window_and_test(int (*callback)(HDC hdc))
87{
88    const char *class_name = "epoxy";
89    const char *window_name = "epoxy";
90    int width = 150;
91    int height = 150;
92    HWND hwnd;
93    HINSTANCE hcurrentinst = NULL;
94    WNDCLASS window_class;
95    MSG msg;
96
97    test_callback = callback;
98
99    memset(&window_class, 0, sizeof(window_class));
100    window_class.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
101    window_class.lpfnWndProc = window_proc;
102    window_class.cbClsExtra = 0;
103    window_class.cbWndExtra = 0;
104    window_class.hInstance = hcurrentinst;
105    window_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
106    window_class.hCursor = LoadCursor(NULL, IDC_ARROW);
107    window_class.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
108    window_class.lpszMenuName = NULL;
109    window_class.lpszClassName = class_name;
110    if (!RegisterClass(&window_class)) {
111        fputs("Failed to register window class\n", stderr);
112        exit(1);
113    }
114
115    /* create window */
116    hwnd = CreateWindow(class_name, window_name,
117                        WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
118                        0, 0, width, height,
119                        NULL, NULL, hcurrentinst, NULL);
120
121    ShowWindow(hwnd, SW_SHOWDEFAULT);
122    UpdateWindow(hwnd);
123
124    while (GetMessage(&msg, NULL, 0, 0)) {
125        TranslateMessage(&msg);
126        DispatchMessage(&msg);
127    }
128}
129