src/video/photon/SDL_ph_mouse.c
author Sam Lantinga <slouken@lokigames.com>
Thu, 26 Apr 2001 16:45:43 +0000
changeset 0 74212992fb08
child 252 e8157fcb3114
permissions -rw-r--r--
Initial revision
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@0
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  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@0
    20
    slouken@devolution.com
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
#include <stdlib.h>
slouken@0
    29
#include <stdio.h>
slouken@0
    30
#include <string.h>
slouken@0
    31
slouken@0
    32
#include "SDL_error.h"
slouken@0
    33
#include "SDL_mouse.h"
slouken@0
    34
#include "SDL_events_c.h"
slouken@0
    35
#include "SDL_cursor_c.h"
slouken@0
    36
#include "SDL_ph_mouse_c.h"
slouken@0
    37
slouken@0
    38
struct  WMcursor {
slouken@0
    39
	PhCursorDef_t *ph_cursor ;
slouken@0
    40
};
slouken@0
    41
slouken@0
    42
slouken@0
    43
void ph_FreeWMCursor(_THIS, WMcursor *cursor)
slouken@0
    44
{
slouken@0
    45
slouken@0
    46
    if ( window != NULL ) {
slouken@0
    47
		SDL_Lock_EventThread();
slouken@0
    48
		
slouken@0
    49
		if (PtSetResource( window, Pt_ARG_CURSOR_TYPE, Ph_CURSOR_INHERIT, 0 ) < 0)
slouken@0
    50
		{
slouken@0
    51
			//TODO: output error msg
slouken@0
    52
		}
slouken@0
    53
		
slouken@0
    54
		SDL_Unlock_EventThread();
slouken@0
    55
	}	
slouken@0
    56
	//free(cursor->ph_cursor.images);
slouken@0
    57
	free(cursor);
slouken@0
    58
}
slouken@0
    59
slouken@0
    60
WMcursor *ph_CreateWMCursor(_THIS,
slouken@0
    61
		Uint8 *data, Uint8 *mask, int w, int h, int hot_x, int hot_y)
slouken@0
    62
{
slouken@0
    63
	WMcursor* cursor;
slouken@0
    64
	int clen, i;
slouken@0
    65
slouken@0
    66
	/* Allocate and initialize the cursor memory */
slouken@0
    67
	if ((cursor = (WMcursor*)malloc(sizeof(WMcursor))) == NULL)
slouken@0
    68
	{
slouken@0
    69
        SDL_OutOfMemory();
slouken@0
    70
        return(NULL);
slouken@0
    71
	}
slouken@0
    72
	memset(cursor,0,sizeof(WMcursor));
slouken@0
    73
	
slouken@0
    74
	cursor->ph_cursor = (PhCursorDef_t *) malloc(sizeof(PhCursorDef_t) + 32*4*2);
slouken@0
    75
	if(cursor->ph_cursor == NULL)
slouken@0
    76
	   printf("cursor malloc failed\n");
slouken@0
    77
slouken@0
    78
	memset(cursor->ph_cursor,0,(sizeof(PhCursorDef_t) + 32*4*2));
slouken@0
    79
	   
slouken@0
    80
	cursor->ph_cursor->hdr.type =Ph_RDATA_CURSOR;   
slouken@0
    81
	cursor->ph_cursor->size1.x = (short)w;
slouken@0
    82
	cursor->ph_cursor->size1.y = (short)h;
slouken@0
    83
	cursor->ph_cursor->offset1.x = (short)hot_x;
slouken@0
    84
	cursor->ph_cursor->offset1.y = (short)hot_y;
slouken@0
    85
	cursor->ph_cursor->bytesperline1 = (char)w/8;
slouken@0
    86
	cursor->ph_cursor->color1 = Pg_WHITE;
slouken@0
    87
	cursor->ph_cursor->size2.x = (short)w;
slouken@0
    88
    cursor->ph_cursor->size2.y = (short)h;
slouken@0
    89
    cursor->ph_cursor->offset2.x = (short)hot_x;
slouken@0
    90
    cursor->ph_cursor->offset2.y = (short)hot_y;
slouken@0
    91
    cursor->ph_cursor->bytesperline2 = (char)w/8;
slouken@0
    92
    cursor->ph_cursor->color2 = Pg_BLACK;
slouken@0
    93
      
slouken@0
    94
	clen = (w/8)*h;
slouken@0
    95
slouken@0
    96
	/* Copy the mask and the data to different 
slouken@0
    97
	   bitmap planes */
slouken@0
    98
	for ( i=0; i<clen; ++i ) {
slouken@0
    99
        cursor->ph_cursor->images[i] = data[i];
slouken@0
   100
        cursor->ph_cursor->images[i+clen] = mask[i];
slouken@0
   101
    }
slouken@0
   102
    
slouken@0
   103
    //#bytes following the hdr struct
slouken@0
   104
	cursor->ph_cursor->hdr.len =sizeof(PhCursorDef_t) + clen*2 - sizeof(PhRegionDataHdr_t); 
slouken@0
   105
slouken@0
   106
	return (cursor);
slouken@0
   107
}
slouken@0
   108
slouken@0
   109
slouken@0
   110
PhCursorDef_t ph_GetWMPhCursor(WMcursor *cursor)
slouken@0
   111
{
slouken@0
   112
slouken@0
   113
	return(*cursor->ph_cursor);
slouken@0
   114
}
slouken@0
   115
slouken@0
   116
slouken@0
   117
int ph_ShowWMCursor(_THIS, WMcursor *cursor)
slouken@0
   118
{
slouken@0
   119
	PtArg_t args[3];
slouken@0
   120
	int nargs = 0;
slouken@0
   121
	short cursor_is_defined = 0;
slouken@0
   122
slouken@0
   123
slouken@0
   124
slouken@0
   125
	/* Don't do anything if the display is gone */
slouken@0
   126
 	if ( window == NULL ) {
slouken@0
   127
    	 return(0);
slouken@0
   128
 	}
slouken@0
   129
slouken@0
   130
	/* Set the photon cursor cursor, or blank if cursor is NULL */
slouken@0
   131
	if ( window ) {
slouken@0
   132
		
slouken@0
   133
		if ( cursor != NULL ) {
slouken@0
   134
			PtSetArg( &args[0], Pt_ARG_CURSOR_TYPE, Ph_CURSOR_BITMAP, 0 );
slouken@0
   135
			// Could set next to any PgColor_t value
slouken@0
   136
			PtSetArg( &args[1], Pt_ARG_CURSOR_COLOR,Ph_CURSOR_DEFAULT_COLOR , 0 );
slouken@0
   137
			PtSetArg( &args[2], Pt_ARG_BITMAP_CURSOR, cursor->ph_cursor, (cursor->ph_cursor->hdr.len + sizeof(PhRegionDataHdr_t)) );
slouken@0
   138
			nargs = 3;
slouken@0
   139
			cursor_is_defined = 1;
slouken@0
   140
		}
slouken@0
   141
		else // Ph_CURSOR_NONE
slouken@0
   142
		{
slouken@0
   143
			PtSetArg( &args[0], Pt_ARG_CURSOR_TYPE,Ph_CURSOR_NONE, 0);
slouken@0
   144
			nargs = 1;
slouken@0
   145
			cursor_is_defined = 1;
slouken@0
   146
		}
slouken@0
   147
		if (cursor_is_defined)
slouken@0
   148
		 {
slouken@0
   149
    	    SDL_Lock_EventThread();
slouken@0
   150
			
slouken@0
   151
			if (PtSetResources( window, nargs, args ) < 0 )
slouken@0
   152
			{
slouken@0
   153
			    return(0);
slouken@0
   154
			}	
slouken@0
   155
						
slouken@0
   156
			SDL_Unlock_EventThread();
slouken@0
   157
		}
slouken@0
   158
		else
slouken@0
   159
			return(0);
slouken@0
   160
	}
slouken@0
   161
	return(1);
slouken@0
   162
}
slouken@0
   163
slouken@0
   164
void ph_WarpWMCursor(_THIS, Uint16 x, Uint16 y)
slouken@0
   165
{
slouken@0
   166
slouken@0
   167
	SDL_Lock_EventThread();
slouken@0
   168
	PhMoveCursorRel( PhInputGroup(NULL), x, y );	
slouken@0
   169
	SDL_Unlock_EventThread();
slouken@0
   170
}
slouken@0
   171
slouken@0
   172
slouken@0
   173
void ph_CheckMouseMode(_THIS)
slouken@0
   174
{
slouken@0
   175
slouken@0
   176
	mouse_relative = 1;
slouken@0
   177
}