view dspad.c @ 9:5e15d64a2d24 default tip

If somebody closed stdin, fd could be both valid and 0, don’t check for that.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 18 Aug 2015 02:09:55 +0100
parents f362b20de51e
children
line wrap: on
line source

/*
    DSPad
    Convert Nintendo DS keypress into joystick events
*/

#define __MAIN_FILE__

#include "ds.h"
#include "uinput.h"
#include "service.h"
#include "restartOp.h"
#include "XML.h"

#include <stdio.h>
#include <stdint.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>


#define DEFAULT_PATH "~/DSPad_server.xml"


/*
    Process xml atoms
*/
int dspad_process_xml(void* elem, char* tag, char* content){

    (void)elem;

    if(tag != NULL && content != NULL){
        if(strcmp(content, "")){

            /* process atom: */
            if(!strcmp(tag, "base_port")){
                base_port = atoi(content);
                return 1;
            }

            else if(!strcmp(tag, "uinput_dev")){
                if(uinput_dev != NULL) free(uinput_dev);
                uinput_dev = (char*)malloc(strlen(content)+1);
                strcpy(uinput_dev, content);
                return 1;
            }

            else if(!strcmp(tag, "nb_pad")){
                nbdev = atoi(content);
                return 1;
            }

            return ReadXML(elem, dspad_process_xml, content);
        }
    }

    /* Should not happend... (compiler warning) */
    return 0;
}



/*
    Read config file
    returns 1 OK
            0 error
*/
int config_read(char* path){
    int ok = 0;
    char* text = NULL;

    text = LoadFile(path);
    if(text){
        ok = ReadXML(NULL, dspad_process_xml, text);
        free(text);
        if(ok){
            fprintf(stderr, "Reading \"%s\" OK\n", path);
        }
        else{
            fprintf(stderr, "Reading \"%s\" FAILED\n", path);
        }
    }
    return ok;
}




int main(int argc, char** argv){
    /* -- gcc -Werror satisfaction... -- */
    (void)argc;
    (void)argv;
    /* --------------------------------- */

    curdev = 0;

    fprintf(stdout, "\n--- Starting DSPad Server v0.2 ---\n");

    pid_t pid = 0;

    if(argc == 3 && !strcmp("-f", argv[1])){
        fprintf(stderr, "Loading \"%s\"\n", argv[2]);
        config_read(argv[2]);
    }
    else{
        fprintf(stderr, "Loading \"%s\"\n", DEFAULT_PATH);
        config_read(DEFAULT_PATH);
    }



    /* fork servers */
    while(curdev < nbdev){
        pid = fork_rs();
        switch(pid){
            case 0:
                curdev++;
                // Wait 2 seconds before next because
                // of kernel dev creation latency
                sleep_rs(2);
                break;
            case -1:
                fprintf(stderr, " [%d] Creation of service: failed\n", curdev);
                exit(EXIT_FAILURE);
                break;
            default:
                start_service();
                sleep(5);
                fprintf(stdout, " [%d] Service finished\n", curdev);
                waitpid(-1, NULL, 0);
                exit(EXIT_SUCCESS);
                break;
        } /* switch */
    }

    return 0;
}