src/video/photon/SDL_ph_events.c
changeset 220 7861d904fb77
parent 19 8cc4dbfab9ab
child 252 e8157fcb3114
equal deleted inserted replaced
219:f928da36f0e9 220:7861d904fb77
   157 	}
   157 	}
   158 	return(posted);
   158 	return(posted);
   159 }
   159 }
   160 */
   160 */
   161 
   161 
       
   162 /* Control which motion flags the window has set, a flags value of -1 sets
       
   163  * MOTION_BUTTON and MOTION_NOBUTTON */
       
   164 static void set_motion_sensitivity(_THIS, unsigned int flags)
       
   165 {
       
   166 	int rid, fields = Ph_EV_PTR_MOTION_BUTTON | Ph_EV_PTR_MOTION_NOBUTTON;
       
   167 	PhRegion_t region;
       
   168 
       
   169 	if( window )
       
   170 	{
       
   171 		rid = PtWidgetRid( window );
       
   172 		if( rid != 0 && PhRegionQuery( rid, &region, NULL, NULL, 0 ) == 0 )
       
   173 		{
       
   174 			region.events_sense = ( region.events_sense & ~fields ) |
       
   175 				( flags & fields );
       
   176 			PhRegionChange( Ph_REGION_EV_SENSE, 0, &region,
       
   177 					NULL, NULL );
       
   178 		}
       
   179 	}
       
   180 }
       
   181 
       
   182 /* Convert the photon button state value to an SDL value */
       
   183 static Uint8 ph2sdl_mousebutton( unsigned short button_state )
       
   184 {
       
   185 	Uint8 mouse_button = 0;
       
   186 
       
   187 	if( button_state & Ph_BUTTON_SELECT )
       
   188 			mouse_button |= SDL_BUTTON_LEFT;
       
   189 	if( button_state & Ph_BUTTON_MENU )
       
   190 			mouse_button |= SDL_BUTTON_RIGHT;
       
   191 	if( button_state & Ph_BUTTON_ADJUST )
       
   192 			mouse_button |= SDL_BUTTON_MIDDLE;
       
   193 
       
   194 	return( mouse_button );
       
   195 }
       
   196 
   162 static int ph_DispatchEvent(_THIS)
   197 static int ph_DispatchEvent(_THIS)
   163 {
   198 {
   164 	int posted;
   199 	int posted;
   165 	PhRect_t* rect;
   200 	PhRect_t* rect;
   166 	PhPointerEvent_t* pointerEvent;
   201 	PhPointerEvent_t* pointerEvent;
   167 	PhKeyEvent_t* keyEvent;
   202 	PhKeyEvent_t* keyEvent;
   168 	PhWindowEvent_t* winEvent;
   203 	PhWindowEvent_t* winEvent;
   169 	int i;
   204 	int i, buttons;
   170 	SDL_Rect sdlrects[50]; 
   205 	SDL_Rect sdlrects[50]; 
   171 	
   206 	
   172 	posted = 0;
   207 	posted = 0;
   173 	
   208 	
   174 	switch (event->type) {
   209 	switch (event->type) {
   175 		case Ph_EV_BOUNDARY:
   210 		case Ph_EV_BOUNDARY:
   176 		{
   211 		{
   177 		
       
   178 			if (event->subtype == Ph_EV_PTR_ENTER)
   212 			if (event->subtype == Ph_EV_PTR_ENTER)
   179 				posted = SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
   213 				posted = SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
   180 			else if (event->subtype ==Ph_EV_PTR_LEAVE)
   214 			else if (event->subtype ==Ph_EV_PTR_LEAVE)
   181 				posted = SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);	
   215 				posted = SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);	
   182 		}
   216 		}
   183 		break;
   217 		break;
   184 
   218 
   185 		case Ph_EV_PTR_MOTION_BUTTON:
   219 		case Ph_EV_PTR_MOTION_BUTTON:
   186 		case Ph_EV_PTR_MOTION_NOBUTTON:
   220 		case Ph_EV_PTR_MOTION_NOBUTTON:
   187 		{
   221 		{
   188 		
       
   189 			if ( SDL_VideoSurface ) {
   222 			if ( SDL_VideoSurface ) {
   190 				pointerEvent = PhGetData( event );
   223 				pointerEvent = PhGetData( event );
   191 				rect = PhGetRects( event );
   224 				rect = PhGetRects( event );
   192 				posted = SDL_PrivateMouseMotion(0, 1,
   225 				posted = SDL_PrivateMouseMotion(0, 0,
   193                 		pointerEvent->pos.x - rect[0].ul.x,
   226 						rect->ul.x, rect->ul.y);		
   194                 		pointerEvent->pos.y - rect[0].ul.y);		
       
   195 			}
   227 			}
   196 		}
   228 		}
   197 		break;
   229 		break;
   198 
   230 
   199 		case Ph_EV_BUT_PRESS:
   231 		case Ph_EV_BUT_PRESS:
   200 		{
   232 		{
   201 
       
   202 			pointerEvent = PhGetData( event );
   233 			pointerEvent = PhGetData( event );
   203 			/* TODO: is 'buttons' the right mapping? */
   234 			buttons = ph2sdl_mousebutton( pointerEvent->buttons );
   204 			posted = SDL_PrivateMouseButton(SDL_PRESSED,
   235 			if( buttons != 0 )
   205                         	pointerEvent->buttons, 0, 0);
   236 			posted = SDL_PrivateMouseButton( SDL_PRESSED, buttons,
       
   237 					0, 0 );
   206 		}
   238 		}
   207 		break;
   239 		break;
   208 
   240 
   209 		case Ph_EV_BUT_RELEASE:
   241 		case Ph_EV_BUT_RELEASE:
   210 		{
   242 		{
   211 			
   243 			pointerEvent = PhGetData(event);
   212 			pointerEvent = PhGetData( event );
   244 			buttons = ph2sdl_mousebutton(pointerEvent->buttons);
   213 			 posted = SDL_PrivateMouseButton(SDL_RELEASED,
   245 			if( event->subtype == Ph_EV_RELEASE_REAL && 
   214          			pointerEvent->buttons, 0, 0);
   246 					buttons != 0 )
       
   247 			{
       
   248 				posted = SDL_PrivateMouseButton( SDL_RELEASED,
       
   249 						buttons, 0, 0 );
       
   250 			}
       
   251 			else if( event->subtype == Ph_EV_RELEASE_PHANTOM )
       
   252 			{
       
   253 				/* If the mouse is outside the window,
       
   254 				 * only a phantom release event is sent, so
       
   255 				 * check if the window doesn't have mouse focus.
       
   256 				 * Not perfect, maybe checking the mouse button
       
   257 				 * state for Ph_EV_BOUNDARY events would be
       
   258 				 * better. */
       
   259 				if( ( SDL_GetAppState() & SDL_APPMOUSEFOCUS ) == 0 )
       
   260 				{
       
   261 					posted = SDL_PrivateMouseButton( SDL_RELEASED,
       
   262 						buttons, 0, 0 );
       
   263 				}
       
   264 			}
   215 		}
   265 		}
   216 		break;
   266 		break;
   217 
   267 
   218 		case Ph_EV_WM:
   268 		case Ph_EV_WM:
   219 		{
   269 		{
   220 
       
   221 		
       
   222 			winEvent = PhGetData( event );
   270 			winEvent = PhGetData( event );
   223 			
   271 			
   224 			/* losing focus */
   272 			/* losing focus */
   225 			if ((winEvent->event_f==Ph_WM_FOCUS)&&
   273 			if ((winEvent->event_f==Ph_WM_FOCUS)&&
   226 				(winEvent->event_state==Ph_WM_EVSTATE_FOCUSLOST))
   274 				(winEvent->event_state==Ph_WM_EVSTATE_FOCUSLOST))
   227 			{
   275 			{
       
   276 				set_motion_sensitivity(this, Ph_EV_PTR_MOTION_BUTTON);
   228 				posted = SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);	
   277 				posted = SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);	
   229 
   278 
   230 				/* Queue leaving fullscreen mode */
   279 				/* Queue leaving fullscreen mode */
   231 				switch_waiting = 0x01;
   280 				switch_waiting = 0x01;
   232 				switch_time = SDL_GetTicks() + 200;
   281 				switch_time = SDL_GetTicks() + 200;
   234 
   283 
   235 			/* gaining focus */
   284 			/* gaining focus */
   236 			else if ((winEvent->event_f==Ph_WM_FOCUS)&&
   285 			else if ((winEvent->event_f==Ph_WM_FOCUS)&&
   237 					(winEvent->event_state==Ph_WM_EVSTATE_FOCUS))
   286 					(winEvent->event_state==Ph_WM_EVSTATE_FOCUS))
   238 			{
   287 			{
       
   288 				set_motion_sensitivity(this, -1);
   239 				posted = SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
   289 				posted = SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
   240 
   290 
   241 				/* Queue entry into fullscreen mode */
   291 				/* Queue entry into fullscreen mode */
   242 				switch_waiting = 0x01 | SDL_FULLSCREEN;
   292 				switch_waiting = 0x01 | SDL_FULLSCREEN;
   243 				switch_time = SDL_GetTicks() + 1500;
   293 				switch_time = SDL_GetTicks() + 1500;
   252 		break;
   302 		break;
   253 		
   303 		
   254 		/* window has been resized, moved or removed */
   304 		/* window has been resized, moved or removed */
   255 		case Ph_EV_EXPOSE:
   305 		case Ph_EV_EXPOSE:
   256 		{
   306 		{
   257 
       
   258 			if (SDL_VideoSurface)
   307 			if (SDL_VideoSurface)
   259 			{
   308 			{
   260 			  
   309 				rect = PhGetRects( event );
   261 			  
       
   262 					rect = PhGetRects( event );
       
   263 
   310 
   264 				//PgSetClipping(1, rect );
   311 				//PgSetClipping(1, rect );
   265 				 for(i=0;i<event->num_rects;i++)
   312 				for(i=0;i<event->num_rects;i++)
   266 			   {
   313 				{
   267 			      sdlrects[i].x = rect[i].ul.x;
   314 					sdlrects[i].x = rect[i].ul.x;
   268 			      sdlrects[i].y = rect[i].ul.y;
   315 					sdlrects[i].y = rect[i].ul.y;
   269 			      sdlrects[i].w = rect[i].lr.x - rect[i].ul.x;
   316 					sdlrects[i].w = rect[i].lr.x - rect[i].ul.x + 1;
   270 			      sdlrects[i].h = rect[i].lr.y - rect[i].ul.y;
   317 					sdlrects[i].h = rect[i].lr.y - rect[i].ul.y + 1;
   271 			      	
       
   272 				}
   318 				}
   273 			
   319 
   274 					this->UpdateRects(this, event->num_rects, sdlrects);
   320 				this->UpdateRects(this, event->num_rects, sdlrects);
   275 
   321 
   276 			}
   322 			}
   277 		}
   323 		}
   278 		break;
   324 		break;
   279 
   325 
   311 }
   357 }
   312 
   358 
   313 /* perform a blocking read if no events available */
   359 /* perform a blocking read if no events available */
   314 int ph_Pending(_THIS)
   360 int ph_Pending(_THIS)
   315 {
   361 {
   316 	
       
   317 	/* Flush the display connection and look to see if events are queued */
   362 	/* Flush the display connection and look to see if events are queued */
   318 
       
   319 	PgFlush();
   363 	PgFlush();
   320 
   364 
   321      while( 1 )
   365      while( 1 )
   322       {   //note this is a non-blocking call
   366       {   //note this is a non-blocking call
   323           switch( PhEventPeek( event, EVENT_SIZE ) )
   367           switch( PhEventPeek( event, EVENT_SIZE ) )
   524 	'sym' is set to the value of the key with modifiers applied to it. 
   568 	'sym' is set to the value of the key with modifiers applied to it. 
   525 	This member is valid only if Pk_KF_Sym_Valid is set in the key_flags.
   569 	This member is valid only if Pk_KF_Sym_Valid is set in the key_flags.
   526 	We will assume it is valid.
   570 	We will assume it is valid.
   527 */
   571 */
   528 	cap = key->key_cap;
   572 	cap = key->key_cap;
   529     switch (cap>>8) {
   573 	switch (cap>>8) {
   530             case 0x00:  /* Latin 1 */
   574             case 0x00:  /* Latin 1 */
   531             case 0x01:  /* Latin 2 */
   575             case 0x01:  /* Latin 2 */
   532             case 0x02:  /* Latin 3 */
   576             case 0x02:  /* Latin 3 */
   533             case 0x03:  /* Latin 4 */
   577             case 0x03:  /* Latin 4 */
   534             case 0x04:  /* Katakana */
   578             case 0x04:  /* Katakana */
   551                 keysym->sym = MISC_keymap[cap&0xFF];
   595                 keysym->sym = MISC_keymap[cap&0xFF];
   552                 break;
   596                 break;
   553             default:
   597             default:
   554                 fprintf(stderr,"Photon: Unknown key_cap, cap = 0x%.4x\n", (unsigned int)cap);
   598                 fprintf(stderr,"Photon: Unknown key_cap, cap = 0x%.4x\n", (unsigned int)cap);
   555                 break;
   599                 break;
   556     }	
   600 	}
       
   601 	keysym->scancode = key->key_scan;
   557 	return (keysym);
   602 	return (keysym);
   558 }
   603 }
   559 
   604 
   560 void ph_InitOSKeymap(_THIS)
   605 void ph_InitOSKeymap(_THIS)
   561 {
   606 {
   562 
       
   563 	ph_InitKeymap();
   607 	ph_InitKeymap();
   564 }
   608 }
   565 
   609