// This may look like C code, but it is really -*- C++ -*-

/***********************************************************
Copyright 1991 by the Massachusetts Institute of Technology

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation, and that the name of M.I.T. not be used in advertising or
publicity pertaining to distribution of the software without specific,
written prior permission.  M.I.T. makes no representations about the
suitability of this software for any purpose.  It is provided "as is"
without express or implied warranty.

Original idea : Charles Haynes - DEC
Author: Roman J. Budzianowski - Project Athena MIT

******************************************************************/

#ifndef _AppContext_h_
#define _AppContext_h_

#include <X11/fd.h>
#include "xt++.h"

class ApplicationContext;

typedef ApplicationContext *XtAppContext;

extern "C" {
   
   XtAppContext XtCreateApplicationContextP(
					    XtAppContext
					    );
   
}

class ApplicationContext {
   typedef struct _ProcessContextRec *ProcessContext;
   typedef struct _CallbackRec *CallbackList;
   typedef struct _TimerEventRec *TimerEvent;
   typedef struct _WorkProcRec WorkProcRec;
   typedef struct _InputEvent InputEvent;
   typedef struct _ConverterRec **ConverterTable;
   
   typedef struct 
     {
  	Fd_set rmask;
	Fd_set wmask;
	Fd_set emask;
	int	nfds;
	int	count;
     } FdStruct;
   
   typedef struct {
      char*	start;
      char*	current;
      int		bytes_remaining;
   } Heap;
   typedef struct _DestroyRec DestroyRec;
   
   typedef XtActionsRec ActionArray[];
   
   static Boolean      inited;
   
   XtAppContext next;		/* link to next app in process context */
   ProcessContext process;	/* back pointer to our process context */
   CallbackList destroy_callbacks;
   Display **list;
   TimerEvent timerQueue;
   WorkProcRec *workQueue;
   InputEvent **input_list;
   InputEvent *outstandingQueue;
   XrmDatabase errorDB;
   XtErrorMsgHandler errorMsgHandler, warningMsgHandler;
   XtErrorHandler errorHandler, warningHandler;
   struct _ActionListRec *action_table;
   ConverterTable converterTable;
   unsigned long selectionTimeout;
   FdStruct fds;
   short count;			/* num of assigned entries in list */
   short max;				/* allocate size of list */
   short last;
   Boolean sync, being_destroyed, error_inited;
#ifndef NO_IDENTIFY_WINDOWS
   Boolean identify_windows;		/* debugging hack */
#endif
   Heap heap;
   String * fallback_resources;	/* Set by XtAppSetFallbackResources. */
   struct _ActionHookRec* action_hook_list;
   int destroy_list_size;		/* state data for 2-phase destroy */
   int destroy_count;
   int dispatch_level;
   DestroyRec* destroy_list;
   Widget in_phase2_destroy;
//
   Boolean run_loop;
 public:
   ApplicationContext() 
     { 
	if(!inited){
	   XtToolkitInitialize();
	   inited = True;
	}
	XtCreateApplicationContextP(this);
	run_loop = True;
     }
   
   void ExitLoop() { run_loop = False; }
   void ProcessEvent(XtInputMask mask) { XtAppProcessEvent(this, mask); }

   void MainLoop() 
     { 
	XEvent event;
	while(run_loop){
	   XtAppNextEvent(this, &event);
	   XtDispatchEvent(&event);
	}
	run_loop = True;
     }

   XtIntervalId AddTimeOut(unsigned long ms, XtTimerCallbackProc p, XtPointer cd) {
      return XtAppAddTimeOut(this, ms, p, cd); }
   void RemoveTimeOut(XtIntervalId id) { XtRemoveTimeOut(id); }
   XtInputId AddInput(int src, Opaque cnd, XtInputCallbackProc p, XtPointer cd) {
      return XtAppAddInput(this, src, cnd, p, cd); }
   void NextEvent(XEvent *event) { XtAppNextEvent(this, event); }
   Boolean PeekEvent(XEvent *event) { return XtAppPeekEvent(this, event); }
   XtInputMask Pending() { return XtAppPending(this); }
   void AddActions(ActionArray& al, Cardinal num)
     { XtAppAddActions(this, &al[0], num); }
   void DisplayInitialize(
			  Display *d,
			  String appName,
			  String appClass,
			  XrmOptionDescRec *opts = NULL,
			  Cardinal numOpts = 0,
			  Cardinal *argc = NULL,
			  char **argv = NULL) {
      XtDisplayInitialize(this, d, appName, appClass, opts, numOpts, argc, argv);
   }
   Display *OpenDisplay(
			String dispName,
			String appName,
			String appClass,
			XrmOptionDescRec *opts = NULL,
			Cardinal numOpts = 0,
			Cardinal *argc = NULL,
			char **argv = NULL) {
      return XtOpenDisplay(
			   this, dispName, appName, appClass, opts, numOpts, argc, argv);
   }
   ~ApplicationContext() { XtDestroyApplicationContext(this); }
   XtWorkProcId AddWorkProc(XtWorkProc wp, Opaque cd) {
      return XtAppAddWorkProc(this, wp, cd); 
   }
};

#endif
/* don't add anything after the endif! */
