forked from coin3d/sogui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spwinput_x11.cpp.in
1982 lines (1733 loc) · 59 KB
/
spwinput_x11.cpp.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* @configure_input@ */
/*-----------------------------------------------------------------------------
*
* (C) 1998 Spacetec IMC Corporation ("Spacetec").
*
* Permission to use, copy, modify, and distribute this software for all
* purposes and without fees is hereby granted provided that this copyright
* notice appears in all copies. Permission to modify this software is granted
* and Spacetec will support such modifications only if said modifications are
* approved by Spacetec.
*
*/
/* Some code cleanup by the Coin team. Also added configure tests for
* header files and added two methods:
*
* int SPW_CheckForSpaceballX11(void * display, int winid, char * product);
* int SPW_TranslateEventX11(void * display, void * xevent, SPW_InputEvent * sbEvent);
*/
/* FIXME: isn't this really "pure" C code? If so, don't use a C++
suffix on the file -- so we don't get tempted to pollute
it. 20010821 mortene. */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */
#if SO@GUI@_DEBUG && 0 // debug
#define SPW_DEBUG 1
#endif // debug
#include "spwinput.h"
/* The setting of this define needs to be added manually to
configure.in for all relevant projects. */
#ifndef HAVE_X11_AVAILABLE
/* just provide empty methods if X is not available */
int
SPW_CheckForSpaceballX11(void *, int, char *)
{
return 0;
}
int
SPW_TranslateEventX11(void *, void *, SPW_InputEvent *)
{
return 0;
}
#else /* HAVE_X11_AVAILABLE */
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif // HAVE_UNISTD_H
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif /* HAVE_NETINET_IN_H */
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif /* HAVE_SYS_TIME_H */
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif /* HAVE_SYS_TYPES_H */
#ifdef HAVE_SELECT_H
#include <select.h>
#endif /* HAVE_SELECT_H */
#define XLIB_ILLEGAL_ACCESS
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#ifdef HAVE_X11_EXTENSIONS_SGIMISC_H
#include <X11/extensions/SGIMisc.h>
#elif HAVE_X11_XPROTO_H
#include <X11/Xproto.h>
#endif /* HAVE_X11_XPROTO */
#ifdef HAVE_X11_EXTENSIONS_XINPUT_H
#include <X11/extensions/XInput.h>
#endif /* HAVE_X11_EXTENSIONS_XINPUT_H */
/* Callbacks for handling motion and button events */
typedef void (*SPW_InputMotionEventHandler)(Display *,float *,void *);
typedef void (*SPW_InputButtonEventHandler)(Display *,int,void *);
typedef int (*SPW_InputOtherEventHandler)(Display *,XEvent *,void *);
/* Dispatch structure for SPW_InputDispatch */
typedef struct {
Display * display;
XEvent * xevent;
SPW_InputEvent * spwevent;
int delay;
SPW_InputMotionEventHandler handle_motion;
SPW_InputButtonEventHandler handle_bpress;
SPW_InputButtonEventHandler handle_brelease;
SPW_InputOtherEventHandler handle_other;
void * pMotionAppData;
void * pButtonAppData;
void * pOtherAppData;
} SPW_InputDispatchStruct;
/* ------------------------------------------------------------------------ */
static int SPW_InputCheckForSpaceball(Display *, Window, char *);
static int SPW_InputIsSpaceballEvent (Display *, XEvent *,
SPW_InputEvent *);
static int SPW_InputXPending (Display *, int);
static void SPW_InputInitDispatchStruct (SPW_InputDispatchStruct *);
static int SPW_InputDispatch (SPW_InputDispatchStruct *);
static int SPW_InputDispatchEx (SPW_InputDispatchStruct *);
static void SPW_InputBeep (Display *, char *);
static void SPW_InputRezero (Display *);
static int SPW_GrabDevice(Display *display, Window window,
int exclusive);
static int SPW_ReleaseDevice(Display * display);
#define SPW_InputDefaultDelay 0 /* 0 Hz update */
/*
Wrapper function which makes it possible to avoid including X11 in the
header file.
*/
int
SPW_CheckForSpaceballX11(void * display, int winid, char * product)
{
return SPW_InputCheckForSpaceball((Display*) display, (Window) winid, product);
}
/*
If xevent is a spaceball event, translates xevent to sbEvent and return 1.
If xevent is not a spaceball event returns 0. void * pointers are
passed to avoid including the X header files in the spwinput.h header file.
*/
int
SPW_TranslateEventX11(void * display, void * xevent, SPW_InputEvent * sbEvent)
{
return SPW_InputIsSpaceballEvent((Display*) display, (XEvent*) xevent, sbEvent);
}
/*
* This is the number of events before we stop asking the
* driver for handshaking information.
*
* 25 events/sec * 60 sec * 1 min = 1500
*/
#define SPW_MaxEventCount 1500
/* these are all the Spaceball XCM atom names */
#define SPW_InputMotionAtomName "SpaceballMotionEventType"
#define SPW_InputButtonPressAtomName "SpaceballButtonPressEventType"
#define SPW_InputButtonReleaseAtomName "SpaceballButtonReleaseEventType"
#define SPW_InputPassThruAtomName "SpaceWarePassThruCommandEventType"
/* found the spaceball */
static int SpaceballFound = 0;
/* the spaceball is using XIE */
static int SpaceballInputExtension = 0;
/* the spaceball is using XCM */
static int SpaceWareAtomsDefined = 0;
/* the magic XCM window used for communication */
static Window SpaceWareXCMWindowID = 0;
/* the window ID that the spaceball data is sent to */
static Window ReturnWindowID = 0;
/* XSendEvent uses these types and Atoms to communicate with the driver */
static int SPW_DevMotionEventType; /* Spaceball motion type */
static int SPW_DevButtonPressEventType; /* Spaceball button press type */
static int SPW_DevButtonReleaseEventType; /* Spaceball button release type */
static Atom SPW_InputMotionAtom; /* Spaceball motion Atom */
static Atom SPW_InputButtonPressAtom; /* Spaceball button press Atom */
static Atom SPW_InputButtonReleaseAtom; /* Spaceball button release Atom */
static Atom SPW_InputPassThruAtom; /* Main daemon communication Atom */
static Atom WM_SAVE_YOURSELF; /* Atoms daemon will use */
#ifdef HAVE_X11_EXTENSIONS_XINPUT_H
static XID SpaceballDevID = 0; /* XIE ID for the Spaceball device */
static XDevice *pSpaceballDev = NULL; /* The spaceball XIE structure */
static XEventClass SPW_SpaceballEventClass[3]; /* XIE Events classes */
#endif /* HAVE_X11_EXTENSIONS_XINPUT_H */
static char strData[19]; /* buffer used to store the string that
is to be sent to XCM daemon */
static int SPW_strLength = 0; /* length of the strData string */
/*-----------------------------------------------------------------------------
*
* static Window FindXCMWindow (Display *display)
*
* Args:
* display (r/o) The display on which to search for the XCM window
*
* Return Value:
* The window ID of the XCM window.
*
* Description:
* Finds the window named "sballd_XCM" and returns its window ID
*
* Notes:
* None.
*
*---------------------------------------------------------------------------*/
static Window
FindXCMWindow (Display *display)
{
Window root; /* the root window for the display */
Window parent; /* parent window in the search tree */
Window *children; /* children windows of the parent window */
unsigned int nChildren; /* number of children the parent window has */
int i; /* counter */
/* if we already have the daemon window don't look again */
if (SpaceWareXCMWindowID != 0) {
return SpaceWareXCMWindowID;
}
XQueryTree(display, RootWindowOfScreen(DefaultScreenOfDisplay(display)),
&root, &parent, &children, &nChildren);
for (i = 0; i < (int) nChildren; i++){
char *name;
int found = 0;
XFetchName (display, children[i], &name);
if (name) {
#ifdef SPW_DEBUG
fprintf(stderr,"FindXCMWindow: %s\n", name);
#endif
if (strcmp(name, "sballd_XCM") == 0) {
found = 1;
}
XFree(name);
}
if (found == 1) {
break;
}
}
if (i == (int)nChildren) {
SpaceWareXCMWindowID = 0;
}
else {
SpaceWareXCMWindowID = children[i];
}
XFree((char *) children);
if (SpaceWareXCMWindowID == 0) {
return False;
}
else {
return SpaceWareXCMWindowID;
}
} /* end of FindXCMWindow */
/*-----------------------------------------------------------------------------
*
* static void StringFlush (Display *display, Window win)
*
* Args:
* display (r/o) The X Display of which the XCM window is a resident
* win (r/o) The window that the XCM daemon replies to
*
* Return Value:
* None.
*
* Description:
* This function sends the string that is in the strData buffer to the
* XCM window. Once it is send it clears the strData buffer.
*
* Notes:
* None.
*
*---------------------------------------------------------------------------*/
static void StringFlush (Display *display, Window win)
{
XClientMessageEvent event; /* event used to send string to XCM daemon */
int i; /* counter used to step through the string */
/* if we don't already have the XCM window id find it */
if (SpaceWareXCMWindowID == 0) {
FindXCMWindow (display);
}
if (SpaceWareXCMWindowID == 0) {
return;
}
if (SPW_strLength == 0) { /* if the string is empty do nothing */
return;
}
/* set up the event structure for sending */
event.type = ClientMessage;
event.display = display;
event.window = SpaceWareXCMWindowID;
event.message_type = SPW_InputPassThruAtom;
event.format = 8;
/* copy the string into the event */
for (i = 0; i < 15; i++) {
event.data.b[i] = strData[i];
}
if (SPW_strLength > 15) {
SPW_strLength = 15;
}
/* force zero on end of data */
event.data.b[SPW_strLength] = 0;
event.data.l[4] = (long)htonl(win);
#ifdef SPW_DEBUG
fprintf (stderr,"StringFlush %s\n", event.data.b);
#endif
/* send the string to the XCM daemon */
if (XSendEvent (display, SpaceWareXCMWindowID, True, 0,
(XEvent *)&event) == 0) {
#ifdef SPW_DEBUG
fprintf (stderr, "SPW_Input: XSendEvent failed!\n");
#endif
}
XSync (display,False);
/* clear the buffer */
SPW_strLength = 0;
} /* end of StringFlush */
/*-----------------------------------------------------------------------------
*
* static void SendString (Display *display, char *string)
*
* Args:
* display (r/o) The X Display of which the XCM window is a resident
* string (r/o) The string that is sent to the XCM daemon
*
* Return Value:
* None.
*
* Description:
* This function sends string to the XCM window and flushes the
* internal buffer.
*
* Notes:
* To flush the string call StringFlush.
*
*---------------------------------------------------------------------------*/
static void SendString (Display *display, char *string)
{
int i; /* counter used to step through the string */
/* copy the string into strData and send strData to the XCM driver */
for (i = 0; i < (int) strlen (string); i++) {
strData[SPW_strLength++] = string[i];
}
StringFlush (display, ReturnWindowID);
} /* end of String */
/*-----------------------------------------------------------------------------
*
* void SPW_InputString (Display *display, char *string)
*
* Args:
* display (r/o) The X Display of which the XCM window is a resident
* string (r/o) The string that is sent to the XCM daemon
*
* Return Value:
* None.
*
* Description:
* This function sends string to the XCM window using SendString.
*
* Notes:
* None.
*
*---------------------------------------------------------------------------*/
void SPW_InputString (Display *display, char *string)
{
SPW_strLength = 0; /* flush the internal buffer */
SendString (display, string); /* setup the string for sending */
}
/*-----------------------------------------------------------------------------
*
* void SPW_InputResetGlobalVars (void)
*
* Args:
* None.
*
* Return Value:
* None.
*
* Description:
* This function resets all the static global variable used in the Spaceball
* input function.
*
* Notes:
* None.
*
*---------------------------------------------------------------------------*/
void SPW_InputResetGlobalVars (void)
{
SpaceballFound = 0;
SpaceballInputExtension = 0;
SpaceWareAtomsDefined = 0;
SpaceWareXCMWindowID = 0;
} /* end of SPW_InputResetGlobalVars */
/*-----------------------------------------------------------------------------
*
* void SPW_InputResetSpaceball (Display *display)
*
* Args:
* display (r/o) The X Display of which the XCM window is a resident
*
* Return Value:
* None.
*
* Description:
* This function resets the Spaceball attached to the display defined in
* the display variable. The daemon will reset all internal Spaceball
* variables to their default and rezeros the Spaceball itself.
*
* Notes:
* None.
*
*---------------------------------------------------------------------------*/
void SPW_InputResetSpaceball (Display *display)
{
SPW_strLength = 0;
static char RESET[] = "RESET";
SendString (display, RESET);
} /* end of SPW_InputResetSpaceball */
/*-----------------------------------------------------------------------------
*
* void SPW_InputCloseXIE (Display *display)
*
* Args:
* display (r/o) The X Display to which the Spaceball is attached
*
* Return Value:
* None.
*
* Description:
* This function closes all communication with the Spaceball XIE driver.
*
* Notes:
* When the XIE driver is closed the Spaceball may lose power.
*
*---------------------------------------------------------------------------*/
void SPW_InputCloseXIE (Display *display)
{
#ifdef HAVE_X11_EXTENSIONS_XINPUT_H
if (SpaceballInputExtension == 1) {
XCloseDevice (display, pSpaceballDev);
SpaceballInputExtension = 0;
SpaceballFound = 0;
}
#endif
} /* end of SPW_InputCloseXIE */
/*-----------------------------------------------------------------------------
*
* int SPW_InputIsXIE (void)
*
* Args:
* None.
*
* Return Value:
* 0 if XIE is disabled or 1 if it is enabled.
*
* Description:
* This function determines if the Spaceball XIE driver is enabled.
*
* Notes:
* This is exported from this library so users of this library
* can determine the method of communication used.
*
*---------------------------------------------------------------------------*/
int SPW_InputIsXIE (void)
{
return SpaceballInputExtension;
} /* end of SPW_InputIsXIE */
/*-----------------------------------------------------------------------------
*
* int SPW_InputIsXCM (void)
*
* Args:
* None.
*
* Return Value:
* 0 if XCM is disabled or 1 if it is enabled.
*
* Description:
* This function determines f the Spaceball XCM driver is enabled.
*
* Notes:
* This is exported from this library so users of this library
* can determine the method of communication used.
*
*---------------------------------------------------------------------------*/
int SPW_InputIsXCM (void)
{
return SpaceWareAtomsDefined;
} /* end of SPW_InputIsXCM */
/*-----------------------------------------------------------------------------
*
* void SPW_InputSelectExtensionEvent (Display *display, Window window)
*
* Args:
* display (r/o) The X Display to which the Spaceball is attached
* window (r/o) What window to check for extension events
*
* Return Value:
* None.
*
* Description:
* Check to see if the Spaceball XInput Extension is enabled.
*
* Notes:
* None.
*
*---------------------------------------------------------------------------*/
void SPW_InputSelectExtensionEvent (Display *display, Window window)
{
#ifdef HAVE_X11_EXTENSIONS_XINPUT_H
XSelectExtensionEvent (display, window, SPW_SpaceballEventClass, 3);
#endif
} /* end of SPW_InputSelectExtensionEvent */
/*-----------------------------------------------------------------------------
*
* void SPW_SendHandshake(Display *display)
*
* Args:
* display (r/o) The display on which to send the message
*
* Return Value:
* None
*
* Description:
* Sends a handshaking packet to the Xdaemon with what version of
* the software we are running. Eventually we may even use this
* information. Right now it's mainly to make sure that the soft
* button window gets the correct cache values.
*
* Notes:
* This function returns nothing, but sends a message to the Xdaemon.
* The message is sent to guaranty the daemon gets the window id
* of the application.
*
*---------------------------------------------------------------------------*/
void SPW_SendHandshake (Display *display)
{
static char pHandshake[20]; /* store some space */
if (SpaceWareXCMWindowID != 0) /* using daemon */
{
SPW_strLength = 0;
pHandshake[0] = '\0';
strcat (pHandshake, "~~SLIM V");
strcat (pHandshake, SPW_INPUT_VERSION);
strcat (pHandshake, "\r");
SendString (display, pHandshake);
}
} /* end of SPW_SendHandshake */
/*-----------------------------------------------------------------------------
*
* int SPW_GrabDevice(Display *display, Window window, int exclusive)
*
* Args:
* display (r/o) The current display pointer
* window (r/o) The window to send to
* exclusive (r/o) Specify if the grab is exclusive
*
* Return Value:
* None
*
* Description:
* This function is used to signal the driver that the application wishes
* to "grab" the Spaceball device. This prevents other applications from
* getting data from the device. There are two possible grab modes: An
* exclusive grab forces the driver to send data to the application window
* regardless of what window has focus. A non-exclusive grab forces the
* driver to send Spaceball data to the application window when no other
* Spaceball enabled window has focus.
*
* Notes:
* This function does not work with XIE.
* It is recommended that exclusive grab not be used.
* This function will change the ReturnWindowID if the window parameter
* is not NULL.
*
*---------------------------------------------------------------------------*/
int SPW_GrabDevice(Display *display, Window window,
int exclusive)
{
if (window != 0) {
ReturnWindowID = window;
}
if ((ReturnWindowID == 0) || /* return window not set */
(SpaceWareXCMWindowID == 0)) {/* daemon not running */
return 0;
}
SPW_strLength = 0;
if (exclusive == 1)
{
static char hard[] = "~hard";
SendString (display, hard);
}
else
{
static char soft[] = "~soft";
SendString (display, soft);
}
return 1;
} /* end of SPW_GrabDevice */
/*-----------------------------------------------------------------------------
*
* void SPW_ReleaseDevice(Display *display)
*
* Args:
* display (r/o) The current display pointer
*
* Return Value:
* None
*
* Description:
* This function is used to release a grabbed device. This frees the
* driver to send data to the application with focus regardless if it has
* been Spaceball enabled.
*
* Notes:
* This function does not work with XIE.
*
*---------------------------------------------------------------------------*/
int SPW_ReleaseDevice(Display *display)
{
if ((ReturnWindowID == 0) || /* return window not set */
(SpaceWareXCMWindowID == 0)) /* daemon not running */
{
return 0;
}
SPW_strLength = 0;
static char relgrab[] = "~relgrab";
SendString (display, relgrab);
return 1;
} /* end of SPW_ReleaseDevice */
/*-----------------------------------------------------------------------------
*
* static int FindXCMAtoms (Display *display)
*
* Args:
* display (r/o) The display on which to search for the XCM atoms
*
* Return Value:
* 1 if the XCM atoms were found, 0 if not.
*
* Description:
* Returns True if all the XCM atoms are found.
*
* Notes:
* Has the side effect of looking up the atoms and putting the
* values in the SPW_Input...Atom variables
*
*---------------------------------------------------------------------------*/
static int FindXCMAtoms (Display *display)
{
/* if we already have the atoms don't look again */
if (SpaceWareAtomsDefined == 1)
{
return 1;
}
/*
* Try to find the atoms; if X does not find the atoms it will try to
* create them.
*/
SPW_InputMotionAtom = XInternAtom(display, SPW_InputMotionAtomName, True);
SPW_InputButtonPressAtom = XInternAtom(display, SPW_InputButtonPressAtomName,
True);
SPW_InputButtonReleaseAtom = XInternAtom(display,
SPW_InputButtonReleaseAtomName,True);
SPW_InputPassThruAtom = XInternAtom(display,
SPW_InputPassThruAtomName, True);
#ifdef SPW_DEBUG
fprintf(stderr,"SPW_InputMotionAtom %d\n", SPW_InputMotionAtom);
fprintf(stderr,"SPW_InputButtonPressAtom %d\n", SPW_InputButtonPressAtom);
fprintf(stderr,"SPW_InputButtonReleaseAtom %d\n",SPW_InputButtonReleaseAtom);
fprintf(stderr,"SPW_InputPassThruAtom %d\n", SPW_InputPassThruAtom);
#endif
/* if any one of the atoms does not exist return a failure */
if ((SPW_InputMotionAtom == None) ||
(SPW_InputButtonPressAtom == None) ||
(SPW_InputButtonReleaseAtom == None) ||
(SPW_InputPassThruAtom == None))
{
return 0;
}
/* set the global that tells us the atoms are defined and found */
SpaceWareAtomsDefined = 1;
return 1;
} /* end of FindXCMAtoms */
/*-----------------------------------------------------------------------------
*
* static int FindXCM (Display *display)
*
* Args:
* display (r/o) The display on which to search for the XCM window
*
* Return Value:
* Returns 1 if the XCM driver is found, 0 if not.
*
* Description:
* Find the XCM driver (window and atoms). This function is used
* to check if the driver is running because if the atoms exist
* then the driver is running.
*
* Notes:
* None.
*
*---------------------------------------------------------------------------*/
static int FindXCM (Display *display)
{
unsigned long win; /* the XCM window */
int atom; /* boolean that tell us if we found the atoms */
win = FindXCMWindow (display);
atom = FindXCMAtoms (display);
#ifdef SPW_DEBUG
fprintf(stderr, "win %d atom %d\n", win, atom);
#endif
if ((win != 0) && (atom == 1)) {
return 1;
}
return 0;
} /* end of FindXCM */
/*-----------------------------------------------------------------------------
*
* int SPW_FindXIE (Display *display)
*
* Args:
* display (r/o) The display on which to search for the XCM window
*
* Return Value:
* 1 if the XIE driver was found, 0 if not.
*
* Description:
* Find the XIE Spaceball extension.
*
* Notes:
* None.
*
*---------------------------------------------------------------------------*/
int SPW_FindXIE (Display *display)
{
#ifdef HAVE_X11_EXTENSIONS_XINPUT_H
int ieMajor;
int ieFirstEvent;
int ieFirstError;
int nDev;
XDeviceInfo *pDev;
int i,j;
char *ptr;
/* if we have already found the XIE driver don't look again */
if (SpaceballInputExtension == 1)
{
return 1;
}
/* See if XIE is configured */
if (XQueryExtension (display, "XInputExtension", &ieMajor,
&ieFirstEvent, &ieFirstError) == 0)
{
return 0;
}
#ifdef SPW_DEBUG
fprintf (stderr, "SPW_FindXIE() Extension found\n");
#endif
/* get a list of available XIE devices */
pDev = XListInputDevices (display, &nDev);
/* Make sure there are valid devices */
if (pDev == 0) {
return 0;
}
/* Try to find the Spaceball device in the list */
for (i = 0; i < nDev; i++) {
#ifdef SPW_DEBUG
fprintf (stderr, "SPW_FindXIE() device %s\n", pDev[i].name);
#endif
/* Compare against type name per device */
if (strcmp(pDev[i].name, XI_SPACEBALL) == 0) break;
}
/*
* if we have gone past the end of the list then no Spaceball XIE
* driver was found
*/
if (i == nDev) {
XFreeDeviceList (pDev);
return 0;
}
/* record all necessary info on the Spaceball XIE driver */
SpaceballDevID = pDev[i].id;
pSpaceballDev = XOpenDevice (display, SpaceballDevID);
/* clean up the device list */
XFreeDeviceList (pDev);
/* enable the Spaceball callbacks */
DeviceMotionNotify (pSpaceballDev, SPW_DevMotionEventType,
SPW_SpaceballEventClass[0]);
DeviceButtonPress (pSpaceballDev, SPW_DevButtonPressEventType,
SPW_SpaceballEventClass[1]);
DeviceButtonRelease (pSpaceballDev, SPW_DevButtonReleaseEventType,
SPW_SpaceballEventClass[2]);
SpaceballInputExtension = 1;
return 1;
#else
return 0;
#endif /* HAVE_X11_EXTENSIONS_XINPUT_H */
} /* end of SPW_FindXIE */
/*-----------------------------------------------------------------------------
*
* static void InitializeSpaceball (Display *display, char *version)
*
* Args:
* display (r/o) The display on which to search for the XCM window
* version (r/o) the XCM driver version.
*
* Return Value:
* None.
*
* Description:
* Find the Spaceball via XIE or XCM.
*
* Notes:
* The returnWindow parameter is not set if the Spaceball is configured
* using XIE.
*
*---------------------------------------------------------------------------*/
static void InitializeSpaceball (Display *display, char *version)
{
/* if we already found the Spaceball don't search again */
if (SpaceballFound == 1) {
return;
}
/* Create some more Atoms the Daemon uses */
(void)XInternAtom (display, "WM_PROTOCOLS", False);
(void)XInternAtom (display, "WM_DELETE_WINDOW", False);
/* search for the XIE driver first */
if (SPW_FindXIE (display) == 1) {
SpaceballFound = 1;
#ifdef SPW_DEBUG
fprintf(stderr, "\n");
fprintf(stderr, "Spaceball (R) device found,");
fprintf(stderr, " using SpaceWare (R) XIE interface.\n");
fprintf(stderr, "SpaceWare Version %s\n", version);
fprintf(stderr, "Copyright (c) 1998 Spacetec IMC Corporation\n");
fprintf(stderr, "All Rights Reserved\n");
#endif
}
/* if we don't find the XIE driver search for the XCM driver */
else if (FindXCM (display) == 1) {
SpaceballFound = 1;
#ifdef SPW_DEBUG
fprintf(stderr, "\n");
fprintf(stderr, "Spaceball (R) device found,");
fprintf(stderr, " using SpaceWare (R) XCM interface.\n");
fprintf(stderr, "SpaceWare Version %s\n", version);
fprintf(stderr, "Copyright (c) 1998 Spacetec IMC Corporation\n");
fprintf(stderr, "All Rights Reserved\n");
#endif
}
} /* end of InitializeSpaceball */
/*-----------------------------------------------------------------------------
*
* int SPW_InputCheckForSpaceball (Display *display, Window window,
* char *product)
*
* Args:
* display (r/o) The display on which to search for the XCM window
* window (r/o) the window on the application side to send messages to.
* product (r/o) name of the application that is opening the Spaceball.
*
* Return Value:
* 1 if the Spaceball was opened, 0 if not.
*
* Description:
* Find the Spaceball via XIE or XCM.
*
* Notes:
* The window parameter is not set if the Spaceball is configured
* using XIE.
*
*---------------------------------------------------------------------------*/
int SPW_InputCheckForSpaceball (Display *display,
Window window,
char *product)
{
char version[256]; /* The version of SpaceWare */
int productlen; /* length of the product string */
#ifdef SPW_DEBUG
fprintf(stderr, "SPW_InputCheckForSpaceball window 0x%x product %s\n",
window, product);
#endif
/* check to make sure the correct parameters were passed */
if ((display == NULL) || (window == 0)) {
return 0;
}
/* build the SpaceWare version and product info string */
strcpy (version, SPW_INPUT_VERSION);
if (product != NULL) {
productlen = strlen (product);
if ((productlen > 0) && (productlen < 200)) {
strcat(version, ".");
strcat(version, product);
}
}
/* save off the application window for future reference */
if (ReturnWindowID == 0) {
ReturnWindowID = window;
}
/* try to open the Spaceball */
InitializeSpaceball (display, version);
if (SpaceballFound == 0) {
return 0;
}