// Copyright 2020 - 2025, project-repo and the NEDM contributors
// SPDX-License-Identifier: MIT

#ifndef NEDM_INPUT_MANAGER_H
#define NEDM_INPUT_MANAGER_H

#include "seat.h"
#include "server.h"
#include <wayland-server-core.h>
#include <wlr/util/box.h>

struct nedm_input_manager *
input_manager_create(struct nedm_server *server);
void
input_manager_destroy(struct nedm_input_manager *input);
void
input_manager_handle_device_destroy(struct wl_listener *listener, void *data);
uint32_t
input_manager_get_mouse_button(const char *name, char **error);
struct nedm_input_config *
input_manager_create_empty_input_config(void);
struct nedm_input_config *
input_manager_merge_input_configs(struct nedm_input_config *cfg1,
                                  struct nedm_input_config *cfg2);
void
nedm_input_manager_configure(struct nedm_server *server);
void
nedm_input_manager_configure_keyboard_group(struct nedm_keyboard_group *group);

struct nedm_input_manager {
	struct wl_list devices;
	struct nedm_server *server;

	struct wlr_virtual_keyboard_manager_v1 *virtual_keyboard;
	struct wlr_virtual_pointer_manager_v1 *virtual_pointer;

	struct wl_listener new_input;
	struct wl_listener virtual_keyboard_new;
	struct wl_listener virtual_pointer_new;
};

struct nedm_input_config_mapped_from_region {
	double x1, y1;
	double x2, y2;
	bool mm;
};

struct calibration_matrix {
	bool configured;
	float matrix[6];
};

enum nedm_input_config_mapped_to {
	MAPPED_TO_DEFAULT,
	MAPPED_TO_OUTPUT,
	MAPPED_TO_REGION,
};

/**
 * options for input devices
 */
struct nedm_input_config {
	char *identifier;

	/* Libinput devices */
	int accel_profile;
	struct calibration_matrix calibration_matrix;
	int click_method;
	int drag;
	int drag_lock;
	int dwt;
	int left_handed;
	int middle_emulation;
	int natural_scroll;
	float pointer_accel;
	float scroll_factor;
	/*int repeat_delay;
	int repeat_rate;*/
	int scroll_button;
	int scroll_method;
	int send_events;
	int tap;
	int tap_button_map;

	/*char *xkb_layout;
	char *xkb_model;
	char *xkb_options;
	char *xkb_rules;
	char *xkb_variant;
	char *xkb_file;

	bool xkb_file_is_set;

	int xkb_numlock;
	int xkb_capslock;*/

	struct wl_list link;
	struct nedm_input_config_mapped_from_region *mapped_from_region;

	enum nedm_input_config_mapped_to mapped_to;
	char *mapped_to_output;
	/*struct wlr_box *mapped_to_region;*/

	/*struct wl_list tools;*/

	bool capturable;
	struct wlr_box region;

	/* Keyboards */
	int enable_keybindings;
	int repeat_delay;
	int repeat_rate;
};

struct nedm_input_device {
	char *identifier;
	struct nedm_server *server;
	struct wlr_input_device *wlr_device;
	struct wl_list link; // input_manager::devices
	struct wl_listener device_destroy;
	bool is_virtual;

	/* Only one of the following is non-NULL depending on the type of the input
	 * device */
	struct nedm_pointer *pointer;
	struct nedm_touch *touch;
};

#endif
