.POSIX:
.SUFFIXES:

include config.mk

SRCS = src/main.c src/input.c src/layout.c src/monitors.c src/clients.c \
	src/cursor.c src/session-lock.c src/xwayland.c src/config.c
OBJS = $(SRCS:.c=.o)

# flags for compiling
ETHERCPPFLAGS = -I. -I.. -DETHER_USE_UNSTABLE -DWLR_USE_UNSTABLE -D_POSIX_C_SOURCE=200809L \
	-DVERSION=\"$(VERSION)\" $(XWAYLAND) $(CPPFLAGS)
ETHERDEVCFLAGS = -g -Wpedantic -Wall -Wextra -Wdeclaration-after-statement \
	-Wno-unused-parameter -Wshadow -Wunused-macros -Werror=strict-prototypes \
	-Werror=implicit -Werror=return-type -Werror=incompatible-pointer-types \
	-Wfloat-conversion

# CFLAGS / LDFLAGS
PKGS      = wayland-server xkbcommon libinput lua5.5 $(XLIBS)
ETHERCFLAGS = `$(PKG_CONFIG) --cflags $(PKGS)` $(WLR_INCS) $(ETHERCPPFLAGS) $(ETHERDEVCFLAGS) $(CFLAGS)
LDLIBS    = `$(PKG_CONFIG) --libs $(PKGS)` $(WLR_LIBS) -lm $(LIBS)

all: ether
ether: $(OBJS) src/config.h src/util.h src/client.h src/ether.h config.mk cursor-shape-v1-protocol.h \
	ext-image-copy-capture-v1-protocol.h \
	pointer-constraints-unstable-v1-protocol.h wlr-layer-shell-unstable-v1-protocol.h \
	wlr-output-power-management-unstable-v1-protocol.h xdg-shell-protocol.h
	$(CC) $(OBJS) $(ETHERCFLAGS) $(LDFLAGS) $(LDLIBS) -o $@

src/%.o: src/%.c cursor-shape-v1-protocol.h ext-image-copy-capture-v1-protocol.h \
	pointer-constraints-unstable-v1-protocol.h wlr-layer-shell-unstable-v1-protocol.h \
	wlr-output-power-management-unstable-v1-protocol.h xdg-shell-protocol.h
	$(CC) -c $(ETHERCFLAGS) $< -o $@

# wayland-scanner is a tool which generates C headers and rigging for Wayland
# protocols, which are specified in XML. wlroots requires you to rig these up
# to your build system yourself and provide them in the include path.
WAYLAND_SCANNER   = `$(PKG_CONFIG) --variable=wayland_scanner wayland-scanner`
WAYLAND_PROTOCOLS = `$(PKG_CONFIG) --variable=pkgdatadir wayland-protocols`

cursor-shape-v1-protocol.h:
	$(WAYLAND_SCANNER) enum-header \
		$(WAYLAND_PROTOCOLS)/staging/cursor-shape/cursor-shape-v1.xml $@
ext-image-copy-capture-v1-protocol.h:
	$(WAYLAND_SCANNER) enum-header \
		$(WAYLAND_PROTOCOLS)/staging/ext-image-copy-capture/ext-image-copy-capture-v1.xml $@
pointer-constraints-unstable-v1-protocol.h:
	$(WAYLAND_SCANNER) enum-header \
		$(WAYLAND_PROTOCOLS)/unstable/pointer-constraints/pointer-constraints-unstable-v1.xml $@
wlr-layer-shell-unstable-v1-protocol.h:
	$(WAYLAND_SCANNER) enum-header \
		protocols/wlr-layer-shell-unstable-v1.xml $@
wlr-output-power-management-unstable-v1-protocol.h:
	$(WAYLAND_SCANNER) server-header \
		protocols/wlr-output-power-management-unstable-v1.xml $@
xdg-shell-protocol.h:
	$(WAYLAND_SCANNER) server-header \
		$(WAYLAND_PROTOCOLS)/stable/xdg-shell/xdg-shell.xml $@

clean:
	rm -f ether $(OBJS) *-protocol.h

dist: clean
	mkdir -p ether-$(VERSION)
	cp -R LICENSES/* Makefile CHANGELOG.md README.md client.h config.lua \
		config.mk protocols ether.1 src util.h ether.desktop \
		ether-$(VERSION)
	tar -caf ether-$(VERSION).tar.gz ether-$(VERSION)
	rm -rf ether-$(VERSION)

install: ether
	mkdir -p $(DESTDIR)$(PREFIX)/bin
	rm -f $(DESTDIR)$(PREFIX)/bin/ether
	cp -f ether $(DESTDIR)$(PREFIX)/bin
	chmod 755 $(DESTDIR)$(PREFIX)/bin/ether
	mkdir -p $(DESTDIR)$(MANDIR)/man1
	cp -f ether.1 $(DESTDIR)$(MANDIR)/man1
	chmod 644 $(DESTDIR)$(MANDIR)/man1/ether.1
	mkdir -p $(DESTDIR)$(DATADIR)/wayland-sessions
	cp -f ether.desktop $(DESTDIR)$(DATADIR)/wayland-sessions/ether.desktop
	chmod 644 $(DESTDIR)$(DATADIR)/wayland-sessions/ether.desktop
uninstall:
	rm -f $(DESTDIR)$(PREFIX)/bin/ether $(DESTDIR)$(MANDIR)/man1/ether.1 \
		$(DESTDIR)$(DATADIR)/wayland-sessions/ether.desktop
