#include #include #include #include #include #include #include #include #include static XtAppContext Context; static XtIntervalId TimeOut; static time_t SystemTime; static struct tm *LocalTime; static Widget Shell, Front, Door, Panel, Logo, Timer, Watts, MoreM, MoreS, LessM, LessS, Power, Start, Stop; static char *WattageText[] = {"90 W", "180 W", "360 W", "600 W"}, TimerText[] = "00:00"; static int WattageSetting = 3, Heat = 0, DisplayingDaytime = 1, TimeToCook = 0, MealDueTime, OpenDoor, Increment; static Arg WattsLabel[] = { { "label", (XtArgVal) NULL}, }, TimerLabel[] = { { "label", (XtArgVal) &TimerText }, }, DoorStatus[] = { { "state", (XtArgVal) &OpenDoor }, }; static void OtherWattage (void) { WattageSetting = (WattageSetting + 1) % 4; WattsLabel->value = (XtArgVal) WattageText [WattageSetting]; XtSetValues (Watts, WattsLabel, 1); } static void ShowTimeOfDay (void) { time (& SystemTime); LocalTime = localtime (& SystemTime); TimeOut = XtAppAddTimeOut (Context, (60 - LocalTime->tm_sec) * 1000, (XtTimerCallbackProc) ShowTimeOfDay, NULL); sprintf (TimerText, "%02d:%02d", LocalTime->tm_hour, LocalTime->tm_min); XtSetValues (Timer, TimerLabel, 1); DisplayingDaytime = 1; } static void ShowTimeToCook (void) { if (TimeToCook < 60) sprintf (TimerText, "%d", TimeToCook); else sprintf (TimerText, "%d\'%02d", TimeToCook/60, TimeToCook%60); XtSetValues (Timer, TimerLabel, 1); DisplayingDaytime = 0; } extern void Cooked1Second (void); /* forward declaration */ static void HeatOn (void) { ShowTimeToCook (); Heat = 1; TimeOut = XtAppAddTimeOut (Context, 1000, (XtTimerCallbackProc) Cooked1Second, NULL); } static void HeatOff (void) { Heat = 0; XtRemoveTimeOut (TimeOut); if (!TimeToCook) ShowTimeOfDay(); } void Cooked1Second (void) { time (& SystemTime); TimeToCook = MealDueTime - SystemTime; if (TimeToCook < 0) { Heat = TimeToCook = 0; XBell (XtDisplay (Shell), -50); ShowTimeOfDay (); } else HeatOn (); } static void TimerButton (Widget Button) { if (DisplayingDaytime) XtRemoveTimeOut (TimeOut); if (Button == MoreS) Increment = 1; else if (Button == LessS) Increment = -1; else if (Button == MoreM) Increment = 60; else Increment = -60; TimeToCook += Increment; if (TimeToCook < 0 || TimeToCook > 5999) TimeToCook -= Increment; else if (! TimeToCook) { if (Heat) HeatOff (); ShowTimeOfDay (); } else { MealDueTime += Increment; ShowTimeToCook (); } } static void StartButton (void) { if (Heat || ! TimeToCook) return; XtGetValues (Door, DoorStatus, 1); if (OpenDoor) return; time (& SystemTime); MealDueTime = SystemTime + --TimeToCook; HeatOn (); } static void StopButton (void) { if (Heat) HeatOff (); else if (! DisplayingDaytime) { TimeToCook = 0; ShowTimeOfDay (); } } static void DoorSafety (void) { if (Heat) HeatOff (); } void main (int argc, char **argv) { Shell = XtAppInitialize (&Context, "XOven", 0, 0, &argc, argv, 0, 0, 0); Front = XtCreateManagedWidget ("Front", boxWidgetClass, Shell, 0, 0); Door = XtCreateManagedWidget ("Door", toggleWidgetClass, Front, 0, 0); Panel = XtCreateManagedWidget ("Panel", boxWidgetClass, Front, 0, 0); Logo = XtCreateManagedWidget ("Logo", labelWidgetClass, Panel, 0, 0); Power = XtCreateManagedWidget ("Power", repeaterWidgetClass, Panel, 0, 0); Watts = XtCreateManagedWidget ("Watts", labelWidgetClass, Panel, 0, 0); Timer = XtCreateManagedWidget ("Timer", labelWidgetClass, Panel, 0, 0); MoreM = XtCreateManagedWidget ("MoreM", repeaterWidgetClass, Panel, 0, 0); MoreS = XtCreateManagedWidget ("MoreS", repeaterWidgetClass, Panel, 0, 0); LessM = XtCreateManagedWidget ("LessM", repeaterWidgetClass, Panel, 0, 0); LessS = XtCreateManagedWidget ("LessS", repeaterWidgetClass, Panel, 0, 0); Start = XtCreateManagedWidget ("Start", commandWidgetClass, Panel, 0, 0); Stop = XtCreateManagedWidget ("Stop", commandWidgetClass, Panel, 0, 0); XtAddCallback (Door, XtNcallback, (XtCallbackProc) DoorSafety, NULL); XtAddCallback (Power, XtNcallback, (XtCallbackProc) OtherWattage, NULL); XtAddCallback (MoreM, XtNcallback, (XtCallbackProc) TimerButton, NULL); XtAddCallback (MoreS, XtNcallback, (XtCallbackProc) TimerButton, NULL); XtAddCallback (LessM, XtNcallback, (XtCallbackProc) TimerButton, NULL); XtAddCallback (LessS, XtNcallback, (XtCallbackProc) TimerButton, NULL); XtAddCallback (Start, XtNcallback, (XtCallbackProc) StartButton, NULL); XtAddCallback (Stop, XtNcallback, (XtCallbackProc) StopButton, NULL); XtRealizeWidget (Shell); ShowTimeOfDay (); XtAppMainLoop (Context); }