1 // Emacs style mode select   -*- C++ -*-
2 //-----------------------------------------------------------------------------
3 //
4 // $Id: i_main.c 1035 2013-08-14 00:38:40Z wesleyjohnson $
5 //
6 // Copyright (C) 1993-1996 by id Software, Inc.
7 // Portions Copyright (C) 1998-2000 by DooM Legacy Team.
8 //
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License
11 // as published by the Free Software Foundation; either version 2
12 // of the License, or (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 //
19 //
20 // $Log: i_main.c,v $
21 // Revision 1.1  2001/04/17 22:23:38  calumr
22 // Initial add
23 //
24 // Revision 1.1  2000/08/21 21:17:32  metzgermeister
25 // Initial import to CVS
26 //
27 // Revision 1.3  2000/04/23 16:19:52  bpereira
28 // Revision 1.2  2000/02/27 00:42:11  hurdler
29 // Revision 1.1.1.1  2000/02/22 20:32:33  hurdler
30 // Initial import into CVS (v1.29 pr3)
31 //
32 //
33 // DESCRIPTION:
34 //      Main program, simply calls D_DoomMain high level loop.
35 //
36 //-----------------------------------------------------------------------------
37 
38 #include <Carbon/Carbon.h>
39 
40 #include "doomincl.h"
41 
42 #include "m_argv.h"
43 #include "d_main.h"
44 
RequiredCheck(const AppleEvent * theAppleEvent)45 OSErr RequiredCheck (const AppleEvent *theAppleEvent)
46 {
47 	OSErr  myErr;
48 	DescType  typeCode;
49 	Size  actualSize;
50 
51 	myErr = AEGetAttributePtr (theAppleEvent, keyMissedKeywordAttr, typeWildCard, &typeCode, 0L, 0, &actualSize);
52 	if (myErr == errAEDescNotFound) return (noErr);
53 	if (myErr == noErr) return (errAEEventNotHandled);
54 
55 	return (myErr);
56 }
57 
HandleODOC(const AppleEvent * theAppleEvent,AppleEvent * reply,long myRefCon)58 pascal OSErr HandleODOC (const AppleEvent *theAppleEvent, AppleEvent *reply, long myRefCon)
59 {
60 	#pragma unused (reply, myRefCon)
61 	long  itemsInList;
62 	int  i;
63 	OSErr  err;
64 	AEDescList  docList;
65 
66 	err = AEGetParamDesc (theAppleEvent, keyDirectObject, typeAEList, &docList);
67 	if (err) return (err);
68 
69 	err = RequiredCheck (theAppleEvent);
70 	if (err) return (err);
71 
72 	err = AECountItems (&docList, &itemsInList);
73 	if (err) return (err);
74 
75 	for (i = 1; i <= itemsInList; i++)
76 	{
77 		FSSpec  fileSpec;
78 		AEKeyword  theKeyword;
79 		DescType  typeCode;
80 		Size  actualSize;
81 
82 		// Get the fileSpec
83 		err = AEGetNthPtr (&docList, i, typeFSS, &theKeyword, &typeCode, (Ptr) &fileSpec, sizeof (FSSpec), &actualSize);
84 		if (err) return (err);
85 
86 		// Convert it to a full pathname and add
87 		{
88 			FSRef new_file;
89 			UInt32 len = 256;
90 			char path[256], command[256];
91 
92 			err = FSpMakeFSRef(&fileSpec, &new_file);
93 			err = FSRefMakePath(&new_file, path, len);
94 
95 			sprintf(command, "addfile \"%s\"\n", path);
96 			COM_BufAddText(command);
97 		}
98 	}
99 
100 	return (noErr);
101 }
102 
HandleQUIT(const AppleEvent * theAppleEvent,AppleEvent * reply,long myRefCon)103 OSErr HandleQUIT (const AppleEvent *theAppleEvent, AppleEvent *reply, long myRefCon)
104 {
105 	OSErr  myErr;
106 
107 	myErr = RequiredCheck (theAppleEvent);
108 	if (myErr) return (myErr);
109 
110 	I_Quit();
111 
112 	return (noErr);
113 }
114 
115 /**************************************************************************
116  * HandleOAPP
117  *
118  * Open a file through the OAPP apple event. This stops the bouncing
119  * icon in the Dock.
120  *************************************************************************/
121 
HandleOAPP(const AppleEvent * theAppleEvent,AppleEvent * reply,long myRefCon)122 OSErr HandleOAPP (const AppleEvent *theAppleEvent, AppleEvent *reply, long myRefCon)
123 {
124 	OSErr  myErr;
125 
126 	myErr = RequiredCheck (theAppleEvent);
127 	if (myErr) return (myErr);
128 
129 	return (noErr);
130 }
131 
InitAppleEvents(void)132 void InitAppleEvents (void)
133 {
134 	AEInstallEventHandler (kCoreEventClass, kAEQuitApplication, NewAEEventHandlerUPP (HandleQUIT), 0, false);
135 	AEInstallEventHandler (kCoreEventClass, kAEOpenApplication, NewAEEventHandlerUPP (HandleOAPP), 0, false);
136 	AEInstallEventHandler (kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerUPP (HandleODOC), 0, false);
137 }
138 
GrabFirstEvent(void)139 static void GrabFirstEvent (void)
140 {
141 	EventRecord theEvent;
142 
143 	do
144 		WaitNextEvent (everyEvent, &theEvent, 0, NULL);
145 	while (theEvent.what != kHighLevelEvent);
146 
147 	switch (theEvent.what)
148 	{
149 		case kHighLevelEvent:
150 			AEProcessAppleEvent (&theEvent);
151 			break;
152 	}
153 }
154 
main(int argc,char ** argv)155 int main (int argc, char** argv)
156 {
157 	myargv = argv;
158     myargc = argc;
159 
160 	InitAppleEvents();
161 
162     D_DoomMain ();
163 	GrabFirstEvent();
164 	while (1)
165 		RunApplicationEventLoop();
166 	//while (1)
167 	//{
168 	//	D_DoomLoop ();
169 	//}
170     return 0;
171 }