project(
  'swindle',
  'c',
  version: '0.1',
  default_options: [
    'c_std=c11',
    'buildtype=debugoptimized',
  ],
)

cc = meson.get_compiler('c')

add_project_arguments(
  '-DWLR_USE_UNSTABLE',
  '-D_POSIX_C_SOURCE=200809L',
  '-DVERSION="0.1"',
  '-Wall',
  '-Wextra',
  '-Wno-unused-parameter',
  '-Wno-stringop-truncation',
  language: 'c',
)

xwayland_opt = get_option('xwayland')

wayland_server = dependency('wayland-server')
wayland_client = dependency('wayland-client')
wayland_protocols = dependency('wayland-protocols')
wayland_scanner_dep = dependency('wayland-scanner', native: true)
wlroots = dependency('wlroots-0.20')
xkbcommon = dependency('xkbcommon')
libinput = dependency('libinput')
lua = dependency('lua5.4')
m_dep = cc.find_library('m', required: false)

swindle_deps = [
  wayland_server,
  wlroots,
  xkbcommon,
  libinput,
  lua,
  m_dep,
]

if xwayland_opt
  swindle_deps += dependency('xcb')
  swindle_deps += dependency('xcb-icccm')
  add_project_arguments('-DXWAYLAND', language: 'c')
endif

wayland_scanner = find_program(
  wayland_scanner_dep.get_variable('wayland_scanner'),
  native: true,
)
wl_protocol_dir = wayland_protocols.get_variable('pkgdatadir')

wayland_scanner_server = generator(
  wayland_scanner,
  output: '@BASENAME@-protocol.h',
  arguments: ['server-header', '@INPUT@', '@OUTPUT@'],
)

wayland_scanner_private = generator(
  wayland_scanner,
  output: '@BASENAME@-protocol.c',
  arguments: ['private-code', '@INPUT@', '@OUTPUT@'],
)

wayland_scanner_enum = generator(
  wayland_scanner,
  output: '@BASENAME@-protocol.h',
  arguments: ['enum-header', '@INPUT@', '@OUTPUT@'],
)

cursor_shape_xml = wl_protocol_dir / 'staging/cursor-shape/cursor-shape-v1.xml'
pointer_constraints_xml = wl_protocol_dir / 'unstable/pointer-constraints/pointer-constraints-unstable-v1.xml'
xdg_shell_xml = wl_protocol_dir / 'stable/xdg-shell/xdg-shell.xml'

layer_shell_xml = 'protocols/wlr-layer-shell-unstable-v1.xml'
output_power_xml = 'protocols/wlr-output-power-management-unstable-v1.xml'
dwl_ipc_xml = 'protocols/dwl-ipc-unstable-v2.xml'
ext_workspace_xml = 'protocols/ext-workspace-v1.xml'

cursor_shape_h = wayland_scanner_enum.process(cursor_shape_xml)
pointer_constraints_h = wayland_scanner_enum.process(pointer_constraints_xml)
layer_shell_h = wayland_scanner_enum.process(layer_shell_xml)
output_power_h = wayland_scanner_server.process(output_power_xml)
xdg_shell_h = wayland_scanner_server.process(xdg_shell_xml)

dwl_ipc_h = wayland_scanner_server.process(dwl_ipc_xml)
dwl_ipc_c = wayland_scanner_private.process(dwl_ipc_xml)
ext_workspace_h = wayland_scanner_server.process(ext_workspace_xml)
ext_workspace_c = wayland_scanner_private.process(ext_workspace_xml)

protocol_headers = [
  cursor_shape_h,
  pointer_constraints_h,
  layer_shell_h,
  output_power_h,
  xdg_shell_h,
]

swindle_sources = [
  'src/swindle.c',
  'src/util.c',
  'src/wlr_ext_workspace_v1.c',
  'parser/parser.c',
  dwl_ipc_c,
  dwl_ipc_h,
  ext_workspace_c,
  ext_workspace_h,
]

executable(
  'swindle',
  swindle_sources + protocol_headers,
  include_directories: include_directories('.', 'include', 'src', 'parser'),
  dependencies: swindle_deps,
  install: true,
)

smsg_dwl_ipc_h = custom_target(
  'smsg-dwl-ipc-unstable-v2-protocol-h',
  input: dwl_ipc_xml,
  output: 'dwl-ipc-unstable-v2-protocol.h',
  command: [wayland_scanner, 'client-header', '@INPUT@', '@OUTPUT@'],
)

smsg_dwl_ipc_c = custom_target(
  'smsg-dwl-ipc-unstable-v2-protocol-c',
  input: dwl_ipc_xml,
  output: 'dwl-ipc-unstable-v2-protocol.c',
  command: [wayland_scanner, 'private-code', '@INPUT@', '@OUTPUT@'],
)

executable(
  'smsg',
  ['smsg/smsg.c', smsg_dwl_ipc_c, smsg_dwl_ipc_h],
  dependencies: [wayland_client],
  install: true,
)

install_subdir('example', install_dir: get_option('sysconfdir') / 'swindle', strip_directory: true)
