src/video/SDL_cursor.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 07 Feb 2006 06:59:48 +0000
changeset 1336 3692456e7b0f
parent 1330 450721ad5436
child 1338 604d73db6802
permissions -rw-r--r--
Use SDL_ prefixed versions of C library functions.
FIXME:
Change #include <stdlib.h> to #include "SDL_stdlib.h"
Change #include <string.h> to #include "SDL_string.h"
Make sure nothing else broke because of this...
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
/* General cursor handling code for SDL */
slouken@0
    24
slouken@0
    25
#include "SDL_mutex.h"
slouken@0
    26
#include "SDL_error.h"
slouken@0
    27
#include "SDL_video.h"
slouken@0
    28
#include "SDL_mouse.h"
slouken@1330
    29
#include "SDL_stdlib.h"
slouken@1330
    30
#include "SDL_string.h"
slouken@0
    31
#include "SDL_blit.h"
slouken@0
    32
#include "SDL_events_c.h"
slouken@0
    33
#include "SDL_sysvideo.h"
slouken@0
    34
#include "SDL_sysevents.h"
slouken@0
    35
#include "SDL_cursor_c.h"
slouken@0
    36
#include "SDL_pixels_c.h"
slouken@0
    37
#include "default_cursor.h"
slouken@0
    38
slouken@0
    39
/* These are static for our cursor handling code */
slouken@0
    40
volatile int SDL_cursorstate = CURSOR_VISIBLE;
slouken@0
    41
SDL_Cursor *SDL_cursor = NULL;
slouken@0
    42
static SDL_Cursor *SDL_defcursor = NULL;
slouken@0
    43
SDL_mutex *SDL_cursorlock = NULL;
slouken@0
    44
slouken@0
    45
/* Public functions */
slouken@0
    46
void SDL_CursorQuit(void)
slouken@0
    47
{
slouken@0
    48
	if ( SDL_cursor != NULL ) {
slouken@0
    49
		SDL_Cursor *cursor;
slouken@0
    50
slouken@0
    51
		SDL_cursorstate &= ~CURSOR_VISIBLE;
slouken@0
    52
		if ( SDL_cursor != SDL_defcursor ) {
slouken@0
    53
			SDL_FreeCursor(SDL_cursor);
slouken@0
    54
		}
slouken@0
    55
		SDL_cursor = NULL;
slouken@0
    56
		if ( SDL_defcursor != NULL ) {
slouken@0
    57
			cursor = SDL_defcursor;
slouken@0
    58
			SDL_defcursor = NULL;
slouken@0
    59
			SDL_FreeCursor(cursor);
slouken@0
    60
		}
slouken@0
    61
	}
slouken@0
    62
	if ( SDL_cursorlock != NULL ) {
slouken@0
    63
		SDL_DestroyMutex(SDL_cursorlock);
slouken@0
    64
		SDL_cursorlock = NULL;
slouken@0
    65
	}
slouken@0
    66
}
slouken@0
    67
int SDL_CursorInit(Uint32 multithreaded)
slouken@0
    68
{
slouken@0
    69
	/* We don't have mouse focus, and the cursor isn't drawn yet */
icculus@1140
    70
#ifndef IPOD
slouken@0
    71
	SDL_cursorstate = CURSOR_VISIBLE;
icculus@1140
    72
#endif
slouken@0
    73
slouken@0
    74
	/* Create the default cursor */
slouken@0
    75
	if ( SDL_defcursor == NULL ) {
slouken@0
    76
		SDL_defcursor = SDL_CreateCursor(default_cdata, default_cmask,
slouken@0
    77
					DEFAULT_CWIDTH, DEFAULT_CHEIGHT,
slouken@0
    78
						DEFAULT_CHOTX, DEFAULT_CHOTY);
slouken@0
    79
		SDL_SetCursor(SDL_defcursor);
slouken@0
    80
	}
slouken@0
    81
slouken@0
    82
	/* Create a lock if necessary */
slouken@0
    83
	if ( multithreaded ) {
slouken@0
    84
		SDL_cursorlock = SDL_CreateMutex();
slouken@0
    85
	}
slouken@0
    86
slouken@0
    87
	/* That's it! */
slouken@0
    88
	return(0);
slouken@0
    89
}
slouken@0
    90
slouken@0
    91
/* Multi-thread support for cursors */
slouken@0
    92
#ifndef SDL_LockCursor
slouken@0
    93
void SDL_LockCursor(void)
slouken@0
    94
{
slouken@0
    95
	if ( SDL_cursorlock ) {
slouken@0
    96
		SDL_mutexP(SDL_cursorlock);
slouken@0
    97
	}
slouken@0
    98
}
slouken@0
    99
#endif
slouken@0
   100
#ifndef SDL_UnlockCursor
slouken@0
   101
void SDL_UnlockCursor(void)
slouken@0
   102
{
slouken@0
   103
	if ( SDL_cursorlock ) {
slouken@0
   104
		SDL_mutexV(SDL_cursorlock);
slouken@0
   105
	}
slouken@0
   106
}
slouken@0
   107
#endif
slouken@0
   108
slouken@0
   109
/* Software cursor drawing support */
slouken@0
   110
SDL_Cursor * SDL_CreateCursor (Uint8 *data, Uint8 *mask, 
slouken@0
   111
					int w, int h, int hot_x, int hot_y)
slouken@0
   112
{
slouken@0
   113
	SDL_VideoDevice *video = current_video;
slouken@0
   114
	int savelen;
slouken@0
   115
	int i;
slouken@0
   116
	SDL_Cursor *cursor;
slouken@0
   117
slouken@0
   118
	/* Make sure the width is a multiple of 8 */
slouken@0
   119
	w = ((w+7)&~7);
slouken@0
   120
slouken@0
   121
	/* Sanity check the hot spot */
slouken@0
   122
	if ( (hot_x < 0) || (hot_y < 0) || (hot_x >= w) || (hot_y >= h) ) {
slouken@0
   123
		SDL_SetError("Cursor hot spot doesn't lie within cursor");
slouken@0
   124
		return(NULL);
slouken@0
   125
	}
slouken@0
   126
slouken@0
   127
	/* Allocate memory for the cursor */
slouken@1336
   128
	cursor = (SDL_Cursor *)SDL_malloc(sizeof *cursor);
slouken@0
   129
	if ( cursor == NULL ) {
slouken@0
   130
		SDL_OutOfMemory();
slouken@0
   131
		return(NULL);
slouken@0
   132
	}
slouken@0
   133
	savelen = (w*4)*h;
slouken@0
   134
	cursor->area.x = 0;
slouken@0
   135
	cursor->area.y = 0;
slouken@0
   136
	cursor->area.w = w;
slouken@0
   137
	cursor->area.h = h;
slouken@0
   138
	cursor->hot_x = hot_x;
slouken@0
   139
	cursor->hot_y = hot_y;
slouken@1336
   140
	cursor->data = (Uint8 *)SDL_malloc((w/8)*h*2);
slouken@0
   141
	cursor->mask = cursor->data+((w/8)*h);
slouken@1336
   142
	cursor->save[0] = (Uint8 *)SDL_malloc(savelen*2);
slouken@0
   143
	cursor->save[1] = cursor->save[0] + savelen;
slouken@0
   144
	cursor->wm_cursor = NULL;
slouken@0
   145
	if ( ! cursor->data || ! cursor->save[0] ) {
slouken@0
   146
		SDL_FreeCursor(cursor);
slouken@0
   147
		SDL_OutOfMemory();
slouken@0
   148
		return(NULL);
slouken@0
   149
	}
slouken@0
   150
	for ( i=((w/8)*h)-1; i>=0; --i ) {
slouken@0
   151
		cursor->data[i] = data[i];
slouken@0
   152
		cursor->mask[i] = mask[i] | data[i];
slouken@0
   153
	}
slouken@1336
   154
	SDL_memset(cursor->save[0], 0, savelen*2);
slouken@0
   155
slouken@0
   156
	/* If the window manager gives us a good cursor, we're done! */
slouken@0
   157
	if ( video->CreateWMCursor ) {
slouken@0
   158
		cursor->wm_cursor = video->CreateWMCursor(video, data, mask,
slouken@0
   159
							w, h, hot_x, hot_y);
slouken@0
   160
	} else {
slouken@0
   161
		cursor->wm_cursor = NULL;
slouken@0
   162
	}
slouken@0
   163
	return(cursor);
slouken@0
   164
}
slouken@0
   165
slouken@0
   166
/* SDL_SetCursor(NULL) can be used to force the cursor redraw,
slouken@0
   167
   if this is desired for any reason.  This is used when setting
slouken@0
   168
   the video mode and when the SDL window gains the mouse focus.
slouken@0
   169
 */
slouken@0
   170
void SDL_SetCursor (SDL_Cursor *cursor)
slouken@0
   171
{
slouken@0
   172
	SDL_VideoDevice *video = current_video;
slouken@0
   173
	SDL_VideoDevice *this  = current_video;
slouken@0
   174
slouken@0
   175
	/* Make sure that the video subsystem has been initialized */
slouken@0
   176
	if ( ! video ) {
slouken@0
   177
		return;
slouken@0
   178
	}
slouken@0
   179
slouken@0
   180
	/* Prevent the event thread from moving the mouse */
slouken@0
   181
	SDL_LockCursor();
slouken@0
   182
slouken@0
   183
	/* Set the new cursor */
slouken@0
   184
	if ( cursor && (cursor != SDL_cursor) ) {
slouken@0
   185
		/* Erase the current mouse position */
slouken@0
   186
		if ( SHOULD_DRAWCURSOR(SDL_cursorstate) ) {
slouken@0
   187
			SDL_EraseCursor(SDL_VideoSurface);
slouken@0
   188
		} else if ( video->MoveWMCursor ) {
slouken@0
   189
			/* If the video driver is moving the cursor directly,
slouken@0
   190
			   it needs to hide the old cursor before (possibly)
slouken@0
   191
			   showing the new one.  (But don't erase NULL cursor)
slouken@0
   192
			 */
slouken@0
   193
			if ( SDL_cursor ) {
slouken@0
   194
				video->ShowWMCursor(this, NULL);
slouken@0
   195
			}
slouken@0
   196
		}
slouken@0
   197
		SDL_cursor = cursor;
slouken@0
   198
	}
slouken@0
   199
slouken@0
   200
	/* Draw the new mouse cursor */
slouken@0
   201
	if ( SDL_cursor && (SDL_cursorstate&CURSOR_VISIBLE) ) {
slouken@0
   202
		/* Use window manager cursor if possible */
slouken@0
   203
		if ( SDL_cursor->wm_cursor && 
slouken@1296
   204
	             video->ShowWMCursor(this, SDL_cursor->wm_cursor) ) {
slouken@0
   205
			SDL_cursorstate &= ~CURSOR_USINGSW;
slouken@1296
   206
		} else {
slouken@0
   207
			SDL_cursorstate |= CURSOR_USINGSW;
slouken@0
   208
			if ( video->ShowWMCursor ) {
slouken@0
   209
				video->ShowWMCursor(this, NULL);
slouken@0
   210
			}
slouken@0
   211
			{ int x, y;
slouken@0
   212
				SDL_GetMouseState(&x, &y);
slouken@0
   213
				SDL_cursor->area.x = (x - SDL_cursor->hot_x);
slouken@0
   214
				SDL_cursor->area.y = (y - SDL_cursor->hot_y);
slouken@0
   215
			}
slouken@0
   216
			SDL_DrawCursor(SDL_VideoSurface);
slouken@0
   217
		}
slouken@0
   218
	} else {
slouken@0
   219
		/* Erase window manager mouse (cursor not visible) */
slouken@0
   220
		if ( SDL_cursor && (SDL_cursorstate & CURSOR_USINGSW) ) {
slouken@0
   221
			SDL_EraseCursor(SDL_VideoSurface);
slouken@0
   222
		} else {
slouken@0
   223
			if ( video ) {
slouken@0
   224
				video->ShowWMCursor(this, NULL);
slouken@0
   225
			}
slouken@0
   226
		}
slouken@0
   227
	}
slouken@0
   228
	SDL_UnlockCursor();
slouken@0
   229
}
slouken@0
   230
slouken@0
   231
SDL_Cursor * SDL_GetCursor (void)
slouken@0
   232
{
slouken@0
   233
	return(SDL_cursor);
slouken@0
   234
}
slouken@0
   235
slouken@0
   236
void SDL_FreeCursor (SDL_Cursor *cursor)
slouken@0
   237
{
slouken@0
   238
	if ( cursor ) {
slouken@0
   239
		if ( cursor == SDL_cursor ) {
slouken@0
   240
			SDL_SetCursor(SDL_defcursor);
slouken@0
   241
		}
slouken@0
   242
		if ( cursor != SDL_defcursor ) {
slouken@77
   243
			SDL_VideoDevice *video = current_video;
slouken@77
   244
			SDL_VideoDevice *this  = current_video;
slouken@77
   245
slouken@0
   246
			if ( cursor->data ) {
slouken@1336
   247
				SDL_free(cursor->data);
slouken@0
   248
			}
slouken@0
   249
			if ( cursor->save[0] ) {
slouken@1336
   250
				SDL_free(cursor->save[0]);
slouken@0
   251
			}
slouken@77
   252
			if ( video && cursor->wm_cursor ) {
slouken@0
   253
				video->FreeWMCursor(this, cursor->wm_cursor);
slouken@0
   254
			}
slouken@1336
   255
			SDL_free(cursor);
slouken@0
   256
		}
slouken@0
   257
	}
slouken@0
   258
}
slouken@0
   259
slouken@0
   260
int SDL_ShowCursor (int toggle)
slouken@0
   261
{
slouken@0
   262
	int showing;
slouken@0
   263
slouken@0
   264
	showing = (SDL_cursorstate & CURSOR_VISIBLE);
slouken@0
   265
	if ( toggle >= 0 ) {
slouken@0
   266
		SDL_LockCursor();
slouken@0
   267
		if ( toggle ) {
slouken@0
   268
			SDL_cursorstate |= CURSOR_VISIBLE;
slouken@0
   269
		} else {
slouken@0
   270
			SDL_cursorstate &= ~CURSOR_VISIBLE;
slouken@0
   271
		}
slouken@0
   272
		SDL_UnlockCursor();
slouken@0
   273
		if ( (SDL_cursorstate & CURSOR_VISIBLE) != showing ) {
slouken@0
   274
			SDL_VideoDevice *video = current_video;
slouken@0
   275
			SDL_VideoDevice *this  = current_video;
slouken@0
   276
slouken@0
   277
			SDL_SetCursor(NULL);
slouken@0
   278
			if ( video && video->CheckMouseMode ) {
slouken@0
   279
				video->CheckMouseMode(this);
slouken@0
   280
			}
slouken@0
   281
		}
slouken@0
   282
	} else {
slouken@0
   283
		/* Query current state */ ;
slouken@0
   284
	}
slouken@0
   285
	return(showing ? 1 : 0);
slouken@0
   286
}
slouken@0
   287
slouken@0
   288
void SDL_WarpMouse (Uint16 x, Uint16 y)
slouken@0
   289
{
slouken@0
   290
	SDL_VideoDevice *video = current_video;
slouken@0
   291
	SDL_VideoDevice *this  = current_video;
slouken@0
   292
slouken@595
   293
	if ( !video || !SDL_PublicSurface ) {
slouken@595
   294
		SDL_SetError("A video mode must be set before warping mouse");
slouken@595
   295
		return;
slouken@595
   296
	}
slouken@595
   297
slouken@527
   298
	/* If we have an offset video mode, offset the mouse coordinates */
icculus@1163
   299
	if (this->screen->pitch == 0) {
icculus@1163
   300
		x += this->screen->offset / this->screen->format->BytesPerPixel;
icculus@1163
   301
		y += this->screen->offset;
icculus@1163
   302
	} else {
icculus@1163
   303
		x += (this->screen->offset % this->screen->pitch) /
icculus@1163
   304
		      this->screen->format->BytesPerPixel;
icculus@1163
   305
		y += (this->screen->offset / this->screen->pitch);
icculus@1163
   306
	}
slouken@527
   307
slouken@0
   308
	/* This generates a mouse motion event */
slouken@0
   309
	if ( video->WarpWMCursor ) {
slouken@0
   310
		video->WarpWMCursor(this, x, y);
slouken@0
   311
	} else {
slouken@0
   312
		SDL_PrivateMouseMotion(0, 0, x, y);
slouken@0
   313
	}
slouken@0
   314
}
slouken@0
   315
slouken@0
   316
void SDL_MoveCursor(int x, int y)
slouken@0
   317
{
slouken@0
   318
	SDL_VideoDevice *video = current_video;
slouken@0
   319
slouken@0
   320
	/* Erase and update the current mouse position */
slouken@0
   321
	if ( SHOULD_DRAWCURSOR(SDL_cursorstate) ) {
slouken@0
   322
		/* Erase and redraw mouse cursor in new position */
slouken@0
   323
		SDL_LockCursor();
slouken@0
   324
		SDL_EraseCursor(SDL_VideoSurface);
slouken@0
   325
		SDL_cursor->area.x = (x - SDL_cursor->hot_x);
slouken@0
   326
		SDL_cursor->area.y = (y - SDL_cursor->hot_y);
slouken@0
   327
		SDL_DrawCursor(SDL_VideoSurface);
slouken@0
   328
		SDL_UnlockCursor();
slouken@0
   329
	} else if ( video->MoveWMCursor ) {
slouken@0
   330
		video->MoveWMCursor(video, x, y);
slouken@0
   331
	}
slouken@0
   332
}
slouken@0
   333
slouken@0
   334
/* Keep track of the current cursor colors */
slouken@0
   335
static int palette_changed = 1;
slouken@0
   336
static Uint32 pixels8[2];
slouken@0
   337
slouken@0
   338
void SDL_CursorPaletteChanged(void)
slouken@0
   339
{
slouken@0
   340
	palette_changed = 1;
slouken@0
   341
}
slouken@0
   342
slouken@0
   343
void SDL_MouseRect(SDL_Rect *area)
slouken@0
   344
{
slouken@0
   345
	int clip_diff;
slouken@0
   346
slouken@0
   347
	*area = SDL_cursor->area;
slouken@0
   348
	if ( area->x < 0 ) {
slouken@0
   349
		area->w += area->x;
slouken@0
   350
		area->x = 0;
slouken@0
   351
	}
slouken@0
   352
	if ( area->y < 0 ) {
slouken@0
   353
		area->h += area->y;
slouken@0
   354
		area->y = 0;
slouken@0
   355
	}
slouken@0
   356
	clip_diff = (area->x+area->w)-SDL_VideoSurface->w;
slouken@0
   357
	if ( clip_diff > 0 ) {
slouken@113
   358
		area->w = area->w < clip_diff ? 0 : area->w-clip_diff;
slouken@0
   359
	}
slouken@0
   360
	clip_diff = (area->y+area->h)-SDL_VideoSurface->h;
slouken@0
   361
	if ( clip_diff > 0 ) {
slouken@113
   362
		area->h = area->h < clip_diff ? 0 : area->h-clip_diff;
slouken@0
   363
	}
slouken@0
   364
}
slouken@0
   365
slouken@0
   366
static void SDL_DrawCursorFast(SDL_Surface *screen, SDL_Rect *area)
slouken@0
   367
{
slouken@0
   368
	const Uint32 pixels[2] = { 0xFFFFFFFF, 0x00000000 };
slouken@0
   369
	int i, w, h;
slouken@0
   370
	Uint8 *data, datab;
slouken@0
   371
	Uint8 *mask, maskb;
slouken@0
   372
slouken@0
   373
	data = SDL_cursor->data + area->y * SDL_cursor->area.w/8;
slouken@0
   374
	mask = SDL_cursor->mask + area->y * SDL_cursor->area.w/8;
slouken@0
   375
	switch (screen->format->BytesPerPixel) {
slouken@0
   376
slouken@0
   377
	    case 1: {
slouken@0
   378
		Uint8 *dst;
slouken@0
   379
		int dstskip;
slouken@0
   380
slouken@0
   381
		if ( palette_changed ) {
slouken@0
   382
			pixels8[0] = SDL_MapRGB(screen->format, 255, 255, 255);
slouken@0
   383
			pixels8[1] = SDL_MapRGB(screen->format, 0, 0, 0);
slouken@0
   384
			palette_changed = 0;
slouken@0
   385
		}
slouken@0
   386
		dst = (Uint8 *)screen->pixels +
slouken@0
   387
                       (SDL_cursor->area.y+area->y)*screen->pitch +
slouken@0
   388
                       SDL_cursor->area.x;
slouken@0
   389
		dstskip = screen->pitch-area->w;
slouken@0
   390
slouken@0
   391
		for ( h=area->h; h; h-- ) {
slouken@0
   392
			for ( w=area->w/8; w; w-- ) {
slouken@0
   393
				maskb = *mask++;
slouken@0
   394
				datab = *data++;
slouken@0
   395
				for ( i=0; i<8; ++i ) {
slouken@0
   396
					if ( maskb & 0x80 ) {
slouken@0
   397
						*dst = pixels8[datab>>7];
slouken@0
   398
					}
slouken@0
   399
					maskb <<= 1;
slouken@0
   400
					datab <<= 1;
slouken@0
   401
					dst++;
slouken@0
   402
				}
slouken@0
   403
			}
slouken@0
   404
			dst += dstskip;
slouken@0
   405
		}
slouken@0
   406
	    }
slouken@0
   407
	    break;
slouken@0
   408
slouken@0
   409
	    case 2: {
slouken@0
   410
		Uint16 *dst;
slouken@0
   411
		int dstskip;
slouken@0
   412
slouken@0
   413
		dst = (Uint16 *)screen->pixels +
slouken@0
   414
                       (SDL_cursor->area.y+area->y)*screen->pitch/2 +
slouken@0
   415
                       SDL_cursor->area.x;
slouken@0
   416
		dstskip = (screen->pitch/2)-area->w;
slouken@0
   417
slouken@0
   418
		for ( h=area->h; h; h-- ) {
slouken@0
   419
			for ( w=area->w/8; w; w-- ) {
slouken@0
   420
				maskb = *mask++;
slouken@0
   421
				datab = *data++;
slouken@0
   422
				for ( i=0; i<8; ++i ) {
slouken@0
   423
					if ( maskb & 0x80 ) {
slouken@0
   424
						*dst = pixels[datab>>7];
slouken@0
   425
					}
slouken@0
   426
					maskb <<= 1;
slouken@0
   427
					datab <<= 1;
slouken@0
   428
					dst++;
slouken@0
   429
				}
slouken@0
   430
			}
slouken@0
   431
			dst += dstskip;
slouken@0
   432
		}
slouken@0
   433
	    }
slouken@0
   434
	    break;
slouken@0
   435
slouken@0
   436
	    case 3: {
slouken@0
   437
		Uint8 *dst;
slouken@0
   438
		int dstskip;
slouken@0
   439
slouken@0
   440
		dst = (Uint8 *)screen->pixels +
slouken@0
   441
                       (SDL_cursor->area.y+area->y)*screen->pitch +
slouken@0
   442
                       SDL_cursor->area.x*3;
slouken@0
   443
		dstskip = screen->pitch-area->w*3;
slouken@0
   444
slouken@0
   445
		for ( h=area->h; h; h-- ) {
slouken@0
   446
			for ( w=area->w/8; w; w-- ) {
slouken@0
   447
				maskb = *mask++;
slouken@0
   448
				datab = *data++;
slouken@0
   449
				for ( i=0; i<8; ++i ) {
slouken@0
   450
					if ( maskb & 0x80 ) {
slouken@1336
   451
						SDL_memset(dst,pixels[datab>>7],3);
slouken@0
   452
					}
slouken@0
   453
					maskb <<= 1;
slouken@0
   454
					datab <<= 1;
slouken@0
   455
					dst += 3;
slouken@0
   456
				}
slouken@0
   457
			}
slouken@0
   458
			dst += dstskip;
slouken@0
   459
		}
slouken@0
   460
	    }
slouken@0
   461
	    break;
slouken@0
   462
slouken@0
   463
	    case 4: {
slouken@0
   464
		Uint32 *dst;
slouken@0
   465
		int dstskip;
slouken@0
   466
slouken@0
   467
		dst = (Uint32 *)screen->pixels +
slouken@0
   468
                       (SDL_cursor->area.y+area->y)*screen->pitch/4 +
slouken@0
   469
                       SDL_cursor->area.x;
slouken@0
   470
		dstskip = (screen->pitch/4)-area->w;
slouken@0
   471
slouken@0
   472
		for ( h=area->h; h; h-- ) {
slouken@0
   473
			for ( w=area->w/8; w; w-- ) {
slouken@0
   474
				maskb = *mask++;
slouken@0
   475
				datab = *data++;
slouken@0
   476
				for ( i=0; i<8; ++i ) {
slouken@0
   477
					if ( maskb & 0x80 ) {
slouken@0
   478
						*dst = pixels[datab>>7];
slouken@0
   479
					}
slouken@0
   480
					maskb <<= 1;
slouken@0
   481
					datab <<= 1;
slouken@0
   482
					dst++;
slouken@0
   483
				}
slouken@0
   484
			}
slouken@0
   485
			dst += dstskip;
slouken@0
   486
		}
slouken@0
   487
	    }
slouken@0
   488
	    break;
slouken@0
   489
	}
slouken@0
   490
}
slouken@0
   491
slouken@0
   492
static void SDL_DrawCursorSlow(SDL_Surface *screen, SDL_Rect *area)
slouken@0
   493
{
slouken@0
   494
	const Uint32 pixels[2] = { 0xFFFFFF, 0x000000 };
slouken@0
   495
	int h;
slouken@0
   496
	int x, minx, maxx;
slouken@0
   497
	Uint8 *data, datab = 0;
slouken@0
   498
	Uint8 *mask, maskb = 0;
slouken@0
   499
	Uint8 *dst;
slouken@0
   500
	int dstbpp, dstskip;
slouken@0
   501
slouken@0
   502
	data = SDL_cursor->data + area->y * SDL_cursor->area.w/8;
slouken@0
   503
	mask = SDL_cursor->mask + area->y * SDL_cursor->area.w/8;
slouken@0
   504
	dstbpp = screen->format->BytesPerPixel;
slouken@0
   505
	dst = (Uint8 *)screen->pixels +
slouken@0
   506
                       (SDL_cursor->area.y+area->y)*screen->pitch +
slouken@0
   507
                       SDL_cursor->area.x*dstbpp;
slouken@0
   508
	dstskip = screen->pitch-SDL_cursor->area.w*dstbpp;
slouken@0
   509
slouken@0
   510
	minx = area->x;
slouken@0
   511
	maxx = area->x+area->w;
slouken@0
   512
	if ( screen->format->BytesPerPixel == 1 ) {
slouken@0
   513
		if ( palette_changed ) {
slouken@0
   514
			pixels8[0] = SDL_MapRGB(screen->format, 255, 255, 255);
slouken@0
   515
			pixels8[1] = SDL_MapRGB(screen->format, 0, 0, 0);
slouken@0
   516
			palette_changed = 0;
slouken@0
   517
		}
slouken@0
   518
		for ( h=area->h; h; h-- ) {
slouken@0
   519
			for ( x=0; x<SDL_cursor->area.w; ++x ) {
slouken@0
   520
				if ( (x%8) == 0 ) {
slouken@0
   521
					maskb = *mask++;
slouken@0
   522
					datab = *data++;
slouken@0
   523
				}
slouken@0
   524
				if ( (x >= minx) && (x < maxx) ) {
slouken@0
   525
					if ( maskb & 0x80 ) {
slouken@1336
   526
						SDL_memset(dst, pixels8[datab>>7], dstbpp);
slouken@0
   527
					}
slouken@0
   528
				}
slouken@0
   529
				maskb <<= 1;
slouken@0
   530
				datab <<= 1;
slouken@0
   531
				dst += dstbpp;
slouken@0
   532
			}
slouken@0
   533
			dst += dstskip;
slouken@0
   534
		}
slouken@0
   535
	} else {
slouken@0
   536
		for ( h=area->h; h; h-- ) {
slouken@0
   537
			for ( x=0; x<SDL_cursor->area.w; ++x ) {
slouken@0
   538
				if ( (x%8) == 0 ) {
slouken@0
   539
					maskb = *mask++;
slouken@0
   540
					datab = *data++;
slouken@0
   541
				}
slouken@0
   542
				if ( (x >= minx) && (x < maxx) ) {
slouken@0
   543
					if ( maskb & 0x80 ) {
slouken@1336
   544
						SDL_memset(dst, pixels[datab>>7], dstbpp);
slouken@0
   545
					}
slouken@0
   546
				}
slouken@0
   547
				maskb <<= 1;
slouken@0
   548
				datab <<= 1;
slouken@0
   549
				dst += dstbpp;
slouken@0
   550
			}
slouken@0
   551
			dst += dstskip;
slouken@0
   552
		}
slouken@0
   553
	}
slouken@0
   554
}
slouken@0
   555
slouken@0
   556
/* This handles the ugly work of converting the saved cursor background from
slouken@0
   557
   the pixel format of the shadow surface to that of the video surface.
slouken@0
   558
   This is only necessary when blitting from a shadow surface of a different
slouken@0
   559
   pixel format than the video surface, and using a software rendered cursor.
slouken@0
   560
*/
slouken@0
   561
static void SDL_ConvertCursorSave(SDL_Surface *screen, int w, int h)
slouken@0
   562
{
slouken@0
   563
	SDL_BlitInfo info;
slouken@0
   564
	SDL_loblit RunBlit;
slouken@0
   565
slouken@0
   566
	/* Make sure we can steal the blit mapping */
slouken@0
   567
	if ( screen->map->dst != SDL_VideoSurface ) {
slouken@0
   568
		return;
slouken@0
   569
	}
slouken@0
   570
slouken@0
   571
	/* Set up the blit information */
slouken@0
   572
	info.s_pixels = SDL_cursor->save[1];
slouken@0
   573
	info.s_width = w;
slouken@0
   574
	info.s_height = h;
slouken@0
   575
	info.s_skip = 0;
slouken@0
   576
	info.d_pixels = SDL_cursor->save[0];
slouken@0
   577
	info.d_width = w;
slouken@0
   578
	info.d_height = h;
slouken@0
   579
	info.d_skip = 0;
slouken@0
   580
	info.aux_data = screen->map->sw_data->aux_data;
slouken@0
   581
	info.src = screen->format;
slouken@0
   582
	info.table = screen->map->table;
slouken@0
   583
	info.dst = SDL_VideoSurface->format;
slouken@0
   584
	RunBlit = screen->map->sw_data->blit;
slouken@0
   585
slouken@0
   586
	/* Run the actual software blit */
slouken@0
   587
	RunBlit(&info);
slouken@0
   588
}
slouken@0
   589
slouken@0
   590
void SDL_DrawCursorNoLock(SDL_Surface *screen)
slouken@0
   591
{
slouken@0
   592
	SDL_Rect area;
slouken@0
   593
slouken@0
   594
	/* Get the mouse rectangle, clipped to the screen */
slouken@0
   595
	SDL_MouseRect(&area);
slouken@0
   596
	if ( (area.w == 0) || (area.h == 0) ) {
slouken@0
   597
		return;
slouken@0
   598
	}
slouken@0
   599
slouken@0
   600
	/* Copy mouse background */
slouken@0
   601
	{ int w, h, screenbpp;
slouken@0
   602
	  Uint8 *src, *dst;
slouken@0
   603
slouken@0
   604
	  /* Set up the copy pointers */
slouken@0
   605
	  screenbpp = screen->format->BytesPerPixel;
slouken@0
   606
	  if ( (screen == SDL_VideoSurface) ||
slouken@0
   607
	          FORMAT_EQUAL(screen->format, SDL_VideoSurface->format) ) {
slouken@0
   608
		dst = SDL_cursor->save[0];
slouken@0
   609
	  } else {
slouken@0
   610
		dst = SDL_cursor->save[1];
slouken@0
   611
	  }
slouken@0
   612
	  src = (Uint8 *)screen->pixels + area.y * screen->pitch +
slouken@0
   613
                                          area.x * screenbpp;
slouken@0
   614
slouken@0
   615
	  /* Perform the copy */
slouken@0
   616
	  w = area.w*screenbpp;
slouken@0
   617
	  h = area.h;
slouken@0
   618
	  while ( h-- ) {
slouken@1336
   619
		  SDL_memcpy(dst, src, w);
slouken@0
   620
		  dst += w;
slouken@0
   621
		  src += screen->pitch;
slouken@0
   622
	  }
slouken@0
   623
	}
slouken@0
   624
slouken@0
   625
	/* Draw the mouse cursor */
slouken@0
   626
	area.x -= SDL_cursor->area.x;
slouken@0
   627
	area.y -= SDL_cursor->area.y;
slouken@0
   628
	if ( (area.x == 0) && (area.w == SDL_cursor->area.w) ) {
slouken@0
   629
		SDL_DrawCursorFast(screen, &area);
slouken@0
   630
	} else {
slouken@0
   631
		SDL_DrawCursorSlow(screen, &area);
slouken@0
   632
	}
slouken@0
   633
}
slouken@0
   634
slouken@0
   635
void SDL_DrawCursor(SDL_Surface *screen)
slouken@0
   636
{
slouken@0
   637
	/* Lock the screen if necessary */
slouken@0
   638
	if ( screen == NULL ) {
slouken@0
   639
		return;
slouken@0
   640
	}
slouken@0
   641
	if ( SDL_MUSTLOCK(screen) ) {
slouken@0
   642
		if ( SDL_LockSurface(screen) < 0 ) {
slouken@0
   643
			return;
slouken@0
   644
		}
slouken@0
   645
	}
slouken@0
   646
slouken@0
   647
	SDL_DrawCursorNoLock(screen);
slouken@0
   648
slouken@0
   649
	/* Unlock the screen and update if necessary */
slouken@0
   650
	if ( SDL_MUSTLOCK(screen) ) {
slouken@0
   651
		SDL_UnlockSurface(screen);
slouken@0
   652
	}
slouken@0
   653
	if ( (screen == SDL_VideoSurface) &&
slouken@0
   654
	     ((screen->flags & SDL_HWSURFACE) != SDL_HWSURFACE) ) {
slouken@0
   655
		SDL_VideoDevice *video = current_video;
slouken@0
   656
		SDL_VideoDevice *this  = current_video;
slouken@0
   657
		SDL_Rect area;
slouken@0
   658
slouken@0
   659
		SDL_MouseRect(&area);
slouken@0
   660
slouken@0
   661
		/* This can be called before a video mode is set */
slouken@0
   662
		if ( video->UpdateRects ) {
slouken@0
   663
			video->UpdateRects(this, 1, &area);
slouken@0
   664
		}
slouken@0
   665
	}
slouken@0
   666
}
slouken@0
   667
slouken@0
   668
void SDL_EraseCursorNoLock(SDL_Surface *screen)
slouken@0
   669
{
slouken@0
   670
	SDL_Rect area;
slouken@0
   671
slouken@0
   672
	/* Get the mouse rectangle, clipped to the screen */
slouken@0
   673
	SDL_MouseRect(&area);
slouken@0
   674
	if ( (area.w == 0) || (area.h == 0) ) {
slouken@0
   675
		return;
slouken@0
   676
	}
slouken@0
   677
slouken@0
   678
	/* Copy mouse background */
slouken@0
   679
	{ int w, h, screenbpp;
slouken@0
   680
	  Uint8 *src, *dst;
slouken@0
   681
slouken@0
   682
	  /* Set up the copy pointers */
slouken@0
   683
	  screenbpp = screen->format->BytesPerPixel;
slouken@0
   684
	  if ( (screen == SDL_VideoSurface) ||
slouken@0
   685
	          FORMAT_EQUAL(screen->format, SDL_VideoSurface->format) ) {
slouken@0
   686
		src = SDL_cursor->save[0];
slouken@0
   687
	  } else {
slouken@0
   688
		src = SDL_cursor->save[1];
slouken@0
   689
	  }
slouken@0
   690
	  dst = (Uint8 *)screen->pixels + area.y * screen->pitch +
slouken@0
   691
                                          area.x * screenbpp;
slouken@0
   692
slouken@0
   693
	  /* Perform the copy */
slouken@0
   694
	  w = area.w*screenbpp;
slouken@0
   695
	  h = area.h;
slouken@0
   696
	  while ( h-- ) {
slouken@1336
   697
		  SDL_memcpy(dst, src, w);
slouken@0
   698
		  src += w;
slouken@0
   699
		  dst += screen->pitch;
slouken@0
   700
	  }
slouken@0
   701
slouken@0
   702
	  /* Perform pixel conversion on cursor background */
slouken@0
   703
	  if ( src > SDL_cursor->save[1] ) {
slouken@0
   704
		SDL_ConvertCursorSave(screen, area.w, area.h);
slouken@0
   705
	  }
slouken@0
   706
	}
slouken@0
   707
}
slouken@0
   708
slouken@0
   709
void SDL_EraseCursor(SDL_Surface *screen)
slouken@0
   710
{
slouken@0
   711
	/* Lock the screen if necessary */
slouken@0
   712
	if ( screen == NULL ) {
slouken@0
   713
		return;
slouken@0
   714
	}
slouken@0
   715
	if ( SDL_MUSTLOCK(screen) ) {
slouken@0
   716
		if ( SDL_LockSurface(screen) < 0 ) {
slouken@0
   717
			return;
slouken@0
   718
		}
slouken@0
   719
	}
slouken@0
   720
slouken@0
   721
	SDL_EraseCursorNoLock(screen);
slouken@0
   722
slouken@0
   723
	/* Unlock the screen and update if necessary */
slouken@0
   724
	if ( SDL_MUSTLOCK(screen) ) {
slouken@0
   725
		SDL_UnlockSurface(screen);
slouken@0
   726
	}
slouken@0
   727
	if ( (screen == SDL_VideoSurface) &&
slouken@0
   728
	     ((screen->flags & SDL_HWSURFACE) != SDL_HWSURFACE) ) {
slouken@0
   729
		SDL_VideoDevice *video = current_video;
slouken@0
   730
		SDL_VideoDevice *this  = current_video;
slouken@0
   731
		SDL_Rect area;
slouken@0
   732
slouken@0
   733
		SDL_MouseRect(&area);
slouken@1
   734
		if ( video->UpdateRects ) {
slouken@1
   735
			video->UpdateRects(this, 1, &area);
slouken@1
   736
		}
slouken@0
   737
	}
slouken@0
   738
}
slouken@0
   739
slouken@0
   740
/* Reset the cursor on video mode change
slouken@0
   741
   FIXME:  Keep track of all cursors, and reset them all.
slouken@0
   742
 */
slouken@0
   743
void SDL_ResetCursor(void)
slouken@0
   744
{
slouken@0
   745
	int savelen;
slouken@0
   746
slouken@0
   747
	if ( SDL_cursor ) {
slouken@0
   748
		savelen = SDL_cursor->area.w*4*SDL_cursor->area.h;
slouken@0
   749
		SDL_cursor->area.x = 0;
slouken@0
   750
		SDL_cursor->area.y = 0;
slouken@1336
   751
		SDL_memset(SDL_cursor->save[0], 0, savelen);
slouken@0
   752
	}
slouken@0
   753
}