# SPDX-License-Identifier: GPL-3.0-or-later
# pomelo-compositor — Pomelo 的 Wayland compositor
PREFIX ?= /usr/local
CFLAGS ?= -Os -pipe -Wall -Wextra -Wno-unused-parameter -Wno-format-truncation
# 編譯防護（Ubuntu 的 gcc 預設就有，Fedora／Rocky 要自己加）：堆疊保護、PIE、FORTIFY、完整 RELRO
override CFLAGS += -fstack-protector-strong -fstack-clash-protection -D_FORTIFY_SOURCE=3 -fPIE -pie
override LDFLAGS += -Wl,-z,relro,-z,now
WLR_PKGS = wlroots wayland-server xkbcommon cairo xcb xcb-ewmh xcb-xfixes pixman-1
WLR_CFLAGS ?= $(shell pkg-config --cflags $(WLR_PKGS)) -Iprotocol
WLR_LIBS   ?= $(shell pkg-config --libs $(WLR_PKGS)) -lm
WAYLAND_SCANNER ?= $(shell pkg-config --variable=wayland_scanner wayland-scanner)
WAYLAND_PROTOCOLS_DIR ?= $(shell pkg-config --variable=pkgdatadir wayland-protocols)
SRCS = pomelo-compositor.c output.c seat.c xwayland.c xdg.c view.c xbridge.c kbgrab.c textinput.c protocols.c layer.c foreign.c manage.c deco.c effects.c ipc.c lock.c outmgr.c workspace.c xdnd.c

# layer-shell 不是 wlroots 自己的協定（在 wlr-protocols），dev 套件不會附現成的標頭檔，
# 要自己在建置時用 wayland-scanner 產生（protocol/*.xml 是來源，標頭/來源檔不進原始碼包）。
# layer-shell 介面會用到 xdg_popup，所以 xdg-shell（系統 wayland-protocols 套件自帶）的
# 產生碼也要一起連結，不然會缺 xdg_popup_interface 這個符號。
PROTO_HDR = protocol/wlr-layer-shell-unstable-v1-protocol.h protocol/pomelo-workspace-v1-protocol.h
PROTO_SRC = protocol/wlr-layer-shell-unstable-v1-protocol.c protocol/xdg-shell-protocol.c \
            protocol/xwayland-keyboard-grab-unstable-v1-protocol.c protocol/pomelo-workspace-v1-protocol.c
PROTO_XDG_HDR = protocol/xdg-shell-protocol.h protocol/xwayland-keyboard-grab-unstable-v1-protocol.h \
                protocol/cursor-shape-v1-protocol.h protocol/pointer-constraints-unstable-v1-protocol.h \
                protocol/wlr-output-power-management-unstable-v1-protocol.h protocol/wlr-output-management-unstable-v1-protocol.h
XKG_XML = protocol/xwayland-keyboard-grab-unstable-v1.xml

# 預設目標：要排第一個，不然下面產生 protocol 檔的規則會被當成預設目標
all: pomelo-compositor pomelo-msg

pomelo-compositor: $(SRCS) $(PROTO_HDR) $(PROTO_XDG_HDR) $(PROTO_SRC) pomelo-compositor.h config.h
	$(CC) $(CFLAGS) $(WLR_CFLAGS) -o $@ $(SRCS) $(PROTO_SRC) $(WLR_LIBS) $(LDFLAGS)

# 命令列工具 pomelo-msg（多桌面）：走自訂協定，只需要 libwayland-client
pomelo-msg: pomelo-msg.c protocol/pomelo-workspace-v1-client-protocol.h protocol/pomelo-workspace-v1-protocol.c
	$(CC) $(CFLAGS) $(shell pkg-config --cflags wayland-client) -Iprotocol -o $@ pomelo-msg.c protocol/pomelo-workspace-v1-protocol.c $(shell pkg-config --libs wayland-client) $(LDFLAGS)

protocol/pomelo-workspace-v1-client-protocol.h: protocol/pomelo-workspace-v1.xml
	$(WAYLAND_SCANNER) client-header $< $@

$(PROTO_HDR): protocol/wlr-layer-shell-unstable-v1.xml
	$(WAYLAND_SCANNER) server-header $< $@

protocol/wlr-layer-shell-unstable-v1-protocol.c: protocol/wlr-layer-shell-unstable-v1.xml
	$(WAYLAND_SCANNER) private-code $< $@

protocol/xdg-shell-protocol.h: $(WAYLAND_PROTOCOLS_DIR)/stable/xdg-shell/xdg-shell.xml
	$(WAYLAND_SCANNER) server-header $< $@

# wlroots 的 wlr_cursor_shape_v1.h、wlr_pointer_constraints_v1.h 要 include 這兩個（只要標頭，協定 wlroots 自己實作）。
# 這三個 XML 放在 protocol/：Ubuntu 22.04 的 wayland-protocols（1.25）還沒有 cursor-shape
protocol/pointer-constraints-unstable-v1-protocol.h: protocol/pointer-constraints-unstable-v1.xml
	$(WAYLAND_SCANNER) server-header $< $@

protocol/cursor-shape-v1-protocol.h: protocol/cursor-shape-v1.xml
	$(WAYLAND_SCANNER) server-header $< $@

# wlroots 的 wlr_output_power_management_v1.h 要 include 這個；兩個 XML 來自 wlroots 0.17.1 原始碼（wlr-protocols）
protocol/wlr-output-power-management-unstable-v1-protocol.h: protocol/wlr-output-power-management-unstable-v1.xml
	$(WAYLAND_SCANNER) server-header $< $@

protocol/wlr-output-management-unstable-v1-protocol.h: protocol/wlr-output-management-unstable-v1.xml
	$(WAYLAND_SCANNER) server-header $< $@

protocol/xwayland-keyboard-grab-unstable-v1-protocol.h: $(XKG_XML)
	$(WAYLAND_SCANNER) server-header $< $@

protocol/xwayland-keyboard-grab-unstable-v1-protocol.c: $(XKG_XML)
	$(WAYLAND_SCANNER) private-code $< $@

# 多桌面的自訂協定（pomelo_workspace_v1）：compositor 是伺服器端，面板、pomelo-msg 是客戶端（libpomelo 與 pomelo-msg 各自產生客戶端碼）
protocol/pomelo-workspace-v1-protocol.h: protocol/pomelo-workspace-v1.xml
	$(WAYLAND_SCANNER) server-header $< $@

protocol/pomelo-workspace-v1-protocol.c: protocol/pomelo-workspace-v1.xml
	$(WAYLAND_SCANNER) private-code $< $@

protocol/xdg-shell-protocol.c: $(WAYLAND_PROTOCOLS_DIR)/stable/xdg-shell/xdg-shell.xml
	$(WAYLAND_SCANNER) private-code $< $@

install: pomelo-compositor pomelo-msg
	install -Dm755 pomelo-compositor $(DESTDIR)$(PREFIX)/bin/pomelo-compositor
	install -Dm755 pomelo-msg $(DESTDIR)$(PREFIX)/bin/pomelo-msg
	install -Dm644 pomelo-portals.conf $(DESTDIR)$(PREFIX)/share/xdg-desktop-portal/pomelo-portals.conf

clean:
	rm -f pomelo-compositor pomelo-msg protocol/pomelo-workspace-v1-client-protocol.h $(PROTO_HDR) $(PROTO_XDG_HDR) $(PROTO_SRC)

.PHONY: all install clean
