Skip to content

Commit 005375a

Browse files
authored
Introduce SDL3 support and autobuild package (#9)
1 parent b6652e7 commit 005375a

5 files changed

Lines changed: 143 additions & 74 deletions

File tree

autobuild.xml

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<string>autobuild</string>
88
<key>installables</key>
99
<map>
10-
<key>SDL2</key>
10+
<key>SDL3</key>
1111
<map>
1212
<key>platforms</key>
1313
<map>
@@ -16,54 +16,26 @@
1616
<key>archive</key>
1717
<map>
1818
<key>hash</key>
19-
<string>ad1b738a7cef16d5cec63ab99b1c80146626f47b</string>
19+
<string>06226c54c57afd601707c0f5f7a2e0736ab1f326</string>
2020
<key>hash_algorithm</key>
2121
<string>sha1</string>
2222
<key>url</key>
23-
<string>https://github.com/secondlife/3p-sdl2/releases/download/v2.30.9-r1/SDL2-2.30.9-r1-linux64-11943333704.tar.zst</string>
23+
<string>https://github.com/secondlife/3p-sdl3/releases/download/v3.2.24-r1/SDL3-3.2.24-r1-linux64-18539840462.tar.zst</string>
2424
</map>
2525
<key>name</key>
2626
<string>linux64</string>
2727
</map>
28-
<key>darwin64</key>
29-
<map>
30-
<key>archive</key>
31-
<map>
32-
<key>hash</key>
33-
<string>a6aa829c1e7f193b624f05415588e4d727454c8f</string>
34-
<key>hash_algorithm</key>
35-
<string>sha1</string>
36-
<key>url</key>
37-
<string>https://github.com/secondlife/3p-sdl2/releases/download/v2.30.9-r1/SDL2-2.30.9-r1-darwin64-11943333704.tar.zst</string>
38-
</map>
39-
<key>name</key>
40-
<string>darwin64</string>
41-
</map>
42-
<key>windows64</key>
43-
<map>
44-
<key>archive</key>
45-
<map>
46-
<key>hash</key>
47-
<string>97153f9d302f964142df971b57680724824f458f</string>
48-
<key>hash_algorithm</key>
49-
<string>sha1</string>
50-
<key>url</key>
51-
<string>https://github.com/secondlife/3p-sdl2/releases/download/v2.30.9-r1/SDL2-2.30.9-r1-windows64-11943333704.tar.zst</string>
52-
</map>
53-
<key>name</key>
54-
<string>windows64</string>
55-
</map>
5628
</map>
5729
<key>license</key>
5830
<string>lgpl</string>
5931
<key>license_file</key>
60-
<string>LICENSES/SDL2.txt</string>
32+
<string>LICENSES/SDL3.txt</string>
6133
<key>copyright</key>
6234
<string>Copyright (C) 1997-2022 Sam Lantinga (slouken@libsdl.org)</string>
6335
<key>version</key>
64-
<string>2.30.9-r1</string>
36+
<string>3.2.24-r1</string>
6537
<key>name</key>
66-
<string>SDL2</string>
38+
<string>SDL3</string>
6739
<key>description</key>
6840
<string>Simple DirectMedia Layer is a cross-platform multimedia library designed to provide low level access to audio, keyboard, mouse, joystick, 3D hardware via OpenGL, and 2D video framebuffer.</string>
6941
</map>

build-cmd.sh

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22

3-
TOP="$(dirname "$0")"
3+
cd "$(dirname "$0")"
44

55
# turn on verbose debugging output for parabuild logs.
66
exec 4>&1; export BASH_XTRACEFD=4; set -x
@@ -9,11 +9,7 @@ set -e
99
# complain about unset env variables
1010
set -u
1111

12-
PROJECT="libndofdev"
13-
VERSION=$(gawk 'END{print MAJOR"."MINOR} /NDOFDEV_MAJOR/{MAJOR=$3} /NDOFDEV_MINOR/{MINOR=$3}' ${TOP}/${PROJECT}/ndofdev_version.h)
14-
SOURCE_DIR="$PROJECT"
15-
16-
if [ -z "$AUTOBUILD" ] ; then
12+
if [ -z "$AUTOBUILD" ] ; then
1713
exit 1
1814
fi
1915

@@ -23,44 +19,58 @@ else
2319
autobuild="$AUTOBUILD"
2420
fi
2521

26-
stage="$(pwd)"
2722

28-
if [ ! -d "$stage/include" ]
29-
then
30-
mkdir -p "$stage/include"
31-
fi
23+
top="$(pwd)"
24+
stage="$(pwd)/stage"
25+
stage_include="$stage/include/"
26+
stage_release="$stage/lib/release/"
27+
28+
mkdir -p ${stage_include}
29+
mkdir -p ${stage_release}
30+
31+
PROJECT="libndofdev"
32+
# 2nd line of CHANGELOG is most recent version number:
33+
# * 0.3
34+
# Tease out just the version number from that line.
35+
VERSION=$(gawk 'END{print MAJOR"."MINOR} /NDOFDEV_MAJOR/{MAJOR=$3} /NDOFDEV_MINOR/{MINOR=$3}' ${PROJECT}/ndofdev_version.h)
36+
SOURCE_DIR="$PROJECT"
3237

3338
"$autobuild" source_environment > "$stage/variables_setup.sh" || exit 1
3439
. "$stage/variables_setup.sh"
3540

36-
3741
build=${AUTOBUILD_BUILD_ID:=0}
3842
echo "${VERSION}.${build}" > "${stage}/VERSION.txt"
3943

44+
# remove_cxxstd
45+
source "$(dirname "$AUTOBUILD_VARIABLES_FILE")/functions"
46+
47+
pushd "$SOURCE_DIR"
4048
case "$AUTOBUILD_PLATFORM" in
4149
windows*|darwin*)
4250
# Given forking and future development work, it seems unwise to
4351
# hardcode the actual URL of the current project's libndofdev
4452
# repository in this message. Try to determine the URL of this
4553
# open-libndofdev repository and remove "open-" as a suggestion.
46-
echo "Windows/Mac libndofdev is in a separate bitbucket repository \
47-
-- try $(hg paths default | sed -E 's/open-(libndofdev)/\1/')" 1>&2 ; exit 1
48-
;;
49-
linux)
50-
echo "Platform linux is unsupported"
51-
exit 1
52-
;;
53-
linux64)
54-
opts="-DTARGET_OS_LINUX -m$AUTOBUILD_ADDRSIZE $LL_BUILD_RELEASE"
55-
#Dumb - $LL_BUILD_RELEASE contains c++ standard flags which are meaningful to the c++
56-
#compiler only, SDL is written in C and polluting CFLAGS with that nonsense just makes
57-
#the compiler spew out a whole load of noise, so lets strip that junk out from CFLAGS
58-
opts=$(echo "$opts" | sed 's/-std=c++[0-9][0-9]*//')
59-
cmake ../libndofdev -DCMAKE_CXX_FLAGS="$opts" -DCMAKE_C_FLAGS="$opts" \
60-
-DCMAKE_OSX_ARCHITECTURES="$AUTOBUILD_CONFIGURE_ARCH" \
61-
-DCMAKE_BUILD_TYPE:STRING=Release
62-
make
63-
cp -a ../libndofdev/*.h $stage/include
64-
cp -a ../libndofdev/LICENSES LICENSES
54+
echo "Windows/Mac libndofdev is in a separate GitHub repository" 1>&2 ; exit 1
55+
;;
56+
linux*)
57+
# Default target per autobuild build --address-size
58+
opts="-m$AUTOBUILD_ADDRSIZE $LL_BUILD_RELEASE"
59+
plainopts="$(remove_cxxstd $opts)"
60+
61+
# release build
62+
CFLAGS="$plainopts -I${stage}/packages/include -Wl,-L${stage}/packages/lib/release" \
63+
CXXFLAGS="$opts -I${stage}/packages/include -Wl,-L${stage}/packages/lib/release" \
64+
LDFLAGS="-L${stage}/packages/lib/release" \
65+
USE_SDL3=1 \
66+
make all
67+
68+
cp libndofdev.a ${stage_release}
69+
cp ndofdev_external.h ${stage_include}
6570
;;
6671
esac
72+
73+
mkdir -p ${stage}/LICENSES
74+
cp LICENSES/libndofdev.txt ${stage}/LICENSES/libndofdev.txt
75+
76+
popd

libndofdev/Makefile

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1-
CFLAGS=-pipe -Wall -Wunused-result
1+
CFLAGS := $(CFLAGS) -pipe -Wall -Wunused-result
2+
ifeq ($(USE_SDL3),1)
3+
CFLAGS := $(CFLAGS) -DUSE_SDL3
4+
ifeq ($(DEBUG),1)
5+
LDFLAGS := ${LDFLAGS} -lSDL3d
6+
else
7+
LDFLAGS := ${LDFLAGS} -lSDL3
8+
endif
9+
else
210
ifeq ($(USE_SDL2),1)
311
CFLAGS := $(CFLAGS) -DUSE_SDL2
4-
LDFLAGS=-lSDL2
12+
ifeq ($(DEBUG),1)
13+
LDFLAGS := ${LDFLAGS} -lSDL2d
514
else
6-
LDFLAGS=-lSDL
15+
LDFLAGS := ${LDFLAGS} -lSDL2
16+
endif
17+
else
18+
LDFLAGS := ${LDFLAGS} -lSDL
19+
endif
720
endif
821

922

libndofdev/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ from the list above - that's the only way to get the problems fixed.
4141

4242
In addition, the library supports every joystick/gamepad device
4343
supported by the SDL library. SDL2 is supported by passing a flag to
44-
the makefile: `USE_SDL2=1 make`
44+
the makefile: `USE_SDL2=1 make` SDL3 is supported by passing USE_SDL3
4545

4646

4747
C A U T I O N !!!

libndofdev/ndofdev.c

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@
4848
#include <stdlib.h>
4949
#include <unistd.h>
5050

51-
#ifdef USE_SDL2
51+
#ifdef USE_SDL3
52+
#include <SDL3/SDL.h>
53+
#elif defined(USE_SDL2)
5254
#include <SDL2/SDL.h>
5355
#else
5456
#include <SDL/SDL.h>
@@ -258,6 +260,32 @@ int ndof_init_first(NDOF_Device *in_out_dev, void *param)
258260
return 0;
259261

260262
} else {
263+
#ifdef USE_SDL3
264+
// SpaceNavigator not found, use SDL Joystick
265+
SDL_Joystick *j = SDL_OpenJoystick(0);
266+
if(j)
267+
{
268+
in_out_dev->axes_count = SDL_GetNumJoystickAxes(j) + SDL_GetNumJoystickHats(j) * 2; // each hat has 2 axes
269+
in_out_dev->btn_count = SDL_GetNumJoystickButtons(j);
270+
in_out_dev->absolute = 0; // always relative on Linux
271+
in_out_dev->valid = 1;
272+
in_out_dev->axes_max = 32767;
273+
in_out_dev->axes_min = -32767;
274+
275+
strncpy(in_out_dev->product, SDL_GetJoystickName(j), 255);
276+
277+
// private data
278+
LinJoystickPrivate *priv = (LinJoystickPrivate *) malloc (sizeof(LinJoystickPrivate));
279+
priv->j = j;
280+
priv->fd = -1;
281+
priv->USE_SDL = 1;
282+
// remember number of hats for later axis mapping in ndof_update()
283+
priv->num_hats = SDL_GetNumJoystickHats(j);
284+
in_out_dev->private_data = priv;
285+
286+
return 0;
287+
}
288+
#else
261289
// SpaceNavigator not found, use SDL Joystick
262290
SDL_Joystick *j = SDL_JoystickOpen(0);
263291
if(j)
@@ -268,7 +296,7 @@ int ndof_init_first(NDOF_Device *in_out_dev, void *param)
268296
in_out_dev->valid = 1;
269297
in_out_dev->axes_max = 32767;
270298
in_out_dev->axes_min = -32767;
271-
#ifdef USE_SDL2
299+
#if defined(USE_SDL2)
272300
strncpy(in_out_dev->product, SDL_JoystickName(j), 255);
273301
#else
274302
strncpy(in_out_dev->product, SDL_JoystickName(0), 255);
@@ -283,7 +311,9 @@ int ndof_init_first(NDOF_Device *in_out_dev, void *param)
283311
in_out_dev->private_data = priv;
284312

285313
return 0;
286-
} else {
314+
}
315+
#endif
316+
else {
287317

288318
// no joysticks found
289319
return -1;
@@ -348,6 +378,50 @@ void ndof_update(NDOF_Device *in_dev)
348378

349379
if(priv->USE_SDL)
350380
{
381+
#ifdef USE_SDL3
382+
SDL_UpdateJoysticks();
383+
SDL_Joystick *j = priv->j;
384+
385+
for(i = 0; i < in_dev->axes_count - priv->num_hats*2; i++) // hats will get mapped to uppermost axes
386+
{
387+
in_dev->axes[i] = (int) (SDL_GetJoystickAxis(j, i));
388+
}
389+
390+
for(i = 0; i < priv->num_hats; i++)
391+
{
392+
int x = 0;
393+
int y = 0;
394+
395+
int value = SDL_GetJoystickHat(j, i);
396+
397+
// map hat values (1, 2, 4, 8) to axis data (-32768, 32768)
398+
if(value & 1)
399+
{
400+
y = -32767;
401+
}
402+
else if(value & 4)
403+
{
404+
y = 32768;
405+
}
406+
if(value & 2)
407+
{
408+
x = 32767;
409+
}
410+
else if(value & 8)
411+
{
412+
x = -32768;
413+
}
414+
415+
// add the hat data to the uppermost axes data
416+
in_dev->axes[in_dev->axes_count - priv->num_hats*2 + i*2 ] = x;
417+
in_dev->axes[in_dev->axes_count - priv->num_hats*2 + i*2+1] = y;
418+
}
419+
420+
for(i = 0; i < in_dev->btn_count; i++)
421+
{
422+
in_dev->buttons[i] = SDL_GetJoystickButton(j, i);
423+
}
424+
#else
351425
SDL_JoystickUpdate();
352426
SDL_Joystick *j = priv->j;
353427

@@ -390,18 +464,18 @@ void ndof_update(NDOF_Device *in_dev)
390464
{
391465
in_dev->buttons[i] = SDL_JoystickGetButton(j, i);
392466
}
467+
#endif
393468
} else {
394469
// update SpaceNavigator
395470

396471
struct input_event ev;
397-
int mapped_code;
398472

399473
while(read(priv->fd, &ev, sizeof(struct input_event)) > 0)
400474
{
401475
switch (ev.type)
402476
{
403477
case EV_KEY:
404-
mapped_code = SPACE_MOUSE_BUTTON_MAPPING[ev.code & 0xff];
478+
int mapped_code = SPACE_MOUSE_BUTTON_MAPPING[ev.code & 0xff];
405479
// printf("Key %d (mapped to %d) pressed %d .\n", ev.code, mapped_code, ev.value);
406480
priv->buttons[mapped_code] = ev.value;
407481
break;

0 commit comments

Comments
 (0)