Esempio n. 1
0
// --------------------------------------------------------------
//	joy_process()
//
// Runs as a separate thread, and updates the state of the joystick
//
DWORD joy_process(DWORD lparam)
{
	MMRESULT		rs;
	JOYINFOEX	ji;
	int			i,state;
	joy_button_info	*bi;	

	for ( i = 0; i < JOY_TOTAL_BUTTONS; i++) {
		bi = &joy_buttons[i];
		bi->actual_state = 0;		// Don't set in flush code!
		bi->state		= 0;
		bi->down_count	= 0;
		bi->up_count	= 0;
		bi->down_time	= 0;
		bi->last_down_check = timer_get_milliseconds();
	}

	while (1) {
		// Wait for the thread to be signaled to end or 1/18th of a second to pass...
		if ( WaitForSingleObject( Joy_tell_thread_to_end_event, joy_pollrate )==WAIT_OBJECT_0)	{
			break;
		}

		memset(&ji, 0, sizeof(ji));
		ji.dwSize = sizeof(ji);
//		ji.dwFlags = JOY_RETURNBUTTONS | JOY_RETURNRAWDATA;
		ji.dwFlags = JOY_RETURNALL;

		EnterCriticalSection(&joy_lock);

		uint joy_state = 0;
		if (Cur_joystick >= 0) {
			rs = joyGetPosEx(Cur_joystick, &ji);
			// If there's an error, assume all buttons down.
			if (rs == JOYERR_NOERROR) {
				joy_state = ji.dwButtons;
			}
		}

		// Process ji.dwButtons
		for (i=0; i<JOY_TOTAL_BUTTONS; i++) {
			state = 0;
			if (i < JOY_NUM_BUTTONS) {
				state = joy_state & (1<<i);

			} else {
				// check for hat presses, which act like buttons
				switch (i) {
					case JOY_HATBACK:
						if (ji.dwPOV == JOY_POVBACKWARD)
							state = 1;
						break;

					case JOY_HATFORWARD:
						if (ji.dwPOV == JOY_POVFORWARD)
							state = 1;
						break;

					case JOY_HATLEFT:
						if (ji.dwPOV == JOY_POVLEFT)
							state = 1;
						break;

					case JOY_HATRIGHT:
						if (ji.dwPOV == JOY_POVRIGHT)
							state = 1;
						break;

					default:
						Int3();	// should never happen
						break;

				}	// end switch
			}	// end if


			if (state != joy_buttons[i].actual_state) {
				// Button position physically changed.
				joy_buttons[i].actual_state = state;

				if ( state )	{
					// went from up to down
					joy_buttons[i].down_count++;
					joy_buttons[i].down_time = 0;

					joy_buttons[i].state = 1;

////////////////////////////
/// SOMETHING TERRIBLE IS ABOUT TO HAPPEN.  I FEEL THIS IS NECESSARY FOR THE DEMO, SINCE
/// I DON'T WANT TO CALL CRITICAL SECTION CODE EACH FRAME TO CHECK ALL THE JOYSTICK BUTTONS.
/// PLEASE SEE ALAN FOR MORE INFORMATION.
////////////////////////////
#ifdef FS2_DEMO
					{
					extern void demo_reset_trailer_timer();
					demo_reset_trailer_timer();
					}
#endif
////////////////////////////
/// IT'S OVER.  SEE, IT WASN'T SO BAD RIGHT?  IT'S IS VERY UGLY LOOKING, I KNOW.
////////////////////////////


				} else {
					// went from down to up
					if ( joy_buttons[i].state )	{
						joy_buttons[i].up_count++;
					}
					joy_buttons[i].state = 0;
				}

			} else {
				// Didn't move... increment time down if down.
				if (joy_buttons[i].state) {
					joy_buttons[i].down_time += joy_pollrate;
				}
			}

		}  // end for

		LeaveCriticalSection(&joy_lock);
	}

	SetEvent(Joy_thread_says_its_done_event);	

	return 0;
}
Esempio n. 2
0
// --------------------------------------------------------------
//	joy_process()
//
// Runs as a separate thread, and updates the state of the joystick
//
DWORD joy_process(DWORD lparam)
{
	MMRESULT		rs;
	JOYINFOEX	ji;
	int			i,state;
	joy_button_info	*bi;
	EXECUTION_STATE last_exectution_state = 0;
	uint last_ssav_time = 0;


	// power management stuff, we need to handle this manually for joysticks
	{
		// give notification that we need both display and system (for multi) resources to remain available
		// NOTE: we'll have to restore the previous execution state before exiting this thread
		last_exectution_state = SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED);

		// turn of screen saver, but save the current timeout so that we can restore it later
		SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0, &last_ssav_time, 0);
		SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0);
	}


	for ( i = 0; i < JOY_TOTAL_BUTTONS; i++) {
		bi = &joy_buttons[i];
		bi->actual_state = 0;		// Don't set in flush code!
		bi->state		= 0;
		bi->down_count	= 0;
		bi->up_count	= 0;
		bi->down_time	= 0;
		bi->last_down_check = timer_get_milliseconds();
	}

	while (1) {
		// Wait for the thread to be signaled to end or 1/18th of a second to pass...
		if ( WaitForSingleObject( Joy_tell_thread_to_end_event, joy_pollrate )==WAIT_OBJECT_0)	{
			break;
		}

		memset(&ji, 0, sizeof(ji));
		ji.dwSize = sizeof(ji);
//		ji.dwFlags = JOY_RETURNBUTTONS | JOY_RETURNRAWDATA;
		ji.dwFlags = JOY_RETURNALL;

		EnterCriticalSection(&joy_lock);

		uint joy_state = 0;
		if (Cur_joystick >= 0) {
			rs = joyGetPosEx(Cur_joystick, &ji);
			// If there's an error, assume all buttons down.
			if (rs == JOYERR_NOERROR) {
				joy_state = ji.dwButtons;
			}
		}

		// Process ji.dwButtons
		for (i=0; i<JOY_TOTAL_BUTTONS; i++) {
			state = 0;
			if (i < JOY_NUM_BUTTONS) {
				state = joy_state & (1<<i);

			} else {
				// check for hat presses, which act like buttons
				switch (i) {
					case JOY_HATBACK:
						if (ji.dwPOV == JOY_POVBACKWARD)
							state = 1;
						break;

					case JOY_HATFORWARD:
						if (ji.dwPOV == JOY_POVFORWARD)
							state = 1;
						break;

					case JOY_HATLEFT:
						if (ji.dwPOV == JOY_POVLEFT)
							state = 1;
						break;

					case JOY_HATRIGHT:
						if (ji.dwPOV == JOY_POVRIGHT)
							state = 1;
						break;

					default:
						Int3();	// should never happen
						break;

				}	// end switch
			}	// end if


			if (state != joy_buttons[i].actual_state) {
				// Button position physically changed.
				joy_buttons[i].actual_state = state;

				if ( state )	{
					// went from up to down
					joy_buttons[i].down_count++;
					joy_buttons[i].down_time = 0;

					joy_buttons[i].state = 1;

////////////////////////////
/// SOMETHING TERRIBLE IS ABOUT TO HAPPEN.  I FEEL THIS IS NECESSARY FOR THE DEMO, SINCE
/// I DON'T WANT TO CALL CRITICAL SECTION CODE EACH FRAME TO CHECK ALL THE JOYSTICK BUTTONS.
/// PLEASE SEE ALAN FOR MORE INFORMATION.
////////////////////////////
#ifdef FS2_DEMO
					{
					extern void demo_reset_trailer_timer();
					demo_reset_trailer_timer();
					}
#endif
////////////////////////////
/// IT'S OVER.  SEE, IT WASN'T SO BAD RIGHT?  IT'S IS VERY UGLY LOOKING, I KNOW.
////////////////////////////


				} else {
					// went from down to up
					if ( joy_buttons[i].state )	{
						joy_buttons[i].up_count++;
					}
					joy_buttons[i].state = 0;
				}

			} else {
				// Didn't move... increment time down if down.
				if (joy_buttons[i].state) {
					joy_buttons[i].down_time += joy_pollrate;
				}
			}

		}  // end for

		LeaveCriticalSection(&joy_lock);
	}

	// power management stuff, we need to handle this manually for joysticks
	{
		// restore the original execution state
		last_exectution_state = SetThreadExecutionState(last_exectution_state);

		// restore the original screensaver timeout value
		SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, last_ssav_time, NULL, 0);
	}

	SetEvent(Joy_thread_says_its_done_event);	

	return 0;
}
Esempio n. 3
0
void mouse_mark_button( uint flags, int set)
{
	if ( !mouse_inited ) return;

	ENTER_CRITICAL_SECTION(&mouse_lock);

	if ( !(mouse_flags & MOUSE_LEFT_BUTTON) )	{

		if ( (flags & MOUSE_LEFT_BUTTON) && (set == 1) ) {
			mouse_left_pressed++;

////////////////////////////
/// SOMETHING TERRIBLE IS ABOUT TO HAPPEN.  I FEEL THIS IS NECESSARY FOR THE DEMO, SINCE
/// I DON'T WANT TO CALL CRITICAL SECTION CODE EACH FRAME TO CHECK THE LEFT MOUSE BUTTON.
/// PLEASE SEE ALAN FOR MORE INFORMATION.
////////////////////////////
#ifdef FS2_DEMO
					{
					extern void demo_reset_trailer_timer();
					demo_reset_trailer_timer();
					}
#endif
////////////////////////////
/// IT'S OVER.  SEE, IT WASN'T SO BAD RIGHT?  IT'S IS VERY UGLY LOOKING, I KNOW.
////////////////////////////

		}
	}
	else {
		if ( (flags & MOUSE_LEFT_BUTTON) && (set == 0) ){
			mouse_left_up++;
		}
	}

	if ( !(mouse_flags & MOUSE_RIGHT_BUTTON) )	{

		if ( (flags & MOUSE_RIGHT_BUTTON) && (set == 1) ){
			mouse_right_pressed++;
		}
	}
	else {
		if ( (flags & MOUSE_RIGHT_BUTTON) && (set == 0) ){
			mouse_right_up++;
		}
	}

	if ( !(mouse_flags & MOUSE_MIDDLE_BUTTON) )	{

		if ( (flags & MOUSE_MIDDLE_BUTTON) && (set == 1) ){
			mouse_middle_pressed++;
		}
	}
	else {
		if ( (flags & MOUSE_MIDDLE_BUTTON) && (set == 0) ){
			mouse_middle_up++;
		}
	}

	if ( set ){
		mouse_flags |= flags;
	} else {
		mouse_flags &= ~flags;
	}

	LEAVE_CRITICAL_SECTION(&mouse_lock);	
}