home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / xmmx.zip / source / mmm_x11.cpp < prev    next >
C/C++ Source or Header  |  1996-12-22  |  15KB  |  497 lines

  1. /**********************************************
  2. *   Linux X11 CPP file for MMMX Editor        *
  3. *   Contains X11    specific functions        *
  4. *   (Tested on i386 RedHat Linux 3.0+ X11R6)  *
  5. ***********************************************/
  6. #include "mmm_x11.h"
  7.  
  8. //&<<&void SetSignalHandler(){
  9. //&<<&void Terminate(int a) {
  10. void Terminate(int a) {
  11.    int r;
  12.    puts("Now In Terminate(...)\n");
  13.    _exit(-1);
  14. }
  15. //&>>&2 0 2 0
  16. void SetSignalHandler(){
  17.     signal(SIGSEGV ,Terminate );
  18.     signal(SIGKILL ,Terminate );
  19.     signal(SIGABRT ,Terminate );
  20.     signal(SIGTERM ,Terminate );
  21.     signal(SIGQUIT ,Terminate );
  22.     signal(SIGALRM ,Terminate );
  23.     signal(SIGHUP  ,Terminate );
  24.     signal(SIGINT  ,Terminate );
  25.     }
  26. //&>>&9 0 9 0
  27. //&<<&void XMemTest(int i,char *msg){
  28. void XMemTest(int i,char *msg){
  29.     if(i)return;
  30.     puts(msg);
  31.     exit(-1);
  32.     }
  33. //&>>&2 0 2 0
  34. //&<<&static void SetWindowManagerHints(...)
  35. static void SetWindowManagerHints(
  36.                     Display     *prDisplay,
  37.                     char        *psPrgClass,
  38.                     char        *argv[],
  39.                     int         argc,
  40.                     Window      nWnd,
  41.                     int         x,
  42.                     int         y,
  43.                     int         nWidth,
  44.                     int         nHeight,
  45.                     int         nMinWidth,
  46.                     int         nMinHeight,
  47.                     char        *psTitle,
  48.                     char        *psIconTitle,
  49.                     Pixmap      nIconPixmap
  50.                     )
  51.     {
  52.     XSizeHints      rSizeHints;
  53.     XWMHints        rWMHints;
  54.     XClassHint      rClassHint;
  55.     XTextProperty   prWindowName, prIconName;
  56.  
  57.     XMemTest( XStringListToTextProperty(&psTitle, 1, &prWindowName ) &&
  58.               XStringListToTextProperty(&psIconTitle, 1, &prIconName ) ,
  59.            "UE: No Memory!" );
  60.  
  61.     rSizeHints.flags=USPosition | USSize | PMinSize;
  62.     rSizeHints.min_width  = nMinWidth;
  63.     rSizeHints.min_height = nMinHeight;
  64.     rSizeHints.x=x;
  65.     rSizeHints.y=y;
  66.  
  67.     rWMHints.flags = StateHint | IconPixmapHint | InputHint;
  68.     rWMHints.initial_state = NormalState;
  69.     rWMHints.input = True;
  70.     rWMHints.icon_pixmap = nIconPixmap;
  71.  
  72.     rClassHint.res_name = argv[0];
  73.     rClassHint.res_class = psPrgClass;
  74.  
  75.     XSetWMProperties (prDisplay, nWnd, &prWindowName, &prIconName,
  76.         argv, argc, &rSizeHints, &rWMHints, &rClassHint );
  77.  
  78.     }
  79.  
  80. //&>>&1A 0 11 24
  81.  
  82. //&<<&mmm_Screen::mmm_Screen(){}
  83. mmm_Screen::mmm_Screen(){
  84. //&<<&    #ifdef _ENABLE_LOGGING_
  85.     #ifdef _ENABLE_LOGGING_
  86.         DoLog("mmm_Screen::mmm_Screen(){}\n");
  87.     #endif
  88. //&>>&1 0 1 0
  89.     SetSignalHandler();
  90. //&<<&    prDisplay=XOpenDisplay(NULL);
  91.     prDisplay=XOpenDisplay(NULL);
  92.     XMemTest(prDisplay!=NULL,"cannot open display");
  93. //&>>&1 0 1 5
  94.     nScreenNum=DefaultScreen(prDisplay);
  95.     nWnd=XCreateSimpleWindow( prDisplay,
  96.                 RootWindow(prDisplay,nScreenNum),
  97.                 WND_X, WND_Y, WND_WDT, WND_HGH, WND_BORDER_WDT,
  98.                 WhitePixel(prDisplay,nScreenNum),
  99.                 BlackPixel(prDisplay,nScreenNum) );
  100. }
  101. //&>>&4 0 4 0
  102. //&<<&void mmm_Screen::InitColorMap(){
  103. //&<<&long mmm_Init_ClrVal[0x10]={
  104. long mmm_Init_ClrVal[0x10]={
  105.      0x000000L,       /* _BLACK        , */
  106.      0x2a0000L,       /* _BLUE         , */
  107.      0x002a00L,       /* _GREEN        , */
  108.      0x2a2a00L,       /* _CYAN         , */
  109.      0x00002aL,       /* _RED          , */
  110.      0x2a002aL,       /* _MAGENTA      , */
  111.      0x00152aL,       /* _BROWN        , */
  112.      0x2a2a2aL,       /* _WHITE        , */
  113.      0x151515L,       /* _GRAY         , */
  114.      0x3F1515L,       /* _LIGHTBLUE    , */
  115.      0x153f15L,       /* _LIGHTGREEN   , */
  116.      0x3f3f15L,       /* _LIGHTCYAN    , */
  117.      0x15153fL,       /* _LIGHTRED     , */
  118.      0x3f153fL,       /* _LIGHTMAGENTA , */
  119.      0x153f3fL,       /* _YELLOW       , */
  120.      0x3f3f3fL        /* _BRIGHTWHITE    */
  121.     };
  122. //&>>&1 0 1 7
  123.  
  124. void mmm_Screen::InitColorMap(){
  125.     XColor rColor;
  126.     int i;
  127.  
  128.     for(i=0;i<0x10;i++){
  129.         rColor.red   = mmm_Init_ClrVal[i]       & 0xff;
  130.         rColor.green = (mmm_Init_ClrVal[i]>>8)  & 0xff;
  131.         rColor.blue  = (mmm_Init_ClrVal[i]>>16) & 0xff;
  132.         rColor.flags=DoRed | DoGreen | DoBlue ;
  133.         XAllocColor( prDisplay, DefaultColormap(prDisplay,nScreenNum) ,
  134.                     &rColor );
  135.  
  136.         nPixel[i]=rColor.pixel;
  137.         }
  138.     }
  139. //&>>&B 0 B 0
  140. //&<<&void mmm_Screen::Init(int argc, char **argv);
  141. void mmm_Screen::Init(int argc, char **argv){
  142.  
  143.     SetWindowManagerHints(prDisplay, PRG_CLASS, argv,argc, nWnd,
  144.                 WND_X, WND_Y, WND_WDT, WND_HGH, WND_MIN_WDT, WND_MIN_HGH,
  145.                 WND_TITLE, WND_ICON_TITLE, 0 );
  146.     XMapWindow(prDisplay, nWnd);
  147.  
  148. //&<<&    prFontInfo=XLoadQueryFont( prDisplay, "-*-lucidatypewriter-*-17-*-m-*-" );
  149. //    prFontInfo=XLoadQueryFont( prDisplay, "*-*--17-*-m-*" );
  150.     prFontInfo=XLoadQueryFont( prDisplay, "*-lucidatypewriter-*-14-*-m-*" );
  151. //    prFontInfo=XLoadQueryFont( prDisplay, "*--10-*-m-*" );
  152.     XMemTest(prFontInfo!=0,"UE: cannot XLoadQueryFont");
  153.  
  154.     FontHeight=prFontInfo->max_bounds.ascent + prFontInfo->max_bounds.descent;
  155.     FontWidth=prFontInfo->max_bounds.width;
  156.     FontDescent=prFontInfo->max_bounds.descent+1;
  157. //&>>&1 0 1 0
  158.  
  159. //    InitColorMap();
  160.  
  161.     ReadWindowParameters();
  162.  
  163.     XSelectInput(prDisplay, nWnd, ExposureMask | KeyPressMask );
  164.     }
  165. //&>>&3 0 3 0
  166. //&<<&mmm_Screen::~mmm_Screen(){
  167. mmm_Screen::~mmm_Screen(){
  168. //&<<&    #ifdef _ENABLE_LOGGING_
  169.     #ifdef _ENABLE_LOGGING_
  170.         DoLog("mmm_Screen::~mmm_Screen(){\n");
  171.     #endif
  172. //&>>&3 0 3 4
  173.     //if(prDisplay!=NULL)XCloseDisplay(prDisplay); // I don't understand why, but it causes SIGSEGV
  174. //&<<&    #ifdef _ENABLE_LOGGING_
  175.     #ifdef _ENABLE_LOGGING_
  176.         DoLog("    }\n");
  177.     #endif
  178. //&>>&0 0 0 0
  179.     }
  180. //&>>&0 0 0 0
  181. //&<<&void mmm_Screen::ReadWindowParameters(){
  182. void mmm_Screen::ReadWindowParameters(){
  183.     XWindowAttributes rXWindowAttributes;
  184.  
  185.     XGetWindowAttributes(prDisplay,
  186.                             nWnd,
  187.                             &rXWindowAttributes);
  188.  
  189.     WIDTH  = rXWindowAttributes.width  / FontWidth  ;
  190.     HEIGHT = rXWindowAttributes.height / FontHeight ;
  191.     }
  192. //&>>&7 0 7 C
  193. //&<<&void mmm_Screen::SetCursor(int x,int y,int attr){
  194. void mmm_Screen::SetCursor(int x,int y,int attr){
  195.     GC      prGC;
  196.     prGC=XCreateGC(prDisplay, nWnd, 0, NULL );
  197.  
  198.     cursx=x;
  199.     cursy=y;
  200.  
  201.     char pccr[2];
  202.     pccr[0]=cur->GetCh(); pccr[1]=0;
  203.     cold=pccr[0];
  204.  
  205.     cattrold=cur->pos->GetTextAttr();
  206.  
  207.     XSetForeground(prDisplay, prGC, (attr>>4)&0xf );
  208.     XFillRectangle(prDisplay, nWnd, prGC,
  209.         FontWidth*cursx, FontHeight*cursy+FontDescent ,
  210.         FontWidth -1 , FontHeight -1 );
  211.  
  212.     XSetForeground(prDisplay, prGC, attr&0xf );
  213.     XSetFont(prDisplay, prGC, prFontInfo->fid );
  214.     XDrawString( prDisplay, nWnd, prGC, FontWidth*cursx, FontHeight + FontHeight*cursy ,
  215.                 pccr, 1 );
  216.  
  217.     XFreeGC(prDisplay, prGC);
  218.     }
  219. //&>>&10 0 10 13
  220. //&<<&void mmm_Screen::ClrCursor();
  221. void mmm_Screen::ClrCursor(){
  222.     if(cursx<0 || cursy<0) return;
  223.     GC      prGC;
  224.     prGC=XCreateGC(prDisplay, nWnd, 0, NULL );
  225.  
  226.     char pccr[2];
  227.     pccr[0]=cold; pccr[1]=0;
  228.  
  229.     XSetForeground(prDisplay, prGC, (cattrold>>4)&0xf );
  230.     XFillRectangle(prDisplay, nWnd, prGC,
  231.         FontWidth*cursx, FontHeight*cursy+FontDescent ,
  232.         FontWidth -1 , FontHeight -1 );
  233.  
  234.     XSetForeground(prDisplay, prGC, cattrold&0xf );
  235.     XSetFont(prDisplay, prGC, prFontInfo->fid );
  236.     XDrawString( prDisplay, nWnd, prGC, FontWidth*cursx, FontHeight + FontHeight*cursy ,
  237.                 pccr, 1 );
  238.  
  239.     cursx=cursy=-1;
  240.     XFreeGC(prDisplay, prGC);
  241.     }
  242. //&>>&B 0 B 25
  243. //&<<&void SetTitle(char *dirn,char *name){
  244. void SetTitle(char *dirn,char *name){
  245.   XTextProperty prWindowName ;
  246.   char tt[0x200];
  247.   strcpy(tt,"mmmX : ");
  248.   strcat(tt,name);
  249.   strcat(tt,"  in  ");
  250.   strcat(tt,dirn);
  251.   char *tt1=tt;
  252.  
  253.   XMemTest(XStringListToTextProperty(&tt1, 1, &prWindowName )!=0,"Memory error");
  254.  
  255.   XSetWMName(mmm_screen.prDisplay, mmm_screen.nWnd, &prWindowName );
  256.   XSetWMIconName(mmm_screen.prDisplay, mmm_screen.nWnd, &prWindowName );
  257. }
  258. //&>>&5 0 5 8
  259.  
  260. //&<<&void DrawLine(char *Str, int PosY, char Attr,int slen){
  261. void DrawLine(char *Str, int PosY, char Attr,int slen){
  262.     GC      prGC;
  263.  
  264.     prGC=XCreateGC(mmm_screen.prDisplay, mmm_screen.nWnd, 0, NULL );
  265.  
  266.     XSetForeground(mmm_screen.prDisplay, prGC, (Attr>>4)&0xf );
  267.     XFillRectangle(mmm_screen.prDisplay, mmm_screen.nWnd, prGC,
  268.         0 ,mmm_screen.FontHeight*PosY+mmm_screen.FontDescent+1,
  269.         mmm_screen.WIDTH*mmm_screen.FontWidth,mmm_screen.FontHeight);
  270.  
  271.     XSetForeground(mmm_screen.prDisplay, prGC, Attr&0xf );
  272.     XSetFont(mmm_screen.prDisplay, prGC, mmm_screen.prFontInfo->fid );
  273.  
  274.     XDrawString( mmm_screen.prDisplay, mmm_screen.nWnd, prGC, 0, mmm_screen.FontHeight*(PosY+1) ,
  275.                 Str, (strlen(Str)<mmm_screen.WIDTH)?strlen(Str):mmm_screen.WIDTH );
  276.  
  277.     XFreeGC(mmm_screen.prDisplay, prGC);
  278.     }
  279.  
  280. //&>>&1 0 1 47
  281.  
  282. //&<<&mmm_KeyBoard::mmm_KeyBoard();
  283. mmm_KeyBoard::mmm_KeyBoard(){
  284. //&<<&    #ifdef _ENABLE_LOGGING_
  285.     #ifdef _ENABLE_LOGGING_
  286.         DoLog("mmm_KeyBoard::mmm_KeyBoard(){\n");
  287.     #endif
  288. //&>>&1 0 1 29
  289.     InitKeyboardLayout();
  290.     AltCase=InitDefaultAltCase();
  291. //&<<&    #ifdef _ENABLE_LOGGING_
  292.     #ifdef _ENABLE_LOGGING_
  293.         DoLog("    }\n");
  294.     #endif
  295. //&>>&1 0 1 0
  296.     }
  297. //&>>&2 0 2 24
  298. //&<<&mmm_KeyBoard::~mmm_KeyBoard();
  299. mmm_KeyBoard::~mmm_KeyBoard(){
  300. //&<<&    #ifdef _ENABLE_LOGGING_
  301.     #ifdef _ENABLE_LOGGING_
  302.         DoLog("mmm_KeyBoard::~mmm_KeyBoard(){}\n");
  303.     #endif
  304. //&>>&1 0 1 2E
  305. }
  306. //&>>&1 0 1 4
  307. //&<<&void mmm_KeyBoard::InitKeyboardLayout();
  308. void mmm_KeyBoard::InitKeyboardLayout(){
  309.     int i,j;
  310.     for(i=0;i<4;i++) for(j=0;j<128;j++) keys[i][j]='.';
  311.     }
  312. //&>>&2 0 2 21
  313. //&<<&char *mmm_KeyBoard::InitDefaultAltCase(){
  314. char *mmm_KeyBoard::InitDefaultAltCase(){
  315.   char *AltCase=new char[256];
  316.   for(int i=0;i<256;i++){
  317.     AltCase[i]=
  318.       i>='A' && i<='Z' ? i+('a'-'A') :
  319.       i>='a' && i<='z' ? i-('a'-'A') :
  320.       i;
  321.   }
  322.   return AltCase;
  323. }
  324. //&>>&6 0 6 0
  325. //&<<&void mmm_KeyBoard::SetACode(int n){ 
  326. void mmm_KeyBoard::SetACode(int n){
  327.     acode=mmm_KBD.keys[n];
  328.     }
  329. //&>>&0 0 0 16
  330. //&<<&int mmm_KeyBoard::GetScan(){
  331. int mmm_KeyBoard::GetScan(){
  332.     XEvent  rEvent;
  333.     while(1){
  334.         if( XEventsQueued(mmm_screen.prDisplay, QueuedAfterFlush)==0) View();
  335.                                 // screen is redrawed only if there is
  336.                                 // no events to be processed.
  337.         XNextEvent(mmm_screen.prDisplay, &rEvent);
  338.         switch(rEvent.type){
  339.             case Expose:     // Resize, Move, Min, Max ...
  340.                 mmm_screen.ReadWindowParameters();
  341.                 Redr();
  342.                 break;
  343.             case KeyPress:
  344.                 mmm_f_Shift=(rEvent.xkey.state&ShiftMask)     ?1:0;
  345.                 mmm_f_Alt  =(rEvent.xkey.state&AltMask)       ?1:0;
  346.                 mmm_f_Ctrl =(rEvent.xkey.state&ControlMask)   ?1:0;
  347.                 mmm_f_Caps =(rEvent.xkey.state&LockMask)      ?1:0;
  348.  
  349.                 return rEvent.xkey.keycode;
  350.  
  351.             }
  352.         }
  353.     }
  354. //&>>&0 0 0 0
  355.  
  356. //&<<&Keyboard Layouts 
  357.  
  358. char *mcode=mmm_KBD.keys[0],*acode=mmm_KBD.keys[2];
  359.  
  360. volatile int   mmm_f_E0=0;
  361. volatile int   mmm_f_LeftShift=0,mmm_f_RightShift=0;
  362. volatile int   mmm_f_LeftCtrl=0,mmm_f_RightCtrl=0;
  363. volatile int   mmm_f_LeftAlt=0,mmm_f_RightAlt=0;
  364. volatile int   mmm_f_Caps=0;
  365. volatile int   mmm_f_FixShift=0,mmm_f_FixAlt=0;
  366. volatile int   mmm_f_Shift=0,mmm_f_Alt=0,mmm_f_Ctrl=0;
  367. //&>>&3 0 3 0
  368.  
  369. //&<<&void MkScan(int code);             
  370.  
  371. void MkScan(int code){
  372. //&<<&    #ifdef _ENABLE_LOGGING_
  373.     #ifdef _ENABLE_LOGGING_
  374.         DoLog("MkScan(int code=%04x){\n",code);
  375.     #endif
  376. //&>>&1 0 1 0
  377.     code|=(mmm_f_Shift<<0x8);
  378.     code|=(mmm_f_Alt  <<0x9);
  379.     code|=(mmm_f_Ctrl <<0xa);
  380.     code|=(mmm_f_Caps <<0xb);
  381.     if(code==0x709) MMMlib_ABORT(0x709);
  382.     Command(code);
  383. //&<<&    #ifdef _ENABLE_LOGGING_
  384.     #ifdef _ENABLE_LOGGING_
  385.         DoLog("    }\n");
  386.     #endif
  387. //&>>&1 0 1 19
  388. }
  389. //&>>&1 0 1 0
  390.  
  391. void Beep(){ XBell(mmm_screen.prDisplay, 100); }
  392.  
  393. //&<<&void __EXIT();
  394. /*
  395.    EXECUTER_FILENAME=
  396.  
  397.    e:
  398.    cd \amstex
  399.    commands
  400.    %comspec
  401.    c:
  402.    cd \
  403.    i:\astr\mmm.bat -1 e:\mmm.dir -2 e:\amstex\astex.tex
  404. */
  405.  
  406. //&<<&void PutCurDir(FILE *f){   // prints current drive & directory into FILE *f
  407. void PutCurDir(FILE *f){
  408.   char *cd=getcurdir();                        // current disk
  409.   int lcd=strlen(cd);
  410.   if(lcd>3 && cd[lcd-1]=='/') cd[--lcd]=0;
  411.   fprintf(f,"cd %s\n",cd);
  412.   delete cd;
  413. }
  414. //&>>&4 0 4 1
  415.  
  416. void __EXIT(){
  417. //&<<&    #ifdef _ENABLE_LOGGING_
  418.   #ifdef _ENABLE_LOGGING_
  419.       DoLog("void __EXIT(){ \n");
  420.   #endif
  421. //&>>&1 0 1 6
  422.     char *s;
  423.     FILE *fRunScript,*filelist;
  424.  
  425.     s=MFileName(EXECUTER_FILENAME);
  426.     fRunScript=fopen(s,"w"); MemTest(fRunScript!=0);
  427.     delete s;
  428.  
  429.     PutCurDir(fRunScript);
  430.     while(mmm_LineClipboard && mmm_LineClipboard->prev)
  431.         mmm_LineClipboard=mmm_LineClipboard->prev;
  432.     while(mmm_LineClipboard && mmm_LineClipboard->next){
  433.         mmm_LineClipboard->Write(fRunScript,FILE_ASCII);
  434.         mmm_LineClipboard=mmm_LineClipboard->next;
  435.         }
  436.     PutCurDir(fRunScript);
  437.     fclose(fRunScript);
  438.  
  439.     s=MFileName("__mmm__.mmm");
  440.     filelist=fopen(s,"w"); MemTest(filelist!=0);
  441.     delete s;
  442.     PerformExitingFromFiles(filelist);
  443.     fclose(filelist);
  444.  
  445.     chmod(MFileName(EXECUTER_FILENAME), 0755 );
  446. //&<<&    #ifdef _ENABLE_LOGGING_
  447.   #ifdef _ENABLE_LOGGING_
  448.       DoLog("    } exit(-1)...\n");
  449.   #endif
  450. //&>>&1 0 1 6
  451.     system(MFileName(EXECUTER_FILENAME));
  452.     exit(-1);
  453. }
  454. ///// mmm_os2.err mmm_os2.err mmm_os2.err mmm_os2.err mmm_os2.err
  455. //&>>&1E 0 F 0
  456. void MMMlib_Console    (int ccode) {  }
  457.  
  458. //&<<&void File::MkWild();
  459. void File::MkWild(){
  460.   char str[0x100];
  461.   DIR *cdir;
  462.   struct dirent *dp;
  463.   struct stat fI;
  464. //&<<&  cdir=opendir("."); /////////read directories
  465.   cdir=opendir(".");
  466.   if(cdir==NULL){ NewLine("here must be directory\nbut sorry....")->LinkBefore(pos); }
  467.   dp=readdir(cdir);
  468.   while(dp!=0){
  469.     stat(dp->d_name,&fI);
  470.     if( (fI.st_mode&S_IFDIR) && strcmp(dp->d_name,".") ) {
  471.         sprintf(str,"%s/%s",dp->d_name,buf);
  472.         NewLine(str)->LinkBefore(pos);
  473.         }
  474.     dp=readdir(cdir);
  475.     }
  476. //&>>&0 0 0 0
  477. //&<<&  cdir=opendir("."); ///////// read files
  478.   cdir=opendir(".");
  479.   if(cdir==NULL){ NewLine("here must be directory\nbut sorry....")->LinkBefore(pos); }
  480.   dp=readdir(cdir);
  481.   while(dp!=0){
  482.     stat(dp->d_name,&fI);
  483.     if(!(fI.st_mode & S_IFDIR)){
  484.         sprintf(str,"%-25s %8ld <%2d> %02d:%02d:%02d",dp->d_name,
  485.                  fI.st_size, fI.st_nlink,
  486.                  (fI.st_mtime>>11)&15, (fI.st_mtime>>5)&63,  (fI.st_mtime&31)*2 );
  487.         NewLine(str)->LinkBefore(pos);
  488.         }
  489.     dp=readdir(cdir);
  490.     }
  491. //&>>&D 0 D C
  492.   while(pos->prev) pos=pos->prev;
  493. }
  494. //&>>&5 0 5 7
  495. ////// /home/machulsk/Xprog/*
  496. //&>>&7 0 7 1C
  497.