/*
PolyGun

Copyright (c) 2023 mrkubax10 <mrkubax10@onet.pl>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#ifndef POLYGUN_WINDOW_X11_WINDOW
#define POLYGUN_WINDOW_X11_WINDOW

#include "game/window/window.hpp"

#include <stdexcept>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

namespace polygun::window {
	class X11Window final : public Window {
	public:
		X11Window(const std::string& title, unsigned width, unsigned height, renderer::RendererType renderer_type);
		~X11Window() override;

		Display* get_display() { return m_display; }
		int get_screen() const { return m_screen; }
		XVisualInfo* get_visual_info() { return m_visual_info; }
		::Window get_window_id() const { return m_window_id; }

		virtual size_t poll_events(Event& event) override;
		virtual void finish_frame() override;
		virtual void grab_mouse(bool grab) override;

	private:
#if defined(RENDERER_GL) || defined(RENDERER_LEGACY_GL)
		bool m_glx_has_doublebuffer;
#endif
		Display* m_display;
		int m_screen;
		XVisualInfo* m_visual_info;
		::Window m_window_id;
		Atom m_wm_delete;
		renderer::RendererType m_renderer_type;
		XIC m_input_context;
		unsigned m_prev_width;
		unsigned m_prev_height;
		bool m_was_mouse_grabbed;
		bool m_skip_motion_event;
		bool m_first_focus_out_event;

	private:
#if defined(RENDERER_GL) || defined(RENDERER_LEGACY_GL)
		XVisualInfo* setup_glx();
#endif
	};
}

#endif // POLYGUN_WINDOW_X11_WINDOW
