[comp.sources.x] v04i008: xcursor, Patch1

argv@island.uu.net (Dan Heller) (05/30/89)

Submitted-by: thor@stout.UCAR.EDU (Rich Neitzel)
Posting-number: Volume 4, Issue 8
Archive-name: xcursor/patch1


These patches to xcursor address the following improvements:
	1> Windows may be selected by name.
	2> Windows may be selected by mouse clicks ala xwininfo.
	3> Default action is 2. To select root a new option was added.
	4> Added support for -display option.

#	This is a shell archive.
#	Remove everything above and including the cut line.
#	Then run the rest of the file through sh.
#----cut here-----cut here-----cut here-----cut here----#
#!/bin/sh
# shar:	Shell Archiver
#	Run the following text with /bin/sh to create:
#	xcpatch1
#	xcpatch2
#	xcpatch3
#	Apply xcpatch1 to xcursor.c, xcpatch2 to xcursor.h and
#	xcpatch3 to xcursor.l.
#
# This archive created: Fri May 26 12:51:40 1989
sed 's/^X//' << \SHAR_EOF > xcpatch1
X*** xcursor.c.old	Fri May 26 12:42:35 1989
X--- xcursor.c	Fri May 26 08:12:48 1989
X***************
X*** 6,11 ****
X--- 6,13 ----
X  revision history
X  ----------------
X  1.0,12may89,rekn      Written.
X+ 2.0,25may89,rekn      Added support for named windows and selection by 
X+                       pointer.
X  
X    This is a quick program that reads the header file defining cursors
X    and lets one change the cursor for a specified window. Window
X***************
X*** 14,35 ****
X  
X  #include "xcursor.h"
X  
X  main(argc,argv)
X  int argc;
X  char **argv;
X  {
X      register int opt;		/* Parsed option letter */
X!     char *id_str;		/* Window id in ASCII */
X      extern int optind;
X      extern char *optarg;
X  
X!     if ((display = XOpenDisplay(NULL)) == NULL)
X!       {
X! 	  perror("xcursor - no display");
X! 	  exit(1);
X!       }
X  
X!     while ((opt = getopt(argc,argv,"lvw:")) != -1)
X        {
X  	  switch(opt)
X  	    {
X--- 16,46 ----
X  
X  #include "xcursor.h"
X  
X+ /* Text for select by pointer case */
X+ char *point_txt[] = {
X+ "           ====> Please select the window",
X+ "           ====> whose cursor you wish to",
X+ "           ====> change by clicking the",
X+ "           ====> pointer in that window.",
X+ 0};
X+ 
X  main(argc,argv)
X  int argc;
X  char **argv;
X  {
X      register int opt;		/* Parsed option letter */
X!     char *id_str = 0;		/* Window id in ASCII */
X!     char *disp_str = 0;		/* Display name */
X!     char *name_str = 0;		/* Window name */
X!     register int uflag = 0;	/* Source specified */
X!     register char **ptr;	/* Fast pointer */
X      extern int optind;
X      extern char *optarg;
X+     extern int opterr;
X  
X!     opterr = 0;			/* No error msgs from getopt */
X  
X!     while ((opt = getopt(argc,argv,"lvd:n:i:r")) != -1)
X        {
X  	  switch(opt)
X  	    {
X***************
X*** 41,67 ****
X  		print_vers();
X  		break;
X  
X! 	      case 'w':		/* Do this window */
X! 		wflg++;
X! 		id_str = optarg;
X  		break;
X  
X! 	      case '?':
X  		local_help();
X  		break;
X  	    }
X        }
X  
X!     if (optind < argc)		/* check for user requested cursor */
X  	  find_cursor = argv[optind];
X      else
X        find_cursor = default_cursor;
X  
X-     if (wflg)			/* Use strtol to handle hex */
X-       window = (Window)strtol(id_str,(char **)NULL,0);
X-     else
X-       window = DefaultRootWindow(display);
X- 
X      work();
X  
X      exit(0);
X--- 52,121 ----
X  		print_vers();
X  		break;
X  
X! 	      case 'd':		/* Get display name */
X! 		disp_str = argv[optind++];
X! 		if (*disp_str == '-') /* Check to see if user skipped */
X! 				      /* required data */
X! 		  local_help();
X  		break;
X  
X! 	      case 'n':		/* Get window name */
X! 		name_str = argv[optind++];
X! 		if (*name_str == '-')
X! 		  local_help();
X! 		uflag++;
X! 		break;
X! 
X! 	      case 'i':		/* Get window id */
X! 		id_str = argv[optind++];
X! 		if (*id_str == '-')
X! 		  local_help();
X! 		uflag++;
X! 		break;
X! 		
X! 	      case 'r':		/* Set root cursor */
X! 		uflag++;
X! 		break;
X! 
X! 	      case '?':		/* Ooops! */
X! 		fprintf(stderr,"xcursor - bad option\n\n");
X  		local_help();
X  		break;
X  	    }
X        }
X+     
X+     if ((display = XOpenDisplay(disp_str)) == NULL)
X+       {
X+ 	  fprintf(stderr,"Cannot open display %s\n",disp_str);
X+ 	  exit(1);
X+       }
X+    
X+     if (name_str != NULL)
X+       {
X+ 	  if ((window = window_by_name(DefaultRootWindow(display),name_str)) 
X+ 	       == 0)
X+ 	      {
X+ 		  fprintf(stderr,"No window named %s found.\n",name_str);
X+ 		  exit(2);
X+ 	      }
X+       }
X+     else if (id_str != NULL)
X+       window = (Window)strtol(id_str,(char **)NULL,0);
X+     else if (uflag)
X+       window = DefaultRootWindow(display);
X+     else if (!lflg)
X+       {
X+ 	  for (ptr = point_txt; *ptr; ptr++)
X+ 	    printf("%s\n",*ptr);
X  
X! 	  window = point_to_window(); /* use pointer to get window */
X!       }
X! 
X!   if (optind < argc)		/* check for user requested cursor */
X  	  find_cursor = argv[optind];
X      else
X        find_cursor = default_cursor;
X  
X      work();
X  
X      exit(0);
X***************
X*** 78,88 ****
X  "xcursor is a small tool to set the cursor for a specified window.",
X  "The syntax is:",
X  "",
X! "        xcursor [-l][-w id] [cursor]",
X  "",
X! "the -l flag lists all known cursors, the -w flag sets the cursor for",
X! "window id (defaults to root window) and cursor is the name of the",
X! "cursor you want (defaults to whatever local builder desired)",
X  0};
X  
X  void local_help()
X--- 132,147 ----
X  "xcursor is a small tool to set the cursor for a specified window.",
X  "The syntax is:",
X  "",
X! "    xcursor [-l] [-v] [-r] [-display display][-name name] [-id id] [cursor]",
X  "",
X! "the -l flag lists all known cursors; the -name option sets the cursor for",
X! "the named window; the -id option sets the cursor for the window with the",
X! "specified id; the -display option causes the named display to be used",
X! "instead of the default display. The -v option prints the current version",
X! "number of xcursor. The -r option causes the root window to be effected.",
X! "Cursor is the name (XC_ prefix optional) of the cursor you want (defaults",
X! "to whatever the local builder desired). If none of -r, -id or -name are",
X! "used, xcursor prompts you to use the pointer to select a window.",
X  0};
X  
X  void local_help()
X***************
X*** 92,98 ****
X      for (ptr = help_text; *ptr; ptr++)
X        fprintf(stderr,"%s\n",*ptr);
X  
X!     exit(1);
X  }
X  
X  
X--- 151,157 ----
X      for (ptr = help_text; *ptr; ptr++)
X        fprintf(stderr,"%s\n",*ptr);
X  
X!     exit(3);
X  }
X  
X  
X***************
X*** 127,133 ****
X  			    flag++;
X  			    printf("%s\n",start);
X  			}
X! 		      else if (!strcmp(start,find_cursor))
X  			{
X  			    curs = XCreateFontCursor(display,cursor);
X  			    XDefineCursor(display,window,curs);
X--- 186,193 ----
X  			    flag++;
X  			    printf("%s\n",start);
X  			}
X! 		      else if (!strcmp(start, find_cursor) ||
X! 			       !strcmp(&start[3], find_cursor))
X  			{
X  			    curs = XCreateFontCursor(display,cursor);
X  			    XDefineCursor(display,window,curs);
X***************
X*** 142,145 ****
X--- 202,292 ----
X        fprintf(stderr,"xcursor - Cannot find %s\n",find_cursor);
X  
X      fclose(fp);
X+ }
X+ 
X+ /* Code for the next two routines was lifted from dsimple.c in 
X+    xwininfo */
X+ 
X+ Window window_by_name(wdw,name)
X+ Window wdw;
X+ char *name;
X+ {
X+     Window *offspring;		/* Any children */
X+     Window junk;		/* Just that */
X+     Window w = 0;		/* Found window */
X+     int count;			/* Number of kids */
X+     int loop;			/* Loop counter */
X+     char *wdw_name;		/* Returnewd name */
X+ 
X+     if (XFetchName(display,wdw,&wdw_name) && !strcmp(wdw_name,name))
X+       return(wdw);
X+     
X+     if (!XQueryTree(display,wdw,&junk,&junk,&offspring,&count))
X+       return(0);
X+ 
X+     for (loop = 0; loop < count; loop++)
X+       {
X+ 	  w = window_by_name(offspring[loop],name);
X+ 	  if (w)
X+ 	    break;
X+       }
X+ 
X+     if (offspring)
X+       XFree(offspring);
X+ 
X+     return(w);
X+ }
X+     
X+ Window point_to_window()
X+ {
X+     int status;
X+     Cursor cursor;
X+     XEvent event;
X+     Window target_win = None;
X+     int buttons = 0;
X+     int screen = DefaultScreen(display);
X+     
X+     /* Make the target cursor */
X+     cursor = XCreateFontCursor(display, XC_crosshair);
X+     
X+     /* Grab the pointer using target cursor, letting it room all over */
X+     status = XGrabPointer(display, RootWindow(display, screen), False,
X+ 			  ButtonPressMask|ButtonReleaseMask, GrabModeSync,
X+ 			  GrabModeAsync, None, cursor, CurrentTime);
X+     if (status != GrabSuccess)
X+       {
X+ 	  fprintf(stderr,"Can't grab the mouse.");
X+ 	  exit(4);
X+       }
X+     
X+     /* Let the user select a window... */
X+     while ((target_win == None) || (buttons != 0)) /* allow one more event */
X+       {
X+ 	  XAllowEvents(display, SyncPointer, CurrentTime);
X+ 	  XWindowEvent(display, RootWindow(display, screen),
X+ 		       ButtonPressMask|ButtonReleaseMask, &event);
X+ 	  switch (event.type) 
X+ 	    {
X+ 	      case ButtonPress:
X+ 		if (target_win == None) 
X+ 		  {
X+ 		      /* window selected */
X+ 		      target_win = event.xbutton.subwindow; 
X+ 		      if (target_win == None)
X+ 			target_win = RootWindow(display,screen);
X+ 		  }
X+ 		buttons++;
X+ 		break;
X+ 
X+ 		/* there may have been some down before we started */
X+ 	      case ButtonRelease:
X+ 		if (buttons > 0) 
X+ 		  buttons--;
X+ 		break;
X+ 	    }
X+       } 
X+     
X+     XUngrabPointer(display, CurrentTime);      /* Done with pointer */
X+     
X+     return(target_win);
X  }
SHAR_EOF
sed 's/^X//' << \SHAR_EOF > xcpatch2
X*** xcursor.h.old	Fri May 26 12:42:45 1989
X--- xcursor.h	Thu May 25 10:15:46 1989
X***************
X*** 1,7 ****
X  /*
X!   
X! */
X  
X  #ifndef INCxcursor
X  #define INCxcursor
X  
X--- 1,14 ----
X  /*
X!   Module: xcursor.h
X!   Author: Richard Neitzel
X!   Date:   12 May 1989
X  
X+ revision history
X+ ----------------
X+ 1.0,12may89,rekn      Written.
X+ 2.0,25may89,rekn      Added support for named windows and selection by 
X+                       pointer.
X+ */
X  #ifndef INCxcursor
X  #define INCxcursor
X  
X***************
X*** 8,15 ****
X  #include <stdio.h>
X  #include <string.h>
X  #include <X11/Xlib.h>
X  
X- 
X  Display *display;
X  Window window;
X  
X--- 15,22 ----
X  #include <stdio.h>
X  #include <string.h>
X  #include <X11/Xlib.h>
X+ #include <X11/cursorfont.h>
X  
X  Display *display;
X  Window window;
X  
X***************
X*** 17,23 ****
X  
X  char default_cursor[] = "XC_gumby";
X  
X! char version_number[] = "1.0";
X  
X  char *find_cursor;
X  
X--- 24,30 ----
X  
X  char default_cursor[] = "XC_gumby";
X  
X! char version_number[] = "2.0";
X  
X  char *find_cursor;
X  
X***************
X*** 28,32 ****
X--- 35,40 ----
X  #define BUFSIZE 100
X  
X  void print_vers(), local_help(), work();
X+ Window window_by_name(), point_to_window();
X  
X  #endif
SHAR_EOF
sed 's/^X//' << \SHAR_EOF > xcpatch3
X*** xcursor.l.old	Fri May 26 12:42:52 1989
X--- xcursor.l	Thu May 25 10:11:20 1989
X***************
X*** 2,15 ****
X  .SH NAME
X  xcursor \- set cursor in an X window
X  .SH SYNOPSIS
X! xcursor [-v][-l][-w id] [cursor]
X  .SH DESCRIPTION
X  .I Xcursor
X  allows the cursor for a window to be set to one of the standard X
X! cursors or lists the available cursors. If no window id is specified,
X  .I xcursor
X! will act on the root window by default. If no cursor name is given,
X! the default cursor selected at the time
X  .I xcursor 
X  was built will be used. Cursors are extracted from the file
X  cursorfont.h.
X--- 2,15 ----
X  .SH NAME
X  xcursor \- set cursor in an X window
X  .SH SYNOPSIS
X! xcursor [-v][-l][-id id] [-name name] [-display display][-r] [cursor]
X  .SH DESCRIPTION
X  .I Xcursor
X  allows the cursor for a window to be set to one of the standard X
X! cursors or lists the available cursors. If no window id or name is specified,
X  .I xcursor
X! will prompt the user to selct a window with the pointer. If no cursor
X! name is given, the default cursor selected at the time
X  .I xcursor 
X  was built will be used. Cursors are extracted from the file
X  cursorfont.h.
X***************
X*** 20,28 ****
X  
X  \-v		Prints the version number.
X  
X! \-w		Denotes that the next option is the window id of the
X  		window whose cursor will be effected. May be entered
X  		in decimal, octal or hexadecimal.
X  .fi
X  .SH FILES
X  Requires read access to the cursorfonts.h file (located in the X
X--- 20,34 ----
X  
X  \-v		Prints the version number.
X  
X! \-id		Denotes that the next option is the window id of the
X  		window whose cursor will be effected. May be entered
X  		in decimal, octal or hexadecimal.
X+ 
X+ \-display       Denotes that the named display should be used.
X+ 
X+ \-name          Denotes that named window should be operated on.
X+ 
X+ \-r             Selects the root window.
X  .fi
X  .SH FILES
X  Requires read access to the cursorfonts.h file (located in the X
X***************
X*** 37,39 ****
X--- 43,48 ----
X  Box 3000
X  Boulder, CO 80307
X  thor@thor.ucar.edu
X+ .fi
X+ The selection of named windows and pointing to windows was stolen from
X+ code by Mark Lillibridge.
SHAR_EOF
#	End of shell archive
exit 0