SDL: Add testmouse

From 830206581b485afb82e1778412815b92c01d3121 Mon Sep 17 00:00:00 2001
From: Cameron Cawley <[EMAIL REDACTED]>
Date: Thu, 9 Sep 2021 21:33:35 +0100
Subject: [PATCH] Add testmouse

---
 .gitignore          |   1 +
 test/CMakeLists.txt |   1 +
 test/Makefile.in    |   4 +
 test/Makefile.os2   |   2 +-
 test/README         |   1 +
 test/testmouse.c    | 192 ++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 200 insertions(+), 1 deletion(-)
 create mode 100644 test/testmouse.c

diff --git a/.gitignore b/.gitignore
index 68a9026cf2..c1eb67f9d8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -136,6 +136,7 @@ test/testloadso
 test/testlocale
 test/testlock
 test/testmessage
+test/testmouse
 test/testmultiaudio
 test/testnative
 test/testoverlay2
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index e9f66edd8f..7292e44f0e 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -57,6 +57,7 @@ add_executable(testjoystick testjoystick.c)
 add_executable(testkeys testkeys.c)
 add_executable(testloadso testloadso.c)
 add_executable(testlock testlock.c)
+add_executable(testmouse testmouse.c)
 
 if(APPLE)
     add_executable(testnative testnative.c
diff --git a/test/Makefile.in b/test/Makefile.in
index 019d0bb353..df70b675d8 100644
--- a/test/Makefile.in
+++ b/test/Makefile.in
@@ -43,6 +43,7 @@ TARGETS = \
 	testlocale$(EXE) \
 	testlock$(EXE) \
 	testmessage$(EXE) \
+	testmouse$(EXE) \
 	testmultiaudio$(EXE) \
 	testnative$(EXE) \
 	testoverlay2$(EXE) \
@@ -326,6 +327,9 @@ testvulkan$(EXE): $(srcdir)/testvulkan.c
 testlocale$(EXE): $(srcdir)/testlocale.c
 	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
 
+testmouse$(EXE): $(srcdir)/testmouse.c
+	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
+
 
 
 clean:
diff --git a/test/Makefile.os2 b/test/Makefile.os2
index 4a78fdf3e9..2d2acdbf61 100644
--- a/test/Makefile.os2
+++ b/test/Makefile.os2
@@ -11,7 +11,7 @@ TARGETS = testatomic.exe testdisplayinfo.exe testbounds.exe testdraw2.exe &
           testshader.exe testshape.exe testsprite2.exe testspriteminimal.exe &
           teststreaming.exe testthread.exe testtimer.exe testver.exe &
           testviewport.exe testwm2.exe torturethread.exe checkkeys.exe &
-          checkkeysthreads.exe &
+          checkkeysthreads.exe testmouse.exe &
           controllermap.exe testhaptic.exe testqsort.exe testresample.exe &
           testaudioinfo.exe testaudiocapture.exe loopwave.exe loopwavequeue.exe &
           testyuv.exe testgl2.exe testvulkan.exe testnative.exe testautomation.exe
diff --git a/test/README b/test/README
index 8329cb96f3..83337cf7aa 100644
--- a/test/README
+++ b/test/README
@@ -14,6 +14,7 @@ These are test programs for the SDL library:
 	testloadso	Tests the loadable library layer
 	testlocale  Test Locale API
 	testlock	Hacked up test of multi-threading and locking
+	testmouse	Tests mouse coordinates
 	testmultiaudio	Tests using several audio devices
 	testoverlay2	Tests the overlay flickering/scaling during playback.
 	testplatform	Tests types, endianness and cpu capabilities
diff --git a/test/testmouse.c b/test/testmouse.c
new file mode 100644
index 0000000000..f4cb1338e0
--- /dev/null
+++ b/test/testmouse.c
@@ -0,0 +1,192 @@
+/*
+  Copyright (C) 1997-2021 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.
+*/
+
+#include "SDL.h"
+
+#ifdef __EMSCRIPTEN__
+#include <emscripten/emscripten.h>
+#endif
+
+#ifdef __IPHONEOS__
+#define SCREEN_WIDTH    320
+#define SCREEN_HEIGHT   480
+#else
+#define SCREEN_WIDTH    640
+#define SCREEN_HEIGHT   480
+#endif
+
+static SDL_Window *window;
+static SDL_Renderer *renderer;
+
+typedef struct Line Line;
+
+typedef struct Line {
+    Line *next;
+
+    int x1, y1, x2, y2;
+    Uint8 r, g, b;
+} Line;
+
+static Line *active = NULL;
+static Line *lines = NULL;
+static int buttons = 0;
+
+static SDL_bool done = SDL_FALSE;
+
+void
+DrawLine(SDL_Renderer * renderer, Line * line)
+{
+    SDL_SetRenderDrawColor(renderer, line->r, line->g, line->b, 255);
+    SDL_RenderDrawLine(renderer, line->x1, line->y1, line->x2, line->y2);
+}
+
+void
+DrawLines(SDL_Renderer * renderer)
+{
+    Line *next = lines;
+    while (next != NULL) {
+        DrawLine(renderer, next);
+        next = next->next;
+    }
+}
+
+void
+AppendLine(Line *line)
+{
+    if (lines) {
+        Line *next = lines;
+        while (next->next != NULL) {
+            next = next->next;
+        }
+        next->next = line;
+    } else {
+        lines = line;
+    }
+}
+
+void
+loop()
+{
+    int i;
+    SDL_Event event;
+
+    /* Check for events */
+    while (SDL_PollEvent(&event)) {
+        switch (event.type) {
+        case SDL_MOUSEMOTION:
+            if (!active)
+                break;
+
+            active->x2 = event.motion.x;
+            active->y2 = event.motion.y;
+            break;
+
+        case SDL_MOUSEBUTTONDOWN:
+            if (!active) {
+                active = SDL_calloc(1, sizeof(*active));
+                active->x1 = active->x2 = event.button.x;
+                active->y1 = active->y2 = event.button.y;
+            }
+
+            switch (event.button.button) {
+            case SDL_BUTTON_LEFT:   active->r = 255; buttons |= SDL_BUTTON_LMASK; break;
+            case SDL_BUTTON_MIDDLE: active->g = 255; buttons |= SDL_BUTTON_MMASK; break;
+            case SDL_BUTTON_RIGHT:  active->b = 255; buttons |= SDL_BUTTON_RMASK; break;
+            }
+            break;
+        case SDL_MOUSEBUTTONUP:
+            if (!active)
+                break;
+
+            switch (event.button.button) {
+            case SDL_BUTTON_LEFT:   buttons &= ~SDL_BUTTON_LMASK; break;
+            case SDL_BUTTON_MIDDLE: buttons &= ~SDL_BUTTON_MMASK; break;
+            case SDL_BUTTON_RIGHT:  buttons &= ~SDL_BUTTON_RMASK; break;
+            }
+
+            if (buttons == 0) {
+                AppendLine(active);
+                active = NULL;
+            }
+            break;
+
+        case SDL_QUIT:
+            done = SDL_TRUE;
+            break;
+
+        default:
+            break;
+        }
+    }
+
+    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
+    SDL_RenderClear(renderer);
+
+    DrawLines(renderer);
+    if (active)
+        DrawLine(renderer, active);
+
+    SDL_RenderPresent(renderer);
+
+#ifdef __EMSCRIPTEN__
+    if (done) {
+        emscripten_cancel_main_loop();
+    }
+#endif
+}
+
+int
+main(int argc, char *argv[])
+{
+    /* Enable standard application logging */
+    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
+
+    /* Initialize SDL (Note: video is required to start event loop) */
+    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
+        exit(1);
+    }
+
+    /* Create a window to display joystick axis position */
+    window = SDL_CreateWindow("Mouse Test", SDL_WINDOWPOS_CENTERED,
+                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
+                              SCREEN_HEIGHT, 0);
+    if (window == NULL) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
+        return SDL_FALSE;
+    }
+
+    renderer = SDL_CreateRenderer(window, -1, 0);
+    if (renderer == NULL) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
+        SDL_DestroyWindow(window);
+        return SDL_FALSE;
+    }
+
+    /* Main render loop */
+#ifdef __EMSCRIPTEN__
+    emscripten_set_main_loop(loop, 0, 1);
+#else
+    while (!done) {
+        loop();
+    }
+#endif
+
+    SDL_DestroyRenderer(renderer);
+    SDL_DestroyWindow(window);
+
+    SDL_Quit();
+
+    return 0;
+}
+
+/* vi: set ts=4 sw=4 expandtab: */