Example #1
0
File: system.c Project: xtools/xt
xt_core_message_t *receive_message(xt_net_client_system_t *client)
{
  xt_core_message_t *message;

  if (ensure_client_is_connected(client)) {

    pthread_mutex_lock(&client->messaging_mutex);
    {
      xt_net_exchange_send_and_receive_messages(client->exchange);
      message = xt_net_post_system_receive_message(client->post);

      if (xt_net_post_system_is_socket_closed(client->post)) {
        client->server_socket_closed = xt_core_bool_true;
        if (!handle_disconnect(client)) {
          xt_core_log_trace(client->log, "xnet", "handle_disconnect");
        }
      }
    }
    pthread_mutex_unlock(&client->messaging_mutex);

  } else {
    message = NULL;
    xt_core_log_trace(client->log, "xnet", "ensure_client_is_connected");
  }

  return message;
}
Example #2
0
void handle(int fd) {
    char buf[200];
    ssize_t n_read;
    while((n_read = read(fd, buf, sizeof(buf))) > 0) {
        buf[n_read-1] = '\0';
        
        char* bufp = buf;
        char* cursor;
        while((cursor = strsep(&bufp, "\n")) != NULL) {
            int fdd = dup(fd);
            FILE* f = fdopen(fdd, "a");

            char command[20];
            char const* const remainder = flatjson_next(chomp(buf), command, sizeof(command), NULL);
            if(strcmp(command, "list") == 0) {
                handle_list(f, true);
            } else if(strcmp(command, "configure") == 0) {
                handle_configure(f, remainder);
            } else if(strcmp(command, "connect") == 0) {
                handle_connect(f, remainder);
            } else if(strcmp(command, "disconnect") == 0) {
                handle_disconnect(f, remainder);
            } else {
                warn("Unknown command");
            }

            fclose(f);
        }
    }
}
Example #3
0
File: system.c Project: xtools/xt
xt_core_bool_t xt_net_client_system_send_message
(xt_net_client_system_t *client, xt_core_message_t *message)
{
  xt_core_bool_t success;

  if (ensure_client_is_connected(client)) {

    pthread_mutex_lock(&client->messaging_mutex);
    {
      if (xt_net_post_system_send_message(client->post, message)) {
        success = xt_core_bool_true;
        xt_net_exchange_send_and_receive_messages(client->exchange);

        if (xt_net_post_system_is_socket_closed(client->post)) {
          client->server_socket_closed = xt_core_bool_true;
          if (!handle_disconnect(client)) {
            xt_core_log_trace(client->log, "xnet", "handle_disconnect");
          }
        }
      } else {
        success = xt_core_bool_false;
        xt_core_log_trace(client->log, "xnet", "x_net_post_send_message");
      }
    }
    pthread_mutex_unlock(&client->messaging_mutex);

  } else {
    success = xt_core_bool_false;
    xt_core_log_trace(client->log, "xnet", "ensure_client_is_connected");
  }

  return success;
}
Example #4
0
void StreamManager::handle_async_write_stream_content(asio::ip::tcp::socket *client_socket, BYTE *buffer, const boost::system::error_code &error, std::size_t transferred)
{
	delete [] buffer;

	if (error)
	{
		handle_disconnect(client_socket);
		return;
	}

	::InterlockedExchangeAdd(&_writtenBytes, transferred);
}
Example #5
0
void Server::send_client_message(std::string name, std::string buff)
{
    if (buff == "")
        return;
    int len = buff.size() + 1; // add one for the terminating NULL
    int result = send_message(buff, clients[name]);
    if (result < len)
    {
        std::cout << "Failed to send message to client: "
                  << name
                  << std::endl;
        handle_disconnect(name);
    }
}
Example #6
0
bool StreamManager::write_stream_content(asio::ip::tcp::socket *client, BYTE *content, int lenght)
{
	try
	{
		std::size_t transferred = asio::write(*client, asio::buffer(content, lenght)); 
	
		::InterlockedExchangeAdd(&_writtenBytes, transferred);
		return transferred > 0;
	}
	catch(const boost::system::system_error &)
	{
		handle_disconnect(client);
		return false;
	}
}
Example #7
0
void handle_tcp_packets(){

    BYTE p_id;

    read_byte(client_recv_buffer, &p_id);

    switch (p_id){

        case TALK:
            handle_talk();
            break;

        case DISCONNECT:
            handle_disconnect();
            break;

    }
}
Example #8
0
void Server::send_message_to_all_clients(std::string buf)
{
    if(buf == "" || num_clients == 0)
        return;
    int len = buf.size() + 1; // add one for the terminating NULL
    typedef std::map< std::string, TCPsocket >::iterator it_type;
    for (it_type i = clients.begin(); i != clients.end(); i++)
    {
        int result = send_message(buf, i->second);
        // number of bytes sent is less than the length of bytes
        if (result < len)
        {
            std::cout << "Failed to send message to client: "
                      << i->first << " \n";
            printf( "SDLNet_TCP_Send: %s\n", SDLNet_GetError());
            handle_disconnect(i->first);
        }
    }
}
void handle_data(int fd)
{
    char buf[1024];
    ssize_t len;

    /* Attempt to read data from the connection */
    len = read(fd, buf, 1024);

    if (len < 0) {
        /* Error during read */
        perror("read");
        exit(EXIT_FAILURE);
    }
    if (len == 0) {
        /* Event on the socket, but no data.
         * Socket has (very likely) been closed remotely */
        handle_disconnect(fd);
        return;
    }

    printf("Read %zd bytes from socket %d: \"%s\"\n", len, fd, buf);
}
Example #10
0
	void response_base< Timer >::handle_request( const json::object& request,
	    const boost::shared_ptr< response_interface >& self, const std::string& connection_name, bool last_message )
	{
		const json::string channel = extract_channel( request );

		if ( channel == meta_handshake_channel )
		{
			handle_handshake( request, connection_name );
			return;
		}

		const json::string client_id = check_client_id( request, channel );
		if ( client_id.empty() )
		    return;

		if ( !check_session( request, client_id, channel ) )
		    return;

        if ( channel == meta_connect_channel )
        {
            handle_connect( request, self, last_message );
        }
        else if ( channel == meta_disconnect_channel )
        {
            handle_disconnect( request );
        }
        else if ( channel == meta_subscribe_channel )
        {
            handle_subscribe( request );
        }
        else if ( channel == meta_unsubscribe_channel )
        {
            handle_unsubscribe( request );
        }
        else
        {
            handle_publish( channel, request );
        }
	}
Example #11
0
	virtual void ProcessEvent(EFlowEvent event, SActivationInfo *pActInfo)
	{
		int found =-1;
		int connected;
		
		
		switch (event)
		{
		case eFE_Initialize:{
			//Sleep(3000);
			wiimotes = wiiuse_init(1); 
			found = wiiuse_find(wiimotes, 1, 5);
			if (found!=0) handle_ctrl_status(wiimotes[0]);

			connected = wiiuse_connect(wiimotes, 1);
			if (connected) CryLogAlways("Connected to %i wiimotes (of %i found).\n", connected, found);
			else CryLogAlways("Failed to connect to any wiimote.\n");
			wiiuse_set_leds(wiimotes[0], WIIMOTE_LED_1);
			
			wiiuse_motion_sensing(wiimotes[0], 1);
			wiiuse_set_ir(wiimotes[0], 1);

			//trop rapide !
			
		}
		case eFE_Activate:
			{	

				//Sleep(3000);

				//wiimotes = wiiuse_init(1); 
				//found = wiiuse_find(wiimotes, 1, 5);
				//if (found==0) CryLogAlways("No wiimotes found.\n");
				
				/*
				connected = wiiuse_connect(wiimotes, 1);
				if (connected) CryLogAlways("Connected to %i wiimotes (of %i found).\n", connected, found);
				else CryLogAlways("Failed to connect to any wiimote.\n");
				wiiuse_set_leds(wiimotes[0], WIIMOTE_LED_1);

				wiiuse_motion_sensing(wiimotes[0], 1);
				wiiuse_set_ir(wiimotes[0], 1);
				//WIIUSE_USING_EXP(wiimotes[0]);
				//WIIUSE_U

				//wiimotes[0]->exp.type = EXP_NUNCHUK;
				
				*/
				

				pActInfo->pGraph->SetRegularlyUpdated(pActInfo->myID,true);
			}
			break;
		
		case eFE_Update:
			{
				//int connected = wiiuse_connect(wiimotes, 1); 
				wiiuse_poll(wiimotes, 1);
				wiimote* wm =  wiimotes[0];
				//Ne change rien ! juste qu'on a un retour d'IR
				//handle_ctrl_status(wiimotes[0]);
				CryLogAlways("attachment:      %i",wm->exp.type);

				/*

				Gestion des evts

				*/

				
				
				// A commenter
				switch (wiimotes[0]->event) {
					case WIIUSE_EVENT:
						/* a generic event occured */
						handle_event(wiimotes[0]);
						break;

					case WIIUSE_STATUS:
						/* a status event occured */
						handle_ctrl_status(wiimotes[0]);
						break;

					case WIIUSE_DISCONNECT:
					case WIIUSE_UNEXPECTED_DISCONNECT:
						/* the wiimote disconnected */
						handle_disconnect(wiimotes[0]);
						break;

					case WIIUSE_READ_DATA:
						/*
						 *	Data we requested to read was returned.
						 *	Take a look at wiimotes[i]->read_req
						 *	for the data.
						 */
						break;

					case WIIUSE_NUNCHUK_INSERTED:
						/*
						 *	a nunchuk was inserted
						 *	This is a good place to set any nunchuk specific
						 *	threshold values.  By default they are the same
						 *	as the wiimote.
						 */
						//struct nunchuk_t* nc = (nunchuk_t*)&wiimotes[0]->exp.nunchuk;
						 wiiuse_set_nunchuk_orient_threshold(wiimotes[0], 90.0f);
						 wiiuse_set_nunchuk_accel_threshold(wiimotes[0], 100);
						CryLogAlways("Nunchuk inserted.\n");
						break;

					case WIIUSE_CLASSIC_CTRL_INSERTED:
						CryLogAlways("Classic controller inserted.\n");
						break;

					case WIIUSE_GUITAR_HERO_3_CTRL_INSERTED:
						/* some expansion was inserted */
						handle_ctrl_status(wiimotes[0]);
						//printf("Guitar Hero 3 controller inserted.\n");
						break;

					case WIIUSE_NUNCHUK_REMOVED:
					case WIIUSE_CLASSIC_CTRL_REMOVED:
					case WIIUSE_GUITAR_HERO_3_CTRL_REMOVED:
						/* some expansion was removed */
						handle_ctrl_status(wiimotes[0]);
						//printf("An expansion was removed.\n");
						break;

					default:
						break;
				}














				
					//if (wiiuse_poll(wiimotes, 1)) {
						//int i = 0;
				
						if (IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_A))			ActivateOutput(pActInfo, WIIMOTE_A,true); else ActivateOutput(pActInfo, WIIMOTE_A,false);
						if (IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_B))			ActivateOutput(pActInfo, WIIMOTE_B,true); else ActivateOutput(pActInfo, WIIMOTE_B,false);
						if (IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_UP))			ActivateOutput(pActInfo, WIIMOTE_UP,true); else ActivateOutput(pActInfo, WIIMOTE_UP,false);
						if (IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_DOWN))		ActivateOutput(pActInfo, WIIMOTE_DOWN,true); else ActivateOutput(pActInfo, WIIMOTE_DOWN,false);
						if (IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_LEFT))		ActivateOutput(pActInfo, WIIMOTE_LEFT,true); else ActivateOutput(pActInfo, WIIMOTE_LEFT,false);
						if (IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_RIGHT))		ActivateOutput(pActInfo, WIIMOTE_RIGHT,true); else ActivateOutput(pActInfo, WIIMOTE_RIGHT,false);
						if (IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_MINUS))		ActivateOutput(pActInfo, WIIMOTE_MINUS,true); else ActivateOutput(pActInfo, WIIMOTE_MINUS,false);
						if (IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_PLUS))		ActivateOutput(pActInfo, WIIMOTE_PLUS,true); else ActivateOutput(pActInfo, WIIMOTE_PLUS,false);
						if (IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_ONE))		ActivateOutput(pActInfo, WIIMOTE_ONE,true); else ActivateOutput(pActInfo, WIIMOTE_ONE,false);
						if (IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_TWO))		ActivateOutput(pActInfo, WIIMOTE_TWO,true); else ActivateOutput(pActInfo, WIIMOTE_TWO,false);
						if (IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_HOME))		ActivateOutput(pActInfo, WIIMOTE_HOME,true); else ActivateOutput(pActInfo, WIIMOTE_HOME,false);

						if (WIIUSE_USING_ACC(wiimotes[0])) {
							Vec3 output = Vec3(wiimotes[0]->orient.roll,wiimotes[0]->orient.pitch,wiimotes[0]->orient.yaw);
							ActivateOutput(pActInfo, WIIMOTE_RPY,output);
						}
						if (WIIUSE_USING_IR(wiimotes[0])) {
							Vec3 output = Vec3((float)wiimotes[0]->ir.x,(float)wiimotes[0]->ir.y,(float)wiimotes[0]->ir.z);
							ActivateOutput(pActInfo, WIIMOTE_IR,output);
						}
						/* nunchuk */
						//Toujours faux !
						
						if (wm->exp.type == EXP_NUNCHUK) {
							
							//nunchuk_t* pt =  wm->exp.nunchuk;
						//REssort 0 a chaque fois !!!!
							struct nunchuk_t* nc = (nunchuk_t*)&wm->exp.nunchuk;
							CryLogAlways("nun gravity x EXPANSION : %f",nc->gforce.x);
							if (IS_PRESSED(nc, NUNCHUK_BUTTON_C)) ActivateOutput(pActInfo, NUNCHUK_C,true); else ActivateOutput(pActInfo, NUNCHUK_C,false);
							if (IS_PRESSED(nc, NUNCHUK_BUTTON_Z)) ActivateOutput(pActInfo, NUNCHUK_Z,true); else ActivateOutput(pActInfo, NUNCHUK_Z,false);
							
							Vec3 output = Vec3(nc->orient.a_roll,nc->orient.a_pitch,nc->orient.yaw);
							ActivateOutput(pActInfo, NUNCHUK_RPY,output);
							
							Vec3 output2 = Vec3(nc->js.ang,nc->js.mag,0);
							ActivateOutput(pActInfo, NUNCHUK_JOYSTICK,output2);
						} 


						/*
						switch (wiimotes[0]->event) {
							//case WIIUSE_EVENT:
								//{
								//}
								//break;

							case WIIUSE_STATUS:
								handle_ctrl_status(wiimotes[0]);
								break;

							case WIIUSE_DISCONNECT:
							case WIIUSE_UNEXPECTED_DISCONNECT:
								handle_disconnect(wiimotes[0]);
								break;
								
							default:
								break;
						}
						*/
					//}
				//}
			}
				
		}
	}
Example #12
0
/**
 *	@brief main()
 *
 *	Connect to up to two wiimotes and print any events
 *	that occur on either device.
 */
int main(int argc, char** argv) {
	wiimote** wiimotes;
	int found, connected;

	/*
	 *	Initialize an array of wiimote objects.
	 *
	 *	The parameter is the number of wiimotes I want to create.
	 */
	wiimotes =  wiiuse_init(MAX_WIIMOTES);

	/*
	 *	Find wiimote devices
	 *
	 *	Now we need to find some wiimotes.
	 *	Give the function the wiimote array we created, and tell it there
	 *	are MAX_WIIMOTES wiimotes we are interested in.
	 *
	 *	Set the timeout to be 5 seconds.
	 *
	 *	This will return the number of actual wiimotes that are in discovery mode.
	 */
	found = wiiuse_find(wiimotes, MAX_WIIMOTES, 5);
	if (!found) {
		printf("No wiimotes found.\n");
		return 0;
	}

	/*
	 *	Connect to the wiimotes
	 *
	 *	Now that we found some wiimotes, connect to them.
	 *	Give the function the wiimote array and the number
	 *	of wiimote devices we found.
	 *
	 *	This will return the number of established connections to the found wiimotes.
	 */
	connected = wiiuse_connect(wiimotes, MAX_WIIMOTES);
	if (connected) {
		printf("Connected to %i wiimotes (of %i found).\n", connected, found);
	} else {
		printf("Failed to connect to any wiimote.\n");
		return 0;
	}

	/*
	 *	Now set the LEDs and rumble for a second so it's easy
	 *	to tell which wiimotes are connected (just like the wii does).
	 */
	wiiuse_set_leds(wiimotes[0], WIIMOTE_LED_1);
	wiiuse_set_leds(wiimotes[1], WIIMOTE_LED_2);
	wiiuse_set_leds(wiimotes[2], WIIMOTE_LED_3);
	wiiuse_set_leds(wiimotes[3], WIIMOTE_LED_4);
	wiiuse_rumble(wiimotes[0], 1);
	wiiuse_rumble(wiimotes[1], 1);

#ifndef WIIUSE_WIN32
	usleep(200000);
#else
	Sleep(200);
#endif

	wiiuse_rumble(wiimotes[0], 0);
	wiiuse_rumble(wiimotes[1], 0);

	printf("\nControls:\n");
	printf("\tB toggles rumble.\n");
	printf("\t+ to start Wiimote accelerometer reporting, - to stop\n");
	printf("\tUP to start IR camera (sensor bar mode), DOWN to stop.\n");
	printf("\t1 to start Motion+ reporting, 2 to stop.\n");
	printf("\n\n");

	/*
	 *	Maybe I'm interested in the battery power of the 0th
	 *	wiimote.  This should be WIIMOTE_ID_1 but to be sure
	 *	you can get the wiimote associated with WIIMOTE_ID_1
	 *	using the wiiuse_get_by_id() function.
	 *
	 *	A status request will return other things too, like
	 *	if any expansions are plugged into the wiimote or
	 *	what LEDs are lit.
	 */
	/* wiiuse_status(wiimotes[0]); */

	/*
	 *	This is the main loop
	 *
	 *	wiiuse_poll() needs to be called with the wiimote array
	 *	and the number of wiimote structures in that array
	 *	(it doesn't matter if some of those wiimotes are not used
	 *	or are not connected).
	 *
	 *	This function will set the event flag for each wiimote
	 *	when the wiimote has things to report.
	 */
	while (any_wiimote_connected(wiimotes, MAX_WIIMOTES)) {
		if (wiiuse_poll(wiimotes, MAX_WIIMOTES)) {
			/*
			 *	This happens if something happened on any wiimote.
			 *	So go through each one and check if anything happened.
			 */
			int i = 0;
			for (; i < MAX_WIIMOTES; ++i) {
				switch (wiimotes[i]->event) {
					case WIIUSE_EVENT:
						/* a generic event occurred */
						handle_event(wiimotes[i]);
						break;

					case WIIUSE_STATUS:
						/* a status event occurred */
						handle_ctrl_status(wiimotes[i]);
						break;

					case WIIUSE_DISCONNECT:
					case WIIUSE_UNEXPECTED_DISCONNECT:
						/* the wiimote disconnected */
						handle_disconnect(wiimotes[i]);
						break;

					case WIIUSE_READ_DATA:
						/*
						 *	Data we requested to read was returned.
						 *	Take a look at wiimotes[i]->read_req
						 *	for the data.
						 */
						break;

					case WIIUSE_NUNCHUK_INSERTED:
						/*
						 *	a nunchuk was inserted
						 *	This is a good place to set any nunchuk specific
						 *	threshold values.  By default they are the same
						 *	as the wiimote.
						 */
						/* wiiuse_set_nunchuk_orient_threshold((struct nunchuk_t*)&wiimotes[i]->exp.nunchuk, 90.0f); */
						/* wiiuse_set_nunchuk_accel_threshold((struct nunchuk_t*)&wiimotes[i]->exp.nunchuk, 100); */
						printf("Nunchuk inserted.\n");
						break;

					case WIIUSE_CLASSIC_CTRL_INSERTED:
						printf("Classic controller inserted.\n");
						break;

					case WIIUSE_WII_BOARD_CTRL_INSERTED:
						printf("Balance board controller inserted.\n");
						break;

					case WIIUSE_GUITAR_HERO_3_CTRL_INSERTED:
						/* some expansion was inserted */
						handle_ctrl_status(wiimotes[i]);
						printf("Guitar Hero 3 controller inserted.\n");
						break;

					case WIIUSE_MOTION_PLUS_ACTIVATED:
						printf("Motion+ was activated\n");
						break;

					case WIIUSE_NUNCHUK_REMOVED:
					case WIIUSE_CLASSIC_CTRL_REMOVED:
					case WIIUSE_GUITAR_HERO_3_CTRL_REMOVED:
					case WIIUSE_WII_BOARD_CTRL_REMOVED:
					case WIIUSE_MOTION_PLUS_REMOVED:
						/* some expansion was removed */
						handle_ctrl_status(wiimotes[i]);
						printf("An expansion was removed.\n");
						break;

					default:
						break;
				}
			}
		}
	}

	/*
	 *	Disconnect the wiimotes
	 */
	wiiuse_cleanup(wiimotes, MAX_WIIMOTES);

	return 0;
}
void FWiimoteInputDevice::SendControllerEvents()
{
    /*
    *   Maybe I'm interested in the battery power of the 0th
    *   wiimote.  This should be WIIMOTE_ID_1 but to be sure
    *   you can get the wiimote associated with WIIMOTE_ID_1
    *   using the wiiuse_get_by_id() function.
    *
    *   A status request will return other things too, like
    *   if any expansions are plugged into the wiimote or
    *   what LEDs are lit.
    */
    /* wiiuse_status(wiimotes[0]); */

    /*
    *   This is the main loop
    *
    *   wiiuse_poll() needs to be called with the wiimote array
    *   and the number of wiimote structures in that array
    *   (it doesn't matter if some of those wiimotes are not used
    *   or are not connected).
    *
    *   This function will set the event flag for each wiimote
    *   when the wiimote has things to report.
    */
    if (AnyWiimoteConnected())
    {
        if (wiiuse_poll(GWiimotes, MAX_WIIMOTES))
        {
            /*
            *   This happens if something happened on any wiimote.
            *   So go through each one and check if anything happened.
            */
            const double CurrentTime = FPlatformTime::Seconds();
            for (int i = 0; i < MAX_WIIMOTES; ++i)
            {
                FControllerState& ControllerState = ControllerStates[i];
                ControllerState.Wiimote = GWiimotes[i];
                if (ControllerState.Wiimote && WIIMOTE_IS_CONNECTED(ControllerState.Wiimote))
                {
                    // Check Analog state
                    if (ControllerState.Wiimote->exp.type == EXP_NUNCHUK ||
                        ControllerState.Wiimote->exp.type == EXP_MOTION_PLUS_NUNCHUK)
                    {
                        /* nunchuk */
                        struct nunchuk_t* nc = (nunchuk_t*)&ControllerState.Wiimote->exp.nunchuk;

                        if (ControllerState.LeftXAnalog != nc->js.x)
                        {
                            MessageHandler->OnControllerAnalog(FGamepadKeyNames::LeftAnalogX, ControllerState.ControllerId, nc->js.x);
                            ControllerState.LeftXAnalog = nc->js.x;
                        }

                        if (ControllerState.LeftYAnalog != nc->js.y)
                        {
                            MessageHandler->OnControllerAnalog(FGamepadKeyNames::LeftAnalogY, ControllerState.ControllerId, nc->js.y);
                            ControllerState.LeftYAnalog = nc->js.y;
                        }
                    }
                }

                FMemory::Memzero(CurrentStates);

                switch (ControllerState.Wiimote->event)
                {
                case WIIUSE_EVENT:
                    /* a generic event occurred */
                    handle_event(ControllerState.Wiimote, i);
                    break;

                case WIIUSE_STATUS:
                    /* a status event occurred */
                    handle_ctrl_status(ControllerState.Wiimote, i);
                    break;

                case WIIUSE_DISCONNECT:
                case WIIUSE_UNEXPECTED_DISCONNECT:
                    /* the wiimote disconnected */
                    handle_disconnect(ControllerState.Wiimote, i);
                    break;

                case WIIUSE_READ_DATA:
                    /*
                    *   Data we requested to read was returned.
                    *   Take a look at ControllerState.Wiimote>read_req
                    *   for the data.
                    */
                    break;

                case WIIUSE_NUNCHUK_INSERTED:
                    /*
                    *   a nunchuk was inserted
                    *   This is a good place to set any nunchuk specific
                    *   threshold values.  By default they are the same
                    *   as the wiimote.
                    */
                    /* wiiuse_set_nunchuk_orient_threshold((struct nunchuk_t*)&wiimotes[i]->exp.nunchuk, 90.0f); */
                    /* wiiuse_set_nunchuk_accel_threshold((struct nunchuk_t*)&wiimotes[i]->exp.nunchuk, 100); */
                    UE_LOG(LogWiimote, Log, TEXT("Nunchuk inserted."));
                    break;

                case WIIUSE_MOTION_PLUS_ACTIVATED:
                    UE_LOG(LogWiimote, Log, TEXT("Motion+ was activated"));
                    break;

                case WIIUSE_NUNCHUK_REMOVED:
                case WIIUSE_MOTION_PLUS_REMOVED:
                    /* some expansion was removed */
                    handle_ctrl_status(ControllerState.Wiimote, i);
                    UE_LOG(LogWiimote, Log, TEXT("An expansion was removed."));
                    break;

                default:
                    break;
                }

                // For each button check against the previous state and send the correct message if any
                for (int32 ButtonIndex = 0; ButtonIndex < MAX_NUM_WIIMOTE_BUTTONS; ++ButtonIndex)
                {
                    if (CurrentStates[ButtonIndex] != ControllerState.ButtonStates[ButtonIndex])
                    {
                        if (CurrentStates[ButtonIndex])
                        {
                            MessageHandler->OnControllerButtonPressed(Buttons[ButtonIndex], ControllerState.ControllerId, false);
                        }
                        else
                        {
                            MessageHandler->OnControllerButtonReleased(Buttons[ButtonIndex], ControllerState.ControllerId, false);
                        }

                        if (CurrentStates[ButtonIndex] != 0)
                        {
                            // this button was pressed - set the button's NextRepeatTime to the InitialButtonRepeatDelay
                            ControllerState.NextRepeatTime[ButtonIndex] = CurrentTime + InitialButtonRepeatDelay;
                        }
                    }
                    else if (CurrentStates[ButtonIndex] != 0 && ControllerState.NextRepeatTime[ButtonIndex] <= CurrentTime)
                    {
                        MessageHandler->OnControllerButtonPressed(Buttons[ButtonIndex], ControllerState.ControllerId, true);

                        // set the button's NextRepeatTime to the ButtonRepeatDelay
                        ControllerState.NextRepeatTime[ButtonIndex] = CurrentTime + ButtonRepeatDelay;
                    }

                    // Update the state for next time
                    ControllerState.ButtonStates[ButtonIndex] = CurrentStates[ButtonIndex];
                }

            }
        }
    }
}
int main(int argc, char** argv) 
{
  long timeout = 0;
  bool connected = 0;
  wiimote** wiimotes;
  wiimotes =  wiiuse_init(MAX_WIIMOTES);
  CWiiController controller;
  wiimote* wm;
  {
  //Main Loop
  while (1)
    {
    if (!connected) connected = (connectWiimote(wiimotes));
    wm = wiimotes[0]; // Only worry about 1 controller. No need for more?
    controller.m_buttonDownTime = getTicks() - timeout;

//Handle ACC, Repeat, and IR outside of the Event loop so that buttons can be continuously sent
    if (timeout)
    {
      if ((controller.m_buttonDownTime > g_hold_button_timeout) && controller.m_holdableHeld)
        controller.handleACC(wm->orient.roll, wm->orient.pitch);
      if ((controller.m_buttonDownTime  > g_repeat_rate) && controller.m_repeatableHeld)
        controller.handleRepeat();
    }
    if (wiiuse_poll(wiimotes, MAX_WIIMOTES)) 
    {
      for (int i = 0; i < MAX_WIIMOTES; ++i)
      //MAX_WIIMOTES hardcoded at 1.
      {
        switch (wiimotes[i]->event) 
        {
        case WIIUSE_EVENT:

	  controller.get_keys(wm);  //Load up the CWiiController
          controller.handleKeyPress();
          if (!controller.m_buttonHeld && (controller.m_holdableHeld || controller.m_repeatableHeld))
          {
            //Prepare to repeat or hold. Do this only once.
            timeout = getTicks();
            EnableMotionSensing(wm);
            controller.m_abs_roll = 0;
            controller.m_abs_pitch = 0;
            controller.m_start_roll = 0;
            controller.m_start_pitch = 0;
          }
          if (controller.m_buttonReleased)
          {
            DisableMotionSensing(wm);
            controller.m_currentAction = ACTION_NONE;
          }
        break;
        case WIIUSE_STATUS:
        break;
        case WIIUSE_DISCONNECT:
        case WIIUSE_UNEXPECTED_DISCONNECT:
        handle_disconnect(wm);
	connected = 0;
        break;
        case WIIUSE_READ_DATA:
        break;
        default:
        break;
	}
      }
    }
  }
  }
  wiiuse_cleanup(wiimotes, MAX_WIIMOTES);
  return 0;
}
int main(void)
{
    wiimote *wm;
    wiimote** wiimotes;
    int found, connected;
    HANDLE canHandle;
    TPCANMsg Message;
    int wiimote_led_state;
    int exit = 0;

    canHandle = initCAN();
    if(canHandle == NULL)
    {
        printf("Error opening CAN device!\n");
        return -1;
    }

    wiimotes =  wiiuse_init(MAX_WIIMOTES);

    found = wiiuse_find(wiimotes, MAX_WIIMOTES, 5);
    if (!found)
    {
        printf ("No wiimotes found.\n");
        return 0;
    }

    connected = wiiuse_connect(wiimotes, MAX_WIIMOTES);
    if (connected)
        printf("Connected to %i wiimotes (of %i found).\n", connected, found);
    else
    {
        printf("Failed to connect to any wiimote.\n");
        return 0;
    }

    wm = wiimotes[0];
    wiiuse_status(wm);
    while(wm->event != WIIUSE_STATUS)
    {
        wiiuse_poll(wiimotes, MAX_WIIMOTES);
    }
    printf("Battery level: %f%%\n", wm->battery_level*100);

    while (1)
    {
        if(exit)
            break;
        if (wiiuse_poll(wiimotes, MAX_WIIMOTES))
        {
            /*
             *	This happens if something happened on any wiimote.
             *	So go through each one and check if anything happened.
             */
            int i = 0;
            for (; i < MAX_WIIMOTES; ++i)
            {
                switch (wiimotes[i]->event)
                {
                    case WIIUSE_EVENT:
                        /* a generic event occured */
                        handle_event(wiimotes[i]);
                        Message.ID = CAN_INPUT_MSG_ID;
                        Message.MSGTYPE = MSGTYPE_STANDARD;
                        Message.LEN = 7;
                        Message.DATA[0] = carInputs.accel;
                        Message.DATA[1] = carInputs.brake;
                        Message.DATA[2] = carInputs.steer;
                        Message.DATA[3] = carInputs.gear;
                        Message.DATA[4] = carInputs.clutch;
                        Message.DATA[5] = carInputs.controls;
			Message.DATA[6] = carInputs.cruisedist;
                        CAN_Write(canHandle,&Message);

                        // Show the status of ABS/TC/Cruise on the LEDs
                        wiimote_led_state = 0;
                        if(carInputs.controls & ABS)
                            wiimote_led_state |= WIIMOTE_LED_1;
                        if(carInputs.controls & TC)
                            wiimote_led_state |= WIIMOTE_LED_2;
                        if(carInputs.controls & STABILITY)
                            wiimote_led_state |= WIIMOTE_LED_3;
                        if(carInputs.controls & CRUISE)
                            wiimote_led_state |= WIIMOTE_LED_4;
                        wiiuse_set_leds(wm, wiimote_led_state);

                        break;
                    case WIIUSE_DISCONNECT:
                    case WIIUSE_UNEXPECTED_DISCONNECT:
                        /* the wiimote disconnected */
                        handle_disconnect(wiimotes[i]);
                        exit = 1;
                        break;
                    default:
                        break;
                }
            }
        }
    }
    wiiuse_cleanup(wiimotes, MAX_WIIMOTES);
    return 0;

}
	virtual void ProcessEvent(EFlowEvent event, SActivationInfo *pActInfo)
	{
		
		
		
		switch (event)
		{
		case eFE_Initialize: 
			//Sleep(5000);
			break;
		case eFE_Activate:
			{	
				//Sleep(3000); // <-- Dernière chose ajoutée
				wiimotes = wiiuse_init(MAX_WIIMOTES); 
				found = wiiuse_find(wiimotes,  MAX_WIIMOTES, 5);
				//if (found!=0) 
					//handle_ctrl_status(wiimotes[0]);

				connected = wiiuse_connect(wiimotes, MAX_WIIMOTES);
				if (connected) {

					CryLogAlways("Connected to %i wiimotes (of %i found).\n", connected, found);
				}
				else CryLogAlways("Failed to connect to any wiimote.\n");
				wiiuse_set_leds(wiimotes[0], WIIMOTE_LED_2);
			
				wiiuse_motion_sensing(wiimotes[0], 1);
				//wiiuse_set_ir(wiimotes[0], 1);
				pActInfo->pGraph->SetRegularlyUpdated(pActInfo->myID,true);
				//break; // <-- Dernière chose commentée !
			}
		
		case eFE_Update:
			{
				//CryLogAlways("Update !");
				if(GetPortBool(pActInfo, 0)){
					wiimotes = wiiuse_init(MAX_WIIMOTES); 
					connected = wiiuse_connect(wiimotes, MAX_WIIMOTES);
				}
				
		
				if (wiiuse_poll(wiimotes, MAX_WIIMOTES)) {
					/*
					 *	This happens if something happened on any wiimote.
					 *	So go through each one and check if anything happened.
					 */
					int i = 0;
					for (; i < MAX_WIIMOTES; ++i) {
						CryLogAlways("attachment:  wiimote :%i  attachement : %i",i,wiimotes[i]->exp.type);

						switch (wiimotes[i]->event) {
							case WIIUSE_EVENT:
								/* a generic event occured */
								CryLogAlways("Generic event");
								handle_event(wiimotes[i]);
								break;
							case WIIUSE_CONNECT:
								CryLogAlways("Connexion");
								break;
							case WIIUSE_STATUS:
								/* a status event occured */
								CryLogAlways("Demande de status");
								handle_ctrl_status(wiimotes[i]);
								break;

							case WIIUSE_DISCONNECT:
							case WIIUSE_UNEXPECTED_DISCONNECT:
								/* the wiimote disconnected */
								CryLogAlways("Deconnexion");
								handle_disconnect(wiimotes[i]);
								break;

							case WIIUSE_READ_DATA:
								/*
								 *	Data we requested to read was returned.
								 *	Take a look at wiimotes[i]->read_req
								 *	for the data.
								 */
								break;

							case WIIUSE_NUNCHUK_INSERTED:
								/*
								 *	a nunchuk was inserted
								 *	This is a good place to set any nunchuk specific
								 *	threshold values.  By default they are the same
								 *	as the wiimote.
								 */
								 //wiiuse_set_nunchuk_orient_threshold((struct nunchuk_t*)&wiimotes[i]->exp.nunchuk, 90.0f);
								 //wiiuse_set_nunchuk_accel_threshold((struct nunchuk_t*)&wiimotes[i]->exp.nunchuk, 100);
								handle_ctrl_status(wiimotes[i]);
								printf("Nunchuk inserted.\n");
								break;

							case WIIUSE_CLASSIC_CTRL_INSERTED:
								handle_ctrl_status(wiimotes[i]);
								printf("Classic controller inserted.\n");
								break;

							case WIIUSE_GUITAR_HERO_3_CTRL_INSERTED:
								/* some expansion was inserted */
								handle_ctrl_status(wiimotes[i]);
								printf("Guitar Hero 3 controller inserted.\n");
								break;

							case WIIUSE_NUNCHUK_REMOVED:
							case WIIUSE_CLASSIC_CTRL_REMOVED:
							case WIIUSE_GUITAR_HERO_3_CTRL_REMOVED:
								/* some expansion was removed */
								handle_ctrl_status(wiimotes[i]);
								printf("An expansion was removed.\n");
								break;

							default:
								break;
						}
			
					}
				}
			}
			//catch(EXCEPINFO e){}
		}
		
		//}
	}
Example #17
0
/**
 *	@brief main()
 *
 *	Connect to up to two wiimotes and print any events
 *	that occur on either device.
 */
int main( int argc, char** argv ) {
  wiimote** wiimotes;
  int found, connected;
  
  /*
   *	Initialize an array of wiimote objects.
   *
   *	The parameter is the number of wiimotes I want to create.
   */
  wiimotes = wiic_init( MAX_WIIMOTES );
  
  /*
   *	Find wiimote devices
   *
   *	Now we need to find some wiimotes.
   *	Give the function the wiimote array we created, and tell it there
   *	are MAX_WIIMOTES wiimotes we are interested in.
   *
   *	Set the timeout to be 5 seconds.
   *
   *	This will return the number of actual wiimotes that are in discovery mode.
   */
  found = wiic_find( wiimotes, MAX_WIIMOTES, 5 );
  if ( !found ) {
    printf( "No wiimotes found." );
    return 0;
  }
  
  /*
   *	Connect to the wiimotes
   *
   *	Now that we found some wiimotes, connect to them.
   *	Give the function the wiimote array and the number
   *	of wiimote devices we found.
   *
   *	This will return the number of established connections to the found wiimotes.
   */
  connected = wiic_connect( wiimotes, found, 0 );
  if ( connected ) printf( "Connected to %i wiimotes (of %i found).\n",
                           connected, found );
  else {
    printf( "Failed to connect to any wiimote.\n" );
    return 0;
  }
  
  /*
   *	Now set the LEDs and rumble for a second so it's easy
   *	to tell which wiimotes are connected (just like the wii does).
   */
  wiic_set_leds( wiimotes[0], WIIMOTE_LED_1 );
  wiic_rumble( wiimotes[0], 1 );
  usleep( 200000 );
  wiic_rumble( wiimotes[0], 0 );
  
  /*
   *	Maybe I'm interested in the battery power of the 0th
   *	wiimote.  This should be WIIMOTE_ID_1 but to be sure
   *	you can get the wiimote assoicated with WIIMOTE_ID_1
   *	using the wiic_get_by_id() function.
   *
   *	A status request will return other things too, like
   *	if any expansions are plugged into the wiimote or
   *	what LEDs are lit.
   */
  wiic_status( wiimotes[0] );
  
  printf(
      "\nPress PLUS (MINUS) to enable (disable) Motion Sensing Report (only accelerometers)\n" );
  printf(
      "Press RIGHT (LEFT) to enable (disable) Motion Plus (requires Motion Sensing enabled)\n" );
  printf(
      "Press UP (DOWN) to enable (disable) IR camera (requires some IR led)\n" );
  
  /*
   *	This is the main loop
   *
   *	wiic_poll() needs to be called with the wiimote array
   *	and the number of wiimote structures in that array
   *	(it doesn't matter if some of those wiimotes are not used
   *	or are not connected).
   *
   *	This function will set the event flag for each wiimote
   *	when the wiimote has things to report.
   */
  while ( !exiting ) {
    if ( wiic_poll( wiimotes, connected ) ) {
      /*
       *	This happens if something happened on any wiimote.
       *	So go through each one and check if anything happened.
       */
      int i = 0;
      for ( ; i < MAX_WIIMOTES; ++i ) {
        switch ( wiimotes[i]->event ) {
        case WIIC_EVENT:
          // a generic event occured
          handle_event( wiimotes[i] );
          break;
          
        case WIIC_STATUS:
          // a status event occured
          handle_ctrl_status( wiimotes[i] );
          break;
          
        case WIIC_DISCONNECT:
        case WIIC_UNEXPECTED_DISCONNECT:
          // the wiimote disconnected
          handle_disconnect( wiimotes[i] );
          break;
          
        case WIIC_READ_DATA:
          /*
           *	Data we requested to read was returned.
           *	Take a look at wiimotes[i]->read_req
           *	for the data.
           */
          break;
          
        case WIIC_NUNCHUK_INSERTED:
          /*
           *	a nunchuk was inserted
           *	This is a good place to set any nunchuk specific
           *	threshold values.  By default they are the same
           *	as the wiimote.
           */
          printf( "Nunchuk inserted.\n" );
          break;
          
        case WIIC_CLASSIC_CTRL_INSERTED:
          printf( "Classic controller inserted.\n" );
          break;
          
        case WIIC_GUITAR_HERO_3_CTRL_INSERTED:
          // some expansion was inserted 
          handle_ctrl_status( wiimotes[i] );
          break;
          
        case WIIC_MOTION_PLUS_INSERTED:
          printf( "Motion Plus inserted.\n" );
          break;
          
        case WIIC_BALANCE_BOARD_INSERTED:
          printf( "Balance Board connected.\n" );
          break;
          
        case WIIC_BALANCE_BOARD_REMOVED:
          printf( "Balance Board disconnected.\n" );
          break;
          
        case WIIC_NUNCHUK_REMOVED:
        case WIIC_CLASSIC_CTRL_REMOVED:
        case WIIC_GUITAR_HERO_3_CTRL_REMOVED:
        case WIIC_MOTION_PLUS_REMOVED:
          // some expansion was removed 
          handle_ctrl_status( wiimotes[i] );
          printf( "An expansion was removed.\n" );
          break;
          
        default:
          break;
        }
      }
    }
  }
  
  /*
   *	Disconnect the wiimotes
   */
  wiic_cleanup( wiimotes, MAX_WIIMOTES );
  
  return 0;
}
//int main(int argc, char** argv) {
int _tmain(int argc, _TCHAR* argv[])
{
		wiimote** wiimotes;
	int found, connected;

	/*
	 *	Initialize an array of wiimote objects.
	 *
	 *	The parameter is the number of wiimotes I want to create.
	 */
	wiimotes =  wiiuse_init(MAX_WIIMOTES);

	/*
	 *	Find wiimote devices
	 *
	 *	Now we need to find some wiimotes.
	 *	Give the function the wiimote array we created, and tell it there
	 *	are MAX_WIIMOTES wiimotes we are interested in.
	 *
	 *	Set the timeout to be 5 seconds.
	 *
	 *	This will return the number of actual wiimotes that are in discovery mode.
	 */
	found = wiiuse_find(wiimotes, MAX_WIIMOTES, 5);
	if (!found) {
		printf ("No wiimotes found.");
		return 0;
	}

	/*
	 *	Connect to the wiimotes
	 *
	 *	Now that we found some wiimotes, connect to them.
	 *	Give the function the wiimote array and the number
	 *	of wiimote devices we found.
	 *
	 *	This will return the number of established connections to the found wiimotes.
	 */
	connected = wiiuse_connect(wiimotes, MAX_WIIMOTES);
	if (connected)
		printf("Connected to %i wiimotes (of %i found).\n", connected, found);
	else {
		printf("Failed to connect to any wiimote.\n");
		return 0;
	}

	/*
	 *	Now set the LEDs and rumble for a second so it's easy
	 *	to tell which wiimotes are connected (just like the wii does).
	 */
	wiiuse_set_leds(wiimotes[0], WIIMOTE_LED_1);
	wiiuse_set_leds(wiimotes[1], WIIMOTE_LED_2);
	wiiuse_set_leds(wiimotes[2], WIIMOTE_LED_3);
	wiiuse_set_leds(wiimotes[3], WIIMOTE_LED_4);
	wiiuse_rumble(wiimotes[0], 1);
	wiiuse_rumble(wiimotes[1], 1);

	#ifndef WIN32
		usleep(200000);
	#else
		Sleep(200);
	#endif

	wiiuse_rumble(wiimotes[0], 0);
	wiiuse_rumble(wiimotes[1], 0);

	/*
	 *	Maybe I'm interested in the battery power of the 0th
	 *	wiimote.  This should be WIIMOTE_ID_1 but to be sure
	 *	you can get the wiimote assoicated with WIIMOTE_ID_1
	 *	using the wiiuse_get_by_id() function.
	 *
	 *	A status request will return other things too, like
	 *	if any expansions are plugged into the wiimote or
	 *	what LEDs are lit.
	 */
	//wiiuse_status(wiimotes[0]);

	/*
	 *	This is the main loop
	 *
	 *	wiiuse_poll() needs to be called with the wiimote array
	 *	and the number of wiimote structures in that array
	 *	(it doesn't matter if some of those wiimotes are not used
	 *	or are not connected).
	 *
	 *	This function will invoke the callbacks set in wiiuse_init()
	 *	when the wiimote has things to report.
	 */
	while (1) {
		if (wiiuse_poll(wiimotes, MAX_WIIMOTES)) {
			/*
			 *	This happens if something happened on any wiimote.
			 *	So go through each one and check if anything happened.
			 */
			int i = 0;
			for (; i < MAX_WIIMOTES; ++i) {
				switch (wiimotes[i]->event) {
					case WIIUSE_EVENT:
						/* a generic event occured */
						handle_event(wiimotes[i]);
						break;

					case WIIUSE_STATUS:
						/* a status event occured */
						handle_ctrl_status(wiimotes[i]);
						break;

					case WIIUSE_DISCONNECT:
						/* the wiimote disconnected */
						handle_disconnect(wiimotes[i]);
						break;

					case WIIUSE_NUNCHUK_INSERTED:
						/*
						 *	a nunchuk was inserted
						 *	This is a good place to set and nunchuk specific
						 *	threshold values.  By default they are the same
						 *	as the wiimote.
						 */
						 //wiiuse_set_nunchuk_orient_threshold((struct nunchuk_t*)&wiimotes[i]->exp.nunchuk, 90.0f);
						 //wiiuse_set_nunchuk_accel_threshold((struct nunchuk_t*)&wiimotes[i]->exp.nunchuk, 100);

					case WIIUSE_CLASSIC_CTRL_INSERTED:
					case WIIUSE_GUITAR_HERO_3_CTRL_INSERTED:
						/* some expansion was inserted */
						handle_ctrl_status(wiimotes[i]);
						printf("An expansion was inserted.\n");
						break;

					case WIIUSE_NUNCHUK_REMOVED:
					case WIIUSE_CLASSIC_CTRL_REMOVED:
					case WIIUSE_GUITAR_HERO_3_CTRL_REMOVED:
						/* some expansion was removed */
						handle_ctrl_status(wiimotes[i]);
						printf("An expansion was removed.\n");
						break;

					default:
						break;
				}
			}
		}
	}

	/*
	 *	Disconnect the wiimotes
	 */
	wiiuse_cleanup(wiimotes, MAX_WIIMOTES);

	return 0;
	return 0;
}
Example #19
0
static void *
manager_input_loop( DirectThread *thread, void *arg )
{
     int            len;
     struct pollfd  pf;
     VoodooManager *manager = arg;

     manager->millis = direct_clock_get_millis();

     while (!manager->quit) {
          D_MAGIC_ASSERT( manager, VoodooManager );

          pf.fd     = manager->fd;
          pf.events = POLLIN;

          switch (poll( &pf, 1, 100 )) {
               case -1:
                    if (errno != EINTR) {
                         D_PERROR( "Voodoo/Input: Could not poll() the socket!\n" );
                         usleep( 200000 );
                    }
                    /* fall through */

               case 0:
                    continue;
          }

          pthread_mutex_lock( &manager->input.lock );

          while (manager->input.end == manager->input.max) {
               pthread_cond_wait( &manager->input.wait, &manager->input.lock );

               if (manager->quit)
                    break;
          }

          if (!manager->quit) {
               len = recv( manager->fd, manager->input.buffer + manager->input.end,
                           manager->input.max - manager->input.end, MSG_DONTWAIT );
               if (len < 0) {
                    switch (errno) {
                         case EINTR:
                         case EAGAIN:
                              break;
                         default:
                              D_PERROR( "Voodoo/Input: Could not recv() data!\n" );
                              usleep( 200000 );
                    }
               }
               else if (len > 0) {
                    D_DEBUG( "Voodoo/Input: Received %d bytes...\n", len );

                    manager->input.end += len;

                    pthread_cond_broadcast( &manager->input.wait );
               }
               else
                    handle_disconnect( manager );
          }

          pthread_mutex_unlock( &manager->input.lock );
     }

     return NULL;
}