home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #4 / amigaacscoverdisc1998-041998.iso / utilities / shareware / dev / ucb_logoppc / source / macterm.c.mac < prev    next >
Encoding:
Text File  |  1997-03-06  |  14.4 KB  |  579 lines

  1. /*
  2.  *      macterm.c          macintosh screen module          mak
  3.  *
  4.  *    Copyright (C) 1993 by the Regents of the University of California
  5.  *
  6.  *      This program is free software; you can redistribute it and/or modify
  7.  *      it under the terms of the GNU General Public License as published by
  8.  *      the Free Software Foundation; either version 2 of the License, or
  9.  *      (at your option) any later version.
  10.  *  
  11.  *      This program is distributed in the hope that it will be useful,
  12.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *      GNU General Public License for more details.
  15.  *  
  16.  *      You should have received a copy of the GNU General Public License
  17.  *      along with this program; if not, write to the Free Software
  18.  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  */
  21.  
  22. #include "logo.h"
  23. #include "globals.h"
  24. #include "macterm.h"
  25. #include <console.h>
  26. #include <math.h>
  27. #include <Sound.h>
  28. #include <Types.h>
  29. #include <GestaltEqu.h>
  30. #include <Palettes.h>
  31.  
  32. char windowtitle[100];
  33. FILE *graphics, *console;
  34. WindowPtr graphics_window, listener_window;
  35. GrafPtr savePort;
  36. extern WindowPtr myWindow; /* the editor window */
  37. FIXNUM pen_color = 7, back_ground = 0;
  38.  
  39. /************************************************************/
  40.  
  41. void nop() {
  42. }
  43.  
  44. int can_do_color = 0, want_color = 0;
  45. PaletteHandle the_palette;
  46.  
  47. void get_can_do_color(void) {
  48.     long ans;
  49.  
  50.     if (!Gestalt(gestaltQuickdrawVersion, &ans))
  51.         can_do_color = (ans != 0);
  52. }
  53.  
  54. void init_mac_memory(void) {
  55.     unsigned long AZ, AL;
  56.     /* SetApplLimit((Ptr)(GetApplLimit() - 150000L)); MaxApplZone(); */
  57.     
  58.     AL = (unsigned long)GetApplLimit(); AZ = (unsigned long)ApplicZone();
  59. /*    SetApplLimit((Ptr) ( (AZ + ((AL-AZ)*3)/4) & 0x00fffffe )); */
  60.         /* SetApplLimit((Ptr)(AZ + 300000L)); */
  61.     SetApplLimit((Ptr)((AZ + ((AL-AZ)*3)/4) & -2L)); /* PCB */
  62.     MaxApplZone();
  63. }
  64.  
  65. BOOLEAN check_mac_stop(void) {
  66.     char        the_key_map[16];
  67.     static int full = 400;
  68.     extern void ProcessEvent(void);
  69.  
  70.     if (FreeMem() < 3000) {
  71.     err_logo(STACK_OVERFLOW, NIL);
  72.     return(1);
  73.     }
  74.     GetKeys(&the_key_map);
  75.     if (the_key_map[5] & 128 && the_key_map[6] & 128) {
  76.             /* period and command are down */
  77.     FlushEvents(everyEvent, 0);
  78.     logo_stop();
  79.     return 1;
  80.     } else if (the_key_map[5] & 8 && the_key_map[6] & 128) {
  81.             /* comma and command are down */
  82.     FlushEvents(everyEvent, 0);
  83.     logo_pause();
  84.     }
  85.     if (--full == 0) {
  86.         ProcessEvent();
  87.         full = 400;
  88.     }
  89.     return(0);
  90. }
  91.  
  92. void term_init_mac(void) {
  93.     MenuHandle menu_handle;
  94.     
  95.     get_can_do_color();
  96.     tty_charmode = 0;
  97.     x_max = 80;
  98.     y_max = 24;
  99.     console_options.title = windowtitle;
  100.     console_options.top-= 5;
  101.     console_options.left-= 5;
  102.     strncpy(console_options.title, "\pGraphics", 9);
  103.     want_color = 1;
  104.     graphics = fopenc();
  105.     want_color = 0;
  106.     cgotoxy(1, 1, graphics);
  107.     graphics_window = FrontWindow();
  108.     if (can_do_color) {
  109.         the_palette = GetNewPalette(129);
  110.         SetPalette(graphics_window, the_palette, 1);
  111.         ActivatePalette(graphics_window);
  112.     }
  113.     SizeWindow(graphics_window, graphics_window->portRect.right,
  114.                 graphics_window->portRect.bottom - 1, 0);
  115.     cs_helper(TRUE);
  116.  
  117.     console_options.top+= 10;
  118.     console_options.left+= 10;
  119.     strncpy(console_options.title, "\pBerkeley Logo", 14);
  120.     console = fopenc();
  121.     lregulartext(NIL);
  122.     cinverse(1,stdout);
  123.     listener_window = FrontWindow();
  124.     
  125.     console_options.title[0] = 0;
  126.     
  127.     menu_handle = GetMHandle(3);
  128.     AppendMenu(menu_handle,
  129.            "\p(-;(Accept Editor Changes/A;(Cancel Editor Changes");
  130.     AppendMenu(menu_handle, "\p(-;(Page Setup;(Print/P");
  131.     SetUpWindows();
  132.     prepare_to_draw;
  133.     mac_set_bg(0L);
  134.     mac_set_pc(7L);
  135.     save_color();
  136.     done_drawing;
  137.  
  138.     x_coord = y_coord = 0;
  139.     so_arr[0] = '\1'; so_arr[1] = '\0';
  140.     se_arr[0] = '\2'; se_arr[1] = '\0';
  141. }
  142.  
  143. void mac_gotoxy(int x, int y) {
  144.     if (x_coord < 0) x_coord = 0;
  145.     if (x_coord >= console_options.ncols) x_coord = console_options.ncols - 1;
  146.     if (y_coord < 0) y_coord = 0;
  147.     if (y_coord >= console_options.nrows) y_coord = console_options.nrows - 1;
  148.     cgotoxy(x_coord + 1, y_coord + 1, stdout);
  149. }
  150.  
  151. /************************************************************/
  152. /* These are primitives that can only exist on the mac and/or are ad hoc
  153.    things Michael invented that we probably don't want to keep around in Berkeley Logo. */
  154.  
  155. NODE *lsetwindowtitle(NODE *arg) {
  156.     NODE *name;
  157.     
  158.     name = string_arg(arg);
  159.     if (name != UNBOUND) {
  160.     noparity_strnzcpy((char *)(windowtitle + 1), getstrptr(name), (int)getstrlen(name));
  161.     windowtitle[0] = (char)getstrlen(name);
  162.     }
  163.     return(UNBOUND);
  164. }
  165.  
  166. void option_helper(short* var, NODE *arg) {
  167.     NODE *val;
  168.     
  169.     val = integer_arg(arg);
  170.     if (NOT_THROWING)
  171.     *var = (short)getint(val);
  172. }
  173.  
  174. NODE *lsettextfont(NODE *arg) {
  175.     option_helper(&console_options.txFont, arg);
  176.     return(UNBOUND);
  177. }
  178.  
  179. NODE *lsettextsize(NODE *arg) {
  180.     option_helper(&console_options.txSize, arg);
  181.     return(UNBOUND);
  182. }
  183.  
  184. NODE *lsettextstyle(NODE *arg) {
  185.     option_helper(&console_options.txFace, arg);
  186.     return(UNBOUND);
  187. }
  188.  
  189. NODE *lsetwindowsize(NODE *args) {
  190.     NODE *xnode, *ynode = UNBOUND, *arg;
  191.  
  192.     arg = pos_int_vector_arg(args);
  193.     if (NOT_THROWING) {
  194.     xnode = car(arg);
  195.     ynode = cadr(arg);
  196.     console_options.ncols =  max((int)getint(xnode), 40);
  197.     console_options.nrows =  max((int)getint(ynode), 5);
  198.     }
  199.     return(UNBOUND);
  200. }
  201.  
  202. NODE *lsetwindowxy(NODE *args) {
  203.     NODE *xnode, *ynode = UNBOUND, *arg;
  204.  
  205.     arg = pos_int_vector_arg(args);
  206.     if (NOT_THROWING) {
  207.     xnode = car(arg);
  208.     ynode = cadr(arg);
  209.     console_options.left = (int)getint(xnode);
  210.     console_options.top = (int)getint(ynode);
  211.     }
  212.     return(UNBOUND);
  213. }
  214.  
  215. NODE *lnewconsole(NODE *args) {
  216.     FILE *c, *old;
  217.     int was_graphics;
  218.     
  219.     was_graphics = (FrontWindow() == graphics_window);
  220.     
  221.     chide(stdin);
  222.     fclose(stdin);
  223.     fclose(stdout);
  224.     fclose(stderr);
  225.     c = fopenc();
  226.     fclose(stdin);
  227.     fclose(stdout);
  228.     freopenc(c, stdin);
  229.     freopenc(c, stdout);
  230.     freopenc(c, stderr);
  231.     lcleartext(NIL);
  232.     cinverse(1,stdout);
  233.     
  234.     if (was_graphics) {
  235.     graphics_window = FrontWindow();
  236.     graphics = c;
  237.     } else {
  238.     listener_window = FrontWindow();
  239.     console = c;
  240.     x_max = console_options.ncols;
  241.     y_max = console_options.nrows;
  242.     }
  243.     return(UNBOUND);
  244. }
  245.  
  246. NODE *lgraphtext(NODE *args) {
  247.     freopenc(graphics, stdin);
  248.     freopenc(graphics, stdout);
  249.     freopenc(graphics, stderr);
  250.     return(UNBOUND);
  251. }
  252.  
  253. NODE *lregulartext(NODE *args) {
  254.     freopenc(console, stdin);
  255.     freopenc(console, stdout);
  256.     freopenc(console, stderr);
  257.     return(UNBOUND);
  258. }
  259.  
  260. NODE *lcaninverse(NODE *args) {
  261.     FIXNUM onoff = int_arg(args);
  262.  
  263.     if (NOT_THROWING) cinverse(onoff, stdin);
  264.     return(UNBOUND);
  265. }
  266.  
  267. /************************************************************/
  268. /* These are the machine-specific graphics definitions.  All versions must provide
  269.    a set of functions analogous to these. */
  270.  
  271. void save_pen(pen_info *p) {
  272.     GetPort(&savePort);
  273.     SetPort(graphics_window);
  274.     GetPenState(&(p->ps));
  275.     p->vis = graphics_window->pnVis;
  276. //    p->color = graphics_window->fgColor;
  277.     p->color = pen_color;
  278.     SetPort(savePort);
  279. }
  280.  
  281. void restore_pen(pen_info *p) {
  282.     GetPort(&savePort);
  283.     SetPort(graphics_window);
  284.     SetPenState(&(p->ps));
  285.     graphics_window->pnVis = p->vis;
  286. //    graphics_window->fgColor = p->color;
  287.     mac_set_pc(p->color);
  288.     SetPort(savePort);
  289. }
  290.  
  291. void plain_xor_pen(void) {
  292.     PenNormal();
  293.     PenMode(patXor);
  294. }
  295.  
  296. FIXNUM color_table[8] = {33, 409, 341, 273, 205, 137, 69, 30};
  297.  
  298. FIXNUM hw_color(FIXNUM c) {
  299.     if (c >= 0 && c < 8) return color_table[c];
  300.     if (c < 0) return c;
  301.     return c-8;
  302. }
  303.  
  304. int palette_color(FIXNUM c) {
  305.     if (c == 7) return 0;
  306.     if (c >= 0 && c < 7) return c+1;
  307.     return c;
  308. }
  309.  
  310. void mac_set_pc(FIXNUM c) {
  311.     pen_color = c;
  312.     if (can_do_color)
  313.         PmForeColor(palette_color(c));
  314.     else
  315.         graphics_window->fgColor = hw_color(c);
  316. }
  317.  
  318. void mac_set_bg(FIXNUM c) {
  319.     back_ground = c;
  320.     if (can_do_color)
  321.         PmBackColor(palette_color(c));
  322.     else
  323.         graphics_window->bkColor = hw_color(c);
  324.     redraw_graphics();
  325. }
  326.  
  327. void set_palette(int slot, unsigned int r, unsigned int g, unsigned int b) {
  328.     RGBColor rgb;
  329.  
  330.     if (can_do_color) {
  331.         rgb.red = r;
  332.         rgb.green = g;
  333.         rgb.blue = b;
  334.         SetEntryColor(the_palette, slot, &rgb);
  335.         SetEntryUsage(the_palette, slot, pmTolerant, 2000);
  336.         ActivatePalette(graphics_window);
  337.         redraw_graphics();
  338.     }
  339. }
  340.  
  341. void get_palette(int slot, unsigned int *r, unsigned int *g, unsigned int *b) {
  342.     RGBColor rgb;
  343.  
  344.     if (can_do_color) {
  345.         GetEntryColor(the_palette, palette_color((FIXNUM)slot), &rgb);
  346.         *r = rgb.red;
  347.         *g = rgb.green;
  348.         *b = rgb.blue;
  349.     } else {
  350.         *r = (slot&4 ? 65000 : 0);
  351.         *g = (slot&2 ? 65000 : 0);
  352.         *b = (slot&1 ? 65000 : 0);
  353.     }
  354. }
  355.  
  356. void set_pen_pattern(char *pat) {
  357.     PenPat(pat);
  358. }
  359.  
  360. void set_list_pen_pattern(NODE *arg) {
  361.     NODE *cur_num, *temp;
  362.     char p_arr[8];
  363.     int count;
  364.  
  365.     cur_num = arg;
  366.     for (count = 0 ; count <= 7 && cur_num != NIL && NOT_THROWING ; count++) {
  367.     temp = cnv_node_to_numnode(car(cur_num));
  368.     p_arr[count] = (char)getint(temp);
  369.     cur_num = cdr(cur_num);
  370.     }
  371.     PenPat(p_arr);
  372. }
  373.  
  374. void get_pen_pattern(char *pat) {
  375.     PenState oil;
  376.     int count;
  377.  
  378.     GetPenState(&oil);
  379.     for (count = 0; count < 8; count++)
  380.      *(char *)(pat + count) = oil.pnPat[count];
  381. }
  382.  
  383. NODE *Get_node_pen_pattern() {
  384.     PenState oil;
  385.     int count;
  386.  
  387.     GetPenState(&oil);
  388.     return(cons(make_intnode((FIXNUM)(oil.pnPat[0])),
  389.         cons(make_intnode((FIXNUM)(oil.pnPat[1])),
  390.          cons(make_intnode((FIXNUM)(oil.pnPat[2])),
  391.           cons(make_intnode((FIXNUM)(oil.pnPat[3])),
  392.            cons(make_intnode((FIXNUM)(oil.pnPat[4])),
  393.         cons(make_intnode((FIXNUM)(oil.pnPat[5])),
  394.          cons(make_intnode((FIXNUM)(oil.pnPat[6])),
  395.           cons(make_intnode((FIXNUM)(oil.pnPat[7])),
  396.            NIL)))))))));
  397. }
  398.  
  399. NODE *Get_node_pen_mode() {
  400.     switch(pen_mode) {
  401.     case patCopy   : return(make_static_strnode("paint"));
  402.     case patBic    : return(make_static_strnode("erase"));
  403.     case patXor    : return(make_static_strnode("reverse"));
  404.     default           : return(make_static_strnode("unknown"));
  405.     }
  406. }
  407.  
  408. void label(char *s) {
  409.     short tmode;
  410.  
  411.     GetPort(&savePort);
  412.     SetPort(graphics_window);
  413.     MoveTo(g_round(graphics_window->portRect.right/2.0 + turtle_x),
  414.        g_round(graphics_window->portRect.bottom/2.0 - turtle_y));
  415.     switch(pen_mode) {
  416.     case patCopy   : tmode = srcOr; break;
  417.     case patBic    : tmode = srcBic; break;
  418.     case patXor    : tmode = srcXor; break;
  419.     default           : tmode = srcCopy; break;    /* can't happen */
  420.     }
  421.     TextFont(monaco); TextSize(9); TextMode(tmode);
  422.     DrawString(s);
  423.     SetPort(savePort);
  424. }
  425.  
  426. void logofill(void) {
  427.     BitMap mask;
  428.  
  429.     if (can_do_color) {
  430.         prepare_to_draw;
  431.         mask.bounds = (*(graphics_window->visRgn))->rgnBBox;
  432.         mask.bounds.right = mask.bounds.right - mask.bounds.left;
  433.         mask.bounds.left = 0;
  434.         mask.bounds.bottom = mask.bounds.bottom - mask.bounds.top;
  435.         mask.bounds.top = 0;
  436.         mask.rowBytes = ((mask.bounds.right + 15) / 8) & ~1;
  437.         mask.baseAddr = malloc(mask.rowBytes * mask.bounds.bottom);
  438.         if (mask.baseAddr == NULL) {
  439.             err_logo(OUT_OF_MEM, NIL);
  440.             done_drawing;
  441.             return;
  442.         }
  443.         SeedCFill(&(graphics_window->portBits), &mask,
  444.                   &((*(graphics_window->visRgn))->rgnBBox),
  445.                   &(mask.bounds),
  446.                   graphics_window->pnLoc.h, graphics_window->pnLoc.v,
  447.                   0, 0L);
  448.         CopyMask(&mask, &mask, &(graphics_window->portBits),
  449.                  &(mask.bounds), &(mask.bounds),
  450.                  &((*(graphics_window->visRgn))->rgnBBox));
  451.         free(mask.baseAddr);
  452.         done_drawing;
  453.     }
  454. }
  455.  
  456. void erase_screen(void) {
  457.     int old_vis;
  458.     
  459.     GetPort(&savePort);
  460.     SetPort(graphics_window);
  461.     old_vis = graphics_window->pnVis;
  462.     graphics_window->pnVis = 0;
  463.     EraseRect(&graphics_window->portRect);
  464.     graphics_window->pnVis = old_vis;
  465.     SetPort(savePort);
  466. }
  467.  
  468. void t_screen(void) {
  469.     console_options.ncols = 80;
  470.     console_options.nrows = 25;
  471.     console_options.left = 15;
  472.     console_options.top = 55;
  473.     strncpy(console_options.title, "\pBerkeley Logo", 14);
  474.     lnewconsole(NIL);
  475.  
  476.     MoveWindow(myWindow, 15, 55, TRUE);
  477.     MySizeWindow(myWindow, 488, 283);
  478.     SelectWindow(listener_window);
  479. }
  480.  
  481. void s_screen(void) {
  482.     Rect bounds;
  483.     int v;
  484.     
  485.     if (can_do_color)
  486.         v = ((*(((CGrafPtr)graphics_window)->portPixMap))->bounds.bottom -
  487.              (*(((CGrafPtr)graphics_window)->portPixMap))->bounds.top) - 84;
  488.     else
  489.         v = (graphics_window->portBits.bounds.bottom -
  490.              graphics_window->portBits.bounds.top) - 84;
  491.  
  492.     console_options.ncols = 80;
  493.     console_options.nrows = 6;
  494.     console_options.left = 5;
  495.     console_options.top = v + 6;
  496.     strncpy(console_options.title, "\pBerkeley Logo", 14);
  497.     lnewconsole(NIL);
  498.  
  499.     MoveWindow(myWindow, 5, v, TRUE);
  500.     MySizeWindow(myWindow, 488, 80);
  501.     SelectWindow(listener_window);
  502. }
  503.  
  504. void f_screen(void) {
  505.     Rect bounds;
  506.     int v;
  507.     
  508.     if (can_do_color)
  509.         v = ((*(((CGrafPtr)graphics_window)->portPixMap))->bounds.bottom -
  510.              (*(((CGrafPtr)graphics_window)->portPixMap))->bounds.top) - 84;
  511.     else
  512.         v = (graphics_window->portBits.bounds.bottom -
  513.              graphics_window->portBits.bounds.top) - 84;
  514.  
  515.     console_options.ncols = 80;
  516.     console_options.nrows = 0;
  517.     console_options.left = 5;
  518.     console_options.top = v + 52;
  519.     strncpy(console_options.title, "\pBerkeley Logo", 14);
  520.     lnewconsole(NIL);
  521.  
  522.     MoveWindow(myWindow, 5, v, TRUE);
  523.     MySizeWindow(myWindow, 488, 80);
  524.     SelectWindow(graphics_window);
  525. }
  526.  
  527. FIXNUM mickey_x(void) {
  528.     Point the_mouse;
  529.     
  530.     GetPort(&savePort);
  531.     SetPort(graphics_window);
  532.     GetMouse(&the_mouse);
  533.     SetPort(savePort);
  534.     
  535.     return((FIXNUM)(the_mouse.h - graphics_window->portRect.right/2));
  536. }
  537.  
  538. FIXNUM mickey_y(void) {
  539.     Point the_mouse;
  540.     
  541.     GetPort(&savePort);
  542.     SetPort(graphics_window);
  543.     GetMouse(&the_mouse);
  544.     SetPort(savePort);
  545.     
  546.     return((FIXNUM)(graphics_window->portRect.bottom/2 - the_mouse.v));
  547. }
  548.  
  549. /* see Inside Macintosh vol. 2 pp. 237-241 for pitch values */
  550. void tone(FIXNUM pitch, FIXNUM duration) {
  551.     struct { int mode;
  552.          int freq;
  553.          int amp;
  554.          int dur;
  555.        } sound_rec;
  556.     
  557.     sound_rec.mode = -1;
  558.     sound_rec.freq = (int)(387205L/pitch);
  559.     sound_rec.amp = 200;
  560.     sound_rec.dur = (int)duration;
  561.     
  562.     StartSound(&sound_rec, (long)8, (char *)(-1));
  563.     while (!SoundDone()) ;
  564. }
  565.  
  566. /************************************************************/
  567.  
  568. void c_to_pascal_string(char *str, int len) {
  569.     int count = len;
  570.     char prev;
  571.     
  572.     while (count > 0) {
  573.     prev = str[count - 1];
  574.     str[count] = clearparity(prev);
  575.     count--;
  576.     }
  577.     str[0] = len;
  578. }
  579.