Skip to content

Commit a100f2b

Browse files
committed
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
1 parent 6ae8328 commit a100f2b

4 files changed

Lines changed: 65 additions & 9 deletions

File tree

examples/Makefile

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@ CC = gcc
22
CFLAGS = -pedantic -Wall -g -I..
33
LDFLAGS = -L.. -lspnav -lX11
44

5-
simple: simple.o
6-
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
5+
all: simple_x11 simple_af_unix
6+
7+
simple_x11: simple.c
8+
$(CC) $(CFLAGS) $(LDFLAGS) -DBUILD_X11 -o $@ $^
9+
simple_af_unix: simple.c
10+
$(CC) $(CFLAGS) $(LDFLAGS) -DBUILD_AF_UNIX -o $@ $^
711

812
.PHONY: clean
913
clean:
10-
rm -f simple simple.o
14+
rm -f simple_x11 simple_x11.o \
15+
simple_af_unix simple_af_unix.o
16+

examples/simple.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,23 @@ void sig(int s)
1212

1313
int main(void)
1414
{
15+
16+
#if defined(BUILD_X11)
1517
Display *dpy;
1618
Window win;
1719
unsigned long bpix;
20+
#endif
21+
1822
spnav_event sev;
1923

2024
signal(SIGINT, sig);
2125

26+
#if defined(BUILD_X11)
27+
2228
if(!(dpy = XOpenDisplay(0))) {
2329
fprintf(stderr, "failed to connect to the X server\n");
2430
return 1;
2531
}
26-
2732
bpix = BlackPixel(dpy, DefaultScreen(dpy));
2833
win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 1, 1, 0, bpix, bpix);
2934

@@ -35,6 +40,15 @@ int main(void)
3540
return 1;
3641
}
3742

43+
#elif defined(BUILD_AF_UNIX)
44+
if(spnav_open()==-1) {
45+
fprintf(stderr, "failed to connect to the space navigator daemon\n");
46+
return 1;
47+
}
48+
#else
49+
#error Unknown build type!
50+
#endif
51+
3852
/* spnav_wait_event() and spnav_poll_event(), will silently ignore any non-spnav X11 events.
3953
*
4054
* If you need to handle other X11 events you will have to use a regular XNextEvent() loop,

spnav.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ OF SUCH DAMAGE.
3636
#include <sys/select.h>
3737
#include "spnav.h"
3838

39+
#define SPNAV_SOCK_PATH "/tmp/.spnav.sock"
40+
3941
#ifdef USE_X11
4042
#include <X11/Xlib.h>
4143
#include <X11/Xutil.h>
@@ -52,9 +54,10 @@ enum {
5254
CMD_APP_SENS
5355
};
5456

55-
#define IS_OPEN (dpy || sock)
57+
/* TODO: note: 0 is a valid socket fd, -1 isn't */
58+
#define IS_OPEN (dpy || (sock != -1))
5659
#else
57-
#define IS_OPEN (sock)
60+
#define IS_OPEN (sock != -1)
5861
#endif
5962

6063
struct event_node {
@@ -65,7 +68,8 @@ struct event_node {
6568
/* only used for non-X mode, with spnav_remove_events */
6669
static struct event_node *ev_queue, *ev_queue_tail;
6770

68-
static int sock;
71+
/* AF_UNIX socket used for alternative communication with daemon */
72+
static int sock = -1;
6973

7074

7175
int spnav_open(void)
@@ -80,6 +84,7 @@ int spnav_open(void)
8084
if(!(ev_queue = malloc(sizeof *ev_queue))) {
8185
return -1;
8286
}
87+
ev_queue->next = 0;
8388
ev_queue_tail = ev_queue;
8489

8590
if((s = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
@@ -88,7 +93,8 @@ int spnav_open(void)
8893

8994
memset(&addr, 0, sizeof addr);
9095
addr.sun_family = AF_UNIX;
91-
strcpy(addr.sun_path, "/tmp/spacenav_usock");
96+
strncpy(addr.sun_path, SPNAV_SOCK_PATH, sizeof(addr.sun_path));
97+
9298

9399
if(connect(s, (struct sockaddr*)&addr, sizeof addr) == -1) {
94100
perror("connect failed");
@@ -258,7 +264,7 @@ int spnav_fd(void)
258264
}
259265
#endif
260266

261-
return sock ? sock : -1;
267+
return sock;
262268
}
263269

264270
static int event_pending(int s)

spnav.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,29 @@ typedef union spnav_event {
6464
extern "C" {
6565
#endif
6666

67+
/* Open connection to the daemon via AF_UNIX socket.
68+
* The unix domain socket interface is an alternative to the original magellan
69+
* protocol, and it is *NOT* compatible with the 3D connexion driver. If you wish
70+
* to remain compatible, use the X11 protocol (spnav_x11_open, see below).
71+
* Returns -1 on failure.
72+
*/
6773
int spnav_open(void);
74+
75+
/* Close connection to the daemon. Use it for X11 or AF_UNIX connections.
76+
* Returns -1 on failure
77+
*/
6878
int spnav_close(void);
6979

7080
/* Retrieves the file descriptor used for communication with the daemon, for
7181
* use with select() by the application, if so required.
7282
* If the X11 mode is used, the socket used to communicate with the X server is
7383
* returned, so the result of this function is always reliable.
84+
* If AF_UNIX mode is used, the fd of the socket is returned or -1 if
85+
* no connection is open / failure occured.
7486
*/
7587
int spnav_fd(void);
7688

89+
/* TODO: document */
7790
int spnav_sensitivity(double sens);
7891

7992
/* blocks waiting for space-nav events. returns 0 if an error occurs */
@@ -94,9 +107,26 @@ int spnav_remove_events(int type);
94107

95108

96109
#ifdef USE_X11
110+
/* Opens a connection to the daemon, using the original magellan X11 protocol.
111+
* Any application using this protocol should be compatible with the proprietary
112+
* 3D connexion driver too.
113+
*/
97114
int spnav_x11_open(Display *dpy, Window win);
115+
116+
/* Sets the application window, that is to receive events by the driver.
117+
*
118+
* NOTE: Any number of windows can be registered for events, when using the
119+
* free spnavd daemon. I suspect that the proprietary 3D connexion daemon only
120+
* sends events to one window at a time, thus this function replaces the window
121+
* that receives events. If compatibility with 3dxsrv is required, do not
122+
* assume that you can register multiple windows.
123+
*/
98124
int spnav_x11_window(Window win);
99125

126+
/* Examines an arbitrary X11 event. If it's a spnav event, it returns the event
127+
* type (SPNAV_EVENT_MOTION or SPNAV_EVENT_BUTTON) and fills in the spnav_event
128+
* structure passed through "event" accordingly. Otherwise, it returns 0.
129+
*/
100130
int spnav_x11_event(const XEvent *xev, spnav_event *event);
101131
#endif
102132

0 commit comments

Comments
 (0)