Subject: A very simple module

From: Matthias Clasen <[email-masked]>
Date: Sat, 13 Feb 1999 01:46:54 +0100

While working on fvwm, using cut and paste   plus
FvwmTalk  to test things can become a bit clumsy.

Here is a very simple module that allows to execute
the selection directly. 

/*
 * FvwmEval! Selection module for Fvwm
 *
 * Copyright (c) 1999 Matthias Clasen <[email-masked]>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "config.h"

#ifdef HAVE_SYS_BSDTYPES_H
#include <sys/bsdtypes.h> /* Saul */
#endif

#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/time.h>

#if HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif

#include <unistd.h>
#include <ctype.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>

#include <X11/Xutil.h>
#include <X11/Xproto.h>
#include <X11/Xatom.h>
#include <X11/Intrinsic.h>

#include "../../fvwm/module.h"

int fd[2];
Display *dpy;
Window window;
Window app_win;
char *display_name = NULL;
char *my_name = "FvwmEval";

void paste_primary(int window,int property,int Delete)
{
  Atom actual_type;
  int actual_format,i;
  unsigned long nitems, bytes_after, nread;
  unsigned char *data, *d, *h, buf[256];

  if (property == None) 
    {
      window = DefaultRootWindow(dpy);
      property = XA_CUT_BUFFER0;
      Delete = False;
    }
      
  nread = 0;
  h = buf;
  do
    {
      if (XGetWindowProperty (dpy, window, property, nread/4, 1024,
			      Delete, AnyPropertyType, &actual_type, 
			      &actual_format, &nitems, &bytes_after, 
			      (unsigned char **)&data) != Success) 
        return;
      if (actual_type != XA_STRING) 
        return;

      /* send the text line-by-line, recognize continuation lines,
         strip leading spaces */
      d = data;
      while (1) {
	switch (*d) {
	case '\n':
	  *h = 0;
	  break;
	case '\\':
	    if (d[1] == '\n')
	      {
		*h = ' ';
		d++;
                break;
	      }
	    /* fall through */
	default:
	  *h = *d;
	}
	if (h - buf == 255) 
	  {
	    fprintf(stderr, "%s: line too long\n", my_name);
	    *h = 0;
	  }
	if (*h == 0)
	  {
            h = buf; 
            while (*h == ' ') h++;
	    SendText (fd, h, app_win);
	    h = buf;
	  } 
	else h++;
	if (*d == 0) break;
	d++;
      }
      
      nread += nitems;
      XFree (data);
    } while (bytes_after > 0);
}

void DeadPipe(int nonsense)
{
  fprintf(stderr,"%s: dead pipe\n", my_name);
  exit(0);
}

int main(int argc, char **argv)
{
  XEvent event;
  Atom sel_property;

  signal (SIGPIPE, DeadPipe);

  fd[0] = atoi (argv[1]);
  fd[1] = atoi (argv[2]);

  sscanf (argv[4], "%x", (unsigned int *)&app_win);

  if (!(dpy = XOpenDisplay (display_name)))
    {
      fprintf (stderr, "%s: can't open display %s", my_name, 
	       XDisplayName (display_name));
      exit (1);
    }

  window = XCreateSimpleWindow (dpy, DefaultRootWindow(dpy), 0,0,10,10, 0,0,0);
  sel_property = XInternAtom (dpy, "VT_SELECTION", False);

  XConvertSelection (dpy, XA_PRIMARY, XA_STRING, sel_property, window, 
		     CurrentTime);

  while (1) 
    {
      XNextEvent (dpy, &event);
      if (event.type == SelectionNotify) 
	{
	  paste_primary (event.xselection.requestor,
			 event.xselection.property, True);
	  exit (0);
	}
    }
}

-- 
Matthias Clasen, 
Tel. 0761/203-5606
Email: [email-masked]
Mathematisches Institut, Albert-Ludwigs-Universitaet Freiburg
--
Visit the official FVWM web page at <URL:http://fvwm.math.uh.edu/>.
To unsubscribe from the list, send "unsubscribe fvwm-workers" in the
body of a message to [email-masked].
To report problems, send mail to [email-masked].