home *** CD-ROM | disk | FTP | other *** search
- /*Copyright (c) 1991 Xerox Corporation. All Rights Reserved.
-
- Permission to use, copy, modify and distribute without
- charge this software, documentation, images, etc. is grant-
- ed, provided that this copyright and the author's name is
- retained.
-
- A fee may be charged for this program ONLY to recover costs
- for distribution (i.e. media costs). No profit can be made
- on this program.
-
- The author assumes no responsibility for disasters (natural
- or otherwise) as a consequence of use of this software.
-
- Adam Stein (stein.wbst129@xerox.com)
- */
-
- #include <stdio.h>
- #include <X11/Xos.h>
- #include <X11/Xlib.h>
- #include <X11/Xutil.h>
-
- extern char *program;
- extern Display *display;
-
- /*This function will get the window and icon geometry and add them to the
- command line. For window geometry, it will add position and size. For
- icon geometry, it will only add position. This function makes some
- assumptions:
-
- 1) windows use "-g" for window geometry (which is an abbreviation for
- "-geom" or "-geometry")
- 2) normal applications use "-iconGeom" for icon geometry while
- xterm uses "#"
- 3) normal applications take the geometry size in pixels while
- xterm takes geometry size in characters
-
- Inputs: command_line - command line arguments
- window - ID of the window that we are going to check
- Outputs: command_line - command line arguments with geometry info added
- Locals: dummy - dummary variable for return value we don't want
- dummywin - dummary variable for return value we don't want
- geometry_args - geometry arguments to add to command line
- height - height of window (in pixels)
- height_inc - height increment
- hints - window size hints
- icon_arg - icon geometry argument
- icon_hints - icon position hints
- icon_x - X coordinate of icon
- icon_y - Y coordinate of icon
- root - root window of this window
- status - status of get call
- supplied - flag telling which size hints are available
- tx - translated X coordinate
- ty - translated Y coordinate
- width - width of window (in pixels)
- width_inc - width increment
- x - X coordinate of window
- y - Y coordinate of window
- Globals: display - interface info to X display
- program - name of currently executing program
- NULL - 0
- PResizeInc - flag bit to check if resize increment hint is avail.
- */
- char *get_geometry(window,command_line)
- register char *command_line;
- register Window window;
- {
- register int width_inc,height_inc;
- int x,y,tx,ty,icon_x,icon_y;
- unsigned int width,height,dummy;
- long supplied;
- register char *icon_arg,geometry_args[100];
- register Status status;
- Window root,dummywin;
- XSizeHints hints;
- register XWMHints *icon_hints;
- char *strip_geometry(),*strip_xterm_geometry(),*realloc(),*strdup();
-
- /*Get icon position*/
- icon_x = icon_y = -1;
- icon_hints = XGetWMHints(display,window);
- if(icon_hints) {
- icon_x = icon_hints->icon_x;
- icon_y = icon_hints->icon_y;
- XFree(hints);
- }
-
- /*Get window geometry*/
- status = XGetGeometry(display,window,&root,&x,&y,&width,&height,&dummy,
- &dummy);
-
- if(status) {
- width_inc = 1;
- height_inc = 1;
- icon_arg = "-iconGeom ";
-
- /*Translate coordinate relative to root window*/
- XTranslateCoordinates(display,window,root,0,0,&tx,&ty,&dummywin);
-
- /*Xterm is a special case (isn't it always?). Unlike other programs,
- xterm takes it's dimensions in characters, not pixels. This
- means we have to get it's increment size (i.e. font size) and
- divide the size of the window (in pixels) by it's font size to
- determine it's character size. In addition, the argument to
- specify icon position is different from normal*/
- if(is_xterm(window) == 1) {
- /*Get increment (i.e. font size)*/
- status = XGetWMNormalHints(display,window,&hints,&supplied);
- if(!status || !(supplied & PResizeInc)) return(command_line);
-
- width_inc = hints.width_inc;
- height_inc = hints.height_inc;
- icon_arg = "#";
-
- /*Strip old geometry arguments out of command line (if the
- command line has any geometry arguments)*/
- command_line = strip_xterm_geometry(command_line);
- } else command_line = strip_geometry(command_line);
-
- /*Put geometry information together in a string. Put in icon
- information is there was icon information*/
- if(icon_x == -1)
- sprintf(geometry_args," -geom %dx%d+%d+%d",width/width_inc,height/height_inc,tx-x,ty-y);
- else
- sprintf(geometry_args," -geom %dx%d+%d+%d %s+%d+%d",width/width_inc,height/height_inc,tx-x,ty-y,icon_arg,icon_x,icon_y);
-
- /*Add window and icon geometry*/
- if((command_line = realloc(command_line,strlen(command_line)+
- strlen(geometry_args)+1)) == NULL) {
- perror(program);
- exit(1);
- }
-
- strcat(command_line,geometry_args);
- }
-
- return(command_line);
- }
-
- /*This will strip out the geometry arguments for a normal X application.
-
- Inputs: command_line - command line arguments
- Outputs: command_line - command line arguments without geometry info
- Locals: none
- Globals: none
- */
- char *strip_geometry(command_line)
- register char *command_line;
- {
- char *strip_arg();
-
- /*Strip window geometry*/
- command_line = strip_arg(command_line,"-g");
-
- /*Strip icon geometry*/
- command_line = strip_arg(command_line,"-iconGeom");
-
- return(command_line);
- }
-
- /*This will strip out the geometry arguments for an xterm window.
-
- Inputs: command_line - command line arguments
- Outputs: command_line - command line arguments without geometry info
- Locals: none
- Globals: none
- */
- char *strip_xterm_geometry(command_line)
- register char *command_line;
- {
- char *strip_arg();
-
- /*Strip window geometry*/
- command_line = strip_arg(command_line,"-g");
-
- /*Strip icon geometry*/
- command_line = strip_arg(command_line,"#");
-
- return(command_line);
- }
-
- /*This function will strip an argument from a string.
-
- Inputs: arg - argument to strip
- command_line - command line arguments
- Outputs: new_command_line - command line without 'arg'
- Locals: new_command_line - command line without 'arg'
- numbytes - number of bytes to copy from old command line
- pointer1 - pointer to start of 'arg'
- pointer2 - pointer to any arguments after 'arg'
- Globals: program - name of currently executing program
- NULL - 0
- */
- char *strip_arg(command_line,arg)
- register char *command_line,*arg;
- {
- register int numbytes;
- register char *pointer1,*pointer2,*new_command_line;
- char *malloc(),*strstr();
-
- /*If the argument to strip isn't here, then just return the original
- string*/
- if((pointer1 = strstr(command_line,arg)) == NULL)
- return(command_line);
- else {
- /*Find next character after argument*/
- pointer2 = pointer1 + strlen(arg);
- pointer2 = index(pointer2+1,' ');
-
- /*Find out how much space to allocate for command line minus
- the argument to strip*/
- /*Null out byte before argument to be able to use strlen()*/
- *pointer1 = '\0';
- numbytes = strlen(command_line);
- /*Count bytes after argument (if there are any) plus 1 for NULL*/
- /*Have to use pointer2+1 instead of doing ++pointer2 so we
- can check if pointer2 = NULL (++pointer2 would have it = 1)*/
- if(pointer2) numbytes += strlen(pointer2+1)+1;
-
- /*Allocate space and copy over all arguments except argument*/
- if((new_command_line = malloc(numbytes)) == NULL) {
- perror(program);
- exit(1);
- }
- strcpy(new_command_line,command_line);
- if(pointer2) strcat(new_command_line,pointer2+1);
-
- /*Restore command_line to it's original condition so we can
- free up all the space it's using*/
- *pointer1 = '-';
- free(command_line);
- }
-
- return(new_command_line);
- }
-
-