int
photon_input_pulse(input_region_data_t *regdat)
{

        static PhEvent_t		*event;

        if (event == NULL) {
                if ((event = malloc(EVENT_SIZE)) == NULL) {
                        perror("malloc");
                        return -1;
                }
        }

        switch( PhEventPeek(event, EVENT_SIZE)) { 

        case Ph_EVENT_MSG: 

                if (event->type == Ph_EV_SYSTEM) {
                        system_event(event, regdat);
                }

                else if (event->type == Ph_EV_SERVICE) {
                        service_event(event, regdat);
                }

                break; 

        case _Ph_SERV_INTERACT:
        case _Ph_SERV_KEYBOARD:
        case _Ph_SERV_POINTER:

                service_message((struct _Ph_msg_forward *) event, regdat);
                break;

        case -1: 
                perror( "PhEventPeek failed" ); 
                break; 
        }
	 
        return (0);
}
Example #2
0
int
service_start(const char *name, int sock, service_limit_func_t *limitfunc,
    service_command_func_t *commandfunc, int argc, char *argv[])
{
	struct service *service;
	struct service_connection *sconn, *sconntmp;
	fd_set fds;
	int maxfd, nfds, serrno;

	assert(argc == 2);

	pjdlog_init(PJDLOG_MODE_STD);
	pjdlog_debug_set(atoi(argv[1]));

	service = service_alloc(name, limitfunc, commandfunc);
	if (service == NULL)
		return (errno);
	if (service_connection_add(service, sock, NULL) == NULL) {
		serrno = errno;
		service_free(service);
		return (serrno);
	}

	for (;;) {
		FD_ZERO(&fds);
		maxfd = -1;
		for (sconn = service_connection_first(service); sconn != NULL;
		    sconn = service_connection_next(sconn)) {
			maxfd = fd_add(&fds, maxfd,
			    service_connection_get_sock(sconn));
		}

		PJDLOG_ASSERT(maxfd >= 0);
		PJDLOG_ASSERT(maxfd + 1 <= (int)FD_SETSIZE);
		nfds = select(maxfd + 1, &fds, NULL, NULL, NULL);
		if (nfds < 0) {
			if (errno != EINTR)
				pjdlog_errno(LOG_ERR, "select() failed");
			continue;
		} else if (nfds == 0) {
			/* Timeout. */
			PJDLOG_ABORT("select() timeout");
			continue;
		}

		for (sconn = service_connection_first(service); sconn != NULL;
		    sconn = sconntmp) {
			/*
			 * Prepare for connection to be removed from the list
			 * on failure.
			 */
			sconntmp = service_connection_next(sconn);
			if (FD_ISSET(service_connection_get_sock(sconn), &fds))
				service_message(service, sconn);
		}
		if (service_connection_first(service) == NULL) {
			/*
			 * No connections left, exiting.
			 */
			break;
		}
	}

	return (0);
}