namespace gui;
-#if (defined(__unix__) || defined(__APPLE__)) && !defined(__ANDROID__)
+#ifdef __EMSCRIPTEN__
+#include <emscripten.h>
+#endif
+
+#ifdef __EMSCRIPTEN__
+#include <emscripten.h>
+#endif
+
+#if (defined(__unix__) || defined(__APPLE__)) && !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
#define property _property
#define new _new
#define class _class
#if !defined(ECERE_VANILLA) && !defined(ECERE_NONET)
#if defined(__WIN32__)
-
+#define SOCKLEN_TYPE int
#define WIN32_LEAN_AND_MEAN
+#define String _String
#include <winsock.h>
-static WSADATA wsaData;
+#undef String
#elif defined(__unix__) || defined(__APPLE__)
default:
+#define SOCKLEN_TYPE socklen_t
#define uint _uint
#define set _set
#include <sys/time.h>
#include <unistd.h>
+#include <sys/select.h>
#include <netinet/in.h>
#include <netdb.h>
import "network"
#endif
-#if defined(__APPLE__)
+#if defined(__APPLE__) && !defined(ECERE_VANILLA)
import "CocoaInterface"
-#elif defined(__unix__) && !defined(__ANDROID__)
+#endif
+
+#if (defined(__unix__) || defined(__APPLE__)) && !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
import "XInterface"
#endif
import "Window"
+import "List"
/*static */bool guiApplicationInitialized = false;
GuiApplication guiApp;
int terminateX;
-enum GuiErrorCode : ErrorCode
-{
- driverNotSupported = ErrorCode { VeryFatal, 1 },
- windowCreationFailed = ErrorCode { VeryFatal, 2 },
- graphicsLoadingFailed = ErrorCode { VeryFatal, 3 },
- modeSwitchFailed = ErrorCode { VeryFatal, 4 }
-};
-
-static Array<String> errorMessages
-{ [
- $"No error",
- $"Graphics driver not supported by any user interface system",
- $"Window creation failed",
- $"Window graphics loading failed",
- $"Driver/Mode switch failed"
-] };
-
public class GuiApplication : Application
{
int numDrivers;
PixelFormat pixelFormat;
int refreshRate;
- char * defaultDisplayDriver;
+ const char * defaultDisplayDriver;
Cursor systemCursors[SystemCursor];
OldList windowTimers;
// Mouse events
- Window prevWindow; // Used for OnMouseLeave
+ Window prevWindow; // Used for OnMouseLeave
+ List<Window> overWindows { }; // Used for OnMouseLeave
Window windowCaptured;
// Mouse based moving & resizing
bool processAll;
+#if !defined(__EMSCRIPTEN__)
Mutex waitMutex {};
+#endif
bool waiting;
+#if !defined(__EMSCRIPTEN__)
Mutex lockMutex {};
+#endif
Window interimWindow;
bool caretEnabled;
char appName[1024];
uint timerResolution;
- Size virtualScreen;
+ Size virtualScreen;
Point virtualScreenPos;
- int mainThread;
+ int64 mainThread;
GuiApplication()
{
SystemCursor c;
-
+
+#if !defined(__EMSCRIPTEN__)
mainThread = GetCurrentThreadID();
+#endif
if(!guiApp)
guiApp = this;
// TODO:
// customCursors.offset = OFFSET(Cursor, prev);
- windowTimers.offset = (uint)&((Timer)0).prev;
+ windowTimers.offset = (uint)(uintptr)&((Timer)0).prev;
for(c = 0; c<SystemCursor::enumSize; c++)
systemCursors[c] = Cursor { systemCursor = c; };
+#if !defined(__EMSCRIPTEN__)
globalSystem.eventSemaphore = Semaphore { };
globalSystem.fileMonitorMutex = Mutex { };
- globalSystem.fileMonitors.offset = (uint)&((FileMonitor)0).prev;
+ globalSystem.fileMonitors.offset = (uint)(uintptr)&((FileMonitor)0).prev;
+#endif
return true;
}
delete desktop;
customCursors.Clear();
-#if defined(__unix__) && !defined(__ANDROID__)
+#if (defined(__unix__) || defined(__APPLE__)) && !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
if(xGlobalDisplay)
XUnlockDisplay(xGlobalDisplay);
#endif
-#if !defined(__ANDROID__)
+#if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
// Because destruction of app won't be from main thread
- lockMutex.Release();
+ if(guiApplicationInitialized)
+ lockMutex.Release();
#endif
if(interfaceDriver)
Network_Terminate();
#endif
+#if !defined(__EMSCRIPTEN__)
delete globalSystem.eventSemaphore;
delete globalSystem.fileMonitorMutex;
delete globalSystem.fileMonitorThread;
+#endif
UnapplySkin(class(Window));
}
// --- Mouse-based window movement ---
- void SetCurrentCursor(Cursor cursor)
+ void SetCurrentCursor(Window window, Cursor cursor)
{
currentCursor = cursor;
if(cursor)
{
if(fullScreenMode && cursor.bitmap)
- interfaceDriver.SetMouseCursor((SystemCursor)-1);
+ interfaceDriver.SetMouseCursor(window ? window : desktop, (SystemCursor)-1);
else
{
- interfaceDriver.SetMouseCursor(cursor.systemCursor);
+ interfaceDriver.SetMouseCursor(window ? window : desktop, cursor.systemCursor);
cursorBackground.Free();
}
}
bool IsModeSwitching()
{
return modeSwitching;
- }
+ }
public bool SetDesktopPosition(int x, int y, int w, int h, bool moveChildren)
{
// Maximized native decorations windows suffer when we drag the dock around, so remaximize them
// It's a little jumpy, but oh well.
+
+ // Made this Windows only as it was causing occasional wrong stacking of windows in X11/Cinnamon
+ // when switching debugged app from full-screen
+
for(child = desktop.children.first; child; child = child.next)
{
if(child.nativeDecorations && child.rootWindow == child && child.state == maximized)
{
+#if defined(__WIN32__)
child.state = normal;
child.state = maximized;
+#else
+ if(child.active)
+ {
+ child.state = normal;
+ child.state = maximized;
+ }
+ else
+ child.requireRemaximize = true;
+#endif
}
}
/*for(child = desktop.children.first; child; child = child.next)
{
child.x = desktop.x;
child.y = desktop.y;
- child.ComputeAnchors(,
- A_LEFT,A_LEFT,A_OFFSET,A_OFFSET,
+ child.ComputeAnchors(,
+ A_LEFT,A_LEFT,A_OFFSET,A_OFFSET,
&x, &y, &w, &h);
child.Position(, x, y, w, h, false, true, true, true, false);
- }
+ }
}
}*/
if(desktop.display)
}
void SetAppFocus(bool state)
- {
+ {
// Shouldn't be property here
desktop.active = state;
}
- bool SelectSkin(char * skinName)
+ bool SelectSkin(const char * skinName)
{
bool result = false;
subclass(Skin) skin;
OldLink link;
-
+
for(link = class(Skin).derivatives.first; link; link = link.next)
{
skin = link.data;
break;
}
if(!link) skin = null;
-
+
if(skin)
{
if(skin != currentSkin || !currentSkin)
}
UnapplySkin(class(Window));
-
+
currentSkin = skin;
ApplySkin(class(Window), skin.name, null);
// if(!initialized && eClass_IsDerived(__ecereModule->app->module.inst.class, guiApplicationClass))
if(!guiApplicationInitialized)
{
- char * defaultDriver = null;
+ const char * defaultDriver = null;
#if defined(ECERE_VANILLA) || defined(ECERE_ONEDRIVER)
char * driver = null;
#else
errorLevel = 2;
+#if !defined(__EMSCRIPTEN__)
lockMutex.Wait();
-#if defined(__unix__) && !defined(__ANDROID__)
+#endif
+/*#if (defined(__unix__) || defined(__APPLE__)) && !defined(__ANDROID__)
if(xGlobalDisplay)
XLockDisplay(xGlobalDisplay);
-#endif
+#endif*/
// Setup Desktop
if(!desktop)
desktop.caption = appName;
*&desktop.visible = true;
desktop.position = Point { };
+#if !defined(__EMSCRIPTEN__)
desktop.mutex = Mutex { };
+#endif
desktop.created = true;
}
{
if(driver)
defaultDriver = driver;
- else if(this.isGUIApp && !textMode)
+ else if((this.isGUIApp & 1) && !textMode)
defaultDriver = "GDI";
else
defaultDriver = "Win32Console";
}
#elif defined(__APPLE__)
{
- if (driver) {
+ if (driver)
defaultDriver = driver;
- } else {
- defaultDriver = "CocoaOpenGL";
- }
+ else
+ defaultDriver = "X"; //"CocoaOpenGL";
}
#elif defined(__ANDROID__)
{
else
defaultDriver = "OpenGL";
}
+ #elif defined(__EMSCRIPTEN__)
+ {
+ if(driver)
+ defaultDriver = driver;
+ else
+ defaultDriver = "OpenGL";
+ }
#else
- if(this.isGUIApp && !textMode)
+ if((this.isGUIApp & 1) && !textMode)
{
char * display = getenv("DISPLAY");
#endif
#if defined(__APPLE__)
- SwitchMode(true, "CocoaOpenGL", 0, 0, 0, null, true);
+ // SwitchMode(true, "X" /*"CocoaOpenGL"*/, 0, 0, 0, null, true);
#endif
#if defined(__unix__)
#endif
#endif
}
- if(!interfaceDriver)
- guiApplicationInitialized = false;
+ /*if(!interfaceDriver)
+ guiApplicationInitialized = false;*/
}
else
defaultDisplayDriver = defaultDriver;
}
}
+#ifdef __EMSCRIPTEN__
+ emscripten_set_main_loop(emscripten_main_loop_callback, 1/*60*/, 1);
+#endif
+
if(desktop)
{
int terminated = 0;
incref desktop;
+ ProcessInput(true);
while(desktop && interfaceDriver)
{
bool wait;
break;
if(!child) break;
+#if !defined(__EMSCRIPTEN__)
for(window = desktop.children.first; window; window = window.next)
if(window.mutex) window.mutex.Wait();
+#endif
UpdateDisplay();
+#if !defined(__EMSCRIPTEN__)
for(window = desktop.children.first; window; window = window.next)
if(window.mutex) window.mutex.Release();
+#endif
wait = !ProcessInput(true);
-#ifdef _DEBUG
+#if !defined(__EMSCRIPTEN__)
+#if defined(_DEBUG) && !defined(MEMINFO)
if(lockMutex.owningThread != GetCurrentThreadID())
PrintLn("WARNING: ProcessInput returned unlocked GUI!");
#endif
+#endif
if(!Cycle(wait))
wait = false;
- if(wait)
+ if(wait)
Wait();
else
{
-#if defined(__unix__) && !defined(__ANDROID__)
+#if (defined(__unix__) || defined(__APPLE__)) && !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
if(xGlobalDisplay)
XUnlockDisplay(xGlobalDisplay);
#endif
+#if !defined(__EMSCRIPTEN__)
lockMutex.Release();
lockMutex.Wait();
+#endif
-#if defined(__unix__) && !defined(__ANDROID__)
+#if (defined(__unix__) || defined(__APPLE__)) && !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
if(xGlobalDisplay)
XLockDisplay(xGlobalDisplay);
#endif
void Wait(void)
{
-#if defined(__unix__) && !defined(__ANDROID__)
+#if (defined(__unix__) || defined(__APPLE__)) && !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
if(xGlobalDisplay)
XUnlockDisplay(xGlobalDisplay);
#endif
+#if !defined(__EMSCRIPTEN__)
lockMutex.Release();
waitMutex.Wait();
+#endif
waiting = true;
if(interfaceDriver)
interfaceDriver.Wait();
waiting = false;
+#if !defined(__EMSCRIPTEN__)
waitMutex.Release();
lockMutex.Wait();
+#endif
-#if defined(__unix__) && !defined(__ANDROID__)
+#if (defined(__unix__) || defined(__APPLE__)) && !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
if(xGlobalDisplay)
XLockDisplay(xGlobalDisplay);
#endif
result |= UpdateTimers();
result |= ProcessFileNotifications();
*/
-
+
result |= ProcessFileNotifications();
result |= UpdateTimers();
result |= interfaceDriver.ProcessInput(useProcessAll && processAll);
#if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D)
if(Desktop3DUpdateDisplay()) return;
#endif
-
+
if(interfaceDriver)
{
if(fullScreenMode && desktop.display)
{
+#if !defined(__EMSCRIPTEN__)
desktop.mutex.Wait();
+#endif
if(desktop.active)
{
desktop.display.Lock(true);
desktop.display.Unlock();
}
+#if !defined(__EMSCRIPTEN__)
desktop.mutex.Release();
+#endif
}
else
{
for(window = desktop.children.first; window; window = window.next)
{
+#if !defined(__EMSCRIPTEN__)
if(window.mutex) window.mutex.Wait();
+#endif
if(window.visible && window.dirty)
{
// Logf("Updating %s\n", window.name);
usleep(1000000);
*/
}
+#if !defined(__EMSCRIPTEN__)
if(window.mutex) window.mutex.Release();
+#endif
}
}
}
void WaitEvent(void)
{
+#if !defined(__EMSCRIPTEN__)
globalSystem.eventSemaphore.Wait();
+#endif
}
#if !defined(ECERE_VANILLA) && !defined(ECERE_NONET)
PauseNetworkEvents();
network.mutex.Wait();
-
+
#ifdef DEBUG_SOCKETS
if(network.connectEvent || network.networkEvent)
Log("[P] [NProcess]\n");
{
SOCKET s;
SOCKADDR_IN a;
- int addrLen = sizeof(a);
+ SOCKLEN_TYPE addrLen = sizeof(a);
s = accept(service.s,(SOCKADDR *)&a,&addrLen);
closesocket(s);
}
for(socket = network.connectSockets.first; socket; socket = next)
{
next = socket.next;
- if(socket._connected && socket._connected != -2)
+ if(socket._connected && socket._connected != -2)
{
network.connectSockets.Remove(socket);
delete socket.connectThread;
if(socket.s == network.ns - 1)
Network_DetermineMaxSocket();
+ socket._connected = 0;
socket.Free(false);
delete socket;
}
FD_SET(socket.s, &network.readSet);
FD_SET(socket.s, &network.exceptSet);
network.mutex.Release();
-
+
// printf("Calling OnConnect on %s\n", socket._class.name);
socket.OnConnect();
network.mutex.Wait();
- network.sockets.Add(socket);
+ if(socket._connected)
+ network.sockets.Add(socket);
}
gotEvent |= true;
goOn = true;
void SignalEvent(void)
{
+#if !defined(__EMSCRIPTEN__)
globalSystem.eventSemaphore.Release();
+#endif
}
// TODO: Might want to make this private with simpler public version?
- bool SwitchMode(bool fullScreen, char * driverName, Resolution resolution, PixelFormat colorDepth, int refreshRate, char * skinName, bool fallBack)
+ bool SwitchMode(bool fullScreen, const char * driverName, Resolution resolution, PixelFormat colorDepth, int refreshRate, const char * skinName, bool fallBack)
{
bool result = false;
OldLink link;
- char * fbDriver;
+ const char * fbDriver;
bool fbFullScreen = 0;
Resolution fbResolution = 0;
PixelFormat fbColorDepth = 0;
if(skinName)
{
OldLink link;
-
+
for(link = class(Skin).derivatives.first; link; link = link.next)
{
skin = link.data;
{
bool foundDriver = false;
int c, numDrivers;
- char ** graphicsDrivers;
+ const char ** graphicsDrivers;
inter = link.data;
graphicsDrivers = inter.GraphicsDrivers(&numDrivers);
break;
}
if(!link)
- inter = null;
+ inter = null;
}
-
+
/*
if(driverName)
- {
+ {
#if defined(__WIN32__)
#if !defined(ECERE_VANILLA)
if(!strcmp(driverName, "Win32Console")) inter = (subclass(Interface))class(Win32ConsoleInterface); else
}
*/
- if(interfaceDriver && (!driverName || (fbDriver && !strcmp(fbDriver, driverName))) &&
+ if(interfaceDriver && (!driverName || (fbDriver && !strcmp(fbDriver, driverName))) &&
fullScreen == fbFullScreen &&
(!resolution || resolution == fbResolution) &&
(!colorDepth || colorDepth == fbColorDepth) &&
{
if(!fbDriver || (driverName && strcmp(fbDriver, driverName)))
defaultDisplayDriver = driverName;
-
+
if(!skinName || !SelectSkin(skinName))
{
if(!currentSkin || currentSkin.textMode != textMode ||
{
OldLink link;
subclass(Skin) skin = null;
-
+
for(link = class(Skin).derivatives.first; link; link = link.next)
{
skin = link.data;
if(currentSkin && desktop.SetupDisplay())
{
desktop.active = true;
-
+
if(fullScreen)
{
desktop.display.Lock(false);
bool ProcessFileNotifications()
{
+#if !defined(__EMSCRIPTEN__)
bool activity = false;
FileMonitor monitor, next;
static int reentrant = 0;
-
+
// Reentrant FileNotification is asking for trouble since each monitor is spawning a Modal() MessageBox
if(reentrant) return false;
- // printf("[%d] Waiting in ProcessFileNotifications for fileMonitor Mutex %x...\n", GetCurrentThreadID(), globalSystem.fileMonitorMutex);
+ // printf("[%d] Waiting in ProcessFileNotifications for fileMonitor Mutex %x...\n", (int)GetCurrentThreadID(), globalSystem.fileMonitorMutex);
globalSystem.fileMonitorMutex.Wait();
reentrant++;
for(monitor = globalSystem.fileMonitors.first; monitor; monitor = next)
incref monitor;
if(next)
incref next;
-
+
if(!monitor.reentrant && !monitor.toBeFreed)
{
monitor.reentrant = true;
monitor.FreeMonitor();
}
}
- // printf("[%d] Releasing in ProcessFileNotifications fileMonitor Mutex %x...\n", GetCurrentThreadID(), globalSystem.fileMonitorMutex);
+ // printf("[%d] Releasing in ProcessFileNotifications fileMonitor Mutex %x...\n", (int)GetCurrentThreadID(), globalSystem.fileMonitorMutex);
globalSystem.fileMonitorMutex.Release();
return activity;
+#else
+ return false;
+#endif
}
void Lock(void)
{
+#if !defined(__EMSCRIPTEN__)
lockMutex.Wait();
-#if defined(__unix__) && !defined(__ANDROID__)
+#if (defined(__unix__) || defined(__APPLE__)) && !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
if(xGlobalDisplay)
XLockDisplay(xGlobalDisplay);
#endif
+#endif
}
void Unlock(void)
{
-#if defined(__unix__) && !defined(__ANDROID__)
+#if !defined(__EMSCRIPTEN__)
+#if (defined(__unix__) || defined(__APPLE__)) && !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
if(xGlobalDisplay)
XUnlockDisplay(xGlobalDisplay);
#endif
lockMutex.Release();
+#endif
}
Cursor GetCursor(SystemCursor cursor)
{
return interfaceDriver.GetMouseState(buttons, x, y);
}
-
+
// Properties
- property char * appName
+ property const char * appName
{
set
{
}
get
{
- return (char *)(this ? appName : null);
+ return (const char *)(this ? appName : null);
}
};
+#if !defined(__EMSCRIPTEN__)
property Semaphore semaphore { get { return globalSystem.eventSemaphore; } };
+#endif
property bool alwaysEmptyInput{ set { processAll = value; } get { return processAll; } };
property bool fullScreen
{
set
{
- SwitchMode(value, defaultDisplayDriver, resolution,
+ SwitchMode(value, defaultDisplayDriver, resolution,
pixelFormat, refreshRate, currentSkin ? currentSkin.name : null, true);
}
get { return this ? fullScreen : false; }
};
- property char * driver
+ property const char * driver
{
set
{
- SwitchMode( fullScreen, value, resolution, pixelFormat, refreshRate,
+ SwitchMode( fullScreen, value, resolution, pixelFormat, refreshRate,
currentSkin ? currentSkin.name : null, true);
- }
+ }
get { return this ? defaultDisplayDriver : null; }
};
property Resolution resolution
{
set
{
- SwitchMode(fullScreen, defaultDisplayDriver, value, pixelFormat, refreshRate,
+ SwitchMode(fullScreen, defaultDisplayDriver, value, pixelFormat, refreshRate,
currentSkin ? currentSkin.name : null, true);
}
get { return this ? resolution : 0; }
{
set
{
- SwitchMode(fullScreen, defaultDisplayDriver, resolution,
+ SwitchMode(fullScreen, defaultDisplayDriver, resolution,
pixelFormat, refreshRate, currentSkin ? currentSkin.name : null, true);
}
get { return this ? pixelFormat : 0; }
{
set
{
- SwitchMode(fullScreen, defaultDisplayDriver, resolution,
+ SwitchMode(fullScreen, defaultDisplayDriver, resolution,
pixelFormat, refreshRate, currentSkin ? currentSkin.name : null, true);
}
get { return this ? refreshRate : 0; }
};
- property char * skin
+ property const char * skin
{
set { SelectSkin(value); }
get { return (this && currentSkin) ? currentSkin.name : null; }
get { return this ? textMode : false; }
};
property Window desktop { get { return this ? desktop : null; } };
- property char ** drivers { get { return null; } };
- property char ** skins { get { return null; } };
+ property const char ** drivers { get { return null; } };
+ property const char * const * skins { get { return null; } };
property subclass(Skin) currentSkin { get { return this ? currentSkin : null; } };
property int numDrivers { get { return 0; } };
property int numSkins { get { return 0; } };
property uint timerResolution
{
- set { timerResolution = value; if(interfaceDriver) interfaceDriver.SetTimerResolution(value); }
+ set { timerResolution = value; if(interfaceDriver) interfaceDriver.SetTimerResolution(value); }
};
};
+
+#ifdef __EMSCRIPTEN__
+private void emscripten_main_loop_callback()
+{
+ guiApp.ProcessInput(false);
+ guiApp.Cycle(false);
+ guiApp.UpdateDisplay();
+}
+#endif