summaryrefslogtreecommitdiff
path: root/maccaml/main.c
blob: 475c1eecb1b76ac4ceff04e07d2d61da8f798d86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/***********************************************************************/
/*                                                                     */
/*                           Objective Caml                            */
/*                                                                     */
/*             Damien Doligez, projet Para, INRIA Rocquencourt         */
/*                                                                     */
/*  Copyright 1997 Institut National de Recherche en Informatique et   */
/*  en Automatique.  All rights reserved.  This file is distributed    */
/*  under the terms of the GNU Library General Public License.         */
/*                                                                     */
/***********************************************************************/

/* $Id$ */

#include "main.h"

QDGlobals qd;
int gHasDragAndDrop = 0;
int gHasPowerManager = 0;
int quit_requested = 0;
int launch_toplevel_requested = 0;

static OSErr Initialise (void)
{
  long gestval;
  int i;
  OSErr err;

  SetApplLimit (GetApplLimit () - kExtraStackSpace);
  MaxApplZone ();
  for (i = 0; i < kMoreMasters; i++) MoreMasters ();
  InitGraf (&qd.thePort);
  InitFonts ();
  InitWindows ();
  InitMenus ();
  TEInit ();
  InitDialogs (nil);
  InitCursor ();
  FlushEvents (everyEvent, 0);

  /* Unload the clipboard to disk if it's too big. */
  if (InfoScrap ()->scrapSize > kScrapThreshold) UnloadScrap ();

  /* Check for system 7. */
  if (Gestalt (gestaltSystemVersion, &gestval) != noErr
      || gestval < kMinSystemVersion){
    InitCursor ();
    StopAlert (kAlertNeedSys7, NULL);
    ExitToShell ();
  }

  /* Check for 32-bit color QuickDraw. */
  if (Gestalt (gestaltQuickdrawVersion, &gestval) != noErr
      || gestval < gestalt32BitQD){
    InitCursor ();
    StopAlert (kAlertNeed32BitQD, NULL);
    ExitToShell ();
  }

  /* Check for Drag Manager. */
  if (Gestalt (gestaltDragMgrAttr, &gestval) == noErr
      && (gestval & (1 << gestaltDragMgrPresent))
      && (&NewDrag != NULL)){
    gHasDragAndDrop = 1;
  }

  /* Check for Power Manager. */
  if (Gestalt (gestaltPowerMgrAttr, &gestval) == noErr
      && (gestval & (1 << gestaltPMgrExists))){
    gHasPowerManager = 1;
  }

  err = InitialiseErrors ();
  if (err != noErr) goto problem;

  if (gHasDragAndDrop){
    err = InstallDragHandlers ();
    if (err != noErr) goto problem;
  }

  err = InitialiseEvents ();
  if (err != noErr) goto problem;

  err = InitialiseMenus ();
  if (err != noErr) goto problem;

  err = InitialiseScroll ();
  if (err != noErr) goto problem;

  err = InitialiseWindows ();
  if (err != noErr) goto problem;

  err = InitialiseModalFilter ();
  if (err != noErr) goto problem;

  ReadPrefs ();

  return noErr;

  problem: return err;
}

static void Finalise (void)
{
  if (gHasDragAndDrop) RemoveDragHandlers ();
  WritePrefs ();
}

void main (void)
{
  OSErr err;

  err = Initialise ();
  if (err != noErr) ExitApplication ();

  while (1){
    GetAndProcessEvents (waitEvent, 0, 0);
    if (launch_toplevel_requested){
      err = launch_caml_main ();   /* does not return */
      if (err != noErr) ErrorAlertGeneric (err);
    }
  }
  ExitApplication ();
}

void ExitApplication (void)
{
  Caml_working (0);
  Finalise ();
  ExitToShell ();
}