Commited Michael Arndt's patch, fixing the alternative AF_UNIX interface.

git-svn-id: svn+ssh://svn.code.sf.net/p/spacenav/code/libspnav@12 ef983eb1-d774-4af8-acfd-baaf7b16a646
pull/2/head
John Tsiombikas 2008-02-20 15:38:05 +00:00
rodzic 6ae83282a6
commit a100f2ba0b
4 zmienionych plików z 65 dodań i 9 usunięć

Wyświetl plik

@ -2,9 +2,15 @@ CC = gcc
CFLAGS = -pedantic -Wall -g -I..
LDFLAGS = -L.. -lspnav -lX11
simple: simple.o
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
all: simple_x11 simple_af_unix
simple_x11: simple.c
$(CC) $(CFLAGS) $(LDFLAGS) -DBUILD_X11 -o $@ $^
simple_af_unix: simple.c
$(CC) $(CFLAGS) $(LDFLAGS) -DBUILD_AF_UNIX -o $@ $^
.PHONY: clean
clean:
rm -f simple simple.o
rm -f simple_x11 simple_x11.o \
simple_af_unix simple_af_unix.o

Wyświetl plik

@ -12,18 +12,23 @@ void sig(int s)
int main(void)
{
#if defined(BUILD_X11)
Display *dpy;
Window win;
unsigned long bpix;
#endif
spnav_event sev;
signal(SIGINT, sig);
#if defined(BUILD_X11)
if(!(dpy = XOpenDisplay(0))) {
fprintf(stderr, "failed to connect to the X server\n");
return 1;
}
bpix = BlackPixel(dpy, DefaultScreen(dpy));
win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 1, 1, 0, bpix, bpix);
@ -35,6 +40,15 @@ int main(void)
return 1;
}
#elif defined(BUILD_AF_UNIX)
if(spnav_open()==-1) {
fprintf(stderr, "failed to connect to the space navigator daemon\n");
return 1;
}
#else
#error Unknown build type!
#endif
/* spnav_wait_event() and spnav_poll_event(), will silently ignore any non-spnav X11 events.
*
* If you need to handle other X11 events you will have to use a regular XNextEvent() loop,

16
spnav.c
Wyświetl plik

@ -36,6 +36,8 @@ OF SUCH DAMAGE.
#include <sys/select.h>
#include "spnav.h"
#define SPNAV_SOCK_PATH "/tmp/.spnav.sock"
#ifdef USE_X11
#include <X11/Xlib.h>
#include <X11/Xutil.h>
@ -52,9 +54,10 @@ enum {
CMD_APP_SENS
};
#define IS_OPEN (dpy || sock)
/* TODO: note: 0 is a valid socket fd, -1 isn't */
#define IS_OPEN (dpy || (sock != -1))
#else
#define IS_OPEN (sock)
#define IS_OPEN (sock != -1)
#endif
struct event_node {
@ -65,7 +68,8 @@ struct event_node {
/* only used for non-X mode, with spnav_remove_events */
static struct event_node *ev_queue, *ev_queue_tail;
static int sock;
/* AF_UNIX socket used for alternative communication with daemon */
static int sock = -1;
int spnav_open(void)
@ -80,6 +84,7 @@ int spnav_open(void)
if(!(ev_queue = malloc(sizeof *ev_queue))) {
return -1;
}
ev_queue->next = 0;
ev_queue_tail = ev_queue;
if((s = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
@ -88,7 +93,8 @@ int spnav_open(void)
memset(&addr, 0, sizeof addr);
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, "/tmp/spacenav_usock");
strncpy(addr.sun_path, SPNAV_SOCK_PATH, sizeof(addr.sun_path));
if(connect(s, (struct sockaddr*)&addr, sizeof addr) == -1) {
perror("connect failed");
@ -258,7 +264,7 @@ int spnav_fd(void)
}
#endif
return sock ? sock : -1;
return sock;
}
static int event_pending(int s)

30
spnav.h
Wyświetl plik

@ -64,16 +64,29 @@ typedef union spnav_event {
extern "C" {
#endif
/* Open connection to the daemon via AF_UNIX socket.
* The unix domain socket interface is an alternative to the original magellan
* protocol, and it is *NOT* compatible with the 3D connexion driver. If you wish
* to remain compatible, use the X11 protocol (spnav_x11_open, see below).
* Returns -1 on failure.
*/
int spnav_open(void);
/* Close connection to the daemon. Use it for X11 or AF_UNIX connections.
* Returns -1 on failure
*/
int spnav_close(void);
/* Retrieves the file descriptor used for communication with the daemon, for
* use with select() by the application, if so required.
* If the X11 mode is used, the socket used to communicate with the X server is
* returned, so the result of this function is always reliable.
* If AF_UNIX mode is used, the fd of the socket is returned or -1 if
* no connection is open / failure occured.
*/
int spnav_fd(void);
/* TODO: document */
int spnav_sensitivity(double sens);
/* blocks waiting for space-nav events. returns 0 if an error occurs */
@ -94,9 +107,26 @@ int spnav_remove_events(int type);
#ifdef USE_X11
/* Opens a connection to the daemon, using the original magellan X11 protocol.
* Any application using this protocol should be compatible with the proprietary
* 3D connexion driver too.
*/
int spnav_x11_open(Display *dpy, Window win);
/* Sets the application window, that is to receive events by the driver.
*
* NOTE: Any number of windows can be registered for events, when using the
* free spnavd daemon. I suspect that the proprietary 3D connexion daemon only
* sends events to one window at a time, thus this function replaces the window
* that receives events. If compatibility with 3dxsrv is required, do not
* assume that you can register multiple windows.
*/
int spnav_x11_window(Window win);
/* Examines an arbitrary X11 event. If it's a spnav event, it returns the event
* type (SPNAV_EVENT_MOTION or SPNAV_EVENT_BUTTON) and fills in the spnav_event
* structure passed through "event" accordingly. Otherwise, it returns 0.
*/
int spnav_x11_event(const XEvent *xev, spnav_event *event);
#endif