shithub: pt2-clone

Download patch

ref: ed51fabbc1feaa0a4fadb7541fb9527bfccbe185
parent: da054584cd20bb9147d3fbc5d281490df6e0fd99
author: Olav Sørensen <olav.sorensen@live.no>
date: Fri Oct 14 14:09:16 EDT 2022

Thread-safe mouse color change from replayer

--- a/src/pt2_mouse.c
+++ b/src/pt2_mouse.c
@@ -75,15 +75,9 @@
 		setSystemCursor(cursors[cursorColorIndex]);
 }
 
-void pointerSetMode(uint8_t pointerMode, bool carry)
+void updatePointerColor(void)
 {
-	assert(pointerMode <= 5);
-
-	ui.pointerMode = pointerMode;
-	if (carry)
-		ui.previousPointerMode = ui.pointerMode;
-
-	switch (pointerMode)
+	switch (ui.pointerMode)
 	{
 		case POINTER_MODE_IDLE:   pointerSetColor(POINTER_GRAY);   break;
 		case POINTER_MODE_PLAY:   pointerSetColor(POINTER_YELLOW); break;
@@ -95,12 +89,30 @@
 	}
 }
 
-void pointerResetThreadSafe(void) // used for effect F00 in replayer (stop song)
+void pointerSetMode(uint8_t pointerMode, bool carry)
 {
-	ui.previousPointerMode = ui.pointerMode = POINTER_MODE_IDLE;
+	assert(pointerMode <= 5);
 
+	ui.pointerMode = pointerMode;
+	if (carry)
+		ui.previousPointerMode = ui.pointerMode;
+
+	updatePointerColor();
+}
+
+void pointerSetModeThreadSafe(uint8_t pointerMode, bool carry)
+{
+	assert(pointerMode <= 5);
+
+	ui.pointerMode = pointerMode;
+	if (carry)
+		ui.previousPointerMode = ui.pointerMode;
+
+	// in hardware mouse mode, the pointer color can only be changed from the main thread
 	if (config.hwMouse)
-		mouse.resetCursorColorFlag = true;
+		mouse.updatePointerColorFlag = true;
+	else
+		updatePointerColor();
 }
 
 void pointerSetPreviousMode(void)
@@ -237,10 +249,10 @@
 {
 	int32_t mx, my, windowX, windowY;
 
-	if (mouse.resetCursorColorFlag) // used for effect F00 in replayer (stop song)
+	if (mouse.updatePointerColorFlag) // used when changing pointer color from other threads
 	{
-		mouse.resetCursorColorFlag = false;
-		pointerSetColor(POINTER_GRAY);
+		mouse.updatePointerColorFlag = false;
+		updatePointerColor();
 	}
 
 	if (mouse.setPosFlag)
--- a/src/pt2_mouse.h
+++ b/src/pt2_mouse.h
@@ -247,7 +247,7 @@
 void setMsgPointer(void);
 void setErrPointer(void);
 void pointerSetMode(uint8_t pointerMode, bool carry);
-void pointerResetThreadSafe(void); // used for effect F00 in replayer (stop song)
+void pointerSetModeThreadSafe(uint8_t pointerMode, bool carry);
 void pointerSetPreviousMode(void);
 bool setSystemCursor(SDL_Cursor *cur);
 void freeMouseCursors(void);
--- a/src/pt2_replayer.c
+++ b/src/pt2_replayer.c
@@ -130,8 +130,7 @@
 	{
 		editor.playMode = PLAY_MODE_NORMAL;
 		editor.currMode = MODE_IDLE;
-
-		pointerSetMode(POINTER_MODE_IDLE, DO_CARRY);
+		pointerSetModeThreadSafe(POINTER_MODE_IDLE, true);
 	}
 
 	if (song != NULL)
@@ -1014,9 +1013,9 @@
 			{
 				doStopIt(false);
 
-				pointerSetMode(POINTER_MODE_EDIT, DO_CARRY);
 				editor.playMode = PLAY_MODE_NORMAL;
 				editor.currMode = MODE_EDIT;
+				pointerSetModeThreadSafe(POINTER_MODE_EDIT, true);
 			}
 			else
 			{
@@ -1260,9 +1259,9 @@
 			{
 				doStopIt(false);
 
-				pointerSetMode(POINTER_MODE_EDIT, DO_CARRY);
 				editor.playMode = PLAY_MODE_NORMAL;
 				editor.currMode = MODE_EDIT;
+				pointerSetModeThreadSafe(POINTER_MODE_EDIT, true);
 			}
 			else
 			{
@@ -1311,10 +1310,10 @@
 		doStopSong = false;
 
 		editor.songPlaying = false;
+
 		editor.playMode = PLAY_MODE_NORMAL;
 		editor.currMode = MODE_IDLE;
-
-		pointerResetThreadSafe(); // set normal gray mouse pointer
+		pointerSetModeThreadSafe(POINTER_MODE_IDLE, true);
 	}
 
 	return renderEndCheck(); // MOD2WAV/PAT2SMP listens to the return value (true = not done yet)
--- a/src/pt2_structs.h
+++ b/src/pt2_structs.h
@@ -118,7 +118,7 @@
 
 typedef struct mouse_t
 {
-	volatile bool setPosFlag, resetCursorColorFlag;
+	volatile bool setPosFlag, updatePointerColorFlag;
 	bool buttonWaiting, leftButtonPressed, rightButtonPressed;
 	uint8_t repeatCounter, buttonWaitCounter;
 	int32_t rawX, rawY, x, y, lastMouseX, setPosX, setPosY, lastGUIButton, prevX, prevY;