/* Wed Jan 24 11:05:54 EST 1990 /leverrier/h1/ksg/src/test plt.c */ /* gsg jr.Oct 15, 1992 added chflushx */ #include #include #include #include #include /* X WINDOW driver for plot package */ /* mul;tiple windows are supported using /* envriomental variables to control windows */ #define MAX_NUM_WINDOWS 10 #define MAX_LENGTH_NAME 256 #define MAX_COLORS 18 #define EV_MASK (ButtonPressMask |\ KeyPressMask |\ ExposureMask |\ StructureNotifyMask ) static int SDepth[MAX_NUM_WINDOWS]; /* depth of screen */ static int SHeight[MAX_NUM_WINDOWS];/* height of screen */ static int SWidth[MAX_NUM_WINDOWS]; /* width of screen */ static int WDepth[MAX_NUM_WINDOWS]; /* depth of window */ static int WHeight[MAX_NUM_WINDOWS];/* height of window */ static int WWidth[MAX_NUM_WINDOWS]; /* width of window */ static int Wx[MAX_NUM_WINDOWS],Wy[MAX_NUM_WINDOWS]; static char Window_Name[MAX_LENGTH_NAME]; static char *win_name; static char *win_geo; static char *env_name[MAX_NUM_WINDOWS]={ "WIN_X_0","WIN_X_1","WIN_X_2","WIN_X_3","WIN_X_4", "WIN_X_5","WIN_X_6","WIN_X_7","WIN_X_8","WIN_X_9"}; static char *env_geo[MAX_NUM_WINDOWS]={ "GEO_X_0","GEO_X_1","GEO_X_2","GEO_X_3","GEO_X_4", "GEO_X_5","GEO_X_6","GEO_X_7","GEO_X_8","GEO_X_9"}; static Display *Display_n[MAX_NUM_WINDOWS]; static Colormap ColorMap_n[MAX_NUM_WINDOWS]; static int Screen_n[MAX_NUM_WINDOWS]; static Window Window_n[MAX_NUM_WINDOWS]; static Pixmap PixMap_n[MAX_NUM_WINDOWS]; static GC GC_m[MAX_NUM_WINDOWS]; static GC *ptGC_m[MAX_NUM_WINDOWS]; static XGCValues GC_Values_m[MAX_NUM_WINDOWS]; static GC GC_n[MAX_NUM_WINDOWS]; static GC *ptGC_n[MAX_NUM_WINDOWS]; static XGCValues GC_Values_n[MAX_NUM_WINDOWS]; static char *colors[]={"black","red","blue","green","OrangeRed","Gold","Yellow","Magenta" ,"Salmon","Cyan","MediumGoldenrod","IndianRed","Plum","Gray","Coral","VioletRed" ,"Tan","Wheat" }; static XColor RBGColor,HardWareColor[MAX_NUM_WINDOWS][MAX_COLORS]; static GC GC_nc[MAX_NUM_WINDOWS][MAX_COLORS]; static int Num_Windows=0; static int get_motion=1; /* set to 0 if we track motion */ static int winmaster = -1; static char ev_motion[]="vgrXmotion"; static char *pt_char; static Cursor Cursor_Master[MAX_NUM_WINDOWS]; static Cursor Cursor_Slave[MAX_NUM_WINDOWS]; void xflushl_() { int i; for ( i=0 ; i < Num_Windows ; i++) XFlush( Display_n[i]); } static int xflush_yes=0; static int i1,i2,i3; void chflushx_( ipen) int *ipen; { i3 = i2; i2 = i1; i1 = xflush_yes; if ( *ipen == -100 ) xflush_yes ^= 1; xflushl_(); } void setXwinmaster( j) int j; { /* set the window that controls the cursor */ int i; if (( j == winmaster ) || ( get_motion != 0 )) return; for ( i=0 ; i < Num_Windows ; i++) { if ( i == j ) { XSelectInput( Display_n[i], Window_n[i], EV_MASK | PointerMotionMask | PointerMotionHintMask ); XDefineCursor( Display_n[i], Window_n[i], Cursor_Master[i]); } else { XSelectInput( Display_n[i], Window_n[i], EV_MASK ); XDefineCursor( Display_n[i], Window_n[i], Cursor_Slave[i]); } XFlush( Display_n[i]); } winmaster = j; return; } void openxwins_() { char *getenv(); int flag; int status; XSetWindowAttributes WindowAttributes; XSizeHints SizeHints; unsigned long Window_Mask; int border_width; unsigned long Black_Pixel, White_Pixel; unsigned int jun; int i,j; int geometryStatus; pt_char = getenv( ev_motion); if ( pt_char != NULL) get_motion = strcmp( pt_char, "yes"); for ( i=0 ; i < MAX_NUM_WINDOWS ; i++) { win_name = getenv( env_name[i]); win_geo = getenv( env_geo[i]); fprintf( stderr,"\n openxwins %s, %s, %s\n", env_name[i], win_name, win_geo); if ( (i > 0) && ( win_name == NULL)) { break; } Display_n[i] = XOpenDisplay( win_name); if ( Display_n[i] == NULL) { fprintf( stderr,"ERROR: could not open connection to %s\n", XDisplayName( win_name)); exit(1); } Screen_n[i] = DefaultScreen( Display_n[i]); SDepth[i] = DefaultDepth( Display_n[i], Screen_n[i]); SWidth[i] = DisplayWidth( Display_n[i], Screen_n[i]); SHeight[i] = DisplayHeight( Display_n[i], Screen_n[i]); WDepth[i] = SDepth[i]; WWidth[i] = SWidth[i]/2; WHeight[i] = SHeight[i]/2; Wx[i] = 0; Wy[i] = 0; if ( win_geo != NULL) { geometryStatus = XParseGeometry( win_geo, &Wx[i], &Wy[i], &WWidth[i], &WHeight[i]); if ( Wx[i] < 0 ) Wx[i] = SWidth[i] - WWidth[i]; if ( Wy[i] < 0 ) Wy[i] = SHeight[i] - WHeight[i]; if ( SWidth[i] < Wx[i] + WWidth[i]) SWidth[i] = Wx[i] + WWidth[i]; if ( SHeight[i] < Wy[i] + WHeight[i]) SHeight[i] = Wy[i] + WHeight[i]; } else { fprintf( stderr,"screen depth %d, width %d, height%d.\n",WDepth[i],WWidth[i],WHeight[i]); } border_width = 1; WindowAttributes.border_pixel = Black_Pixel = BlackPixel( Display_n[i], Screen_n[i]); WindowAttributes.background_pixel = White_Pixel = WhitePixel( Display_n[i], Screen_n[i]); WindowAttributes.override_redirect = False; Window_Mask = CWBackPixel | CWBorderPixel | CWOverrideRedirect; Window_n[i] = XCreateWindow( Display_n[i], RootWindow( Display_n[i], Screen_n[i]), Wx[i], Wy[i], WWidth[i], WHeight[i], border_width, WDepth[i], InputOutput, CopyFromParent, Window_Mask, &WindowAttributes); PixMap_n[i] = XCreatePixmap( Display_n[i], RootWindow( Display_n[i], Screen_n[i]), SWidth[i], SHeight[i], SDepth[i] ); if ( i > 0 ) XSelectInput( Display_n[i], Window_n[i], EV_MASK ); else XSelectInput( Display_n[i], Window_n[i], EV_MASK | PointerMotionMask | PointerMotionHintMask ); ptGC_m[i] = &GC_m[i]; GC_m[i] = XCreateGC( Display_n[i], Window_n[i], (unsigned long) 0, &GC_Values_n[i] ); XSetForeground( Display_n[i], GC_m[i], White_Pixel); XSetBackground( Display_n[i], GC_m[i], Black_Pixel); ptGC_n[i] = &GC_n[i]; GC_n[i] = XCreateGC( Display_n[i], Window_n[i], (unsigned long) 0, &GC_Values_n[i] ); XSetForeground( Display_n[i], GC_n[i], Black_Pixel); XSetBackground( Display_n[i], GC_n[i], White_Pixel); ColorMap_n[i] = DefaultColormap( Display_n[i], Screen_n[i]); /* if ( WDepth[i] > 1) */ if ( WDepth[i] > 0) { for ( j=0 ; j < MAX_COLORS ; j++) { jun = j; status = XLookupColor( Display_n[i], ColorMap_n[i], colors[j], &RBGColor, &HardWareColor[i][j]); if ( status != 0) { status = XAllocColor( Display_n[i], ColorMap_n[i], &HardWareColor[i][j]); GC_nc[i][j] = XCreateGC( Display_n[i], Window_n[i], (unsigned long) 0, &GC_Values_n[i]); XSetForeground( Display_n[i], GC_nc[i][j], &HardWareColor[i][j].pixel); XSetLineAttributes( Display_n[i], GC_nc[i][j], jun, LineSolid, CapNotLast, JoinMiter); status = -1; } } } SizeHints.flags = PPosition | PSize; SizeHints.x = Wx[i]; SizeHints.y = Wy[i]; SizeHints.width = WWidth[i]; SizeHints.height= WHeight[i]; XSetNormalHints( Display_n[i], Window_n[i], &SizeHints); XMapWindow( Display_n[i], Window_n[i]); XMoveWindow( Display_n[i], Window_n[i], Wx[i], Wy[i]); XFlush( Display_n[i]); if ( win_name == NULL) { Num_Windows = 1; break; } Num_Windows++; Cursor_Master[i] = XCreateFontCursor( Display_n[i], XC_crosshair ); Cursor_Slave[i] = XCreateFontCursor( Display_n[i], XC_plus ); } setXwinmaster( 0); } static int anycolor=0; void chpenx_( ipen) int *ipen; { int i,j; unsigned int jun; /* j = *ipen + MAX_COLORS - 1; */ if ( *ipen == -100 ) { /* change b/w color usage */ anycolor ^= 1; return; } j = *ipen + MAX_COLORS - 1; j %= MAX_COLORS; for ( i=0 ; i < Num_Windows ; i++) { if ( WDepth[i] > 1 ) { XSetForeground( Display_n[i], GC_n[i], HardWareColor[i][j].pixel); jun = *ipen / MAX_COLORS + 1; if ( *ipen < 0 ) jun = 0; } else { jun = *ipen ; if ( *ipen < 0 ) jun = 0; if ( anycolor != 0 ) jun = 0; } XSetLineAttributes( Display_n[i], GC_n[i], jun, LineSolid, CapNotLast, JoinMiter); } } void closexwins_() { int i; for ( i=0 ; i < Num_Windows ; i++) { XDestroyWindow( Display_n[i], Window_n[i]); XDestroyWindow( Display_n[i], PixMap_n[i]); } } static float x1=0.; static float y1=0.; static float x2=11.; static float y2=8.5; static float dx=11.; static float dy=8.5; void changw_( x00, x20, y00, y20) float *x00,*x20,*y00,*y20; { int i; for ( i=0 ; i < Num_Windows ; i++) { /* XClearWindow( Display_n[i], PixMap_n[i]); */ XFillRectangle( Display_n[i], PixMap_n[i], GC_m[i], 0, 0, WWidth[i], WHeight[i]); XClearWindow( Display_n[i], Window_n[i]); } if ( *x00 == *x20 ) return ; x1 = *x00; y1 = *y00; x2 = *x20; y2 = *y20; dx = x2 - x1; dy = y2 - y1; } void xline_( xx1, yy1, xx2, yy2) float *xx1,*yy1,*xx2,*yy2; { int i; int ix1,ix2,iy1,iy2, idx,idy; for ( i=0 ; i < Num_Windows ; i++) { ix1 = WWidth[i]*( *xx1 - x1)/dx; iy1 = (1.0 - ( *yy1 - y1)/dy)*WHeight[i]; ix2 = WWidth[i]*( *xx2 - x1)/dx; iy2 = (1.0 - ( *yy2 - y1)/dy)*WHeight[i]; idx = ix2 - ix1 + 1; idy = iy2 - iy1 + 1; XDrawLine( Display_n[i], PixMap_n[i], GC_n[i], ix1, iy1, ix2, iy2); XDrawLine( Display_n[i], Window_n[i], GC_n[i], ix1, iy1, ix2, iy2); XFlush( Display_n[i]); } } static float xnow=0.; static float ynow=0.; lineabs2_( x, y) float *x, *y; { xline_( &xnow, &ynow, x, y); xnow = *x; ynow = *y; } moveabs2_( x, y) float *x, *y; { xnow = *x; ynow = *y; } /* static Time last_time,time; // old statment, I see no need fot time */ static Time last_time; void ProcessButtonPress( event) XButtonPressedEvent *event; { int i,j; for ( i=0 ; i < Num_Windows ; i++) { if ((Window_n[i] == event->window) && (Display_n[i] == event->display)) break; } if ( i >= Num_Windows ) { fprintf( stderr,"\nERROR on ButtonPress\n"); return; } setXwinmaster( i); } void ProcessConfigure( event) XConfigureEvent *event; { /* resive event */ int i,j; for ( i=0 ; i < Num_Windows ; i++) { if ((Window_n[i] == event->window) && (Display_n[i] == event->display)) break; } if ( i >= Num_Windows ) { fprintf( stderr,"\nERROR on configure\n"); return; } WWidth[i] = event->width; WHeight[i] = event->height; } void ProcessExpose( event) XExposeEvent *event; { /* process expose event, flush only at end */ /* find which display */ int i; for ( i=0 ; i < Num_Windows ; i++) { if ((Window_n[i] == event->window) && (Display_n[i] == event->display)) break; } if ( i >= Num_Windows ) { fprintf( stderr,"\nERROR on expose\n"); return; } XCopyArea( Display_n[i], PixMap_n[i], Window_n[i], GC_n[i], event->x, event->y, event->width, event->height, event->x, event->y); XFlush( Display_n[i]); if ( event->count == 0 ) XFlush( Display_n[i]); } void ProcessMotion( event) XPointerMovedEvent *event; { /* keep the cursors together */ int i,j; int nevents; XTimeCoord *mptr; XTimeCoord *mptr0; for ( i=0 ; i < Num_Windows ; i++) { if ((Window_n[i] == event->window) && (Display_n[i] == event->display)) break; } if ( i >= Num_Windows ) { fprintf( stderr,"\nERROR on expose\n"); return; } if ( Display_n[i]->motion_buffer != 0) { /* use motion buffer */ int k; int t,x,y; mptr = XGetMotionEvents( Display_n[i], Window_n[i], last_time, CurrentTime, &nevents); mptr0 = mptr; for ( k=0 ; k < nevents ; k++) { t = mptr->time; x = mptr->x; y = mptr->y; for ( j=0 ; j < Num_Windows ; j++) { int xx,yy; if ( j == i ) continue; xx = ( x*WWidth[j])/WWidth[i]; xx = ( xx <= 0 ) ? 1 : xx; xx = ( xx >= WWidth[i] ) ? WWidth[i]-1 : xx; yy = ( y*WHeight[j])/WHeight[i]; yy = ( yy <= 0 ) ? 1 : yy; yy = ( yy >= WHeight[i] ) ? WHeight[i]-1 : yy; XWarpPointer( Display_n[j], Window_n[j], Window_n[j], 0, 0, 0, 0, xx, yy ); } mptr++; } XFree( mptr0); } else { /* no motion buffer */ Window rw,cw; int xr,yr,xw,yw; int keys_buttons; XQueryPointer( Display_n[i], Window_n[i], &rw, &cw, &xr, &yr, &xw, &yw, &keys_buttons); for ( j=0 ; j < Num_Windows ; j++) { int xx,yy; if ( j == i ) continue; xx = ( xw*WWidth[j])/WWidth[i]; xx = ( xx <= 0 ) ? 1 : xx; xx = ( xx >= WWidth[i] ) ? WWidth[i]-1 : xx; yy = ( yw*WHeight[j])/WHeight[i]; yy = ( yy <= 0 ) ? 1 : yy; yy = ( yy >= WHeight[i] ) ? WHeight[i]-1 : yy; XWarpPointer( Display_n[j], Window_n[j], Window_n[j], 0, 0, 0, 0, xx, yy ); XFlush( Display_n[j]); } } last_time = event->time ; } static int mask; static int readfds; static int writefds; static int exfds; static struct timeval timeout; static int nfds; static int linit=0; static XEvent event; void iopause_() { int no_io,nfound; int i,j; static int icount; static int inum=0; static int nfa[5]; inum++; if ( linit == 0 ) { mask = 1 << 0; for ( i=0 ; i < Num_Windows ; i++) { j = ConnectionNumber( Display_n[i]); mask |= ( 1<< j); nfds = ( nfds > j) ? nfds : j; } timeout.tv_sec = 1; timeout.tv_usec = 100000; /* 100 millisec */ } no_io = 0; icount = 0; while ( no_io == 0) { icount++; for ( i=0 ; i < Num_Windows ; i++) { j = XEventsQueued( Display_n[i], QueuedAfterReading ); if ( j > 0 ) { /* process event */ XNextEvent( Display_n[i], &event); /* which event */ switch ( event.type) { case ButtonPress: ProcessButtonPress( &event); break; case ConfigureNotify: ProcessConfigure( &event); break; case MapNotify: /*fprintf( stderr," event MapNotify\n");*/ break; case Expose: ProcessExpose( &event); break; case NoExpose: /* no processing for now */ break; case MotionNotify: ProcessMotion( &event); break; default: fprintf( stderr,"%d, %d, EVENT %d \n",inum,icount,event.type); break; } } } readfds = mask; readfds = 1; writefds = 0; exfds = 0; nfound = select( nfds, &readfds, &writefds, &exfds, &timeout); nfa[4] = nfa[3]; nfa[3] = nfa[2]; nfa[2] = nfa[1]; nfa[1] = nfa[0]; nfa[0] = nfound; no_io = nfound; } }