home *** CD-ROM | disk | FTP | other *** search
- /*****
- * main.c for fixation © 1996 Adam Miller
- for more info see http://zoo.cs.yale.edu:8000/~miller/comp/fix.html
-
- compiled in Metrowerks Codewarrior (! yeah !) 1.2.2
-
- *
- *
- *****/
-
- #define ICC 0
-
- #include <string.h>
- #include <profiler.h>
-
- #include "mytypes.h"
- #include "ResRefs.h"
- #include "error.h"
-
- #include "menus.h"
- #include "window.h"
- #include "main.h"
- #include "util.h"
- #include "incoming.h"
- #include "preffile.h"
- #include "connexions.h"
- #include "game.h"
- #include "globals.h"
-
- #include "tcpstuff.h"
-
- // some globals 'n shit
-
- enum {
- kCurPrefVersion = 1
- };
-
- int timeToQuit = false;
- int gInBackground = false;
- Shortcut *gShortcuts;
-
- extern Rect dragRect;
-
- void InitMacintosh(void);
- void HandleMouseDown (EventRecord *theEvent);
- void HandleEvent(void);
- void Bolo(void);
-
- static char *
- RandomName(void)
- {
- short rnd = Random() % 8;
- if (rnd < 0) rnd *= -1;
-
- switch (rnd) {
- case 0: return "Tessai";
- case 1: return "Benisato";
- case 2: return "Mushizo";
- case 3: return "Utsutso";
- case 4: return "Shujima";
- case 5: return "Yurimaru";
- case 6: return "Zakuru";
- default: return "Genma";
- }
- }
-
- static void
- InitializePrefValues(void)
- {
- gPrefs.versionNumber = kCurPrefVersion;
- strcpy(gPrefs.name, RandomName());
- #if ICC
- strcpy(gPrefs.serverAddress, "chess.lm.com");
- gPrefs.serverType = kIcc;
- strcpy(gPrefs.name, "guest"); // how boring
- gPrefs.timeseal = true;
- #else
- strcpy(gPrefs.serverAddress, "ics.onenet.net");
- gPrefs.serverType = kFics;
- gPrefs.timeseal = true;
- #endif
- SetRect(&gPrefs.textWindPos, 0, 0, 0, 0);
- SetRect(&gPrefs.gameWindPos1, 0, 0, 0, 0);
- gPrefs.useSound = true;
- gPrefs.port = 5000;
- gPrefs.password[0] = 0;
- gPrefs.numLogins = 0;
- gPrefs.saidHi = false;
- gPrefs.smartClose = true;
- gPrefs.autoFlag = false;
- gPrefs.autoBug = true;
- gPrefs.autoFlip = true;
- gPrefs.adminColors = false; gPrefs.channelColors = true;
- gPrefs.smartText = true;
- gPrefs.slowText = false;
- gPrefs.minimal = false;
- gPrefs.colWhite.red = 0xE000; gPrefs.colWhite.green = 0xE000; gPrefs.colWhite.blue = 0xE000;
- gPrefs.colBlack.red = 22318; gPrefs.colBlack.green = 36864; gPrefs.colBlack.blue = 34744;
- gPrefs.trueColors = true;
- gPrefs.pieceSet = 0;
- gPrefs.currentSize = 9;
- GetFNum("\pMonaco", &gPrefs.currentFont); /* Initial font should be monaco */
- gPrefs.boardSize1 = 32 * 8;
- gPrefs.boardSize2 = 32 * 8;
- gPrefs.backgroundOnDrag = false;
- gPrefs.soundTells = true;
- }
-
- static void
- GetPrefs(void)
- {
- int i;
- Boolean prefsValid = false;
-
- // if (!prefsValid)
- InitializePrefValues();
-
- // try to read the prefs in from the file
- if (DoReadPrefs((Ptr) &gPrefs, sizeof(prefStruct), "\pFixation Prefs") == sizeof(prefStruct)) {
- // check the version of the saved prefs is adequate
- prefsValid = true;
- // if (gPrefs.versionNumber < kCurPrefVersion)
- // prefsValid = false;
- }
-
- gPrefs.numLogins++;
-
-
- // now read shortcuts
- gShortcuts = (Shortcut *) NewPtr(sizeof(Shortcut) * kShortcuts); verify(gShortcuts);
- for (i=0;i<kShortcuts;i++)
- gShortcuts[i].jexiste = false;
- if (DoReadPrefs((Ptr) gShortcuts, sizeof(Shortcut) * kShortcuts,
- "\pFixation Shortcuts") == 0) {
- // make some initial shortcuts
- Shortcut *sc = gShortcuts;
- #if !(ICC)
- strcpy(sc->shortcut, "Generic FICS");
- strcpy(sc->serverAddress, "ics.onenet.net");
- sc->name[0] = 0; sc->password[0] = 0; sc->port = 5000; sc->serverType = kFics;
- sc->timeseal = false; sc->jexiste = true;
-
- sc = &gShortcuts[1];
- #else
- sc = &gShortcuts[0];
- #endif
- strcpy(sc->shortcut, "Generic ICC");
- strcpy(sc->serverAddress, "chess.lm.com");
- sc->name[0] = 0; sc->password[0] = 0; sc->port = 5000; sc->serverType = kIcc;
- sc->timeseal = true; sc->jexiste = true;
- }
- }
-
- void InitMacintosh(void)
- {
- MaxApplZone();
-
- InitGraf(&qd.thePort);
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
- InitCursor();
- GetDateTime((ulong *) &qd.randSeed);
-
- MyInitUtil();
-
- // if (DoReadPrefs((Ptr) &prefs, sizeof(preftype)) == sizeof(preftype)) {
- // }
- // prefs.shouldpatchbutt = true;
- }
-
- void Adjust_text( void )
- {
- short oldScroll, newScroll, delta;
-
- oldScroll = (**myText).viewRect.top - (**myText).destRect.top;
- newScroll = GetCtlValue(gVBar) * GetLineHeight(myText);
- delta = oldScroll - newScroll;
- if (delta != 0)
- TEScroll(0, delta, myText);
- }
-
- /*
- static void Adjust_text( void )
- {
- short scroll_down;
- short old_scroll;
- Rect update_rect;
- ControlHandle the_bar = gVBar;
-
- old_scroll = (**myText).viewRect.top - (**myText).destRect.top;
- scroll_down = old_scroll - GetCtlValue( the_bar );
- if (scroll_down == 0)
- return;
- TEScroll( 0, scroll_down, myText );
- }
- */
-
- static pascal void Scroll_text( ControlHandle the_bar, int part_code )
- {
- short offset;
-
- switch (part_code) {
- case inUpButton:
- offset = -1; break;
- case inDownButton:
- offset = 1; break;
- case inPageUp:
- offset = -10; break;
- case inPageDown:
- offset = 10; break;
- default:
- offset = 0;
- }
-
- SetCtlValue(the_bar, GetCtlValue(the_bar) + offset);
- Adjust_text();
- }
-
-
-
- /* ---------------------- Handle_scroll ---------------------------- */
- /*
- Called by Help_filter to handle mouseDown events in the scroll bar.
- */
- static void Handle_scroll( WindowPtr dialog, short the_part, Point where )
- {
- ControlHandle the_bar;
- ControlActionUPP myupp = NewControlActionProc(Scroll_text);
-
- SetPort( myWindow );
- the_bar = gVBar;
- if (the_part == inThumb)
- {
- (void) TrackControl( the_bar, where, nil );
- Adjust_text( );
- }
- else
- (void) TrackControl( the_bar, where, myupp );
-
- }
-
-
- void HandleMouseDown (EventRecord *theEvent)
- {
- WindowPtr theWindow;
- int windowCode = FindWindow (theEvent->where, &theWindow);
- short the_part;
- ControlHandle the_control;
-
- switch (windowCode)
- {
- case inSysWindow:
- SystemClick (theEvent, theWindow);
- break;
-
- case inMenuBar:
- AdjustMenus();
- HandleMenu(MenuSelect(theEvent->where));
- break;
-
- case inDrag:
- DragWindow(theWindow, theEvent->where, &dragRect);
- /* if (theWindow == (WindowPtr) gameWindow) {
- Point pt = {0, 0};
- SetPort((WindowPtr) gameWindow);
- LocalToGlobal(&pt);
- // make sure window aligned to 4 pixels
- if (pt.h & 3) { // it's not
- pt.h -= pt.h & 3;
- MoveWindow((WindowPtr) gameWindow, pt.h, pt.v, false);
- }
- }*/
- break;
-
- case inContent:
- if (theWindow != FrontWindow()) {
- SelectWindow(theWindow);
- if (theWindow == myWindow)
- DrawGrowIcon(myWindow);
- }
- else if (theWindow == myWindow) {
- Point pt = theEvent->where;
- SetPort(myWindow);
- GlobalToLocal(&pt);
- the_part = FindControl( pt, theWindow, &the_control );
-
- if (the_part && ((**the_control).contrlMax > 1) )
- Handle_scroll( myWindow, the_part, pt );
- else if (PtInRect( pt, &(**myText).viewRect ))
- TEClick(pt, false, myText);
- else if (PtInRect( pt, &(**typeText).viewRect ))
- TEClick(pt, false, typeText);
- }
- else {
- Point pt = theEvent->where;
- SetPort(FrontWindow());
- GlobalToLocal(&pt);
- HandleGameClick(&pt);
- }
- break;
-
- case inGrow:
- if (theWindow == myWindow)
- GrowTextWindow(theEvent->where);
- else
- GrowGameWindow((CWindowPtr) theWindow, theEvent->where);
- break;
-
- case inGoAway:
- if (TrackGoAway(theWindow, theEvent->where))
- if (theWindow == myWindow)
- HideWindow(myWindow);
- else
- CloseGameWindow(theWindow);
- break;
-
- case inZoomIn: case inZoomOut:
- ZoomWindow(theWindow, windowCode, true);
- break;
-
- default: break;
- }
- }
- /* end HandleMouseDown */
-
-
- /****
- * HandleEvent()
- *
- * The main event dispatcher. This routine should be called
- * repeatedly (it handles only one event).
- *
- *****/
-
- extern short gClickType;
-
- void HandleEvent(void)
- {
- WindowPtr theWindow;
- short ok;
- short item;
- EventRecord theEvent;
- DialogPtr dp;
- char c;
-
- // HiliteMenu(0);
- // SystemTask(); /* Handle desk accessories */
-
- if (gPrefs.backgroundOnDrag || (gClickType == 0))
- ok = WaitNextEvent(everyEvent, &theEvent, 5, nil);
- else
- ok = GetOSEvent(everyEvent, &theEvent);
-
- /* if (ok) // don't need to call regardless, no editable text
- // we only call with mousedown to avoid destruction of updates
- // if we had stuff there besides buttons, shit would hit the fan
- if (theEvent.what == mouseDown && IsDialogEvent(&theEvent))
- if (DialogSelect(&theEvent, &dp, &item))
- if (dp == (DialogPtr) itemWindow)
- ItemWindowButton(item);
- */
-
- // ok = GetNextEvent (everyEvent, &theEvent);
- if (ok)
- switch (theEvent.what)
- {
- case mouseDown:
- HandleMouseDown(&theEvent);
- break;
-
- case keyDown:
- case autoKey:
- if ((theEvent.modifiers & cmdKey) != 0)
- {
- AdjustMenus();
- HandleMenu(MenuKey((char) (theEvent.message & charCodeMask)));
- }
- else // if (FrontWindow() == myWindow)
- {
- c = (char) (theEvent.message & charCodeMask);
- if (c == '\r' || c == '\n')
- EnteredText(); // they finished what they were saying
- else if (c == 11) { // page up
- SetCtlValue(gVBar, GetCtlValue(gVBar) - gLinesInText + 2);
- Adjust_text();
- }
- else if (c == 12) { // page down
- SetCtlValue(gVBar, GetCtlValue(gVBar) + gLinesInText - 2);
- Adjust_text();
- }
- else if (c == 5) { // help (up one line)
- SetCtlValue(gVBar, GetCtlValue(gVBar) - 1);
- Adjust_text();
- }
- else if (c == 127) { // del (down one line)
- SetCtlValue(gVBar, GetCtlValue(gVBar) + 1);
- Adjust_text();
- }
- else if (c == 1) { // home
- SetCtlValue(gVBar, 0);
- Adjust_text();
- }
- else if (c == 4) { // end
- SetCtlValue(gVBar, GetCtlMax(gVBar));
- Adjust_text();
- }
- else {
- //TEKey(c, typeText);
- //tprintf("%d ", c);
- HandleKey(c);
- }
- }
- break;
-
- case updateEvt:
- theWindow = (WindowPtr) theEvent.message;
- BeginUpdate(theWindow);
- SetPort(theWindow);
- if (theWindow == myWindow)
- DrawTextWindow();
- else
- DrawGameWindow(theWindow);
- EndUpdate(theWindow);
- break;
-
- case osEvt:
- switch ((theEvent.message >> 24) & 0x0FF) { /* high byte of message */
- case suspendResumeMessage: /* suspend/resume is also an activate/deactivate */
- gInBackground = (theEvent.message & resumeFlag) == 0;
- if (!gInBackground) {
- InitCursor();
- TEFromScrap();
- }
- break;
- }
- break;
-
- case activateEvt:
- // SetPort(myWindow);
- // InvalRect(&myWindow->portRect);
- InitCursor();
- TEFromScrap();
- break;
-
- default: break;
- }
- }
- /* end HandleEvent */
-
-
- Rect
- GetWindowRect(WindowPtr win)
- {
- Point pt = {0, 0};
- Rect r;
-
- SetPort(win);
- LocalToGlobal(&pt);
- r = win->portRect;
- OffsetRect(&r, pt.h, pt.v);
-
- return r;
- }
-
- static void
- PreparePrefs(void)
- {
- short nnn;
-
- gPrefs.textWindPos = GetWindowRect(myWindow);
- }
-
- void Terminate(void);
- void Terminate(void)
- {
- ReleaseStreams();
- ExitToShell();
- }
-
- static void
- ExitStuff(void)
- {
- if (gPlaying)
- StopPlaying();
- PreparePrefs();
- DoSavePrefs((Ptr) &gPrefs, sizeof(prefStruct), "\pFixation Prefs");
- DoSavePrefs((Ptr) gShortcuts, sizeof(Shortcut) * kShortcuts,
- "\pFixation Shortcuts");
- }
-
- static void
- BuffersToScreen(void)
- {
- short nnn;
-
- for (nnn=0;nnn<kMaxStreams;nnn++)
- if (bufdata[nnn] > 0) {
- short i;
-
- /* for (i=0;i<bufdata[nnn];i++) {
- if (buf[nnn][i] != '\f' && buf[nnn][i] != '\n')
- TEKey(buf[nnn][i], myText);
- }*/
- for (i=0;i<bufdata[nnn];i++) {
- if (buf[nnn][i] == '\f' || buf[nnn][i] == '\n')
- buf[nnn][i] = ' ';
- }
- TEInsert(buf[nnn], bufdata[nnn], myText);
- CalcVBar();
-
- bufdata[nnn] = 0;
- }
- }
-
- void main()
- {
- InitMacintosh();
- GetPrefs();
- SetUpMenus();
- SetUpWindow();
- if (NetInit()) {
- stdmessage("\pError initializing MacTCP.");
- Terminate();
- }
- ConnInit();
- InitGame();
-
- // tprintf("PRERELEASE COPY -- DO NOT DISTRIBUTE\r\r");
- #if ICC
- tprintf("Hi, I'm adum, and welcome to Fixation 1.3, a TCP client for the Internet Chess Club.\r");
- tprintf("For more information on the Internet Chess Club check out http://www.hydra.com/icc/\r");
- #else
- tprintf("Hi, I'm adum, and welcome to Fixation 1.3, a TCP client for the Free Internet Chess Server and ICC.\r");
- tprintf("For more information on the Free Internet Chess Server check out http://chess.onenet.net/chess/\r");
- #endif
- tprintf("Fixation is free and without warrantee. If you want");
- tprintf(" to thank me for Fixation, help my chess game (my handle is adum)!\r");
- tprintf("More info + source code at http://zoo.cs.yale.edu:8000/~miller/comp/fix.html\r");
- tprintf("Before reporting bugs and suggestions, please read the Bugs & Suggestions info available from the Windows menu above.\r");
- tprintf("Email bugs, comments, and suggestions to miller@minerva.cis.yale.edu\r\r");
- // tprintf("Local IP: "); PrintAddress(gMyAddress); tprintf("\r");
-
- // OpenTalker();
-
- #if __profile__
- ProfilerInit(collectDetailed, bestTimeBase, 150, 150);
- #endif
-
- do {
- NetIdle();
- if (!gInBackground && FrontWindow() == myWindow)
- TEIdle(typeText);
- ConnIdle();
- ScanIncomingServer(0);
- GameIdle();
- HandleEvent();
- } while (!timeToQuit);
-
- #if __profile__
- ProfilerDump("\patrahasis.prof");
- ProfilerTerm();
- #endif
-
- ExitStuff();
- Terminate();
- }
- /* end main */