The LHAPI Event Loop

After a LHAPI application is done initializing itself, it usually falls into the event loop. The event loop just gets events, and doles them out. The entire life of an application is normally spent inside an event loop. One single event loop (as opposed to multiple event loops or nested event loops) is preferred; the System Manager can lose track of the thread of execution in deeply nested LHAPI calls with multiple event loops. A somewhat standard event loop might look like this:

int EventLoop(void) 

{ 

int Done=0;                     /* Application termination flag */ 

EVENT app_event; 

while (!Done) {                 /* While app not terminated */ 

app_event.do_event = DO_EVENT;  /* Grab event from system manager */ 

m_action(&app_event); 

switch (app_event.kind) {       /* Branch on SysMgr event */ 

case E_REFRESH:                 /* SysMgr wants to redisplay our screen*/ 

case E_ACTIV:                   /* Swapping back to our app */ 

FixupFarPtrs();                 /* If app needs to fix pointers, do it here */ 

ReactivateLHAPI(&LHAPIData); 

break; 

case E_DEACT:                   /* User swapping to another app */ 

DeactivateLHAPI(); 

break; 

case E_TERM:                    /* User wants to kill our app */ 

FixupFarPtrs();                 /* Fix up any far pointers */ 

Done = TRUE;                    /* Set application terminate flag */ 

break; 

case E_NONE:                    /* Nothing has happened for 1/2 second */ 

SendFocusMsg(NULLEVENT, app_event.shifts, app_event.scan); 

break; 

case E_KEY:                     /* User has pressed a key */ 

SendFocusMsg(KEYSTROKE,app_event.data,app_event.scan); 

break; 

     } 

} 

} 

Obviously, application may need to modify this basic event loop. For example, an application that supports the database engine needs to take other precautions. This event loop can serve a starting place, however.