1/*
2    SDL - Simple DirectMedia Layer
3    Copyright (C) 1997-2012 Sam Lantinga
4
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with this library; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19    Sam Lantinga
20    slouken@libsdl.org
21*/
22#include "SDL_config.h"
23
24#if defined(__APPLE__) && defined(__MACH__)
25#include <Carbon/Carbon.h>
26#elif TARGET_API_MAC_CARBON && (UNIVERSAL_INTERFACES_VERSION > 0x0335)
27#include <Carbon.h>
28#else
29#include <Quickdraw.h>
30#endif
31
32/* Routines that are not supported by the Carbon API... */
33#if !TARGET_API_MAC_CARBON
34#include <CursorDevices.h>
35#endif
36
37#include "SDL_mouse.h"
38#include "SDL_macmouse_c.h"
39
40
41/* The implementation dependent data for the window manager cursor */
42struct WMcursor {
43	Cursor curs;
44};
45
46
47void Mac_FreeWMCursor(_THIS, WMcursor *cursor)
48{
49	SDL_free(cursor);
50}
51
52WMcursor *Mac_CreateWMCursor(_THIS,
53		Uint8 *data, Uint8 *mask, int w, int h, int hot_x, int hot_y)
54{
55	WMcursor *cursor;
56	int row, bytes;
57
58	/* Allocate the cursor memory */
59	cursor = (WMcursor *)SDL_malloc(sizeof(WMcursor));
60	if ( cursor == NULL ) {
61		SDL_OutOfMemory();
62		return(NULL);
63	}
64	SDL_memset(cursor, 0, sizeof(*cursor));
65
66    if (w > 16)
67        w = 16;
68
69    if (h > 16)
70        h = 16;
71
72	bytes = (w+7)/8;
73
74	for ( row=0; row<h; ++row ) {
75		SDL_memcpy(&cursor->curs.data[row], data, bytes);
76		data += bytes;
77	}
78	for ( row=0; row<h; ++row ) {
79		SDL_memcpy(&cursor->curs.mask[row], mask, bytes);
80		mask += bytes;
81	}
82	cursor->curs.hotSpot.h = hot_x;
83	cursor->curs.hotSpot.v = hot_y;
84
85	/* That was easy. :) */
86	return(cursor);
87}
88
89int Mac_cursor_showing = 1;
90
91int Mac_ShowWMCursor(_THIS, WMcursor *cursor)
92{
93	if ( cursor == NULL ) {
94		if ( Mac_cursor_showing ) {
95			HideCursor();
96			Mac_cursor_showing = 0;
97		}
98	} else {
99		SetCursor(&cursor->curs);
100		if ( ! Mac_cursor_showing ) {
101			ShowCursor();
102			Mac_cursor_showing = 1;
103		}
104	}
105	return(1);
106}
107
108void Mac_WarpWMCursor(_THIS, Uint16 x, Uint16 y)
109{
110#if !TARGET_API_MAC_CARBON
111	CursorDevice *cursordevice;
112
113	cursordevice = nil;
114	CursorDeviceNextDevice(&cursordevice);
115	if ( cursordevice != nil ) {
116		WindowPtr saveport;
117		Point where;
118
119		GetPort(&saveport);
120		SetPort(SDL_Window);
121		where.h = x;
122		where.v = y;
123		LocalToGlobal(&where);
124		SetPort(saveport);
125		CursorDeviceMoveTo(cursordevice, where.h, where.v);
126	}
127#endif /* !TARGET_API_MAC_CARBON */
128}
129
130