* expedite: Massive cleanup.
[framework/uifw/expedite.git] / src / bin / engine_direct3d.cpp
1 #include "main.h"
2
3 #include <windowsx.h>
4 #include <Evas_Engine_Direct3D.h>
5
6
7 static HWND window;
8 static HINSTANCE instance;
9
10
11 static LRESULT CALLBACK
12 MainWndProc(HWND   hwnd,
13             UINT   uMsg,
14             WPARAM wParam,
15             LPARAM lParam)
16 {
17    switch (uMsg)
18      {
19      case WM_CREATE:
20        return 0;
21      case WM_DESTROY:
22        PostQuitMessage(0);
23        return 0;
24      case WM_CLOSE:
25        PostQuitMessage(0);
26        return 0;
27      case WM_PAINT: {
28        PAINTSTRUCT ps;
29        HDC hdc;
30
31        hdc = BeginPaint (window, &ps);
32        evas_damage_rectangle_add(evas,
33                                  ps.rcPaint.left, ps.rcPaint.top,
34                                  ps.rcPaint.right - ps.rcPaint.left,
35                                  ps.rcPaint.bottom - ps.rcPaint.top);
36        EndPaint(window, &ps);
37        return 0;
38      }
39      case WM_SIZING:
40        {
41           PRECT rect = (PRECT)lParam;
42
43           evas_output_viewport_set(evas, 0, 0,
44                                    rect->right - rect->left,
45                                    rect->bottom - rect->top);
46           evas_output_size_set(evas,
47                                rect->right - rect->left,
48                                rect->bottom - rect->top);
49           win_w = rect->right - rect->left;
50           win_h = rect->bottom - rect->top;
51           return 0;
52        }
53      case WM_RBUTTONDOWN:
54        evas_event_feed_mouse_move(evas, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), 0, NULL);
55        evas_event_feed_mouse_down(evas, 3, EVAS_BUTTON_NONE, 0, NULL);
56        return 0;
57      case WM_LBUTTONDOWN:
58        evas_event_feed_mouse_move(evas, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), 0, NULL);
59        evas_event_feed_mouse_down(evas, 1, EVAS_BUTTON_NONE, 0, NULL);
60        return 0;
61      case WM_LBUTTONUP:
62        evas_event_feed_mouse_move(evas, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), 0, NULL);
63        evas_event_feed_mouse_up(evas, 1, EVAS_BUTTON_NONE, 0, NULL);
64        return 0;
65      case WM_RBUTTONUP:
66        evas_event_feed_mouse_move(evas, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), 0, NULL);
67        evas_event_feed_mouse_up(evas, 3, EVAS_BUTTON_NONE, 0, NULL);
68        return 0;
69      case WM_MOUSEMOVE:
70        if (!evas_pointer_inside_get(evas)) evas_event_feed_mouse_in(evas, 0, NULL);
71        evas_event_feed_mouse_move(evas, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), 0, NULL);
72        return 0;
73      case WM_MOUSELEAVE:
74        evas_event_feed_mouse_out(evas, 0, NULL);
75        return 0;
76        /* FIXME : can't find an event when the mouse is entering */
77      case WM_KEYDOWN:
78      case WM_SYSKEYDOWN: {
79         int key;
80
81         key = LOWORD(wParam);
82
83         if ((key == VK_SHIFT) ||
84             (key == VK_LSHIFT) ||
85             (key == VK_RSHIFT))
86           evas_key_modifier_on(evas, "Shift");
87         if ((key == VK_CONTROL) ||
88             (key == VK_LCONTROL) ||
89             (key == VK_RCONTROL))
90           evas_key_modifier_on(evas, "Control");
91         if ((key == VK_MENU) ||
92             (key == VK_LMENU) ||
93             (key == VK_RMENU))
94           evas_key_modifier_on(evas, "Alt");
95         if ((key == VK_LWIN) ||
96             (key == VK_RWIN))
97           evas_key_modifier_on(evas, "Super");
98
99         if (key == VK_CAPITAL)
100           {
101              if (evas_key_lock_is_set(evas_key_lock_get(evas), "Caps_Lock"))
102                evas_key_lock_off(evas, "Caps_Lock");
103              else
104                evas_key_lock_on(evas, "Caps_Lock");
105           }
106         if (key == VK_NUMLOCK)
107           {
108              if (evas_key_lock_is_set(evas_key_lock_get(evas), "Num_Lock"))
109                evas_key_lock_off(evas, "Num_Lock");
110              else
111                evas_key_lock_on(evas, "Num_Lock");
112           }
113         if (key == VK_SCROLL)
114           {
115              if (evas_key_lock_is_set(evas_key_lock_get(evas), "Scroll_Lock"))
116                evas_key_lock_off(evas, "Scroll_Lock");
117              else
118                evas_key_lock_on(evas, "Scroll_Lock");
119           }
120         if (key == VK_ESCAPE)
121           evas_event_feed_key_down(evas, "Escape", "Escape", NULL, NULL, 0, NULL);
122         if (key == VK_RETURN)
123           evas_event_feed_key_down(evas, "Return", "Return", NULL, NULL, 0, NULL);
124         if (key == VK_LEFT)
125           evas_event_feed_key_down(evas, "Left", "Left", NULL, NULL, 0, NULL);
126         if (key == VK_RIGHT)
127           evas_event_feed_key_down(evas, "Right", "Right", NULL, NULL, 0, NULL);
128         if (key == 81)
129           evas_event_feed_key_down(evas, "Q", "Q", NULL, NULL, 0, NULL);
130         if (key == 113)
131           evas_event_feed_key_down(evas, "q", "q", NULL, NULL, 0, NULL);
132         return 0;
133      }
134      case WM_KEYUP:
135      case WM_SYSKEYUP: {
136         int key;
137
138         key = LOWORD(wParam);
139
140         if ((key == VK_SHIFT) ||
141             (key == VK_LSHIFT) ||
142             (key == VK_RSHIFT))
143           evas_key_modifier_off(evas, "Shift");
144         if ((key == VK_CONTROL) ||
145             (key == VK_LCONTROL) ||
146             (key == VK_RCONTROL))
147           evas_key_modifier_off(evas, "Control");
148         if ((key == VK_MENU) ||
149             (key == VK_LMENU) ||
150             (key == VK_RMENU))
151           evas_key_modifier_off(evas, "Alt");
152         if ((key == VK_LWIN) ||
153             (key == VK_RWIN))
154           evas_key_modifier_off(evas, "Super");
155         if (key == VK_ESCAPE)
156           evas_event_feed_key_up(evas, "Escape", "Escape", NULL, NULL, 0, NULL);
157         if (key == VK_RETURN)
158           evas_event_feed_key_up(evas, "Return", "Return", NULL, NULL, 0, NULL);
159         if (key == VK_LEFT)
160           evas_event_feed_key_up(evas, "Left", "Left", NULL, NULL, 0, NULL);
161         if (key == VK_RIGHT)
162           evas_event_feed_key_up(evas, "Right", "Right", NULL, NULL, 0, NULL);
163         if (key == 81)
164           evas_event_feed_key_up(evas, "Q", "Q", NULL, NULL, 0, NULL);
165         if (key == 113)
166           evas_event_feed_key_up(evas, "q", "q", NULL, NULL, 0, NULL);
167         return 0;
168      }
169      default:
170        return DefWindowProc(hwnd, uMsg, wParam, lParam);
171      }
172 }
173
174 Eina_Bool
175 engine_direct3d_args(const char *engine, int width, int height)
176 {
177    WNDCLASS                   wc;
178    RECT                       rect;
179    HDC                        dc;
180    MSG                        msg;
181    Evas_Engine_Info_Direct3D *einfo;
182    DWORD                      style;
183    int                        depth;
184    int                        i;
185
186    instance = GetModuleHandle(NULL);
187    if (!instance) return EINA_FALSE;
188
189    wc.style = 0;
190    wc.lpfnWndProc = MainWndProc;
191    wc.cbClsExtra = 0;
192    wc.cbWndExtra = 0;
193    wc.hInstance = instance;
194    wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
195    wc.hCursor = LoadCursor (NULL, IDC_ARROW);
196    wc.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
197    wc.lpszMenuName =  NULL;
198    wc.lpszClassName = "Evas_Direct3D_Test";
199
200    if(!RegisterClass(&wc))
201      goto free_library;
202
203    rect.left = 0;
204    rect.top = 0;
205    rect.right = width;
206    rect.bottom = height;
207    AdjustWindowRect (&rect, WS_OVERLAPPEDWINDOW | WS_SIZEBOX, FALSE);
208
209    window = CreateWindowEx(0,
210                            "Evas_Direct3D_Test",
211                            "Evas_Direct3D_Test",
212                            WS_OVERLAPPEDWINDOW | WS_SIZEBOX,
213                            CW_USEDEFAULT, CW_USEDEFAULT,
214                            rect.right - rect.left, rect.bottom - rect.top,
215                            NULL, NULL, instance, NULL);
216    if (!window)
217      goto unregister_class;
218
219    /* make the window non resizable */
220    style = GetWindowLong(window, GWL_STYLE);
221    style &= ~WS_THICKFRAME;
222    if (!SetWindowLong(window, GWL_STYLE, style))
223      goto unregister_class;
224
225    dc = GetDC(NULL);
226    if (!dc)
227      goto destroy_window;
228
229    depth = GetDeviceCaps(dc, BITSPIXEL);
230    ReleaseDC(NULL, dc);
231
232    evas_output_method_set(evas, evas_render_method_lookup("direct3d"));
233    einfo = (Evas_Engine_Info_Direct3D *)evas_engine_info_get(evas);
234    if (!einfo)
235      {
236         fprintf(stderr, "Evas does not support the Direct3D Engine\n");
237         goto destroy_window;
238      }
239
240    einfo->info.window = window;
241    einfo->info.depth = depth;
242    einfo->info.rotation = 0;
243    if (!evas_engine_info_set(evas, (Evas_Engine_Info *)einfo))
244      {
245         printf("Evas can not setup the informations of the Direct3D Engine\n");
246         goto destroy_window;
247      }
248
249    /* the second parameter is ignored, as it's the first call of ShowWindow */
250    ShowWindow(window, SW_SHOWDEFAULT);
251    UpdateWindow(window);
252
253    return EINA_TRUE;
254
255  destroy_window:
256    DestroyWindow(window);
257  unregister_class:
258    UnregisterClass("Evas_Direct3D_Test", instance);
259  free_library:
260    FreeLibrary(instance);
261
262    return EINA_FALSE;
263 }
264
265 void
266 engine_direct3d_loop(void)
267 {
268    MSG msg;
269    int res;
270
271  again:
272    if (!PeekMessage (&msg, window, 0, 0, PM_NOREMOVE))
273      return;
274
275    res = GetMessage (&msg, NULL, 0, 0);
276    TranslateMessage (&msg);
277    DispatchMessage (&msg);
278
279    goto again;
280 }
281
282 void
283 engine_direct3d_shutdown(void)
284 {
285    DestroyWindow(window);
286    UnregisterClass("Evas_Direct3D_Test", instance);
287    FreeLibrary(instance);
288 }