1848b8605Smrg/**************************************************************************
2848b8605Smrg *
3848b8605Smrg * Copyright 2010 VMware, Inc.
4848b8605Smrg * All Rights Reserved.
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the
8848b8605Smrg * "Software"), to deal in the Software without restriction, including
9848b8605Smrg * without limitation the rights to use, copy, modify, merge, publish,
10848b8605Smrg * distribute, sub license, and/or sell copies of the Software, and to
11848b8605Smrg * permit persons to whom the Software is furnished to do so, subject to
12848b8605Smrg * the following conditions:
13848b8605Smrg *
14848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15848b8605Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17848b8605Smrg * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18848b8605Smrg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19848b8605Smrg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20848b8605Smrg * USE OR OTHER DEALINGS IN THE SOFTWARE.
21848b8605Smrg *
22848b8605Smrg * The above copyright notice and this permission notice (including the
23848b8605Smrg * next paragraph) shall be included in all copies or substantial portions
24848b8605Smrg * of the Software.
25848b8605Smrg *
26848b8605Smrg *
27848b8605Smrg **************************************************************************/
28848b8605Smrg
29848b8605Smrg#include "gdi/gdi_sw_winsys.h"
30848b8605Smrg#include "pipe/p_screen.h"
31848b8605Smrg#include "state_tracker/graw.h"
32848b8605Smrg#include "target-helpers/inline_debug_helper.h"
33848b8605Smrg#include "target-helpers/inline_sw_helper.h"
34848b8605Smrg#include <windows.h>
35848b8605Smrg
36848b8605Smrg
37848b8605Smrgstatic LRESULT CALLBACK
38848b8605Smrgwindow_proc(HWND hWnd,
39848b8605Smrg            UINT uMsg,
40848b8605Smrg            WPARAM wParam,
41848b8605Smrg            LPARAM lParam)
42848b8605Smrg{
43848b8605Smrg   switch (uMsg) {
44848b8605Smrg   case WM_DESTROY:
45848b8605Smrg      PostQuitMessage(0);
46848b8605Smrg      break;
47848b8605Smrg
48848b8605Smrg   default:
49848b8605Smrg      return DefWindowProc(hWnd, uMsg, wParam, lParam);
50848b8605Smrg   }
51848b8605Smrg
52848b8605Smrg   return 0;
53848b8605Smrg}
54848b8605Smrg
55848b8605Smrgstatic struct {
56848b8605Smrg   void (* draw)(void);
57848b8605Smrg} graw;
58848b8605Smrg
59848b8605Smrgstruct pipe_screen *
60848b8605Smrggraw_create_window_and_screen(int x,
61848b8605Smrg                              int y,
62848b8605Smrg                              unsigned width,
63848b8605Smrg                              unsigned height,
64848b8605Smrg                              enum pipe_format format,
65848b8605Smrg                              void **handle)
66848b8605Smrg{
67848b8605Smrg   struct sw_winsys *winsys = NULL;
68848b8605Smrg   struct pipe_screen *screen = NULL;
69848b8605Smrg   WNDCLASSEX wc;
70848b8605Smrg   UINT style = WS_VISIBLE | WS_TILEDWINDOW;
71848b8605Smrg   RECT rect;
72848b8605Smrg   HWND hWnd = NULL;
73848b8605Smrg   HDC hDC = NULL;
74848b8605Smrg
75848b8605Smrg   if (format != PIPE_FORMAT_R8G8B8A8_UNORM)
76848b8605Smrg      goto fail;
77848b8605Smrg
78848b8605Smrg   winsys = gdi_create_sw_winsys();
79848b8605Smrg   if (winsys == NULL)
80848b8605Smrg      goto fail;
81848b8605Smrg
82848b8605Smrg   screen = sw_screen_create(winsys);
83848b8605Smrg   if (screen == NULL)
84848b8605Smrg      goto fail;
85848b8605Smrg
86848b8605Smrg   memset(&wc, 0, sizeof wc);
87848b8605Smrg   wc.cbSize = sizeof wc;
88848b8605Smrg   wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
89848b8605Smrg   wc.lpfnWndProc = window_proc;
90848b8605Smrg   wc.lpszClassName = "graw-gdi";
91848b8605Smrg   wc.hInstance = GetModuleHandle(NULL);
92848b8605Smrg   wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
93848b8605Smrg   wc.hCursor = LoadCursor(NULL, IDC_ARROW);
94848b8605Smrg   wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
95848b8605Smrg   RegisterClassEx(&wc);
96848b8605Smrg
97848b8605Smrg   SetRect(&rect, 0, 0, width, height);
98848b8605Smrg   AdjustWindowRectEx(&rect, style, FALSE, 0);
99848b8605Smrg
100848b8605Smrg   hWnd = CreateWindowEx(0,
101848b8605Smrg                         wc.lpszClassName,
102848b8605Smrg                         wc.lpszClassName,
103848b8605Smrg                         style,
104848b8605Smrg                         x,
105848b8605Smrg                         y,
106848b8605Smrg                         rect.right - rect.left,
107848b8605Smrg                         rect.bottom - rect.top,
108848b8605Smrg                         NULL,
109848b8605Smrg                         NULL,
110848b8605Smrg                         wc.hInstance,
111848b8605Smrg                         0);
112848b8605Smrg   if (hWnd == NULL)
113848b8605Smrg      goto fail;
114848b8605Smrg
115848b8605Smrg   hDC = GetDC(hWnd);
116848b8605Smrg   if (hDC == NULL)
117848b8605Smrg      goto fail;
118848b8605Smrg
119848b8605Smrg   *handle = (void *)hDC;
120848b8605Smrg
121848b8605Smrg   return debug_screen_wrap(screen);
122848b8605Smrg
123848b8605Smrgfail:
124848b8605Smrg   if (hWnd)
125848b8605Smrg      DestroyWindow(hWnd);
126848b8605Smrg
127848b8605Smrg   if (screen)
128848b8605Smrg      screen->destroy(screen);
129848b8605Smrg
130848b8605Smrg   return NULL;
131848b8605Smrg}
132848b8605Smrg
133848b8605Smrgvoid
134848b8605Smrggraw_set_display_func(void (* draw)(void))
135848b8605Smrg{
136848b8605Smrg   graw.draw = draw;
137848b8605Smrg}
138848b8605Smrg
139848b8605Smrgvoid
140848b8605Smrggraw_main_loop(void)
141848b8605Smrg{
142848b8605Smrg   for (;;) {
143848b8605Smrg      MSG msg;
144848b8605Smrg
145848b8605Smrg      while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
146848b8605Smrg         if (msg.message == WM_QUIT) {
147848b8605Smrg            return;
148848b8605Smrg         }
149848b8605Smrg         TranslateMessage(&msg);
150848b8605Smrg         DispatchMessage(&msg);
151848b8605Smrg      }
152848b8605Smrg
153848b8605Smrg      if (graw.draw) {
154848b8605Smrg         graw.draw();
155848b8605Smrg      }
156848b8605Smrg
157848b8605Smrg      Sleep(0);
158848b8605Smrg   }
159848b8605Smrg}
160