/*  
 * xradioinit.c
 *
 * freely adapted from gideon legrange's radio.c for xradiotrack
 *
 * for explanation of the X-coding, see comments in xradiotrack.c
 *
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "xradioinit.h"
#include <X11/Intrinsic.h>      /* Intrinsics Definitions */
#include <X11/StringDefs.h>     /* Standard Name-String definitions */
#include <X11/Shell.h>          /* Shell definitions */
#include <X11/Xaw/Command.h>    /* Athena command button widgets */
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Label.h>




/* ******* Quit button callback function *********** */

void Doquit(w, client_data, call_data)
Widget w;
XtPointer client_data, call_data;
{ exit(0); }

/* ******* The Actual Error Handler **************** */

void have_error(int errcode)  { 

int n;  
char *errlines[7]; 

Dimension msgwidth;  /* typedef in Intrinsic.h! */ 
Widget form, errtext, okquit;  
Arg errtextargs[3];  /* error text */ 
Arg okargs[6];  /* OK-button */

extern XtAppContext app_context;
extern Widget toplevel;

errlines[0] = " unrecognised error ";
errlines[1] = " error opening /etc/radio.conf ";
errlines[2] = " port not set or illegal value in /etc/radio.conf ";
errlines[3] = " need IO permissions, must run as root or setuid root ";
errlines[4] = " Invalid frequency in /etc/radio.conf";
errlines[5] = " Invalid default station in /etc/radio.conf";
errlines[6] = " syntax error in /etc/radio.conf";

/* application intialize in mainline code ! */

form    = MMW ( "form", formWidgetClass, toplevel, NULL, NULL);

errtext = MMW ( "badtext", labelWidgetClass, form, NULL, NULL);

    /* insert text as required */

    n=0;
    XtSetArg(errtextargs[n], XtNlabel, errlines[errcode]);n++;
    XtSetValues(errtext,errtextargs,n);

    /* find width of the thing now */

    XtSetArg(errtextargs[0], XtNwidth, &msgwidth);
    XtGetValues(errtext,errtextargs,1);

    /* make reasonable value to put OK button in middle */

    msgwidth = (msgwidth/2)-20;


okquit  = MMW ( "  OK  ", commandWidgetClass, form, NULL, NULL);

    n=0;
    XtSetArg(okargs[n], XtNhorizDistance, msgwidth);n++; 
    XtSetArg(okargs[n], XtNvertDistance, 4);n++;
    XtSetArg(okargs[n], XtNheight, 15);n++;
    XtSetArg(okargs[n], XtNfromVert, errtext);n++;
    XtSetArg(okargs[n], XtNshapeStyle, XmuShapeOval);n++;
    XtSetValues(okquit,okargs,n);

    XtAddCallback(okquit, XtNcallback, Doquit, NULL);

XtRealizeWidget(toplevel);     /* put on screen */

XtAppMainLoop(app_context);    /* go wait for click on OK button */
}

/* *************************INIT Routines*************** */

int do_init(void)
{
int m;

/* to be certain, init frequencies to zero and names to empty */

for (m=0;m<NumStations;m++)
   {
   stations[m].freq=0;
   stations[m].name[0]='\0';
   }

yyin=fopen("/etc/radio.conf","r");
if (yyin==NULL) { have_error(1); }

yylex();           

fclose(yyin);

/* ok, now station info in station array and default array */

if (!(RadioBase==0x30c || RadioBase==0x20c)) { have_error(2);}

/* first ask for the radio track ports */

m=get_io_privs(RadioBase);
if (m!=0) { have_error(3);}

/* now give up root */

give_up_root();
return(0);
}
