1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2010 VMware, Inc.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
12bf215546Sopenharmony_ci * the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17bf215546Sopenharmony_ci * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
21bf215546Sopenharmony_ci *
22bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
23bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
24bf215546Sopenharmony_ci * of the Software.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci *
27bf215546Sopenharmony_ci **************************************************************************/
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include "gdi/gdi_sw_winsys.h"
30bf215546Sopenharmony_ci#include "pipe/p_screen.h"
31bf215546Sopenharmony_ci#include "frontend/graw.h"
32bf215546Sopenharmony_ci#include "target-helpers/inline_debug_helper.h"
33bf215546Sopenharmony_ci#include "target-helpers/inline_sw_helper.h"
34bf215546Sopenharmony_ci#include <windows.h>
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_cistatic LRESULT CALLBACK
38bf215546Sopenharmony_ciwindow_proc(HWND hWnd,
39bf215546Sopenharmony_ci            UINT uMsg,
40bf215546Sopenharmony_ci            WPARAM wParam,
41bf215546Sopenharmony_ci            LPARAM lParam)
42bf215546Sopenharmony_ci{
43bf215546Sopenharmony_ci   switch (uMsg) {
44bf215546Sopenharmony_ci   case WM_DESTROY:
45bf215546Sopenharmony_ci      PostQuitMessage(0);
46bf215546Sopenharmony_ci      break;
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci   default:
49bf215546Sopenharmony_ci      return DefWindowProc(hWnd, uMsg, wParam, lParam);
50bf215546Sopenharmony_ci   }
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci   return 0;
53bf215546Sopenharmony_ci}
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_cistatic struct {
56bf215546Sopenharmony_ci   void (* draw)(void);
57bf215546Sopenharmony_ci} graw;
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_cistruct pipe_screen *
60bf215546Sopenharmony_cigraw_create_window_and_screen(int x,
61bf215546Sopenharmony_ci                              int y,
62bf215546Sopenharmony_ci                              unsigned width,
63bf215546Sopenharmony_ci                              unsigned height,
64bf215546Sopenharmony_ci                              enum pipe_format format,
65bf215546Sopenharmony_ci                              void **handle)
66bf215546Sopenharmony_ci{
67bf215546Sopenharmony_ci   struct sw_winsys *winsys = NULL;
68bf215546Sopenharmony_ci   struct pipe_screen *screen = NULL;
69bf215546Sopenharmony_ci   WNDCLASSEX wc;
70bf215546Sopenharmony_ci   UINT style = WS_VISIBLE | WS_TILEDWINDOW;
71bf215546Sopenharmony_ci   RECT rect;
72bf215546Sopenharmony_ci   HWND hWnd = NULL;
73bf215546Sopenharmony_ci   HDC hDC = NULL;
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci   if (format != PIPE_FORMAT_R8G8B8A8_UNORM)
76bf215546Sopenharmony_ci      goto fail;
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci   winsys = gdi_create_sw_winsys();
79bf215546Sopenharmony_ci   if (winsys == NULL)
80bf215546Sopenharmony_ci      goto fail;
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci   screen = sw_screen_create(winsys);
83bf215546Sopenharmony_ci   if (screen == NULL)
84bf215546Sopenharmony_ci      goto fail;
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci   memset(&wc, 0, sizeof wc);
87bf215546Sopenharmony_ci   wc.cbSize = sizeof wc;
88bf215546Sopenharmony_ci   wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
89bf215546Sopenharmony_ci   wc.lpfnWndProc = window_proc;
90bf215546Sopenharmony_ci   wc.lpszClassName = TEXT("graw-gdi");
91bf215546Sopenharmony_ci   wc.hInstance = GetModuleHandle(NULL);
92bf215546Sopenharmony_ci   wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
93bf215546Sopenharmony_ci   wc.hCursor = LoadCursor(NULL, IDC_ARROW);
94bf215546Sopenharmony_ci   wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
95bf215546Sopenharmony_ci   RegisterClassEx(&wc);
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ci   SetRect(&rect, 0, 0, width, height);
98bf215546Sopenharmony_ci   AdjustWindowRectEx(&rect, style, FALSE, 0);
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci   hWnd = CreateWindowEx(0,
101bf215546Sopenharmony_ci                         wc.lpszClassName,
102bf215546Sopenharmony_ci                         wc.lpszClassName,
103bf215546Sopenharmony_ci                         style,
104bf215546Sopenharmony_ci                         x,
105bf215546Sopenharmony_ci                         y,
106bf215546Sopenharmony_ci                         rect.right - rect.left,
107bf215546Sopenharmony_ci                         rect.bottom - rect.top,
108bf215546Sopenharmony_ci                         NULL,
109bf215546Sopenharmony_ci                         NULL,
110bf215546Sopenharmony_ci                         wc.hInstance,
111bf215546Sopenharmony_ci                         0);
112bf215546Sopenharmony_ci   if (hWnd == NULL)
113bf215546Sopenharmony_ci      goto fail;
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ci   hDC = GetDC(hWnd);
116bf215546Sopenharmony_ci   if (hDC == NULL)
117bf215546Sopenharmony_ci      goto fail;
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci   *handle = (void *)hDC;
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci   return debug_screen_wrap(screen);
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_cifail:
124bf215546Sopenharmony_ci   if (hWnd)
125bf215546Sopenharmony_ci      DestroyWindow(hWnd);
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci   if (screen)
128bf215546Sopenharmony_ci      screen->destroy(screen);
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_ci   return NULL;
131bf215546Sopenharmony_ci}
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_civoid
134bf215546Sopenharmony_cigraw_set_display_func(void (* draw)(void))
135bf215546Sopenharmony_ci{
136bf215546Sopenharmony_ci   graw.draw = draw;
137bf215546Sopenharmony_ci}
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_civoid
140bf215546Sopenharmony_cigraw_main_loop(void)
141bf215546Sopenharmony_ci{
142bf215546Sopenharmony_ci   for (;;) {
143bf215546Sopenharmony_ci      MSG msg;
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci      while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
146bf215546Sopenharmony_ci         if (msg.message == WM_QUIT) {
147bf215546Sopenharmony_ci            return;
148bf215546Sopenharmony_ci         }
149bf215546Sopenharmony_ci         TranslateMessage(&msg);
150bf215546Sopenharmony_ci         DispatchMessage(&msg);
151bf215546Sopenharmony_ci      }
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_ci      if (graw.draw) {
154bf215546Sopenharmony_ci         graw.draw();
155bf215546Sopenharmony_ci      }
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_ci      Sleep(0);
158bf215546Sopenharmony_ci   }
159bf215546Sopenharmony_ci}
160