summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Bosveld <Joel.Bosveld@gmail.com>2009-06-25 09:36:48 +0800
committerJoel Bosveld <Joel.Bosveld@gmail.com>2009-06-25 09:38:06 +0800
commit0ac1f017e7b5a6a6f71cddbf04778d16137c1cc4 (patch)
tree212a687321c31e8e238270cb8461b0806c292ad0
parent0c7381bbe58dc29426037caf5686b0474c9a9b25 (diff)
paint: initial commit of xi2 test program
-rw-r--r--paint/Makefile15
-rw-r--r--paint/init.c74
-rw-r--r--paint/paint.c109
-rw-r--r--paint/paint.h49
4 files changed, 247 insertions, 0 deletions
diff --git a/paint/Makefile b/paint/Makefile
new file mode 100644
index 0000000..03cb468
--- /dev/null
+++ b/paint/Makefile
@@ -0,0 +1,15 @@
+PROJECT = paint
+OBJ = paint.o init.o
+
+C99 = gcc --std=gnu99
+CFLAGS = -Wall -g `pkg-config --cflags --libs xi`
+
+$(PROJECT) : $(OBJ)
+ $(C99) $(CFLAGS) -o $(PROJECT) $(OBJ)
+
+%.o : %.c
+ $(C99) $(CFLAGS) -c $< -o $@
+
+clean :
+ rm -f $(OBJ)
+
diff --git a/paint/init.c b/paint/init.c
new file mode 100644
index 0000000..3eced0e
--- /dev/null
+++ b/paint/init.c
@@ -0,0 +1,74 @@
+#include "paint.h"
+
+int
+xInit (void)
+{
+ paint.d = XOpenDisplay(NULL);
+ if(!paint.d) return 1;
+
+ int event, error;
+ if(!XQueryExtension(paint.d, "XInputExtension", &paint.opcode, &event, &error))
+ {
+ fprintf(stdout, "XInput extension not available\n");
+ return 1;
+ }
+
+ int major=2, minor=0;
+ if(XIQueryVersion (paint.d, &major, &minor) == BadRequest)
+ {
+ fprintf(stdout, "XInput 2 not available. Server supports %d.%d\n", major, minor);
+ }
+
+
+ paint.window = XCreateSimpleWindow(paint.d, RootWindow(paint.d, 0),
+ 0, 0, 400, 400, 0, 0, 0);
+
+ XMapWindow(paint.d, paint.window);
+ XFlush(paint.d);
+
+ XIEventMask eventmask;
+ unsigned char mask[1] = {};
+
+ eventmask.deviceid = XIAllMasterDevices;
+ eventmask.mask_len = sizeof (mask);
+ eventmask.mask = mask;
+ XISetMask(mask, XI_ButtonPress);
+ XISetMask(mask, XI_ButtonRelease);
+ XISetMask(mask, XI_Motion);
+
+ XISelectEvents(paint.d, paint.window, &eventmask, 1);
+
+ paint.gc = XCreateGC (paint.d, paint.window, 0, 0);
+
+ XSetLineAttributes(paint.d, paint.gc, 5, LineSolid, CapButt, JoinRound);
+
+ return 0;
+}
+
+int
+pInit (void)
+{
+ paint.paintbox.windows = calloc (sizeof(TestPaintWindow), 3);
+ if(!paint.paintbox.windows) return 1;
+
+ paint.paintbox.nWindows = 3;
+
+ setColour(&paint.paintbox.windows[0].colour, 255,0,0);
+ setColour(&paint.paintbox.windows[1].colour, 0,255,0);
+ setColour(&paint.paintbox.windows[2].colour, 0,0,255);
+
+ paint.paintbox.windows[0].id = XCreateSimpleWindow(paint.d, paint.window,
+ 0, 0, 20, 20, 0, 0,
+ paint.paintbox.windows[0].colour);
+ paint.paintbox.windows[1].id = XCreateSimpleWindow(paint.d, paint.window,
+ 0, 20, 20, 20, 0, 0,
+ paint.paintbox.windows[1].colour);
+ paint.paintbox.windows[2].id = XCreateSimpleWindow(paint.d, paint.window,
+ 0, 40, 20, 20, 0, 0,
+ paint.paintbox.windows[2].colour);
+
+ for(int i=0; i<3; i++) XMapWindow(paint.d, paint.paintbox.windows[i].id);
+ XFlush (paint.d);
+
+ return 0;
+}
diff --git a/paint/paint.c b/paint/paint.c
new file mode 100644
index 0000000..0ef8f80
--- /dev/null
+++ b/paint/paint.c
@@ -0,0 +1,109 @@
+/*
+ * Author: Joel Bosveld <joel.bosveld@gmail.com>
+ */
+
+#include "paint.h"
+#include <limits.h>
+TestPaint paint;
+
+void setColour (long *c, char r, char g, char b)
+{
+ char *colour = (char*) c;
+
+ colour[0]=b;
+ colour[1]=g;
+ colour[2]=r;
+}
+
+void setColourFromWindow (TestCursor *c, Window w)
+{
+ for (int i=0; i<paint.paintbox.nWindows; i++)
+ {
+ if (paint.paintbox.windows[i].id == w)
+ {
+ c->colour = paint.paintbox.windows[i].colour;
+ return;
+ }
+ }
+}
+
+TestCursor *createTestCursor (int xid)
+{
+ TestCursor *c = calloc(sizeof(TestCursor),1);
+ if(!c) return NULL;
+ setColour(&c->colour, 255, 0, 0);
+ c->device_id = xid;
+ c->next = paint.head;
+ paint.head = c;
+
+ return c;
+}
+
+TestCursor *findTestCursor (int xid)
+{
+ TestCursor *c;
+ for(c=paint.head; c; c=c->next) if(c->device_id == xid) return c;
+ return createTestCursor (xid);
+}
+
+int
+main (int argc, char **argv)
+{
+ if(xInit ())
+ return 1;
+ if(pInit ())
+ return 1;
+
+ while(1)
+ {
+ XEvent event;
+ XNextEvent(paint.d, &event);
+ XIDeviceEvent *ev = (XIDeviceEvent *) &event;
+ if (ev->type == GenericEvent && ev->extension == paint.opcode)
+ {
+ switch(ev->evtype)
+ {
+ TestCursor *c = NULL;
+ case XI_ButtonPress:
+ c = findTestCursor(ev->deviceid);
+ if(!c) break;
+ if(ev->child == None)
+ {
+ c->x = (int) ev->event_x;
+ c->y = (int) ev->event_y;
+ c->drawing = 1;
+ }
+ else
+ {
+ setColourFromWindow (c, ev->child);
+ }
+ break;
+
+ case XI_ButtonRelease:
+ c = findTestCursor(ev->deviceid);
+ if(!c) break;
+ c->drawing = 0;
+ break;
+
+ case XI_Motion:
+ c = findTestCursor(ev->deviceid);
+ if(!c) break;
+ if(c->drawing)
+ {
+ XSetForeground (paint.d, paint.gc, c->colour);
+ XDrawLine(paint.d, paint.window, paint.gc, c->x, c->y,
+ (int) ev->event_x, (int) ev->event_y);
+ }
+ c->x = (int) ev->event_x;
+ c->y = (int) ev->event_y;
+
+ break;
+
+ default:
+ fprintf(stderr,"Unknow event type: %d\n", ev->evtype);
+ break;
+ }
+ XIFreeEventData ((XIEvent *) ev);
+ }
+ }
+}
diff --git a/paint/paint.h b/paint/paint.h
new file mode 100644
index 0000000..3faadd0
--- /dev/null
+++ b/paint/paint.h
@@ -0,0 +1,49 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <X11/Xlib.h>
+#include <X11/extensions/XInput2.h>
+
+typedef struct _TestCursor TestCursor;
+
+struct _TestCursor
+{
+ TestCursor *next;
+
+ int x;
+ int y;
+
+ int drawing;
+
+ long colour;
+
+ int device_id;
+};
+
+typedef struct
+{
+ Window id;
+ long colour;
+} TestPaintWindow;
+
+typedef struct
+{
+ TestPaintWindow *windows;
+ int nWindows;
+} TestPaintBox;
+
+typedef struct
+{
+ Display *d;
+ TestCursor *head;
+ TestPaintBox paintbox;
+ Window window;
+
+ int opcode;
+ GC gc;
+} TestPaint;
+
+extern TestPaint paint;
+
+int xInit (void);
+int pInit (void);
+void setColour (long *c, char r, char g, char b);