brewpanel/brewpanel.c

Wed, 12 May 2021 21:17:59 +0200

author
Michiel Broek
date
Wed, 12 May 2021 21:17:59 +0200
changeset 611
732d482f47c8
parent 574
362c4700937e
child 637
21e542c15832
permissions
-rw-r--r--

Improved logging if wiringpi failed.

/*****************************************************************************
 * Copyright (C) 2015-2019
 *   
 * Michiel Broek <mbroek at mbse dot eu>
 *
 * This file is part of the mbsePi-apps
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * mbsePi-apps is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ThermFerm; see the file COPYING.  If not, write to the Free
 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *****************************************************************************/

#include "brewpanel.h"
#include "sdlgui.h"
#include "dlgBrew.h"

#ifdef HAVE_SDL_SDL_H

int			debug = FALSE;			/* Console debugging		*/
static pid_t		pgrp, mypid;
int			my_shutdown = FALSE;		/* Shutdown requested		*/
char			*Paneltype = NULL;		/* Panel to emulate		*/
int			PAN_x;				/* Screen X size		*/
int			PAN_y;				/* Screen Y size		*/

int			LCD_fcol;			/* LCD foreground color		*/
int			LCD_bcol;			/* LCD background color		*/

SDL_Surface		*PAN_surface = NULL;		/* Main surface			*/


int server(void);

void help(void)
{
    fprintf(stdout, "Usage: brewpanel [-d] [-h] [-p <paneltype>]\n");
    fprintf(stdout, "  -d --debug                    Debug on\n");
    fprintf(stdout, "  -h --help                     Display this help\n");
    fprintf(stdout, "  -p --paneltype <ferm|brew>    Select panel type\n");
}



void die(int onsig)
{
    switch (onsig) {
	case SIGHUP:    syslog(LOG_NOTICE, "Got SIGHUP, shutting down");
			break;
	case SIGINT:    syslog(LOG_NOTICE, "Keyboard interrupt, shutting down");
			break;
	case SIGTERM:   syslog(LOG_NOTICE, "Got SIGTERM, shutting down");
			break;
	case SIGSEGV:   syslog(LOG_NOTICE, "Got SIGSEGV, shutting down");
			my_shutdown = TRUE;
			exit(SIGSEGV);
			break;
	default:        syslog(LOG_NOTICE, "die() on signal %d", onsig);
    }

    my_shutdown = TRUE;
}



int main(int argc, char *argv[])
{
    int		i, c, rc = 0;
    pid_t	frk;

    Paneltype = calloc(128, sizeof(char));

    while (1) {
	int option_index = 0;
	static struct option long_options[] = {
	    {"debug", 0, 0, 'c'},
	    {"help", 0, 0, 'h'},
	    {"paneltype", 1, 0, 'p'},
	    {0, 0, 0, 0}
	};

	c = getopt_long(argc, argv, "dhp:", long_options, &option_index);
	if (c == -1)
	    break;

	switch (c) {
	    case 'd':   debug = TRUE;
			break;
	    case 'h':   help();
			return 1;
	    case 'p':   snprintf(Paneltype, 127, "%s", optarg);
			break;
	}
    }

    openlog("brewpanel", LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_USER);
    syslog(LOG_NOTICE, "mbsePi-apps brewpanel v%s starting", VERSION);

    /*
     *  Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored
     *  but that's live. This daemon should only be stopped by SIGTERM.
     *  Don't catch SIGCHLD.
     */
    for (i = 0; i < NSIG; i++) {
	if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP))
	    signal(i, (void (*))die);
    }

    if (debug) {
	/*
	 * Run in foreground when debugging
	 */
    	rc = server();
    } else {
	/*
	 * Fork the daemon
	 */
	if ((pgrp = setpgid(0, 0)) == -1) {
	    syslog(LOG_NOTICE, "setpgpid failed");
	}

	frk = fork();
	switch (frk) {
	    case -1:
		    	syslog(LOG_NOTICE, "Daemon fork failed: %s", strerror(errno));
			exit(1);
	    case 0:	/*
			 * Run the daemon
			 */
			fclose(stdin);
			if (open("/dev/null", O_RDONLY) != 0) {
			    syslog(LOG_NOTICE, "Reopen of stdin to /dev/null failed");
			    _exit(2);
			}
			fclose(stdout);
			if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 1) {
			    syslog(LOG_NOTICE, "Reopen of stdout to /dev/null failed");
			    _exit(2);
			}
			fclose(stderr);
			if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 2) {
			    syslog(LOG_NOTICE, "Reopen of stderr to /dev/null failed");
			    _exit(2);
			}
			mypid = getpid();
			rc = server();
			break;
			/* Not reached */
	    default:
			/*
			 * Here we detach this process and let the child
			 * run the deamon process.
			 */
			syslog(LOG_NOTICE, "Starting daemon with pid %d", frk);
			exit(0);
	}
    }

    syslog(LOG_NOTICE, "Finished, rc=%d", rc);
    return rc;
}


int server(void)
{
    SDL_Rect    **modes;
    char        title[81];

    /* 
     * Initialize defaults, Video and Audio
     */
    if ((SDL_Init(SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_VIDEO) == -1)) { 
	syslog(LOG_NOTICE, "[main] could not initialize SDL: %s", SDL_GetError());
	exit(-1);
    }

    modes = SDL_ListModes(NULL, SDL_FULLSCREEN);
    if (modes != (SDL_Rect **)0) {
	/* Check if our resolution is restricted */
	if (modes == (SDL_Rect **)-1) {
	    syslog(LOG_NOTICE, "SDL all resolutions available.\n");
	}
    }

    PAN_x = 384;
    PAN_y = 480;

    PAN_surface = SDL_SetVideoMode( PAN_x, PAN_y, 32, SDL_SWSURFACE|SDL_DOUBLEBUF );
    if (PAN_surface == NULL) {
	syslog(LOG_NOTICE, "Could not create main surface: %s", SDL_GetError());
	exit(-1);
    }

    SDLGui_Init();
    snprintf(title, 80, "brewpanel v%s", VERSION);
    SDL_WM_SetCaption(title, NULL);

    syslog(LOG_NOTICE, "Starting mainloop");
    if (strlen(Paneltype) && !strcmp(Paneltype, (char *)"ferm") ) {
	Dialog_BrewDlg(1);
    } else {
    	Dialog_BrewDlg(0);
    }
    syslog(LOG_NOTICE, "Out of mainloop, cleanup");

    SDLGui_UnInit();
    SDL_FreeSurface(PAN_surface);
    SDL_Quit();

    return 0;
}

#else

int main(int argc, char *argv[])
{
    fprintf(stderr, "brewpanel not compiled because SDL is missing.\n");
    return 1;
}

#endif

mercurial