ifneq (${KERNELRELEASE},)

# KERNELRELEASE defined: we are being compiled as part of the Kernel
        obj-m := vga_ball.o

else

# We are being compiled as a module or user space program: use the Kernel build system
KERNEL_SOURCE := /usr/src/linux-headers-$(shell uname -r)
PWD := $(shell pwd)

# Flags and Objects for user-space programs
CFLAGS = -Wall
USB_OBJECTS = usbkeyboard.o hello.o fm_model.o matrix.o  

# Target executables
default: module hello

module:
	$(MAKE) -C $(KERNEL_SOURCE) SUBDIRS=$(PWD) modules

hello: $(USB_OBJECTS)
	cc $(CFLAGS) -o hello $(USB_OBJECTS) -lusb-1.0 -pthread -lm 

hello.o: hello.c usbkeyboard.h fm_model.h matrix.h  # Ensure hello.c dependencies are listed
fm_model.o: fm_model.c fm_model.h  # Add compilation rule for fm_model
matrix.o: matrix.c matrix.h  # Add compilation rule for matrix
usbkeyboard.o: usbkeyboard.c usbkeyboard.h

clean:
	$(MAKE) -C $(KERNEL_SOURCE) SUBDIRS=$(PWD) clean
	rm -rf *.o hello

TARFILES = Makefile README vga_ball.h vga_ball.c hello.c usbkeyboard.h usbkeyboard.c fm_model.h fm_model.c matrix.h matrix.c 
TARFILE = project.tar.gz

.PHONY: tar
tar: $(TARFILE)

$(TARFILE): $(TARFILES)
	tar zcfC $(TARFILE) .. $(TARFILES:%=project/%)

endif
