home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / comms / network / amigaelm.lzh / winio.c < prev   
C/C++ Source or Header  |  1992-03-15  |  22KB  |  852 lines

  1. #include "prefs.h"
  2.  
  3. #include <exec/types.h>
  4. #include <exec/io.h>
  5. #ifdef LATTICE
  6. #include <proto/asl.h>
  7. #include <proto/dos.h>
  8. #include <proto/exec.h>
  9. #include <proto/graphics.h>
  10. #include <proto/intuition.h>
  11. #include <proto/diskfont.h>
  12. #endif
  13. #include <dos/dosextens.h>
  14. #include <graphics/text.h>
  15. #include <intuition/screens.h>
  16. #include <intuition/intuition.h>
  17. #include <utility/tagitem.h>
  18. #include <graphics/view.h>
  19. #include <graphics/displayinfo.h>
  20. #include <devices/console.h>
  21. #include <devices/inputevent.h>
  22. #include <exec/devices.h>
  23. #include <libraries/asl.h>
  24. #include <string.h>
  25.  
  26.  
  27.  
  28. struct Library  *IntuitionBase = NULL;
  29. struct Library  *GfxBase = NULL;
  30. struct Library  *DiskfontBase = NULL;
  31. struct Library  *AslBase = NULL;
  32. struct Screen   *screen = NULL;
  33. struct Window   *window = NULL;
  34. struct Window   *wintmp = NULL;
  35. struct IOStdReq *writereq = NULL;
  36. struct MsgPort  *writeport = NULL;
  37. struct IOStdReq *readreq = NULL;
  38. struct MsgPort  *readport = NULL;
  39. struct TextAttr textAttrStd;
  40. struct TextAttr textAttrPager;
  41. struct TextAttr textAttrIntui;
  42. struct TextFont *textFontStd = NULL;
  43. struct TextFont *textFontPager = NULL;
  44. struct TextFont *textFontIntui = NULL;
  45. struct TextFont *oldTextFont = NULL;
  46. struct Process  *process = NULL;
  47. BOOL consoleOpen = FALSE;
  48. BOOL showtitle   = FALSE;
  49. int Win_Height = 0;
  50. int Win_Width  = 0;
  51. int Std_Win_Height = 0;
  52. int Std_Win_Width  = 0;
  53. int Pager_Win_Height = 0;
  54. int Pager_Win_Width  = 0;
  55. int Screen_Height = 200;
  56. int Screen_Width  = 640;
  57. int Screen_Interlace = 0;
  58. int shell_count = 0;
  59. UWORD PublicModes = 0;
  60. char WinTitleBuffer[256];
  61.  
  62.  
  63.  
  64. Prototype void close_intuition(void);
  65. Prototype BOOL IsAslOpen(void);
  66. Prototype BOOL GetAslFRQ(char *,char *,char *);
  67. Prototype BOOL open_intuition(void);
  68. Prototype BOOL intui_interface(void);
  69. Prototype void SetScreenTitle(char *);
  70. Prototype char getch(void);
  71. Prototype long getst(char *,long);
  72. Prototype void flush_input(void);
  73. Prototype void putch(char);
  74. Prototype void putst(char *);
  75. Prototype void putst_center(char *);
  76. Prototype void putst_center_bold(char *);
  77. Prototype void putstl(char *,long);
  78. Prototype void ElmToFront(void);
  79. Prototype void WorkbenchToFront(void);
  80. Prototype void SetStdFont(void);
  81. Prototype void SetPagerFont(void);
  82. Prototype void open_shell(void);
  83.  
  84.  
  85.  
  86. void close_intuition(void)
  87. {
  88.   if (consoleOpen) CloseDevice(writereq);        consoleOpen = FALSE;
  89.   if (readreq) DeleteExtIO(readreq);            readreq = NULL;
  90.   if (readport) DeletePort(readport);            readport = NULL;
  91.   if (writereq) DeleteExtIO(writereq);            writereq = NULL;
  92.   if (writeport) DeletePort(writeport);            writeport = NULL;
  93.   if (window) {
  94.     SetFont(window->RPort,oldTextFont);
  95.     if (process) process->pr_WindowPtr = wintmp;
  96.     CloseWindow(window);
  97.     window = NULL;
  98.   }
  99.   if (screen) {
  100.     if (IntuitionBase->lib_Version<36)
  101.       CloseScreen(screen);
  102.     else {
  103.       if (PubScreenName)
  104.         PubScreenStatus(screen,PSNF_PRIVATE);
  105.       while(!CloseScreen(screen))
  106.         Delay(50L);
  107.     }
  108.     screen = NULL;
  109.   }
  110.  
  111.   if (textFontStd)   CloseFont(textFontStd);      textFontStd   = NULL;
  112.   if (textFontPager) CloseFont(textFontPager);    textFontPager = NULL;
  113.   if (textFontIntui) CloseFont(textFontIntui);    textFontIntui = NULL;
  114.   if (IntuitionBase) CloseLibrary(IntuitionBase); IntuitionBase = NULL;
  115.   if (GfxBase)       CloseLibrary(GfxBase);       GfxBase       = NULL;
  116.   if (DiskfontBase)  CloseLibrary(DiskfontBase);  DiskfontBase  = NULL;
  117.   if (AslBase)       CloseLibrary(AslBase);       AslBase       = NULL;
  118. }
  119.  
  120.  
  121.  
  122. BOOL IsAslOpen(void)
  123. {
  124.   return(AslBase!=NULL);
  125. }
  126.  
  127.  
  128.  
  129. BOOL GetAslFRQ(char *dir,char *file,char *res)
  130. {
  131.   BOOL ret = FALSE;
  132.  
  133.   if (AslBase) {
  134.     struct FileRequester *frq;
  135.     struct TagItem frq_tags[5];
  136.  
  137.     frq_tags[0].ti_Tag  = ASL_Hail;
  138.     frq_tags[0].ti_Data = (ULONG)"Select a file";
  139.     frq_tags[1].ti_Tag  = ASL_Window;
  140.     frq_tags[1].ti_Data = (ULONG)window;
  141.     frq_tags[2].ti_Tag  = ASL_Dir;
  142.     frq_tags[2].ti_Data = (ULONG)dir;
  143.     frq_tags[3].ti_Tag  = ASL_File;
  144.     frq_tags[3].ti_Data = (ULONG)file;
  145.     frq_tags[4].ti_Tag  = TAG_DONE;
  146.  
  147.     if (frq = AllocAslRequest(ASL_FileRequest,frq_tags)) {
  148.       if (ret=AslRequest(frq,NULL)) {
  149.         strcpy(res,frq->rf_Dir);
  150.         AddPart(res,frq->rf_File,256L);
  151. /*
  152.         strcpy(res,frq->rf_Dir);
  153.         if (strlen(res)>0) {
  154.           switch(res[strlen(res)-1]) {
  155.           case ':': break;
  156.           case '/': break;
  157.           default:
  158.             strcat(res,"/");
  159.             break;
  160.           }
  161.         }
  162.         strcat(res,frq->rf_File);
  163. */
  164.       }
  165.  
  166.       FreeAslRequest(frq);
  167.     }
  168.   }
  169.  
  170.   return(ret);
  171. }
  172.  
  173.  
  174. void SetConBorders(void)
  175. {
  176.   if (UseOwnScreen) {
  177.     if (IntuitionBase->lib_Version<36)
  178.       sprintf(TBuffer,"%c%d%c",0x9b,screen->BarHeight+3,0x79);
  179.     else
  180.       sprintf(TBuffer,"%c%d%c",0x9b,screen->BarHeight+4,0x79);
  181.   }
  182.   else {
  183.     if (IntuitionBase->lib_Version<36)
  184.       sprintf(TBuffer,"%c%d%c",0x9b,window->BorderTop,0x79);
  185.     else
  186.       sprintf(TBuffer,"%c%d%c%c%d%c",0x9b,window->BorderTop+1,0x79,0x9b,window->BorderLeft+1,0x78);
  187.   }
  188.   outline();
  189. }
  190.  
  191.  
  192.  
  193. void GetWinDimensions(int *width,int *height)
  194. {
  195.   char str[80];
  196.   int pos=0;
  197.  
  198.   /* get window height and width */
  199.  
  200.   putst("\033c");
  201.   SetConBorders();
  202.   sprintf(TBuffer,"%c%c%c%c",0x9b,0x30,0x20,0x71);
  203.   outline();
  204.   do {
  205.     str[pos++]=getch();
  206.   }
  207.   while (str[pos-1]!='r');
  208.   pos--;  /* 'r' */
  209.   pos--;  /* ' ' */
  210.   str[pos]='\0';
  211.  
  212. #define isnum(x) ((x)>='0' && (x)<='9')
  213.  
  214.   do {pos--;} while (isnum(str[pos]));
  215.   pos++;
  216.  
  217.   *width = atoi(&str[pos]);
  218.  
  219.   pos--;
  220.   str[pos]='\0';
  221.   do {pos--;} while (isnum(str[pos]));
  222.   pos++;
  223.  
  224.   *height = atoi(&str[pos]);
  225.  
  226.   /*printf("Dimensions: %dx%d chars\n",*width,*height);*/
  227. }
  228.  
  229.  
  230.  
  231. void SetMyFont(struct TextFont *myFont)
  232. {
  233.   SetFont(window->RPort,myFont);
  234.   putst("\033c");
  235.   SetConBorders();
  236. }
  237.  
  238.  
  239.  
  240. void SetStdFont(void)
  241. {
  242.   SetMyFont(textFontStd);
  243.   Win_Width  = Std_Win_Width;
  244.   Win_Height = Std_Win_Height;
  245. }
  246.  
  247.  
  248.  
  249. void SetPagerFont(void)
  250. {
  251.   SetMyFont(textFontPager);
  252.   Win_Width  = Pager_Win_Width;
  253.   Win_Height = Pager_Win_Height;
  254. }
  255.  
  256.  
  257.  
  258. BOOL open_intuition(void)
  259. {
  260.   struct NewScreen newscreen;
  261.   struct NewWindow newwindow;
  262.   UWORD pen_array = ~0;
  263.   WORD size_array[4];
  264.   struct TagItem screen_tags[4];
  265.   struct TagItem window_tags[2];
  266.   int i;
  267.   char *scanpnt;
  268.   long color;
  269.  
  270.   /* initialize */
  271.   screen_tags[0].ti_Tag  = SA_Pens;
  272.   screen_tags[0].ti_Data = (ULONG)&pen_array;
  273.   screen_tags[1].ti_Tag  = TAG_END;
  274.   screen_tags[1].ti_Data = NULL;
  275.   screen_tags[2].ti_Tag  = TAG_END;
  276.   screen_tags[2].ti_Data = NULL;
  277.   screen_tags[3].ti_Tag  = TAG_END;
  278.   screen_tags[3].ti_Data = NULL;
  279.  
  280.   if (!(IntuitionBase=OpenLibrary("intuition.library",33L))) {
  281.     fprintf(stderr,"elm: cannot open intuition.library\n");
  282.     close_intuition();
  283.     return(FALSE);
  284.   }
  285.  
  286.   if (!(GfxBase=OpenLibrary("graphics.library",33L))) {
  287.     fprintf(stderr,"elm: cannot open graphics.library\n");
  288.     close_intuition();
  289.     return(FALSE);
  290.   }
  291.  
  292.   if (!(DiskfontBase=OpenLibrary("diskfont.library",33L))) {
  293.     fprintf(stderr,"elm: cannot open diskfont.library\n");
  294.     close_intuition();
  295.     return(FALSE);
  296.   }
  297.  
  298.   if (!(AslBase=OpenLibrary("asl.library",37L))) {
  299.   /*
  300.     fprintf(stderr,"elm: cannot open asl.library (V37)\n");
  301.     close_intuition();
  302.     return(FALSE);
  303.   */
  304.   }
  305.  
  306. /*
  307.   textAttrIntui.ta_Name  = "topaz.font";
  308.   textAttrIntui.ta_YSize = 8;
  309.   textAttrIntui.ta_Style = FS_NORMAL;
  310.   textAttrIntui.ta_Flags = FPF_ROMFONT;
  311. */
  312.  
  313.   textAttrIntui.ta_Name  = IntuiFontName;
  314.   textAttrIntui.ta_YSize = IntuiFontSize;
  315.   textAttrIntui.ta_Style = NULL;
  316.   textAttrIntui.ta_Flags = NULL;
  317.  
  318.   if (!(textFontIntui=OpenDiskFont(&textAttrIntui))) {
  319.     fprintf(stderr,"elm: cannot open font %s/%d\n",IntuiFontName,IntuiFontSize);
  320.     textAttrIntui.ta_Name  = "topaz.font";
  321.     textAttrIntui.ta_YSize = 8;
  322.     textAttrIntui.ta_Style = FS_NORMAL;
  323.     textAttrIntui.ta_Flags = FPF_ROMFONT;
  324.   }
  325.  
  326.   textAttrStd.ta_Name  = StdFontName;
  327.   textAttrStd.ta_YSize = StdFontSize;
  328.   textAttrStd.ta_Style = NULL;
  329.   textAttrStd.ta_Flags = NULL;
  330.  
  331.   if (!(textFontStd=OpenDiskFont(&textAttrStd))) {
  332.     fprintf(stderr,"elm: cannot open font %s/%d\n",StdFontName,StdFontSize);
  333.     close_intuition();
  334.     return(FALSE);
  335.   }
  336.  
  337.   textAttrPager.ta_Name  = PagerFontName;
  338.   textAttrPager.ta_YSize = PagerFontSize;
  339.   textAttrPager.ta_Style = NULL;
  340.   textAttrPager.ta_Flags = NULL;
  341.  
  342.   if (!(textFontPager=OpenDiskFont(&textAttrPager))) {
  343.     fprintf(stderr,"elm: cannot open font %s/%d\n",PagerFontName,PagerFontSize);
  344.     close_intuition();
  345.     return(FALSE);
  346.   }
  347.  
  348.   if (UseOwnScreen) {
  349.     newscreen.LeftEdge     = 0;
  350.     newscreen.TopEdge      = 0;
  351.     newscreen.Width        = Screen_Width;
  352.     newscreen.Height       = Screen_Height;
  353.     newscreen.Depth        = 2;
  354.     newscreen.DetailPen    = AUTOFRONTPEN;
  355.     newscreen.BlockPen     = AUTOBACKPEN;
  356.     newscreen.Type         = CUSTOMSCREEN;
  357.     newscreen.Font         = &textAttrIntui;
  358.     newscreen.DefaultTitle = "Amiga Elm ⌐ 1991-1992 Andreas M. Kirchwitz (original Heiko W. Rupp)";
  359.     newscreen.Gadgets      = NULL;
  360.     newscreen.CustomBitMap = NULL;
  361.  
  362.     if (Screen_Interlace)
  363.       newscreen.ViewModes  = HIRES|LACE;
  364.     else
  365.       newscreen.ViewModes  = HIRES;
  366.  
  367.     if (IntuitionBase->lib_Version<36) {
  368.       struct Screen WBScreen;
  369.       if (GetScreenData(&WBScreen,sizeof(struct Screen),WBENCHSCREEN,NULL)) {
  370.         newscreen.ViewModes = WBScreen.ViewPort.Modes;
  371.         if (Screen_Interlace)
  372.           newscreen.ViewModes  |= LACE;
  373.         else
  374.           newscreen.ViewModes  &= (~LACE);
  375.       }
  376.  
  377.       if (!(screen=OpenScreen(&newscreen))) {
  378.         fprintf(stderr,"elm: cannot open screen\n");
  379.         close_intuition();
  380.         return(FALSE);
  381.       }
  382.     }
  383.     else {
  384.       long WBModeID;
  385.       struct Screen *WBScreen;
  386.       if (WBScreen=LockPubScreen("Workbench")) {
  387.         WBModeID=GetVPModeID(&WBScreen->ViewPort);
  388.         UnlockPubScreen(NULL,WBScreen);
  389.         screen_tags[1].ti_Tag  = SA_DisplayID;
  390.         screen_tags[1].ti_Data = WBModeID;
  391.         if (Screen_Interlace) {
  392.           screen_tags[1].ti_Data |= 4L;
  393.         }
  394.         else {
  395.           screen_tags[1].ti_Data &= (~4L);
  396.         }
  397.         if (PubScreenName) {
  398.           screen_tags[2].ti_Tag  = SA_PubName;
  399.           screen_tags[2].ti_Data = (ULONG)PubScreenName;
  400.         }
  401.       }
  402.  
  403.       if (!(screen=OpenScreenTagList(&newscreen,screen_tags))) {
  404.         fprintf(stderr,"elm: cannot open screen\n");
  405.         fprintf(stderr,"(subliminal hint: public screen name not unique)\n");
  406.         close_intuition();
  407.         return(FALSE);
  408.       }
  409.  
  410.       PublicModes = SetPubScreenModes(0);
  411.       SetPubScreenModes(PublicModes);
  412.  
  413.       /* Are we to make our screen public? */
  414.  
  415.       if (PubScreenName)
  416.         PubScreenStatus(screen,NULL);
  417. /*
  418.       else
  419.         PubScreenStatus(screen,PSNF_PRIVATE);
  420. */
  421.  
  422.       /* Are we to `shanghai' Workbench windows? */
  423.  
  424.       if (ShanghaiWindows) {
  425.         PublicModes |= SHANGHAI;
  426.         SetPubScreenModes(PublicModes);
  427.         /* Make this the default public screen. */
  428.         SetDefaultPubScreen(PubScreenName);
  429.       }
  430.       else {
  431.         char DefaultPubScreen[MAXPUBSCREENNAME+1];
  432.         PublicModes &= ~SHANGHAI;
  433.         GetDefaultPubScreen(DefaultPubScreen);
  434.         if (LockPubScreen(DefaultPubScreen)) {
  435.           SetDefaultPubScreen(DefaultPubScreen);
  436.           UnlockPubScreen(DefaultPubScreen,NULL);
  437.         }
  438.         else
  439.           SetDefaultPubScreen(NULL);
  440.         SetPubScreenModes(PublicModes);
  441.       }
  442.     }
  443.  
  444.     if (scanpnt=ColorData) {
  445.       for(i=0;i<(1L<<newscreen.Depth);i++) {
  446.         color = strtol(scanpnt,&scanpnt,0);
  447.         SetRGB4(&(screen->ViewPort),i,(color >> 8) & 0xf, (color >> 4) & 0xf, color & 0xf);
  448.       }
  449.     }
  450.  
  451.     /* test output for OS 1.3 -vs- OS 2.0 */
  452.  
  453.     /*
  454.     printf("BarHeight: %d, BarVBorder: %d, BarHBorder: %d\n",
  455.         screen->BarHeight,screen->BarVBorder,screen->BarHBorder);
  456.     printf("MenuVBorder: %d, MenuHBorder: %d\n",
  457.         screen->MenuVBorder,screen->MenuHBorder);
  458.     printf("WBorTop: %d, WBorLeft: %d, WBorRight: %d, WBorBottom: %d\n",
  459.         screen->WBorTop,screen->WBorLeft,screen->WBorRight,screen->WBorBottom);
  460.     */
  461.  
  462.     /* end of test output */
  463.   }
  464.  
  465.   newwindow.LeftEdge     = 0;
  466.   newwindow.TopEdge      = 0;
  467.   newwindow.DetailPen    = AUTOFRONTPEN;
  468.   newwindow.BlockPen     = AUTOBACKPEN;
  469.   newwindow.IDCMPFlags   = NULL;
  470.   newwindow.FirstGadget  = NULL;
  471.   newwindow.CheckMark    = NULL;
  472.   newwindow.BitMap       = NULL;
  473.  
  474.   if (UseOwnScreen) {
  475.     newwindow.Width      = screen->Width;
  476.     newwindow.Height     = screen->Height;
  477.     newwindow.Flags      = WFLG_BACKDROP|WFLG_BORDERLESS|WFLG_RMBTRAP|WFLG_NOCAREREFRESH|WFLG_ACTIVATE;
  478.     newwindow.Screen     = screen;
  479.     newwindow.Type       = CUSTOMSCREEN;
  480.     newwindow.MinWidth   = screen->Width;
  481.     newwindow.MinHeight  = screen->Height;
  482.     newwindow.MaxWidth   = screen->Width;
  483.     newwindow.MaxHeight  = screen->Height;
  484.     newwindow.Title      = NULL;
  485.   }
  486.   else {
  487.     newwindow.Width      = Screen_Width;
  488.     newwindow.Height     = Screen_Height;
  489.     if (IntuitionBase->lib_Version<36)
  490.       newwindow.Flags    = WFLG_RMBTRAP|WFLG_NOCAREREFRESH|WFLG_ACTIVATE|WFLG_DRAGBAR|WFLG_DEPTHGADGET;
  491.     else
  492.       newwindow.Flags    = WFLG_RMBTRAP|WFLG_ACTIVATE|WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_SIMPLE_REFRESH;
  493. /*      newwindow.Flags    = WFLG_RMBTRAP|WFLG_NOCAREREFRESH|WFLG_ACTIVATE|WFLG_DRAGBAR|WFLG_DEPTHGADGET;*/
  494. /*      newwindow.Flags    = WFLG_RMBTRAP|WFLG_NOCAREREFRESH|WFLG_ACTIVATE|WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_SMART_REFRESH;*/
  495. /*      newwindow.Flags    = WFLG_RMBTRAP|WFLG_NOCAREREFRESH|WFLG_ACTIVATE|WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_SIMPLE_REFRESH;*/
  496. /*
  497.     bugs:
  498.         1. Setzt man WFLG_SIMPLE_REFRESH, geht der Inhalt verloren,
  499.            wenn das Elm-Window von anderen Windows ueberlagert wird.
  500.            (aber ohne dieses Flag geht console.device, unit 1 nicht)
  501.         2. Setzt man WFLG_SIMPLE_REFRESH und benutzt das "Zoom"-Gadget,
  502.            wird beim zweiten Anklicken der Inhalt des Windows falsch
  503.            wiederhergestellt.  Anfaengliche Leerzeilen werden geloescht,
  504.            der Windowaufbau ist also ggf. verschoben um eine oder mehr
  505.            Zeilen.  (somit voellig unbrauchbar)
  506. */
  507.     newwindow.Screen     = NULL;
  508.     newwindow.Type       = WBENCHSCREEN;
  509.     newwindow.MinWidth   = Screen_Width;
  510.     newwindow.MinHeight  = Screen_Height;
  511.     newwindow.MaxWidth   = Screen_Width;
  512.     newwindow.MaxHeight  = Screen_Height;
  513.     newwindow.Title      = "Amiga Elm ⌐ 1991-1992 Andreas M. Kirchwitz (original Heiko W. Rupp)";
  514.   }
  515.  
  516.   if (IntuitionBase->lib_Version<36) {
  517.     if (!(window=OpenWindow(&newwindow))) {
  518.       fprintf(stderr,"elm: cannot open window\n");
  519.       close_intuition();
  520.       return(FALSE);
  521.     }
  522.   }
  523.   else {
  524.     struct Screen *WBScreen;
  525.     if (WBScreen=LockPubScreen("Workbench")) {
  526.       size_array[3] = WBScreen->BarHeight+1;
  527.       UnlockPubScreen(NULL,WBScreen);
  528.     }
  529.     else {
  530.       fprintf(stderr,"strange: cannot examine Workbench screen\n");
  531.       size_array[3] = 13;
  532.     }
  533.     /* dimensions of zoomed window */
  534.     size_array[0] = 0;
  535.     size_array[1] = 0;
  536.     size_array[2] = newwindow.Width;
  537.  
  538.     window_tags[0].ti_Tag  = WA_Zoom;
  539.     window_tags[0].ti_Data = (ULONG)&size_array;
  540.     window_tags[1].ti_Tag  = TAG_END;
  541.     window_tags[1].ti_Data = NULL;
  542.     if (!(window=OpenWindowTagList(&newwindow,window_tags))) {
  543.       fprintf(stderr,"elm: cannot open window\n");
  544.       close_intuition();
  545.       return(FALSE);
  546.     }
  547.   }
  548.  
  549.   process = (struct Process *) FindTask(NULL);
  550.   wintmp = process->pr_WindowPtr;
  551.   process->pr_WindowPtr = (APTR) window;
  552.   oldTextFont = window->RPort->Font;
  553.   SetFont(window->RPort,textFontStd);
  554.  
  555.   if (!(writeport=CreatePort("Elm-Console.write",0L))) {
  556.     fprintf(stderr,"elm: cannot open console write port\n");
  557.     close_intuition();
  558.     return(FALSE);
  559.   }
  560.  
  561.   if (!(readport=CreatePort("Elm-Console.read",0L))) {
  562.     fprintf(stderr,"elm: cannot open console read port\n");
  563.     close_intuition();
  564.     return(FALSE);
  565.   }
  566.  
  567.   if (!(writereq=CreateExtIO(writeport,(long)sizeof(struct IOStdReq)))) {
  568.     fprintf(stderr,"elm: cannot open console write request\n");
  569.     close_intuition();
  570.     return(FALSE);
  571.   }
  572.  
  573.   if (!(readreq=CreateExtIO(readport,(long)sizeof(struct IOStdReq)))) {
  574.     fprintf(stderr,"elm: cannot open console read request\n");
  575.     close_intuition();
  576.     return(FALSE);
  577.   }
  578.  
  579.   writereq->io_Data = (APTR)window;
  580.   writereq->io_Length = sizeof(struct Window);
  581.  
  582.   if (IntuitionBase->lib_Version>=36 && !UseOwnScreen) {
  583.     if (OpenDevice("console.device",1L,writereq,0L)) {
  584.       fprintf(stderr,"elm: cannot open console.device\n");
  585.       close_intuition();
  586.       clean_exit(20);
  587.     }
  588.   }
  589.   else {
  590.     if (OpenDevice("console.device",0L,writereq,0L)) {
  591.       fprintf(stderr,"elm: cannot open console.device\n");
  592.       close_intuition();
  593.       clean_exit(20);
  594.     }
  595.   }
  596.   consoleOpen = TRUE;
  597.  
  598.   readreq->io_Device = writereq->io_Device;
  599.   readreq->io_Unit   = writereq->io_Unit;
  600.  
  601.   GetWinDimensions(&Std_Win_Width,&Std_Win_Height);
  602.   SetFont(window->RPort,textFontPager);
  603.   GetWinDimensions(&Pager_Win_Width,&Pager_Win_Height);
  604.  
  605.   if (Std_Win_Width<80 || Std_Win_Height<20) {
  606.     fprintf(stderr,"elm: minimum standard display is 80x20 characters (you have %dx%d)\n",Std_Win_Width,Std_Win_Height);
  607.     close_intuition();
  608.     return(FALSE);
  609.   }
  610.  
  611.   if (Pager_Win_Width<80 || Pager_Win_Height<20) {
  612.     fprintf(stderr,"elm: minimum pager display is 80x20 characters (you have %dx%d)\n",Pager_Win_Width,Pager_Win_Height);
  613.     close_intuition();
  614.     return(FALSE);
  615.   }
  616.  
  617.   SetStdFont();
  618.  
  619.   return(TRUE);
  620. }
  621.  
  622.  
  623.  
  624. BOOL intui_interface(void)
  625. {
  626.   return(window!=NULL);
  627. }
  628.  
  629.  
  630.  
  631. void SetScreenTitle(char *text)
  632. {
  633.   strcpy(WinTitleBuffer,text);
  634.   if (UseOwnScreen) {
  635.     SetWindowTitles(window,(UBYTE *)~0,WinTitleBuffer);
  636.     ShowTitle(screen,TRUE);
  637.   }
  638.   else
  639.     SetWindowTitles(window,WinTitleBuffer,(UBYTE *)~0);
  640. }
  641.  
  642.  
  643.  
  644. void ElmToFront(void)
  645. {
  646.   if (UseOwnScreen)
  647.     ScreenToFront(screen);
  648.   else
  649.     WBenchToFront();
  650.     
  651.   ActivateWindow(window);
  652. }
  653.  
  654.  
  655.  
  656. void WorkbenchToFront(void)
  657. {
  658.   if (WBToFront)
  659.     WBenchToFront();
  660. }
  661.  
  662.  
  663.  
  664. void flush_input(void)
  665. {
  666.   if (UseCmdClearKludge && IntuitionBase->lib_Version>=36) {
  667.     char *pnt;
  668.     pnt = (char *)readreq->io_Unit;
  669.     *(long *)((char *)pnt + 374) = 0L;
  670.   }
  671.   else {
  672.     readreq->io_Command = CMD_CLEAR;
  673.     DoIO(readreq);
  674.   }
  675. }
  676.  
  677.  
  678.  
  679. char getch(void)
  680. {
  681.   char ch = '\0';
  682.   readreq->io_Command = CMD_READ;
  683.   readreq->io_Data    = (APTR)&ch;
  684.   readreq->io_Length  = 1;
  685.   DoIO(readreq);
  686.   return(ch);
  687. }
  688.  
  689.  
  690.  
  691. long getst(char *buffer,long limit)
  692. {
  693.   register long len = 0L;
  694.   char ch;
  695.  
  696.   while ((ch=getch())!='\r') {
  697.     if (ch=='\b') {
  698.       if (len>0) {
  699.         putst("\b \b");
  700.         len--;
  701.       }
  702.     }
  703.     else if (ch==0x1b || ch==0x9b) {
  704.     }
  705.     else {
  706.       if (len<limit)
  707.         putch( buffer[len++] = ch );
  708.     }
  709.   }
  710.  
  711.   buffer[len] = '\0';
  712.  
  713.   return(len);
  714. }
  715.  
  716.  
  717.  
  718. void putch(char ch)
  719. {
  720.   writereq->io_Command = CMD_WRITE;
  721.   writereq->io_Data    = (APTR)&ch;
  722.   writereq->io_Length  = 1;
  723.   DoIO(writereq);
  724. }
  725.  
  726.  
  727.  
  728. void putst(char *buffer)
  729. {
  730.   writereq->io_Command = CMD_WRITE;
  731.   writereq->io_Data    = (APTR)buffer;
  732.   writereq->io_Length  = strlen(buffer);
  733.   DoIO(writereq);
  734. }
  735.  
  736.  
  737.  
  738. void putst_center(char *buffer)
  739. {
  740.   int i,pos;
  741.   char *cbuf;
  742.  
  743.   if (cbuf=malloc(Win_Width+1)) {   /* null terminator */
  744.     for(pos=0,i=strlen(buffer)+1;i<Win_Width;i+=2,pos++)
  745.       cbuf[pos] = ' ';
  746.     cbuf[pos] = '\0';
  747.  
  748.     writereq->io_Command = CMD_WRITE;
  749.     writereq->io_Data    = (APTR)cbuf;
  750.     writereq->io_Length  = strlen(cbuf);
  751.     DoIO(writereq);
  752.     free(cbuf);
  753.   }
  754.  
  755.   writereq->io_Command = CMD_WRITE;
  756.   writereq->io_Data    = (APTR)buffer;
  757.   writereq->io_Length  = strlen(buffer);
  758.   DoIO(writereq);
  759. }
  760.  
  761.  
  762.  
  763. void putst_center_bold(char *buffer)
  764. {
  765.   int i,pos;
  766.   char *cbuf;
  767.  
  768.   if (cbuf=malloc(Win_Width+5)) {   /* null terminator + bold face on */
  769.     for(pos=0,i=strlen(buffer)+1;i<Win_Width;i+=2,pos++)
  770.       cbuf[pos] = ' ';
  771.     cbuf[pos] = '\0';
  772.     strcat(cbuf,"\033[1m");
  773.  
  774.     writereq->io_Command = CMD_WRITE;
  775.     writereq->io_Data    = (APTR)cbuf;
  776.     writereq->io_Length  = strlen(cbuf);
  777.     DoIO(writereq);
  778.     free(cbuf);
  779.   }
  780.  
  781.   writereq->io_Command = CMD_WRITE;
  782.   writereq->io_Data    = (APTR)buffer;
  783.   writereq->io_Length  = strlen(buffer);
  784.   DoIO(writereq);
  785.  
  786.   putst("\033[m");
  787. }
  788.  
  789.  
  790.  
  791. void putstl(char *buffer,long length)
  792. {
  793.   writereq->io_Command = CMD_WRITE;
  794.   writereq->io_Data    = (APTR)buffer;
  795.   writereq->io_Length  = length;
  796.   DoIO(writereq);
  797. }
  798.  
  799.  
  800.  
  801. void open_shell(void)
  802. {
  803.   struct Window *sh_win;
  804.   struct TagItem window_tags[2];
  805.   struct NewWindow newwindow;
  806.   WORD size_array[4];
  807.   char exec[256];
  808.   int magic_offset = 20;
  809.  
  810.   shell_count++;
  811.  
  812.   newwindow.LeftEdge    = magic_offset;
  813.   newwindow.TopEdge     = (shell_count%2)?(magic_offset):(screen->Height/2+magic_offset/2);
  814.   newwindow.DetailPen   = AUTOFRONTPEN;
  815.   newwindow.BlockPen    = AUTOBACKPEN;
  816.   newwindow.IDCMPFlags  = NULL;
  817.   newwindow.FirstGadget = NULL;
  818.   newwindow.CheckMark   = NULL;
  819.   newwindow.BitMap      = NULL;
  820.   newwindow.Width       = screen->Width-2*magic_offset;
  821.   newwindow.Height      = screen->Height/2-2*magic_offset+magic_offset/2;
  822.   newwindow.Flags       = WFLG_NOCAREREFRESH|WFLG_ACTIVATE|WFLG_SIZEGADGET|
  823.                           WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|
  824.                           WFLG_SIZEBBOTTOM;
  825.   newwindow.Screen      = screen;
  826.   newwindow.Type        = CUSTOMSCREEN;
  827.   newwindow.MinWidth    = screen->Width/2;
  828.   newwindow.MinHeight   = /* 2* */(screen->BarHeight+1);
  829.   newwindow.MaxWidth    = screen->Width;
  830.   newwindow.MaxHeight   = screen->Height;
  831.   newwindow.Title       = NULL;
  832.  
  833.   /* dimensions of zoomed window */
  834.   size_array[0] = (shell_count%2)?(0):(screen->Width/2);
  835.   size_array[1] = screen->BarHeight+1;
  836.   size_array[2] = screen->Width/2;
  837.   size_array[3] = screen->BarHeight+1;
  838.  
  839.   window_tags[0].ti_Tag  = WA_Zoom;
  840.   window_tags[0].ti_Data = (ULONG)&size_array;
  841.   window_tags[1].ti_Tag  = TAG_END;
  842.   window_tags[1].ti_Data = NULL;
  843.  
  844.   if (IntuitionBase->lib_Version>=36 && screen) {
  845.     if (sh_win=OpenWindowTagList(&newwindow,window_tags)) {
  846.       sprintf(exec,"newshell \"con:////AmigaElm Shell/WINDOW0x%lx\"",sh_win);
  847.       Execute(exec,NULL,NULL);
  848.     }
  849.   }
  850. }
  851.  
  852.