Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GHOST_WindowSDL.cpp « intern « ghost « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: aeb6188daef24511f0db18da5c5a28a6c2522fe4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Contributor(s): Campbell Barton
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file ghost/intern/GHOST_WindowSDL.cpp
 *  \ingroup GHOST
 */

#include "GHOST_WindowSDL.h"
#include "SDL_mouse.h"
#include "glew-mx.h"

#include "GHOST_ContextSDL.h"

#include <assert.h>

GHOST_WindowSDL::GHOST_WindowSDL(GHOST_SystemSDL *system,
                                 const STR_String& title,
                                 GHOST_TInt32 left,
                                 GHOST_TInt32 top,
                                 GHOST_TUns32 width,
                                 GHOST_TUns32 height,
                                 GHOST_TWindowState state,
                                 const GHOST_TEmbedderWindowID parentWindow,
                                 GHOST_TDrawingContextType type,
                                 const bool stereoVisual,
                                 const bool exclusive,
                                 const GHOST_TUns16 numOfAASamples
                                 )
    : GHOST_Window(width, height, state, stereoVisual, exclusive, numOfAASamples),
      m_system(system),
      m_valid_setup(false),
      m_invalid_window(false),
      m_sdl_custom_cursor(NULL)
{

	/* creating the window _must_ come after setting attributes */
	m_sdl_win = SDL_CreateWindow(
	        title,
	        left, top,
	        width, height,
	        SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);

	/* now set up the rendering context. */
	if (setDrawingContextType(type) == GHOST_kSuccess) {
		m_valid_setup = true;
		GHOST_PRINT("Created window\n");
	}

	if (exclusive) {
		SDL_RaiseWindow(m_sdl_win);
	}

	setTitle(title);
}

GHOST_WindowSDL::~GHOST_WindowSDL()
{
	if (m_sdl_custom_cursor) {
		SDL_FreeCursor(m_sdl_custom_cursor);
	}

	releaseNativeHandles();

	SDL_DestroyWindow(m_sdl_win);
}


GHOST_Context *
GHOST_WindowSDL::newDrawingContext(GHOST_TDrawingContextType type)
{
	if (type == GHOST_kDrawingContextTypeOpenGL) {
		GHOST_Context *context = new GHOST_ContextSDL(
		        m_wantStereoVisual,
		        m_wantNumOfAASamples,
		        m_sdl_win,
		        0, // profile bit
		        0, 0,
		        GHOST_OPENGL_SDL_CONTEXT_FLAGS,
		        GHOST_OPENGL_SDL_RESET_NOTIFICATION_STRATEGY);

		if (context->initializeDrawingContext())
			return context;
		else
			delete context;
	}

	return NULL;
}


GHOST_TSuccess
GHOST_WindowSDL::invalidate(void)
{
	if (m_invalid_window == false) {
		m_system->addDirtyWindow(this);
		m_invalid_window = true;
	}

	return GHOST_kSuccess;
}

GHOST_TSuccess
GHOST_WindowSDL::setState(GHOST_TWindowState state)
{
	switch (state) {
		case GHOST_kWindowStateNormal:
			SDL_SetWindowFullscreen(m_sdl_win, SDL_FALSE);
			SDL_RestoreWindow(m_sdl_win);
			break;
		case GHOST_kWindowStateMaximized:
			SDL_SetWindowFullscreen(m_sdl_win, SDL_FALSE);
			SDL_MaximizeWindow(m_sdl_win);
			break;
		case GHOST_kWindowStateMinimized:
			SDL_MinimizeWindow(m_sdl_win);
			break;
		case GHOST_kWindowStateFullScreen:
			SDL_SetWindowFullscreen(m_sdl_win, SDL_TRUE);
			break;
		default:
			break;
	}

	return GHOST_kSuccess;
}


GHOST_TWindowState
GHOST_WindowSDL::getState() const
{
	Uint32 flags = SDL_GetWindowFlags(m_sdl_win);

	if (flags & SDL_WINDOW_FULLSCREEN) return GHOST_kWindowStateFullScreen;
	else if (flags & SDL_WINDOW_MAXIMIZED) return GHOST_kWindowStateMaximized;
	else if (flags & SDL_WINDOW_MINIMIZED) return GHOST_kWindowStateMinimized;
	return GHOST_kWindowStateNormal;
}

bool
GHOST_WindowSDL::
getValid() const
{
	return GHOST_Window::getValid() && m_valid_setup;
}

void
GHOST_WindowSDL::setTitle(const STR_String& title)
{
	SDL_SetWindowTitle(m_sdl_win, title.ReadPtr());
}


void
GHOST_WindowSDL::getTitle(STR_String& title) const
{
	title = SDL_GetWindowTitle(m_sdl_win);
}


void
GHOST_WindowSDL::getWindowBounds(GHOST_Rect& bounds) const
{
	getClientBounds(bounds);
}


void
GHOST_WindowSDL::getClientBounds(GHOST_Rect& bounds) const
{
	int x, y, w, h;
	SDL_GetWindowSize(m_sdl_win, &w, &h);
	SDL_GetWindowPosition(m_sdl_win, &x, &y);

	bounds.m_l = x;
	bounds.m_r = x + w;
	bounds.m_t = y;
	bounds.m_b = y + h;
}

GHOST_TSuccess
GHOST_WindowSDL::setClientWidth(GHOST_TUns32 width)
{
	int height;
	SDL_GetWindowSize(m_sdl_win, NULL, &height);
	SDL_SetWindowSize(m_sdl_win, width, height);
	return GHOST_kSuccess;
}

GHOST_TSuccess
GHOST_WindowSDL::setClientHeight(GHOST_TUns32 height)
{
	int width;
	SDL_GetWindowSize(m_sdl_win, &width, NULL);
	SDL_SetWindowSize(m_sdl_win, width, height);
	return GHOST_kSuccess;
}

GHOST_TSuccess
GHOST_WindowSDL::setClientSize(GHOST_TUns32 width,
		GHOST_TUns32 height)
{
	SDL_SetWindowSize(m_sdl_win, width, height);
	return GHOST_kSuccess;
}

void
GHOST_WindowSDL::screenToClient(GHOST_TInt32 inX, GHOST_TInt32 inY, GHOST_TInt32& outX, GHOST_TInt32& outY) const
{
	/* XXXSDL_WEAK_ABS_COORDS */
	int x_win, y_win;
	SDL_GetWindowPosition(m_sdl_win, &x_win, &y_win);

	outX = inX - x_win;
	outY = inY - y_win;
}
void
GHOST_WindowSDL::clientToScreen(GHOST_TInt32 inX, GHOST_TInt32 inY, GHOST_TInt32& outX, GHOST_TInt32& outY) const
{
	/* XXXSDL_WEAK_ABS_COORDS */
	int x_win, y_win;
	SDL_GetWindowPosition(m_sdl_win, &x_win, &y_win);

	outX = inX + x_win;
	outY = inY + y_win;
}

/* mouse cursor */
static unsigned char sdl_std_cursor_mask_xterm[] = {0xef, 0x01, 0xff, 0x01, 0xff, 0x01, 0x7c, 0x00, 0x38, 0x00, 0x38, 0x00, 0x38, 0x00, 0x38, 0x00, 0x38, 0x00, 0x38, 0x00, 0x38, 0x00, 0x38, 0x00, 0x7c, 0x00, 0xff, 0x01, 0xff, 0x01, 0xef, 0x01, };
static unsigned char sdl_std_cursor_xterm[] = {0x00, 0x77, 0x00, 0x1c, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0x00, 0x1c, 0x00, 0x77, 0x00, 0x00, 0x00, 0x00, };
#define sdl_std_cursor_WIDTH_xterm  9
#define sdl_std_cursor_HEIGHT_xterm 16
#define sdl_std_cursor_HOT_X_xterm  -3
#define sdl_std_cursor_HOT_Y_xterm  -7

static unsigned char sdl_std_cursor_mask_watch[] = {0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f, 0xfe, 0x1f, 0xff, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xfe, 0x1f, 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f, };
static unsigned char sdl_std_cursor_watch[] = {0xf8, 0x07, 0xf8, 0x07, 0xf8, 0x07, 0xfc, 0x0f, 0x86, 0x18, 0x83, 0x30, 0x81, 0xe0, 0xc1, 0xe1, 0xc1, 0xe1, 0x21, 0xe0, 0x13, 0x30, 0x06, 0x18, 0xfc, 0x0f, 0xf8, 0x07, 0xf8, 0x07, 0xf8, 0x07, };
#define sdl_std_cursor_WIDTH_watch  16
#define sdl_std_cursor_HEIGHT_watch 16
#define sdl_std_cursor_HOT_X_watch  -15
#define sdl_std_cursor_HOT_Y_watch  -7

static unsigned char sdl_std_cursor_mask_umbrella[] = {0xe8, 0x76, 0xfb, 0xdf, 0xfd, 0x3f, 0xfe, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xcf, 0x79, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x07, 0xc0, 0x07, 0xc0, 0x07, 0xc0, 0x07, 0x80, 0x03, };
static unsigned char sdl_std_cursor_umbrella[] = {0x88, 0x04, 0x20, 0x0a, 0xc9, 0x32, 0xf2, 0x09, 0x4c, 0x06, 0x43, 0x18, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x01, 0x40, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, };
#define sdl_std_cursor_WIDTH_umbrella  16
#define sdl_std_cursor_HEIGHT_umbrella 16
#define sdl_std_cursor_HOT_X_umbrella  -7
#define sdl_std_cursor_HOT_Y_umbrella  -12

static unsigned char sdl_std_cursor_mask_top_side[] = {0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xc0, 0x01, 0xe0, 0x03, 0xf0, 0x07, 0xf8, 0x0f, 0xdc, 0x1d, 0xcc, 0x19, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01, };
static unsigned char sdl_std_cursor_top_side[] = {0xff, 0x1f, 0xff, 0x1f, 0x00, 0x00, 0x40, 0x00, 0xe0, 0x00, 0x50, 0x01, 0x48, 0x02, 0x44, 0x04, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, };
#define sdl_std_cursor_WIDTH_top_side  15
#define sdl_std_cursor_HEIGHT_top_side 16
#define sdl_std_cursor_HOT_X_top_side  -6
#define sdl_std_cursor_HOT_Y_top_side  -14

static unsigned char sdl_std_cursor_mask_top_right_corner[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xf0, 0xfc, 0xf7, 0xfc, 0xf7, 0xfc, 0xf7, 0xc0, 0xf7, 0xe0, 0xf7, 0x70, 0xf7, 0x38, 0xf7, 0x1c, 0xf7, 0x0c, 0xf7, 0x00, 0xf0, 0x00, 0xf0, };
static unsigned char sdl_std_cursor_top_right_corner[] = {0xff, 0x3f, 0xff, 0x3f, 0x00, 0x30, 0x00, 0x30, 0x00, 0x30, 0xfc, 0x31, 0x80, 0x31, 0x40, 0x31, 0x20, 0x31, 0x10, 0x31, 0x08, 0x31, 0x04, 0x31, 0x00, 0x30, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, };
#define sdl_std_cursor_WIDTH_top_right_corner  16
#define sdl_std_cursor_HEIGHT_top_right_corner 16
#define sdl_std_cursor_HOT_X_top_right_corner  -13
#define sdl_std_cursor_HOT_Y_top_right_corner  -14

static unsigned char sdl_std_cursor_mask_top_left_corner[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x00, 0xef, 0x3f, 0xef, 0x3f, 0xef, 0x3f, 0xef, 0x03, 0xef, 0x07, 0xef, 0x0e, 0xef, 0x1c, 0xef, 0x38, 0xef, 0x30, 0x0f, 0x00, 0x0f, 0x00, };
static unsigned char sdl_std_cursor_top_left_corner[] = {0xff, 0x3f, 0xff, 0x3f, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0xe3, 0x0f, 0x63, 0x00, 0xa3, 0x00, 0x23, 0x01, 0x23, 0x02, 0x23, 0x04, 0x23, 0x08, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, };
#define sdl_std_cursor_WIDTH_top_left_corner  16
#define sdl_std_cursor_HEIGHT_top_left_corner 16
#define sdl_std_cursor_HOT_X_top_left_corner  0
#define sdl_std_cursor_HOT_Y_top_left_corner  -14

static unsigned char sdl_std_cursor_mask_spraycan[] = {0x00, 0x0c, 0x18, 0x0d, 0x7c, 0x0d, 0x7c, 0x0d, 0x7e, 0x0d, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, };
static unsigned char sdl_std_cursor_spraycan[] = {0x00, 0x06, 0x80, 0x00, 0x2c, 0x06, 0x9e, 0x00, 0x16, 0x06, 0x3f, 0x00, 0x21, 0x00, 0x27, 0x00, 0x25, 0x00, 0x27, 0x00, 0x25, 0x00, 0x27, 0x00, 0x27, 0x00, 0x21, 0x00, 0x21, 0x00, 0x3f, 0x00, };
#define sdl_std_cursor_WIDTH_spraycan  12
#define sdl_std_cursor_HEIGHT_spraycan 16
#define sdl_std_cursor_HOT_X_spraycan  -9
#define sdl_std_cursor_HOT_Y_spraycan  -14

static unsigned char sdl_std_cursor_mask_sb_v_double_arrow[] = {0x38, 0x00, 0x7c, 0x00, 0xfe, 0x00, 0xff, 0x01, 0xff, 0x01, 0x7c, 0x00, 0x7c, 0x00, 0x7c, 0x00, 0x7c, 0x00, 0x7c, 0x00, 0xff, 0x01, 0xff, 0x01, 0xfe, 0x00, 0x7c, 0x00, 0x38, 0x00, };
static unsigned char sdl_std_cursor_sb_v_double_arrow[] = {0x10, 0x00, 0x38, 0x00, 0x7c, 0x00, 0xfe, 0x00, 0x28, 0x00, 0x28, 0x00, 0x28, 0x00, 0x28, 0x00, 0x28, 0x00, 0x28, 0x00, 0x28, 0x00, 0xfe, 0x00, 0x7c, 0x00, 0x38, 0x00, 0x10, 0x00, };
#define sdl_std_cursor_WIDTH_sb_v_double_arrow  9
#define sdl_std_cursor_HEIGHT_sb_v_double_arrow 15
#define sdl_std_cursor_HOT_X_sb_v_double_arrow  -3
#define sdl_std_cursor_HOT_Y_sb_v_double_arrow  -8

static unsigned char sdl_std_cursor_mask_sb_h_double_arrow[] = {0x18, 0x0c, 0x1c, 0x1c, 0xfe, 0x3f, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xfe, 0x3f, 0x1c, 0x1c, 0x18, 0x0c, };
static unsigned char sdl_std_cursor_sb_h_double_arrow[] = {0x00, 0x00, 0x08, 0x08, 0x0c, 0x18, 0xfe, 0x3f, 0x0f, 0x78, 0xfe, 0x3f, 0x0c, 0x18, 0x08, 0x08, 0x00, 0x00};
#define sdl_std_cursor_WIDTH_sb_h_double_arrow  15
#define sdl_std_cursor_HEIGHT_sb_h_double_arrow 9
#define sdl_std_cursor_HOT_X_sb_h_double_arrow  -7
#define sdl_std_cursor_HOT_Y_sb_h_double_arrow  -4

static unsigned char sdl_std_cursor_mask_right_side[] = {0x00, 0xf0, 0x00, 0xf0, 0xc0, 0xf0, 0xc0, 0xf1, 0x80, 0xf3, 0x00, 0xf7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xf7, 0x80, 0xf3, 0xc0, 0xf1, 0xc0, 0xf0, 0x00, 0xf0, 0x00, 0xf0, };
static unsigned char sdl_std_cursor_right_side[] = {0x00, 0x30, 0x00, 0x30, 0x40, 0x30, 0x80, 0x30, 0x00, 0x31, 0x00, 0x32, 0xff, 0x37, 0x00, 0x32, 0x00, 0x31, 0x80, 0x30, 0x40, 0x30, 0x00, 0x30, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, };
#define sdl_std_cursor_WIDTH_right_side  16
#define sdl_std_cursor_HEIGHT_right_side 15
#define sdl_std_cursor_HOT_X_right_side  -13
#define sdl_std_cursor_HOT_Y_right_side  -7

static unsigned char sdl_std_cursor_mask_right_ptr[] = {0x00, 0x03, 0x80, 0x03, 0xc0, 0x03, 0xe0, 0x03, 0xf0, 0x03, 0xf8, 0x03, 0xfc, 0x03, 0xfe, 0x03, 0xff, 0x03, 0xff, 0x03, 0xf8, 0x03, 0xbc, 0x03, 0x3c, 0x03, 0x1e, 0x00, 0x1e, 0x00, 0x0c, 0x00, };
static unsigned char sdl_std_cursor_right_ptr[] = {0x00, 0x80, 0x00, 0xc0, 0x00, 0xe0, 0x00, 0xf0, 0x00, 0xf8, 0x00, 0xfc, 0x00, 0xfe, 0x00, 0xff, 0x00, 0xf8, 0x00, 0xd8, 0x00, 0x8c, 0x00, 0x0c, 0x00, 0x06, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, };
#define sdl_std_cursor_WIDTH_right_ptr  10
#define sdl_std_cursor_HEIGHT_right_ptr 16
#define sdl_std_cursor_HOT_X_right_ptr  -7
#define sdl_std_cursor_HOT_Y_right_ptr  -14

static unsigned char sdl_std_cursor_mask_question_arrow[] = {0xf8, 0x00, 0xfc, 0x01, 0xfe, 0x03, 0xff, 0x07, 0x8f, 0x07, 0x9f, 0x07, 0xde, 0x07, 0xfc, 0x03, 0xf8, 0x01, 0xf8, 0x00, 0xf8, 0x00, 0xfc, 0x01, 0xfe, 0x03, 0xfc, 0x01, 0xf8, 0x00, 0x70, 0x00, };
static unsigned char sdl_std_cursor_question_arrow[] = {0x7c, 0x00, 0xfe, 0x00, 0xc7, 0x01, 0x83, 0x01, 0x87, 0x01, 0xc6, 0x01, 0xe0, 0x00, 0x78, 0x00, 0x38, 0x00, 0x28, 0x00, 0x28, 0x00, 0xee, 0x00, 0x6c, 0x00, 0x38, 0x00, 0x10, 0x00, 0x00, 0x00, };
#define sdl_std_cursor_WIDTH_question_arrow  11
#define sdl_std_cursor_HEIGHT_question_arrow 16
#define sdl_std_cursor_HOT_X_question_arrow  -4
#define sdl_std_cursor_HOT_Y_question_arrow  -8

static unsigned char sdl_std_cursor_mask_pirate[] = {0xf0, 0x03, 0xf8, 0x07, 0xfc, 0x0f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfc, 0x0f, 0xf8, 0x07, 0xf1, 0x83, 0xf1, 0xe3, 0xf3, 0xf3, 0xef, 0x39, 0x1e, 0x1e, 0xe0, 0x01, 0xfe, 0xc7, 0xff, 0xff, 0x0f, 0x7c, };
static unsigned char sdl_std_cursor_pirate[] = {0xe0, 0x01, 0xf0, 0x03, 0xf8, 0x07, 0xcc, 0x0c, 0xcc, 0x0c, 0xf8, 0x07, 0xf0, 0x03, 0xe0, 0x01, 0xe1, 0x21, 0xe1, 0x61, 0xc2, 0x10, 0x1c, 0x0e, 0xe0, 0x01, 0xf8, 0x47, 0x0f, 0x7c, 0x01, 0x20, };
#define sdl_std_cursor_WIDTH_pirate  16
#define sdl_std_cursor_HEIGHT_pirate 16
#define sdl_std_cursor_HOT_X_pirate  -7
#define sdl_std_cursor_HOT_Y_pirate  -4

static unsigned char sdl_std_cursor_mask_left_side[] = {0x0f, 0x00, 0x0f, 0x00, 0x0f, 0x03, 0x8f, 0x03, 0xcf, 0x01, 0xef, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0x00, 0xcf, 0x01, 0x8f, 0x03, 0x0f, 0x03, 0x0f, 0x00, 0x0f, 0x00, };
static unsigned char sdl_std_cursor_left_side[] = {0x03, 0x00, 0x03, 0x00, 0x83, 0x00, 0x43, 0x00, 0x23, 0x00, 0x13, 0x00, 0xfb, 0x3f, 0x13, 0x00, 0x23, 0x00, 0x43, 0x00, 0x83, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, };
#define sdl_std_cursor_WIDTH_left_side  16
#define sdl_std_cursor_HEIGHT_left_side 15
#define sdl_std_cursor_HOT_X_left_side  0
#define sdl_std_cursor_HOT_Y_left_side  -7

static unsigned char sdl_std_cursor_mask_left_ptr[] = {0x03, 0x00, 0x07, 0x00, 0x0f, 0x00, 0x1f, 0x00, 0x3f, 0x00, 0x7f, 0x00, 0xff, 0x00, 0xff, 0x01, 0xff, 0x03, 0xff, 0x03, 0x7f, 0x00, 0xf7, 0x00, 0xf3, 0x00, 0xe0, 0x01, 0xe0, 0x01, 0xc0, 0x00, };
static unsigned char sdl_std_cursor_left_ptr[] = {0x00, 0x00, 0x02, 0x00, 0x06, 0x00, 0x0e, 0x00, 0x1e, 0x00, 0x3e, 0x00, 0x7e, 0x00, 0xfe, 0x00, 0xfe, 0x00, 0x3e, 0x00, 0x36, 0x00, 0x62, 0x00, 0x60, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x00, 0x00, };
#define sdl_std_cursor_WIDTH_left_ptr  10
#define sdl_std_cursor_HEIGHT_left_ptr 16
#define sdl_std_cursor_HOT_X_left_ptr  -8
#define sdl_std_cursor_HOT_Y_left_ptr  -14

static unsigned char sdl_std_cursor_mask_exchange[] = {0xe3, 0x07, 0xf7, 0x0f, 0xff, 0x1f, 0xff, 0x3f, 0x3f, 0x38, 0xff, 0x30, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x0c, 0xfe, 0x1c, 0xfc, 0xfc, 0xff, 0xf8, 0xff, 0xf0, 0xef, 0xe0, 0xc7, };
static unsigned char sdl_std_cursor_exchange[] = {0xf1, 0x03, 0xfb, 0x07, 0x1f, 0x0c, 0x09, 0x08, 0x19, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x26, 0x04, 0x24, 0x0c, 0x3e, 0xf8, 0x37, 0xf0, 0x23, 0x00, 0x00, 0x00, 0x00, };
#define sdl_std_cursor_WIDTH_exchange  16
#define sdl_std_cursor_HEIGHT_exchange 16
#define sdl_std_cursor_HOT_X_exchange  -6
#define sdl_std_cursor_HOT_Y_exchange  -8

static unsigned char sdl_std_cursor_mask_crosshair[] = {0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01, };
static unsigned char sdl_std_cursor_crosshair[] = {0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x7f, 0xff, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, };
#define sdl_std_cursor_WIDTH_crosshair  16
#define sdl_std_cursor_HEIGHT_crosshair 16
#define sdl_std_cursor_HOT_X_crosshair  -7
#define sdl_std_cursor_HOT_Y_crosshair  -8

static unsigned char sdl_std_cursor_mask_bottom_side[] = {0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x01, 0xcc, 0x19, 0xdc, 0x1d, 0xf8, 0x0f, 0xf0, 0x07, 0xe0, 0x03, 0xc0, 0x01, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, };
static unsigned char sdl_std_cursor_bottom_side[] = {0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x44, 0x04, 0x48, 0x02, 0x50, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0xff, 0x1f, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, };
#define sdl_std_cursor_WIDTH_bottom_side  15
#define sdl_std_cursor_HEIGHT_bottom_side 16
#define sdl_std_cursor_HOT_X_bottom_side  -6
#define sdl_std_cursor_HOT_Y_bottom_side  -1

static unsigned char sdl_std_cursor_mask_bottom_right_corner[] = {0x00, 0xf0, 0x00, 0xf0, 0x0c, 0xf7, 0x1c, 0xf7, 0x38, 0xf7, 0x70, 0xf7, 0xe0, 0xf7, 0xc0, 0xf7, 0xfc, 0xf7, 0xfc, 0xf7, 0xfc, 0xf7, 0x00, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, };
static unsigned char sdl_std_cursor_bottom_right_corner[] = {0x00, 0x30, 0x00, 0x30, 0x04, 0x31, 0x08, 0x31, 0x10, 0x31, 0x20, 0x31, 0x40, 0x31, 0x80, 0x31, 0xfc, 0x31, 0x00, 0x30, 0x00, 0x30, 0x00, 0x30, 0xff, 0x3f, 0xff, 0x3f, 0x00, 0x00, 0x00, 0x00, };
#define sdl_std_cursor_WIDTH_bottom_right_corner  16
#define sdl_std_cursor_HEIGHT_bottom_right_corner 16
#define sdl_std_cursor_HOT_X_bottom_right_corner  -13
#define sdl_std_cursor_HOT_Y_bottom_right_corner  -1

static unsigned char sdl_std_cursor_mask_bottom_left_corner[] = {0x0f, 0x00, 0x0f, 0x00, 0xef, 0x30, 0xef, 0x38, 0xef, 0x1c, 0xef, 0x0e, 0xef, 0x07, 0xef, 0x03, 0xef, 0x3f, 0xef, 0x3f, 0xef, 0x3f, 0x0f, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, };
static unsigned char sdl_std_cursor_bottom_left_corner[] = {0x03, 0x00, 0x03, 0x00, 0x23, 0x08, 0x23, 0x04, 0x23, 0x02, 0x23, 0x01, 0xa3, 0x00, 0x63, 0x00, 0xe3, 0x0f, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0xff, 0x3f, 0xff, 0x3f, 0x00, 0x00, 0x00, 0x00, };
#define sdl_std_cursor_WIDTH_bottom_left_corner  16
#define sdl_std_cursor_HEIGHT_bottom_left_corner 16
#define sdl_std_cursor_HOT_X_bottom_left_corner  0
#define sdl_std_cursor_HOT_Y_bottom_left_corner  -1

static unsigned char sdl_std_cursor_mask_arrow[] = {0x00, 0xe0, 0x00, 0xf8, 0x00, 0xfe, 0x80, 0x7f, 0xe0, 0x7f, 0xf8, 0x3f, 0xfc, 0x3f, 0xfc, 0x1f, 0xe0, 0x1f, 0xf0, 0x0f, 0xf8, 0x0f, 0x7c, 0x07, 0x3e, 0x07, 0x1f, 0x02, 0x0e, 0x00, 0x04, 0x00, };
static unsigned char sdl_std_cursor_arrow[] = {0x00, 0x30, 0x00, 0x3c, 0x00, 0x1f, 0xc0, 0x1f, 0xf0, 0x0f, 0xfc, 0x0f, 0xc0, 0x07, 0xe0, 0x07, 0x70, 0x03, 0x38, 0x03, 0x1c, 0x01, 0x0e, 0x01, 0x07, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, };
#define sdl_std_cursor_WIDTH_arrow  16
#define sdl_std_cursor_HEIGHT_arrow 16
#define sdl_std_cursor_HOT_X_arrow  -13
#define sdl_std_cursor_HOT_Y_arrow  -14
/* end cursor data */


static SDL_Cursor *sdl_std_cursor_array[(int)GHOST_kStandardCursorNumCursors] = {0};

/* utility function mostly a copy of SDL_CreateCursor but allows us to change
 * color and supports blenders flipped bits */
static SDL_Cursor *
sdl_ghost_CreateCursor(const Uint8 *data,
                       const Uint8 *mask,
                       int w,
                       int h,
                       int hot_x,
                       int hot_y)
{
	SDL_Surface *surface;
	SDL_Cursor *cursor;
	int x, y;
	Uint32 *pixel;
	Uint8 datab = 0, maskb = 0;
	const Uint32 black = 0xFF000000;
	const Uint32 white = 0xFFFFFFFF;
	const Uint32 transparent = 0x00000000;

	/* Make sure the width is a multiple of 8 */
	w = ((w + 7) & ~7);

	/* Create the surface from a bitmap */
	surface = SDL_CreateRGBSurface(0, w, h, 32,
	                               0x00FF0000,
	                               0x0000FF00,
	                               0x000000FF,
	                               0xFF000000);
	if (!surface) {
		return NULL;
	}
	for (y = 0; y < h; ++y) {
		pixel = (Uint32 *) ((Uint8 *) surface->pixels + y * surface->pitch);
		for (x = 0; x < w; ++x) {
			if ((x % 8) == 0) {
				datab = *data++;
				maskb = *mask++;

				/* reverse bit order */
				datab = (datab * 0x0202020202ULL & 0x010884422010ULL) % 1023;
				maskb = (maskb * 0x0202020202ULL & 0x010884422010ULL) % 1023;
			}
			if (maskb & 0x80) {
				*pixel++ = (datab & 0x80) ?  white : black;
			}
			else {
				*pixel++ = (datab & 0x80) ? white : transparent;
			}
			datab <<= 1;
			maskb <<= 1;
		}
	}

	cursor = SDL_CreateColorCursor(surface, hot_x, hot_y);

	SDL_FreeSurface(surface);

	return cursor;
}

/* TODO, this is currently never freed but it wont leak either. */
static void sdl_cursor_init(void)
{

#define DEF_CURSOR(name, ind) \
	{ \
		sdl_std_cursor_array[(int)ind] = \
			sdl_ghost_CreateCursor(sdl_std_cursor_##name, \
								   sdl_std_cursor_mask_##name, \
								   sdl_std_cursor_WIDTH_##name, \
								   sdl_std_cursor_HEIGHT_##name, \
								   (sdl_std_cursor_WIDTH_##name + (sdl_std_cursor_HOT_X_##name)) - 1, \
								   (sdl_std_cursor_HEIGHT_##name + (sdl_std_cursor_HOT_Y_##name)) - 1); \
		assert(sdl_std_cursor_array[(int)ind] != NULL); \
	} (void)0


	DEF_CURSOR(left_ptr, GHOST_kStandardCursorDefault);
	DEF_CURSOR(right_ptr, GHOST_kStandardCursorRightArrow);
	DEF_CURSOR(left_ptr, GHOST_kStandardCursorLeftArrow);
	DEF_CURSOR(umbrella, GHOST_kStandardCursorInfo); // TODO, replace this one.
	DEF_CURSOR(pirate, GHOST_kStandardCursorDestroy);
	DEF_CURSOR(question_arrow, GHOST_kStandardCursorHelp);
	DEF_CURSOR(exchange, GHOST_kStandardCursorCycle);
	DEF_CURSOR(spraycan, GHOST_kStandardCursorSpray);
	DEF_CURSOR(watch, GHOST_kStandardCursorWait);
	DEF_CURSOR(xterm, GHOST_kStandardCursorText);
	DEF_CURSOR(crosshair, GHOST_kStandardCursorCrosshair);
	DEF_CURSOR(sb_v_double_arrow, GHOST_kStandardCursorUpDown);
	DEF_CURSOR(sb_h_double_arrow, GHOST_kStandardCursorLeftRight);
	DEF_CURSOR(top_side, GHOST_kStandardCursorTopSide);
	DEF_CURSOR(bottom_side, GHOST_kStandardCursorBottomSide);
	DEF_CURSOR(left_side, GHOST_kStandardCursorLeftSide);
	DEF_CURSOR(right_side, GHOST_kStandardCursorRightSide);
	DEF_CURSOR(top_left_corner, GHOST_kStandardCursorTopLeftCorner);
	DEF_CURSOR(top_right_corner, GHOST_kStandardCursorTopRightCorner);
	DEF_CURSOR(bottom_right_corner, GHOST_kStandardCursorBottomRightCorner);
	DEF_CURSOR(bottom_left_corner, GHOST_kStandardCursorBottomLeftCorner);
	DEF_CURSOR(arrow, GHOST_kStandardCursorCopy);
	//DEF_CURSOR(arrow, GHOST_kStandardCursorCustom);
	DEF_CURSOR(arrow, GHOST_kStandardCursorPencil);

#undef DEF_CURSOR

}



GHOST_TSuccess
GHOST_WindowSDL::setWindowCursorGrab(GHOST_TGrabCursorMode mode)
{
	return GHOST_kSuccess;
}


GHOST_TSuccess
GHOST_WindowSDL::setWindowCursorShape(GHOST_TStandardCursor shape)
{
	if (sdl_std_cursor_array[0] == NULL) {
		sdl_cursor_init();
	}

	SDL_SetCursor(sdl_std_cursor_array[(int)shape]);
	return GHOST_kSuccess;
}


GHOST_TSuccess
GHOST_WindowSDL::setWindowCustomCursorShape(GHOST_TUns8 bitmap[16][2],
                                            GHOST_TUns8 mask[16][2],
                                            int hotX,
                                            int hotY)
{
	return setWindowCustomCursorShape((GHOST_TUns8 *)bitmap,
	                                  (GHOST_TUns8 *)mask,
	                                  16, 16,
	                                  hotX, hotY,
	                                  0, 1);
}


GHOST_TSuccess
GHOST_WindowSDL::setWindowCustomCursorShape(GHOST_TUns8 *bitmap,
                                            GHOST_TUns8 *mask,
                                            int sizex, int sizey,
                                            int hotX, int hotY,
                                            int fg_color, int bg_color)
{
	if (m_sdl_custom_cursor) {
		SDL_FreeCursor(m_sdl_custom_cursor);
	}

	m_sdl_custom_cursor = sdl_ghost_CreateCursor((const Uint8 *)bitmap,
	                                             (const Uint8 *)mask,
	                                             sizex, sizex,
	                                             hotX, hotY);

	SDL_SetCursor(m_sdl_custom_cursor);
	return GHOST_kSuccess;
}


GHOST_TSuccess
GHOST_WindowSDL::setWindowCursorVisibility(bool visible)
{
	SDL_ShowCursor(visible);
	return GHOST_kSuccess;
}

GHOST_TUns16
GHOST_WindowSDL::getDPIHint()
{
	int displayIndex = SDL_GetWindowDisplayIndex(m_sdl_win);
	if (displayIndex < 0) {
		return 96;
	}

	float ddpi;
	if (SDL_GetDisplayDPI(displayIndex, &ddpi, NULL, NULL) != 0) {
		return 96;
	}

	return (int)ddpi;
}