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