inter.c

Go to the documentation of this file.
00001 /*--------------------------------------------------
00002  * Copyright (C) 1998 Chancelier Jean-Philippe 
00003  *--------------------------------------------------*/
00004 
00005 #ifdef SYSV
00006 #include <string.h>
00007 #else
00008 #include <strings.h>
00009 #endif
00010 
00011 #include "x_ptyxP.h"
00012 #include "x_data.h"
00013 #include "machine.h"
00014 #include "All-extern-x.h"
00015 #include "All-extern.h"
00016 #include "xscion.h"
00017 #include "TclEvents.h" /* flushTKEvents() */
00018 #include "dynamic_menus.h" /* ismenu() */
00019 
00020 #include <X11/Xlib.h>
00021 #include <X11/Xatom.h>
00022 #include <X11/Xutil.h>
00023 #include <X11/cursorfont.h>
00024 #include <X11/Intrinsic.h>
00025 #include <X11/StringDefs.h>
00026 #include <X11/Shell.h>
00027 #include <X11/Xos.h>
00028 #include <ctype.h>
00029 #include <errno.h>
00030 #include <stdio.h>
00031 #include "inter.h"
00032 
00036 #ifdef hpux
00037         #include <sys/time.h>
00038 #else 
00039 #include <sys/select.h>
00040 #endif 
00041 
00042 #ifdef aix
00043 #include <sys/select.h>
00044 #endif
00045 
00046 #include <stdlib.h>
00047 
00048 #ifdef WITH_TK
00049 #include "TCL_Global.h"
00050 #endif
00051 
00052 #include "Os_specific.h" 
00053 
00054 extern int  Scierror __PARAMS((int iv,char *fmt,...));
00055 
00056 
00057 #define LF                    0x000a
00058 #define MAXBUFFSIZE 256
00059 static int ClickMenu=0;
00060 static char buffstring[MAXBUFFSIZE];
00061 static int lenbuffstring;
00062 static int posinbuff=0;
00063 extern int Xscilab  __PARAMS((Display **dpy, Widget *topwid));  
00064 extern void xevents1  __PARAMS((void));  
00065 extern int GetWITH_GUI(void);
00066 /*-------------------------------------------------------
00067  * Functions to set or to get the scilab status 
00068  * is it a scilab or a scilab -nw 
00069  *-------------------------------------------------------*/
00070 
00071 
00072 static fd_set Select_mask_ref,select_mask,Write_mask_ref,write_mask;
00073 static int inter_max_plus1 = 0;
00074 Display *the_dpy = (Display *) NULL;
00075 int BasicScilab = 1;
00076 XtAppContext app_con;
00077 int IsClick_menu(void);
00078 void Click_menu(int n);
00079 int charfromclick(void);
00080 
00081 
00082 static int intoemacs=0;
00083 
00084 
00085 int IntoEmacs(void )
00086 {
00087   return(strcmp(getenv("TERM"),"dumb")==0);
00088 }
00089 
00090 
00091 int C2F(checkevts)(int *i)
00092 {
00093   *i= Max(getINXscilab(),1-BasicScilab);
00094   return(0);
00095 }
00096 
00097 
00098 
00099 static String bgfallback_resources[] = {
00100 #include "Xscilab.ad.h"
00101   NULL,
00102 };
00103 
00104 /*-------------------------------------------------------
00105  * Display Init :
00106  * This function is initialize only Once  
00107  * the scilab display 
00108  *-------------------------------------------------------*/
00109 
00110 static XtActionsRec actionProcs[] = {
00111   {"DeleteWindow", DeleteWindow},
00112   {"SGDeleteWindow", SGDeleteWindow},
00113   {"KeyboardMapping", KeyboardMapping}
00114 };
00115 
00116 
00117 
00118 /* 
00119  * used to start tk at run time when scilab 
00120  * was started with scilab -nw 
00121  * open a display with DisplayInit and initialize 
00122  * Tk. If TK_Started is set to one then the initialization 
00123  * was correct.
00124  */
00125 
00126 static void basic_scilab_mask( Display **dpy);
00127 
00128 void sci_tk_activate(void)
00129 {
00130   Display *dpy = (Display *) NULL;
00131   Widget toplevel = (Widget) NULL;
00132   /*Cout("You have started Scilab in a mode in which TK is not initialized.\n");
00133     Cout("Trying to initialize \n");*/
00134   DisplayInit("",&dpy,&toplevel);
00135   BasicScilab = 0;
00136   basic_scilab_mask(&dpy);
00137 }
00138 
00139 void DisplayInit(char *string,Display **dpy,Widget * toplevel)
00140 {
00141   static XrmOptionDescRec *optionDescList = NULL;
00142   static Display *dpy1;
00143   static Widget toplevel1;
00144   static int count=0;
00145   int argc=0;
00146   char *argv = 0;
00147   if ( count > 0) 
00148     {
00149       *dpy=dpy1;
00150       *toplevel=toplevel1;
00151       return;
00152     }
00153   count++;
00154   Xscilab(dpy,toplevel);
00155   if ( *toplevel != (Widget) NULL)
00156     {
00157       toplevel1 = *toplevel;
00158       dpy1 = *dpy;
00159     }
00160   else
00161     {
00162       *toplevel=toplevel1=XtAppInitialize (&app_con,"Xscilab",optionDescList,
00163                                            0,&argc, (String *)argv,
00164                                            bgfallback_resources,(ArgList) NULL,(Cardinal) 0);
00165       XtAppAddActions(app_con, actionProcs, XtNumber(actionProcs));
00166       the_dpy = *dpy=dpy1=XtDisplay(toplevel1);
00167       BasicScilab = 0;
00168       basic_scilab_mask(dpy);
00169       intoemacs=IntoEmacs();
00170     }
00171   XSync(dpy1,0);
00172 }
00173 
00174 /* fix the select masks : used graphics or tk is activated from a scilab -nw */
00175 
00176 static int Xsocket=0,fd_in=0,fd_out=0,fd_err=0 ;
00177 
00178 static void basic_scilab_mask( Display **dpy)
00179 {
00180   Xsocket = ConnectionNumber(*dpy);
00181   fd_in = fileno(stdin) ;
00182   fd_out = fileno(stdout);
00183   fd_err = fileno(stderr);
00184 
00185   FD_ZERO(&Select_mask_ref);
00186   FD_SET(fd_in , &Select_mask_ref);
00187   FD_SET(Xsocket, &Select_mask_ref);
00188 #ifdef WITH_TK 
00189   if ( XTKsocket != 0 ) FD_SET(XTKsocket, &Select_mask_ref);
00190 #endif 
00191   FD_ZERO(&Write_mask_ref);
00192   /* the two next FD_SET causes select not to wait 
00193    * 
00194    */
00195   /* FD_SET(fd_out,&Write_mask_ref);
00196      FD_SET(fd_err,&Write_mask_ref);*/
00197 
00198   inter_max_plus1 = Max(fd_in,Xsocket);      
00199   inter_max_plus1 = Max(fd_out,inter_max_plus1);
00200   inter_max_plus1 = Max(fd_err,inter_max_plus1);
00201 #ifdef WITH_TK 
00202   inter_max_plus1 = Max(XTKsocket,inter_max_plus1);
00203 #endif 
00204   inter_max_plus1++;
00205 }  
00206 
00207 
00208 /*-------------------------------------------------------
00209  * 
00210  *  Xorgetchar : function used while in the scilab -nw mode 
00211  *      by zzledt to get the next typed char (in stdin)
00212  *      but also checks for X11 events if we are using an X Window 
00213  *      ( ex a graphic window) with scilab -nw )
00214  *      
00215  *      stdin is supposed to be changed 
00216  *      so as not to wait for <cr> this is done inside zzledt 
00217  *      (in the following code  the key function is select )
00218  *
00219  *  Additionally, the case of emacs-shell is added
00220  *                the case of calling from user graphical menu is added
00221  *                now it is possible to run scicos from -nw mode
00222  *-------------------------------------------------------*/
00223 
00224 int Xorgetchar(int interrupt)
00225      /* interrupt is not used here (console mode) */
00226 {
00227   register int i;
00228   static struct timeval select_timeout;
00229   static int state = 0;
00230   if ( BasicScilab) return(getchar());
00231   for( ; ; ) {
00232     XFlush(the_dpy); /* always flush writes before waiting */
00233     fflush(stdout); 
00234     fflush(stderr); 
00235 #ifdef WITH_TK 
00236     flushTKEvents();
00237 #endif 
00238     /* Initialize masks  */
00239     select_mask = Select_mask_ref;
00240     write_mask  = Write_mask_ref;
00241 
00242     select_timeout.tv_sec = 0;
00243     select_timeout.tv_usec = 10;
00244     i = select(inter_max_plus1, &select_mask, &write_mask, (fd_set *)NULL, &select_timeout);
00245     if (i < 0) {
00246       if (errno != EINTR)
00247         { 
00248           Scistring("Error\n");
00249           exit(0);
00250           continue;
00251         }
00252     } 
00253 
00254     /* if there's something to output */
00255     if ( FD_ISSET(fd_out,&write_mask)) { 
00256       fflush(stdout); 
00257     }
00258     if ( FD_ISSET(fd_err,&write_mask)) { 
00259       fflush(stderr); 
00260     }
00261 
00262 #ifdef WITH_TK 
00263     if ( XTKsocket != 0 && FD_ISSET(XTKsocket,&select_mask )) { 
00264       flushTKEvents();
00265     }
00266 #endif 
00267 
00268     /* if there's something to read */
00269     if (FD_ISSET(fd_in,&select_mask) || IsClick_menu()) 
00270       state=1;
00271     else
00272       if (QLength(the_dpy) ||  FD_ISSET(Xsocket,&select_mask ) ||!(intoemacs))
00273         state=0;
00274 
00275     if (state) {
00276       if(IsClick_menu())
00277         i=charfromclick();
00278       else
00279         i=getchar();
00280       if (i==LF) state=0;
00281       return(i);
00282     }
00283         
00284     C2F(sxevents)();
00285     if (interrupt&&(C2F(ismenu)()==1)) return(-1);
00286 
00287   }
00288 
00289 
00290 }
00291 
00292 
00293 
00294 
00295 /*-------------------------------------------------------
00296  *  Dealing with X11 Events.
00297  *  xevents is called by Xorgetchar and also by DispatchEvents in 
00298  *  routines/system 
00299  *  xevents must work for scilab and scilab -nw 
00300  *-------------------------------------------------------*/
00301 
00302 int C2F(sxevents)()
00303 {
00304         if ( GetWITH_GUI() )
00305         {
00306                   if (getINXscilab()==1) 
00307     {
00308       xevents1();
00309     }
00310   else 
00311     {
00312       XEvent event;
00313       if (BasicScilab) return(0);
00314       if ( the_dpy == (Display *) NULL)  return(0);
00315 #ifdef WITH_TK 
00316       if ( XTKsocket != 0 ) flushTKEvents();
00317 #endif 
00318       if (!XPending (the_dpy))
00319         /* protect against events/errors being swallowed by us or Xlib */
00320         return(0);
00321       do {
00322         XNextEvent (the_dpy, &event);
00323         XtDispatchEvent(&event);
00324       } while (QLength(the_dpy) > 0);
00325     }
00326         }
00327         
00328   return(0);
00329 }
00330 
00331 
00332 
00333 void Click_menu(int n);
00334 
00335 static void str_to_xterm ( string, nbytes)
00336      register char *string;
00337      int nbytes;
00338 {
00339   register TScreen *screen = &term->screen;
00340   StringInput(screen,string,strlen(string));
00341 }
00342 
00343 
00344 static void str_to_xterm_nw ( string, nbytes)
00345      register char *string;
00346      int nbytes;
00347 {
00348   posinbuff=0;
00349   if (nbytes>MAXBUFFSIZE-1)
00350     {
00351       Scierror(1020,"%s\n", "The menu  name is too long  to be strored");
00352       return;
00353     }
00354   sprintf(buffstring,string);
00355   buffstring[nbytes]='\n';
00356   lenbuffstring=nbytes+1;
00357   Click_menu(1);
00358 }
00359 
00360 void write_scilab(s)
00361      char   *s;
00362 {
00363   int  i;
00364   C2F(xscion)(&i);
00365   if (i==1)
00366     str_to_xterm(s,strlen(s));
00367   else
00368     str_to_xterm_nw(s,strlen(s));
00369   /*            printf(" asynchronous actions are not supported with -nw option\n");*/
00370 }
00371 
00372 int IsClick_menu(void)
00373 {
00374   return(ClickMenu);
00375 }
00376 
00377 void Click_menu(int n)
00378 {
00379   ClickMenu=n;
00380   return;
00381 }
00382 
00383 int charfromclick(void)
00384 {
00385   if (posinbuff==lenbuffstring-1)
00386     {
00387       ClickMenu=0;
00388     }
00389   if (posinbuff==lenbuffstring)
00390     return(9999);
00391   else
00392     return(buffstring[posinbuff++]);
00393 }
00394   
00395 
00396 
00397 
00398 
00399 
00400 

Generated on Sun Mar 4 15:03:56 2007 for Scilab [trunk] by  doxygen 1.5.1