src/video/photon/SDL_ph_mouse.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 20 Feb 2002 01:05:51 +0000
changeset 283 3d8b6b9f1e18
parent 266 c6abdda2f666
child 297 f6ffac90895c
permissions -rw-r--r--
Date: Mon, 18 Feb 2002 16:46:59 +1200
From: Julian Kinraid <jkinraid@clear.net.nz>
Subject: Patches for photon port of SDL

Hi,

A couple more patches for photon and the nto audio. Adds mouse grabbing
support, fixed cursor images, unicode keyboard events (though no unicode
data on kye release, is that a problem?), hopefully fixing some audio
lag problems, and a few other fixes.

Thanks,
Julian Kinraid
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@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
#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@283
    65
	unsigned char bit, databit, maskbit;
slouken@0
    66
slouken@0
    67
	/* Allocate and initialize the cursor memory */
slouken@0
    68
	if ((cursor = (WMcursor*)malloc(sizeof(WMcursor))) == NULL)
slouken@0
    69
	{
slouken@0
    70
        SDL_OutOfMemory();
slouken@0
    71
        return(NULL);
slouken@0
    72
	}
slouken@0
    73
	memset(cursor,0,sizeof(WMcursor));
slouken@283
    74
slouken@0
    75
	cursor->ph_cursor = (PhCursorDef_t *) malloc(sizeof(PhCursorDef_t) + 32*4*2);
slouken@0
    76
	if(cursor->ph_cursor == NULL)
slouken@0
    77
	   printf("cursor malloc failed\n");
slouken@0
    78
slouken@0
    79
	memset(cursor->ph_cursor,0,(sizeof(PhCursorDef_t) + 32*4*2));
slouken@283
    80
slouken@0
    81
	cursor->ph_cursor->hdr.type =Ph_RDATA_CURSOR;   
slouken@0
    82
	cursor->ph_cursor->size1.x = (short)w;
slouken@0
    83
	cursor->ph_cursor->size1.y = (short)h;
slouken@0
    84
	cursor->ph_cursor->offset1.x = (short)hot_x;
slouken@0
    85
	cursor->ph_cursor->offset1.y = (short)hot_y;
slouken@0
    86
	cursor->ph_cursor->bytesperline1 = (char)w/8;
slouken@0
    87
	cursor->ph_cursor->color1 = Pg_WHITE;
slouken@0
    88
	cursor->ph_cursor->size2.x = (short)w;
slouken@266
    89
        cursor->ph_cursor->size2.y = (short)h;
slouken@266
    90
        cursor->ph_cursor->offset2.x = (short)hot_x;
slouken@266
    91
        cursor->ph_cursor->offset2.y = (short)hot_y;
slouken@266
    92
        cursor->ph_cursor->bytesperline2 = (char)w/8;
slouken@266
    93
        cursor->ph_cursor->color2 = Pg_BLACK;
slouken@283
    94
slouken@0
    95
	clen = (w/8)*h;
slouken@0
    96
slouken@0
    97
	/* Copy the mask and the data to different 
slouken@0
    98
	   bitmap planes */
slouken@266
    99
	for ( i=0; i<clen; ++i )
slouken@283
   100
	{
slouken@283
   101
		for ( bit = 0; bit < 8; bit++ )
slouken@283
   102
		{
slouken@283
   103
			databit = data[i] & (1 << bit);
slouken@283
   104
			maskbit = mask[i] & (1 << bit);
slouken@283
   105
slouken@283
   106
			cursor->ph_cursor->images[i] |= 
slouken@283
   107
				(databit == 0) ? maskbit : 0;
slouken@283
   108
			/* If the databit != 0, treat it as a black pixel and
slouken@283
   109
			 * ignore the maskbit (can't do an inverted color) */
slouken@283
   110
			cursor->ph_cursor->images[i+clen] |= databit;
slouken@283
   111
		}
slouken@283
   112
	}
slouken@283
   113
slouken@283
   114
        /* #bytes following the hdr struct */
slouken@0
   115
	cursor->ph_cursor->hdr.len =sizeof(PhCursorDef_t) + clen*2 - sizeof(PhRegionDataHdr_t); 
slouken@0
   116
slouken@0
   117
	return (cursor);
slouken@0
   118
}
slouken@0
   119
slouken@0
   120
slouken@0
   121
PhCursorDef_t ph_GetWMPhCursor(WMcursor *cursor)
slouken@0
   122
{
slouken@0
   123
	return(*cursor->ph_cursor);
slouken@0
   124
}
slouken@0
   125
slouken@0
   126
int ph_ShowWMCursor(_THIS, WMcursor *cursor)
slouken@0
   127
{
slouken@0
   128
	PtArg_t args[3];
slouken@0
   129
	int nargs = 0;
slouken@0
   130
	short cursor_is_defined = 0;
slouken@0
   131
slouken@0
   132
	/* Don't do anything if the display is gone */
slouken@0
   133
 	if ( window == NULL ) {
slouken@0
   134
    	 return(0);
slouken@0
   135
 	}
slouken@0
   136
slouken@0
   137
	/* Set the photon cursor cursor, or blank if cursor is NULL */
slouken@0
   138
	if ( window ) {
slouken@0
   139
		
slouken@0
   140
		if ( cursor != NULL ) {
slouken@0
   141
			PtSetArg( &args[0], Pt_ARG_CURSOR_TYPE, Ph_CURSOR_BITMAP, 0 );
slouken@283
   142
			/* Could set next to any PgColor_t value */
slouken@0
   143
			PtSetArg( &args[1], Pt_ARG_CURSOR_COLOR,Ph_CURSOR_DEFAULT_COLOR , 0 );
slouken@0
   144
			PtSetArg( &args[2], Pt_ARG_BITMAP_CURSOR, cursor->ph_cursor, (cursor->ph_cursor->hdr.len + sizeof(PhRegionDataHdr_t)) );
slouken@0
   145
			nargs = 3;
slouken@0
   146
			cursor_is_defined = 1;
slouken@0
   147
		}
slouken@283
   148
		else /* Ph_CURSOR_NONE */
slouken@0
   149
		{
slouken@0
   150
			PtSetArg( &args[0], Pt_ARG_CURSOR_TYPE,Ph_CURSOR_NONE, 0);
slouken@0
   151
			nargs = 1;
slouken@0
   152
			cursor_is_defined = 1;
slouken@0
   153
		}
slouken@0
   154
		if (cursor_is_defined)
slouken@266
   155
		{
slouken@266
   156
    	                SDL_Lock_EventThread();
slouken@0
   157
			
slouken@0
   158
			if (PtSetResources( window, nargs, args ) < 0 )
slouken@0
   159
			{
slouken@0
   160
			    return(0);
slouken@0
   161
			}	
slouken@0
   162
						
slouken@0
   163
			SDL_Unlock_EventThread();
slouken@0
   164
		}
slouken@0
   165
		else
slouken@0
   166
			return(0);
slouken@0
   167
	}
slouken@0
   168
	return(1);
slouken@0
   169
}
slouken@0
   170
slouken@0
   171
void ph_WarpWMCursor(_THIS, Uint16 x, Uint16 y)
slouken@0
   172
{
slouken@283
   173
	short abs_x, abs_y;
slouken@283
   174
slouken@283
   175
	SDL_Lock_EventThread();
slouken@283
   176
	PtGetAbsPosition( window, &abs_x, &abs_y );
slouken@283
   177
	PhMoveCursorAbs( PhInputGroup(NULL), x + abs_x, y + abs_y );
slouken@283
   178
	SDL_Unlock_EventThread();
slouken@0
   179
}
slouken@0
   180
slouken@0
   181
slouken@0
   182
void ph_CheckMouseMode(_THIS)
slouken@0
   183
{
slouken@283
   184
        /* If the mouse is hidden and input is grabbed, we use relative mode */
slouken@283
   185
        if ( !(SDL_cursorstate & CURSOR_VISIBLE) &&
slouken@283
   186
             (this->input_grab != SDL_GRAB_OFF) ) {
slouken@283
   187
                mouse_relative = 1;
slouken@283
   188
        } else {
slouken@283
   189
                mouse_relative = 0;
slouken@283
   190
        }
slouken@0
   191
}