# The MIT License (MIT)
#
# Copyright (C) 2026 Scott Duensing
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

# Joystick suite (game platform Phase 4): calibration and dead-zone math,
# the game port's axis timing against simulated hardware (missing axes,
# timeouts, interrupted samples, buttons), device discovery through the
# driver registry, fallback to the keyboard joystick, polling on the timer
# service, event delivery, per-program cleanup and the BASIC functions.
# The library and both drivers are compiled from their sources (not the
# DOS port access); the few shell functions they call are stubbed in the
# test.

include ../dvxtest/dvxtest.mk

.DEFAULT_GOAL := all

JOY_DIR     = $(DVXTEST_ROOT)/src/libs/kpunch/joystick
BINDIR      = $(DVXTEST_BINDIR)/joystick
JOY_TEST    = $(BINDIR)/joystickTest
WORKDIR     = $(BINDIR)/work
DRVDIR      = $(WORKDIR)/libs/kpunch/joystick/drivers

# The library, and the game port's timing code for the measure tests; the
# drivers themselves are loaded from DRVDIR like on DOS.
JOY_SRCS    = $(wildcard $(JOY_DIR)/*.c) $(JOY_DIR)/drivers/joyport/joyPort.c
JOY_OBJS    = $(patsubst $(JOY_DIR)/%.c,$(BINDIR)/joy/%.o,$(JOY_SRCS))
LIBS_A      = $(DVXTEST_ARCHIVES)

CFLAGS      = $(DVXTEST_CFLAGS) -I$(DVXTEST_ROOT)/src/libs/kpunch -I$(DVXTEST_ROOT)/src/libs/kpunch/dvxshell -I$(JOY_DIR) -I$(JOY_DIR)/drivers/joyport -DJOY_WORKDIR='"$(WORKDIR)"'

# Drivers bind their own symbols first (the suite also links joyPort.c);
# -MMD so a header change (the ABI version) rebuilds them.
DRV_CFLAGS  = $(CFLAGS) -shared -Wl,-Bsymbolic -MMD -MP

# Fake drivers (fakeJoyDrv.c): the test pad, and three the library must
# refuse (no poll function, another DVX version, a name already taken).
FAKE_testpad = -DFAKE_NAME='"testpad"' -DFAKE_PRIORITY=50 -DFAKE_PAD
FAKE_nopoll  = -DFAKE_NAME='"nopoll"' -DFAKE_PRIORITY=60 -DFAKE_NO_POLL
FAKE_badabi  = -DFAKE_NAME='"badabi"' -DFAKE_PRIORITY=60 -DFAKE_BAD_ABI
FAKE_dup     = -DFAKE_NAME='"gameport"' -DFAKE_PRIORITY=10
FAKES        = testpad nopoll badabi dup
DRVS         = $(DRVDIR)/joyport.drv $(DRVDIR)/joykeys.drv $(patsubst %,$(DRVDIR)/%.drv,$(FAKES))

.PHONY: all run clean

all: run

$(BINDIR)/joy/%.o: $(JOY_DIR)/%.c $(DVX_MAKEFILES)
	@mkdir -p $(dir $@)
	$(HOSTCC) $(CFLAGS) -c -o $@ $<

$(DRVDIR)/joyport.drv: $(JOY_DIR)/drivers/joyport/joyPort.c joyPortSim.c $(DVX_MAKEFILES)
	@mkdir -p $(DRVDIR)
	$(HOSTCC) $(DRV_CFLAGS) -o $@ $(JOY_DIR)/drivers/joyport/joyPort.c joyPortSim.c

$(DRVDIR)/joykeys.drv: $(JOY_DIR)/drivers/joykeys/joyKeys.c $(DVX_MAKEFILES)
	@mkdir -p $(DRVDIR)
	$(HOSTCC) $(DRV_CFLAGS) -o $@ $<

$(DRVDIR)/%.drv: fakeJoyDrv.c $(JOY_DIR)/joystick.h $(DVX_MAKEFILES)
	@mkdir -p $(DRVDIR)
	$(HOSTCC) $(DRV_CFLAGS) $(FAKE_$*) -o $@ fakeJoyDrv.c

$(JOY_TEST): joystickTest.c $(JOY_OBJS) $(DVX_MAKEFILES) $(LIBS_A) | dvxtest-libs
	@mkdir -p $(BINDIR)
	$(HOSTCC) $(CFLAGS) -o $@ $< $(JOY_OBJS) $(DVXTEST_LINK)

-include $(JOY_OBJS:.o=.d) $(JOY_TEST).d $(DRVS:.drv=.d)

run: $(JOY_TEST) $(DRVS)
	cd $(BINDIR) && ASAN_OPTIONS=detect_leaks=1 ./joystickTest

clean:
	rm -rf $(BINDIR)
