From ee9e2d290aed2d9206b911140662ec2aa2babd4f Mon Sep 17 00:00:00 2001
From: Puck Meerburg <puck@puckipedia.nl>
Date: Tue, 2 Dec 2014 15:06:43 +0000
Subject: Fix Haiku compilation, add screen_blanker wrapper for screen savers


diff --git a/include/rsHaikuScreenSaver/rsHaikuScreenSaver.h b/include/rsHaikuScreenSaver/rsHaikuScreenSaver.h
new file mode 100644
index 0000000..098b748
--- /dev/null
+++ b/include/rsHaikuScreenSaver/rsHaikuScreenSaver.h
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2006  Terence M. Welsh (rsXScreenSaver)
+ * Modified by Puck Meerburg, (c) 2014
+ * 
+ * This file is part of rsHaikuScreenSaver.
+ *
+ * rsHaikuScreenSaver is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * rsHaikuScreenSaver is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+
+/*
+ * rsHaikuScreenSaver library, based on rsXScreenSaver
+ *
+ * Library for handling standard OpenGL screensaver functionality, such
+ * as opening windows, choosing pixel formats, setting video modes,
+ * handling user input, etc...
+ */
+
+
+
+#ifndef RSHAIKUSCREENSAVER_H
+#define RSHAIKUSCREENSAVER_H
+
+
+#include <GL/gl.h>
+#include <stdlib.h>
+#include <util/rsTimer.h>
+#include <pthread.h>
+#include <util/arguments.h>
+
+
+
+extern int checkingPassword;  // A saver should check this and stop drawing if true
+extern int isSuspended;  // power save mode
+extern int doingPreview;  // Previews take extra long to initialize because Windows
+// has lots of screwy resource hogging problems.  It's good to check this so that you
+// can simplify your preview.
+
+// These variables can be useful information for saver programmer
+extern int pfd_swap_exchange;  // Is this bit set in the PIXELFORMATDESCRIPTOR?
+extern int pfd_swap_copy;  // Is this bit set in the PIXELFORMATDESCRIPTOR?
+// used to limit frame rate of saver (0 = no limit)
+extern unsigned int dFrameRateLimit;
+// Keyboard toggle for displaying statistics, such as frames per second.
+// User must implement the displaying of statistics if kStatistics is true.
+extern int kStatistics;
+
+
+
+//----------------------------------------------------------------------------
+
+
+// All idle processing is done here.  Typically, you would just draw 
+// frames during this routine.
+//void idleProc();
+
+
+
+#endif
diff --git a/include/rsMath/rsMatrix.h b/include/rsMath/rsMatrix.h
index 75d8cfc..08794b8 100644
--- a/include/rsMath/rsMatrix.h
+++ b/include/rsMath/rsMatrix.h
@@ -24,7 +24,7 @@
 
 
 
-#include <ostream>
+#include <iostream>
 
 
 
diff --git a/include/util/arguments.h b/include/util/arguments.h
index 7295a0c..899bebe 100644
--- a/include/util/arguments.h
+++ b/include/util/arguments.h
@@ -33,7 +33,10 @@
 
 // Handy template for converting std::string to numeric types.
 template <class T>
-bool from_string(T& t, const std::string& str, std::ios_base& (*f)(std::ios_base&)){
+bool from_string(
+	T& t,
+	const std::string& str,
+	std::ios& (*f)(std::ios&)){
 	std::istringstream iss(str);
 	return !(iss >> f >> t).fail();
 }
diff --git a/include/util/rsTimer.h b/include/util/rsTimer.h
index 0eeed2d..0a9855d 100644
--- a/include/util/rsTimer.h
+++ b/include/util/rsTimer.h
@@ -28,6 +28,7 @@
 #include <windows.h>
 #else
 #include <stdlib.h>
+#include <unistd.h>
 #include <sys/time.h>
 #include <iostream>
 #endif
diff --git a/src/Flocks/Flocks.cpp b/src/Flocks/Flocks.cpp
index 12515fc..3eb5189 100644
--- a/src/Flocks/Flocks.cpp
+++ b/src/Flocks/Flocks.cpp
@@ -32,6 +32,9 @@
 #ifdef RS_XSCREENSAVER
 #include <rsXScreenSaver/rsXScreenSaver.h>
 #endif
+#ifdef RS_HAIKUSCREENSAVER
+#include <rsHaikuScreenSaver/rsHaikuScreenSaver.h>
+#endif
 
 #include <stdio.h>
 #include <math.h>
@@ -417,7 +420,7 @@ void setDefaults(){
 }
 
 
-#ifdef RS_XSCREENSAVER
+#if defined(RS_XSCREENSAVER) || defined(RS_HAIKUSCREENSAVER)
 void handleCommandLine(int argc, char* argv[]){
 	setDefaults();
 	getArgumentsValue(argc, argv, std::string("-leaders"), dLeaders, 1, 100);
@@ -468,7 +471,7 @@ void initSaver(HWND hwnd){
 	
 	reshape(rect.right, rect.bottom);
 #endif
-#ifdef RS_XSCREENSAVER
+#if defined(RS_XSCREENSAVER) || defined(RS_HAIKUSCREENSAVER)
 void initSaver(){
 #endif
 	int i;
@@ -529,7 +532,7 @@ void initSaver(){
 }
 
 
-#ifdef RS_XSCREENSAVER
+#if defined(RS_XSCREENSAVER) | defined(RS_HAIKUSCREENSAVER)
 void cleanUp(){
 	// Free memory
 	delete[] lBugs;
diff --git a/src/Flocks/Makefile b/src/Flocks/Makefile
index c8e926e..b816e29 100644
--- a/src/Flocks/Makefile
+++ b/src/Flocks/Makefile
@@ -1,6 +1,6 @@
 INCLUDE = -I../../include
-LIBRARY = -lm -lGL -lGLU ../../lib/rsXScreenSaver.a ../../lib/Rgbhsl.a ../../lib/rsText.a
-CFLAGS = -O3 -DRS_XSCREENSAVER $(INCLUDE)
+LIBRARY = -shared -g -lGL -lGLU ../../lib/rsHaikuScreenSaver.a ../../lib/Rgbhsl.a ../../lib/rsText.a # -lm 
+CFLAGS = -O3 -g -DRS_HAIKUSCREENSAVER $(INCLUDE)
 
 OBJECTS = Flocks.o
 
diff --git a/src/Flux/Flux.cpp b/src/Flux/Flux.cpp
index 0bff6c1..f297801 100644
--- a/src/Flux/Flux.cpp
+++ b/src/Flux/Flux.cpp
@@ -32,6 +32,9 @@
 #ifdef RS_XSCREENSAVER
 #include <rsXScreenSaver/rsXScreenSaver.h>
 #endif
+#ifdef RS_HAIKUSCREENSAVER
+#include <rsHaikuScreenSaver/rsHaikuScreenSaver.h>
+#endif
 
 #include <stdio.h>
 #include <rsText/rsText.h>
@@ -82,7 +85,7 @@ int dRotation;
 int dWind;
 int dInstability;
 int dBlur;
-#ifdef RS_XSCREENSAVER
+#if defined(RS_XSCREENSAVER) || defined(RS_HAIKUSCREENSAVER)
 #define DEFAULTS1 1
 #define DEFAULTS2 2
 #define DEFAULTS3 3
@@ -606,7 +609,7 @@ void setDefaults(int which){
 }
 
 
-#ifdef RS_XSCREENSAVER
+#if defined(RS_XSCREENSAVER) || defined(RS_HAIKUSCREENSAVER)
 void handleCommandLine(int argc, char* argv[]){
 	int defaults = DEFAULTS1;
 	setDefaults(defaults);
@@ -654,7 +657,7 @@ void initSaver(HWND hwnd){
 	glLoadIdentity();
 	gluPerspective(100.0, aspectRatio, 0.01, 200.0);
 #endif
-#ifdef RS_XSCREENSAVER
+#if defined(RS_XSCREENSAVER) || defined(RS_HAIKUSCREENSAVER)
 void initSaver(){
 #endif
 	int i, j;
@@ -749,7 +752,7 @@ void initSaver(){
 }
 
 
-#ifdef RS_XSCREENSAVER
+#if defined(RS_XSCREENSAVER) || defined(RS_HAIKUSCREENSAVER)
 void cleanUp(){
 	// Free memory
 	delete[] fluxes;
diff --git a/src/Flux/Makefile b/src/Flux/Makefile
index 903cfd4..0b14ab8 100644
--- a/src/Flux/Makefile
+++ b/src/Flux/Makefile
@@ -1,6 +1,6 @@
 INCLUDE = -I../../include
-LIBRARY = -L../../lib -lm -lGL -lGLU ../../lib/rsXScreenSaver.a ../../lib/Rgbhsl.a ../../lib/rsText.a
-CFLAGS = -O3 -DRS_XSCREENSAVER $(INCLUDE)
+LIBRARY = -L../../lib -shared -lGL -lGLU ../../lib/rsHaikuScreenSaver.a ../../lib/Rgbhsl.a ../../lib/rsText.a # -lm 
+CFLAGS = -O3 -DRS_HAIKUSCREENSAVER $(INCLUDE)
 
 OBJECTS = Flux.o
 
diff --git a/src/Hyperspace/Hyperspace.cpp b/src/Hyperspace/Hyperspace.cpp
index 490db7b..c9998d3 100644
--- a/src/Hyperspace/Hyperspace.cpp
+++ b/src/Hyperspace/Hyperspace.cpp
@@ -35,6 +35,9 @@
 #ifdef RS_XSCREENSAVER
 #include <rsXScreenSaver/rsXScreenSaver.h>
 #endif
+#ifdef RS_HAIKUSCREENSAVER
+#include <rsHaikuScreenSaver/rsHaikuScreenSaver.h>
+#endif
 
 #include <stdio.h>
 #include <rsText/rsText.h>
@@ -446,7 +449,7 @@ void setDefaults(){
 }
 
 
-#ifdef RS_XSCREENSAVER
+#if defined(RS_XSCREENSAVER) || defined(RS_HAIKUSCREENSAVER)
 void handleCommandLine(int argc, char* argv[]){
 	setDefaults();
 	getArgumentsValue(argc, argv, std::string("-speed"), dSpeed, 1, 100);
@@ -506,7 +509,7 @@ void initSaver(HWND hwnd){
 
 	reshape(rect.right, rect.bottom);
 #endif
-#ifdef RS_XSCREENSAVER
+#if defined(RS_XSCREENSAVER) || defined(RS_HAIKUSCREENSAVER)
 void initSaver(){
 #endif
 	// Seed random number generator
@@ -630,7 +633,7 @@ void initSaver(){
 }
 
 
-#ifdef RS_XSCREENSAVER
+#if defined(RS_XSCREENSAVER) || defined(RS_HAIKUSCREENSAVER)
 void cleanUp(){
 	// Free memory
 	if(dUseGoo)
diff --git a/src/Hyperspace/Makefile b/src/Hyperspace/Makefile
index 99f26cb..721b982 100644
--- a/src/Hyperspace/Makefile
+++ b/src/Hyperspace/Makefile
@@ -1,6 +1,6 @@
 INCLUDE = -I. -I../../include
-LIBRARY = -lm -lGL -lGLU ../../lib/rsXScreenSaver.a ../../lib/rsMath.a ../../lib/Implicit.a ../../lib/rsText.a  ../../lib/Rgbhsl.a
-CFLAGS = -O3 -DRS_XSCREENSAVER -DGL_GLEXT_PROTOTYPES $(INCLUDE)
+LIBRARY = -shared -lGL -lGLU ../../lib/rsHaikuScreenSaver.a ../../lib/rsMath.a ../../lib/Implicit.a ../../lib/rsText.a  ../../lib/Rgbhsl.a # -lm
+CFLAGS = -O3 -DRS_HAIKUSCREENSAVER -DGL_GLEXT_PROTOTYPES $(INCLUDE)
 
 OBJECTS = Hyperspace.o \
 	causticTextures.o \
diff --git a/src/Implicit/Makefile b/src/Implicit/Makefile
index 8659f8b..3ce93f1 100644
--- a/src/Implicit/Makefile
+++ b/src/Implicit/Makefile
@@ -1,5 +1,5 @@
 INCLUDE = -I../../include
-COMPILE = gcc $(INCLUDE) -O3 -DGL_GLEXT_PROTOTYPES -msse -mfpmath=sse
+COMPILE = gcc $(INCLUDE) -shared -O3 -DGL_GLEXT_PROTOTYPES # -msse -mfpmath=sse
 OBJECTS = impCubeTables.o \
 	impCubeVolume.o \
 	impSurface.o \
diff --git a/src/Microcosm/Makefile b/src/Microcosm/Makefile
index ddaade3..2262be1 100644
--- a/src/Microcosm/Makefile
+++ b/src/Microcosm/Makefile
@@ -1,6 +1,6 @@
 INCLUDE = -I. -I../../include
-LIBRARY = -lm -lGL -lGLU ../../lib/rsXScreenSaver.a ../../lib/rsMath.a ../../lib/Implicit.a ../../lib/Rgbhsl.a ../../lib/rsText.a -pthread
-CFLAGS = -O3 -DRS_XSCREENSAVER -DGL_GLEXT_PROTOTYPES $(INCLUDE) -msse -mfpmath=sse
+LIBRARY = -lGL -lGLU ../../lib/rsHaikuScreenSaver.a ../../lib/rsMath.a ../../lib/Implicit.a ../../lib/Rgbhsl.a ../../lib/rsText.a # -pthread -lm 
+CFLAGS = -O3 -shared -DRS_HAIKUSCREENSAVER -DGL_GLEXT_PROTOTYPES $(INCLUDE) # -msse -mfpmath=sse
 
 OBJECTS = Microcosm.o \
 	MirrorBox.o \
diff --git a/src/Microcosm/Microcosm.cpp b/src/Microcosm/Microcosm.cpp
index 3c6b2be..c05584e 100644
--- a/src/Microcosm/Microcosm.cpp
+++ b/src/Microcosm/Microcosm.cpp
@@ -31,6 +31,9 @@
 #ifdef RS_XSCREENSAVER
 #include <rsXScreenSaver/rsXScreenSaver.h>
 #endif
+#ifdef RS_HAIKUSCREENSAVER
+#include <rsHaikuScreenSaver/rsHaikuScreenSaver.h>
+#endif
 
 //#include <iostream>
 #include <vector>
@@ -869,7 +872,7 @@ void setDefaults(int which){
 }
 
 
-#ifdef RS_XSCREENSAVER
+#if defined(RS_XSCREENSAVER) || defined(RS_HAIKUSCREENSAVER)
 void handleCommandLine(int argc, char* argv[]){
 	setDefaults(0);
 	getArgumentsValue(argc, argv, std::string("-stime"), dSingleTime, 0, 300);
@@ -921,7 +924,7 @@ void initSaver(HWND hwnd){
 
 	reshape(rect.right, rect.bottom);
 #endif
-#ifdef RS_XSCREENSAVER
+#if defined(RS_XSCREENSAVER) || defined(RS_HAIKUSCREENSAVER)
 void initSaver(){
 #endif
 	srand((unsigned)time(NULL));
@@ -1132,7 +1135,7 @@ void initSaver(){
 }
 
 
-#ifdef RS_XSCREENSAVER
+#if defined(RS_XSCREENSAVER) || defined(RS_HAIKUSCREENSAVER)
 void cleanUp(){
 	// Free memory
 }
diff --git a/src/Plasma/Makefile b/src/Plasma/Makefile
index 697ece9..18e152c 100644
--- a/src/Plasma/Makefile
+++ b/src/Plasma/Makefile
@@ -1,6 +1,6 @@
 INCLUDE = -I../../include
-LIBRARY = -L../../lib -lm -lGL -lGLU ../../lib/rsXScreenSaver.a ../../lib/rsText.a
-CFLAGS = -O3 -DRS_XSCREENSAVER $(INCLUDE)
+LIBRARY = -shared -L../../lib -lGL -lGLU ../../lib/rsHaikuScreenSaver.a ../../lib/rsText.a
+CFLAGS = -O3 -DRS_HAIKUSCREENSAVER $(INCLUDE)
 
 OBJECTS = Plasma.o
 
diff --git a/src/Plasma/Plasma.cpp b/src/Plasma/Plasma.cpp
index 82444e4..6011e6f 100644
--- a/src/Plasma/Plasma.cpp
+++ b/src/Plasma/Plasma.cpp
@@ -33,6 +33,9 @@
 #ifdef RS_XSCREENSAVER
 #include <rsXScreenSaver/rsXScreenSaver.h>
 #endif
+#ifdef RS_HAIKUSCREENSAVER
+#include <rsHaikuScreenSaver/rsHaikuScreenSaver.h>
+#endif
 
 #include <stdio.h>
 #include <math.h>
@@ -263,7 +266,7 @@ void setDefaults(){
 }
 
 
-#ifdef RS_XSCREENSAVER
+#if defined(RS_XSCREENSAVER) || defined(RS_HAIKUSCREENSAVER)
 void handleCommandLine(int argc, char* argv[]){
 	setDefaults();
 	getArgumentsValue(argc, argv, std::string("-zoom"), dZoom, 1, 100);
@@ -302,7 +305,7 @@ void initSaver(HWND hwnd){
 	glLoadIdentity();
 	gluOrtho2D(0.0f, 1.0f, 0.0f, 1.0f);
 #endif
-#ifdef RS_XSCREENSAVER
+#ifndef WIN32
 void initSaver(){
 #endif
 	int i, j;
@@ -339,7 +342,7 @@ void initSaver(){
 }
 
 
-#ifdef RS_XSCREENSAVER
+#if defined(RS_XSCREENSAVER) || defined(RS_HAIKUSCREENSAVER)
 void cleanUp(){
 	;
 }
diff --git a/src/rsHaikuScreenSaver/Makefile b/src/rsHaikuScreenSaver/Makefile
new file mode 100644
index 0000000..5853683
--- /dev/null
+++ b/src/rsHaikuScreenSaver/Makefile
@@ -0,0 +1,17 @@
+INCLUDE = -I../../include
+COMPILE = gcc $(INCLUDE) -O3
+OBJECTS = rsHaikuScreenSaver.o \
+	../util/arguments.o
+
+
+all: $(OBJECTS)
+	ar cr ../../lib/rsHaikuScreenSaver.a $(OBJECTS)
+
+clean:
+	rm $(OBJECTS)
+
+
+.SUFFIXES: .cpp .o
+
+.cpp.o:
+	$(COMPILE) -o $@ -c $<
diff --git a/src/rsHaikuScreenSaver/rsHaikuScreenSaver.cpp b/src/rsHaikuScreenSaver/rsHaikuScreenSaver.cpp
new file mode 100644
index 0000000..521bfe8
--- /dev/null
+++ b/src/rsHaikuScreenSaver/rsHaikuScreenSaver.cpp
@@ -0,0 +1,173 @@
+/*
+ * Copyright (C) 2014  Puck Meerburg.
+ *
+ * This file is part of rsHaikuScreenSaver.
+ *
+ * rsHaikuScreenSaver is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2 of the License,
+ * or (at your option) any later version.
+ *
+ * rsHaikuScreenSaver is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include <ScreenSaver.h>
+#include <View.h>
+#include <GLView.h>
+#include <stdio.h>
+
+int checkingPassword = 0;
+int isSuspended = 0;
+int doingPreview = 0;
+unsigned int dFrameRateLimit = 0;
+int kStatistics = 0;
+bool useRootWindow = false;
+
+bool quit = false;
+
+// Functions that must be implemented by screensaver
+extern void handleCommandLine(int argc, char* argv[]);
+extern void initSaver();
+extern void cleanUp();
+extern void idleProc();
+extern void reshape(int, int);
+
+class ScreenSaverGLView : public BGLView {
+	public:
+							ScreenSaverGLView(BRect bounds);
+		void				FrameResized(float width, float height);
+		void				EnableDirectMode(bool enabled);
+		void				DrawSaver();
+	private:
+		bool 				mConnected;
+		bool				mFirst;
+};
+
+ScreenSaverGLView::ScreenSaverGLView(BRect bounds) :
+	BGLView(bounds, (const char *)NULL, B_FOLLOW_ALL,
+		B_FRAME_EVENTS | B_WILL_DRAW,
+		BGL_RGB | BGL_ALPHA | BGL_DEPTH | BGL_DOUBLE) {
+	mFirst = true;
+}
+
+
+void
+ScreenSaverGLView::DrawSaver()
+{
+	if (mConnected) {
+		LockGL();
+		if (mFirst) {
+			char *stuff[2] = { "saver", 0 };
+			handleCommandLine(1, stuff);
+			initSaver();
+			reshape((int)Bounds().Width(), (int)Bounds().Height());
+			mFirst = false;
+		}
+		idleProc();
+		SwapBuffers();
+		UnlockGL();	
+	}
+}
+
+
+void
+ScreenSaverGLView::EnableDirectMode(bool enabled)
+{
+	BGLView::EnableDirectMode(enabled);
+	mConnected = enabled;
+}
+
+
+void
+ScreenSaverGLView::FrameResized(float width, float height)
+{
+	if (mConnected) {
+		LockGL();
+		reshape((int)width, (int)height);
+		UnlockGL();
+	}
+}
+
+class rsHaikuScreenSaver : public BScreenSaver
+{
+	public:
+							rsHaikuScreenSaver(BMessage *archive, image_id);
+		void				DirectConnected(direct_buffer_info* info);
+		void				Draw(BView *, int32 frame);
+		void				StopSaver();
+		void				StartConfig(BView *view);
+		status_t			StartSaver(BView *view, bool preview);
+		
+	private:
+		ScreenSaverGLView  *fSaverView;
+};
+
+
+BScreenSaver* instantiate_screen_saver(BMessage *msg, image_id image) 
+{ 
+	return new rsHaikuScreenSaver(msg, image);
+} 
+
+
+rsHaikuScreenSaver::rsHaikuScreenSaver(BMessage *archive, image_id id)
+	:	
+	BScreenSaver(archive, id)
+{
+}
+
+
+void 
+rsHaikuScreenSaver::StartConfig(BView *view) 
+{
+} 
+
+
+status_t 
+rsHaikuScreenSaver::StartSaver(BView *view, bool preview)
+{
+	float viewWidth = view->Bounds().Width();
+	float viewHeight = view->Bounds().Height();
+
+	fSaverView = new ScreenSaverGLView(BRect(0, 0, viewWidth, viewHeight));
+	
+	view->AddChild(fSaverView);
+
+	// Set tick size to 50,000 microseconds = 0.05 seconds
+	SetTickSize(50000);
+	
+	return B_OK;
+}
+
+void
+rsHaikuScreenSaver::Draw(BView *, int32 frame)
+{
+	fSaverView->DrawSaver();
+}
+
+void
+rsHaikuScreenSaver::DirectConnected(direct_buffer_info* info)
+{
+	if (fSaverView) {
+		fSaverView->DirectConnected(info);
+		fSaverView->EnableDirectMode(true);
+	}
+}
+
+void 
+rsHaikuScreenSaver::StopSaver()
+{
+	if (fSaverView)
+		fSaverView->EnableDirectMode(false);
+
+	fSaverView->LockGL();
+	cleanUp();
+	fSaverView->UnlockGL();
+}
+
-- 
1.8.3.4

