/*
  Simple DirectMedia Layer
  Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.
*/

#include "SDL_internal.h"

#if defined(SDL_VIDEO_DRIVER_X11) && defined(SDL_VIDEO_DRIVER_X11_XSYNC)

#include "SDL_x11video.h"
#include "SDL_x11xsync.h"

static int xsync_initialized = 0;

static int query_xsync_version(Display *display, int major, int minor)
{
    /* We don't care if this fails, so long as it sets major/minor on it's way out the door. */
    X11_XSyncInitialize(display, &major, &minor);
    return (major * 1000) + minor;
}

static bool xsync_version_atleast(const int version, const int wantmajor, const int wantminor)
{
    return version >= ((wantmajor * 1000) + wantminor);
}

void X11_InitXsync(SDL_VideoDevice *_this)
{
    SDL_VideoData *data = (SDL_VideoData *) _this->internal;

    int version = 0;
    int event, error;
    int sync_opcode;

    if (!SDL_X11_HAVE_XSYNC ||
        !X11_XQueryExtension(data->display, "SYNC", &sync_opcode, &event, &error)) {
        return;
    }

    /* We need at least 5.0 for barriers. */
    version = query_xsync_version(data->display, 5, 0);
    if (!xsync_version_atleast(version, 3, 0)) {
        return; /* X server does not support the version we want at all. */
    }

    xsync_initialized = 1;
}

int X11_XsyncIsInitialized(void)
{
    return xsync_initialized;
}

int X11_InitResizeSync(SDL_Window *window)
{
    SDL_assert(window != NULL);
    SDL_WindowData *data = (SDL_WindowData *) window->internal;
    Display *display = data->videodata->display;
    Atom counter_prop = data->videodata->atoms._NET_WM_SYNC_REQUEST_COUNTER;
    XSyncCounter counter;
    CARD32 counter_id;

    if (!X11_XsyncIsInitialized()){
        return SDL_Unsupported();
    }

    counter = X11_XSyncCreateCounter(display, (XSyncValue){0, 0});
    data->resize_counter = counter;
    data->resize_id.lo = 0;
    data->resize_id.hi = 0;
    data->resize_in_progress = false;

    if (counter == None){
        return SDL_Unsupported();
    }

    counter_id = counter;
    X11_XChangeProperty(display, data->xwindow, counter_prop, XA_CARDINAL, 32,
                        PropModeReplace, (unsigned char *)&counter_id, 1);

    return 0;
}

void X11_TermResizeSync(SDL_Window *window)
{
    SDL_WindowData *data = (SDL_WindowData *) window->internal;
    Display *display = data->videodata->display;
    Atom counter_prop = data->videodata->atoms._NET_WM_SYNC_REQUEST_COUNTER;
    XSyncCounter counter = data->resize_counter;

    X11_XDeleteProperty(display, data->xwindow, counter_prop);
    if (counter != None) {
        X11_XSyncDestroyCounter(display, counter);
    }
}

void X11_HandleSyncRequest(SDL_Window *window, XClientMessageEvent *event)
{
    SDL_WindowData *data = (SDL_WindowData *) window->internal;

    data->resize_id.lo = event->data.l[2];
    data->resize_id.hi = event->data.l[3];
    data->resize_in_progress = false;
}

void X11_HandleConfigure(SDL_Window *window, XConfigureEvent *event)
{
    SDL_WindowData *data = (SDL_WindowData *) window->internal;

    if (data->resize_id.lo || data->resize_id.hi) {
        data->resize_in_progress = true;
    }
}

void X11_HandlePresent(SDL_Window *window)
{
    SDL_WindowData *data = (SDL_WindowData *) window->internal;
    Display *display = data->videodata->display;
    XSyncCounter counter = data->resize_counter;

    if ((counter == None) || (!data->resize_in_progress)) {
        return;
    }

    X11_XSyncSetCounter(display, counter, data->resize_id);

    data->resize_id.lo = 0;
    data->resize_id.hi = 0;
    data->resize_in_progress = false;
}

#endif /* SDL_VIDEO_DRIVER_X11 && SDL_VIDEO_DRIVER_X11_XSYNC */
