Hello List,

I just made the attached sample application using the C-API from CVS.

It is generating X-events from two buttons connected to a DS2413. This may
also work with similar IO-devices.

I intend to use this for control of OO-Presenter in a talk about owfs I
will give to our local Linux User Group next week :)

Sven

-- 
Threading is a performance hack.
(The Art of Unix Programming by Eric S. Raymond)

/me is [EMAIL PROTECTED], http://sven.gegg.us/ on the Web
/* xevent.c

generate xevents from two buttons
connected via 1-wire BUS to a DS2413 device

(c) 2005 Sven Geggus <[EMAIL PROTECTED]>

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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <X11/extensions/XTest.h>
#include <owcapi.h>

#define BUS "localhost:4711"
#define PIOID "3A.A13100000000"
#define EVENTA "Left"
#define EVENTB "Right"

struct keymodlist_t {
  char *name;
  Mask mask;
};

static struct keymodlist_t keymodlist[]=
  {
    {"SHIFT",   ShiftMask},
    {"CAPS",    LockMask},
    {"CTRL",    ControlMask},
    {"ALT",     Mod1Mask},{"META",    Mod1Mask},
    {"NUMLOCK", Mod2Mask},
    {"MOD3",    Mod3Mask},  /* I don't have a clue what key maps to this. */
    {"MOD4",    Mod4Mask},  /* I don't have a clue what key maps to this. */
    {"SCRLOCK", Mod5Mask},
    {NULL,0},
  };

const char *key_delimiter ="-";


Display *dpy;
Window root,w;
XEvent xev;

void make_key(char *keyname,int x, int y,XKeyEvent *xev) {
  char *part, *part2;
  struct keymodlist_t *kmlptr;

  part2=malloc(128);

  xev->type = KeyPress;
  xev->display=dpy;
  xev->root=root;
  xev->subwindow = None;
  xev->x=x; xev->y=y;
  xev->x_root=1; xev->y_root=1;
  xev->same_screen = True;

  xev->state=0;
  while ((part=strsep(&keyname, key_delimiter)))
    {
      part2=strncpy(part2,part,128);
      kmlptr=keymodlist;
      while (kmlptr->name)
	{
	  if (!strcasecmp(kmlptr->name, part))
	    xev->state|=kmlptr->mask;
	  kmlptr++;
	}
    } 
  xev->keycode=XKeysymToKeycode(dpy,XStringToKeysym(part2));
  free(part2);
  return ;
}

void sendkey(char *keyname,int x,int y,Window w,Window s,int edge) {
  make_key(keyname ,x,y,(XKeyEvent*)&xev);
  xev.xkey.window=w;
  xev.xkey.subwindow=s;

  XTestFakeKeyEvent(dpy, xev.xkey.keycode, edge, 0);
  XSync(dpy,True);
  return;
}

void die(const char *s) { perror(s); exit(EXIT_FAILURE); }

int main() {
  char *s;
  size_t n;
  int ioA,ioB,cioA,cioB;
  struct timeval timeout;

  dpy=XOpenDisplay(NULL);
  if(dpy==NULL) {
    fprintf(stderr,"Can't open DISPLAY.\n");
    exit(1);
  }
                                                
  if(OW_init(BUS) !=0) die("OW_init");

  timeout.tv_sec=0;
  timeout.tv_usec=100000;
  
  ioA=0;
  ioB=0;
  while (1) {
    OW_get("/uncached/"PIOID"/PIO.ALL",&s,&n);
    cioA=s[0]-48;
    cioB=s[2]-48;
    /* positive edge Button A */
    if ((ioA==0)&&(cioA==1)) {
      sendkey(EVENTA,1,1,w,0,True);
    }
    /* negative edge Button A */
    if ((ioA==1)&&(cioA==0)) {
      sendkey(EVENTA,1,1,w,0,False);
    }
    /* positive edge Button B */
    if ((ioB==0)&&(cioB==1)) {
      sendkey(EVENTB,1,1,w,0,True);
    }
    /* negative edge Button B */
    if ((ioB==1)&&(cioB==0)) {
      sendkey(EVENTB,1,1,w,0,False);
    }

    ioA=cioA;
    ioB=cioB;          
    
    select(0,NULL, NULL, NULL, &timeout);
    timeout.tv_sec=0;
    timeout.tv_usec=100000;
  }

  OW_finish();
  exit(EXIT_SUCCESS);
}
              
INCLUDES = -I/opt/owfs/include/

CFLAGS = -g -Wall -W $(INCLUDES)

LDLIBS = -lX11 -lXtst -lowcapi -L/usr/X11R6/lib -L/opt/owfs/lib 

SRC = xevent.c

OBJ = $(SRC:%.c=%.o)

xevent:$(OBJ)
        $(CC) -o $@ $(CFLAGS) $(OBJ) $(LDLIBS)
        
clean:
        rm -f *.o *~ xevent


Reply via email to