- Move protocol handling events to main thread through qt's main loop qmlfrontend
authorunc0rr
Thu, 21 May 2015 00:19:06 +0300
branchqmlfrontend
changeset 10951 89a7f617e091
parent 10949 6a1f5f452460
child 10953 360e57620df3
- Move protocol handling events to main thread through qt's main loop - Reorganize code a bit
hedgewars/CMakeLists.txt
hedgewars/hwLibrary.pas
hedgewars/uFLGameConfig.pas
hedgewars/uFLNet.pas
hedgewars/uFLNetProtocol.pas
hedgewars/uFLTypes.pas
hedgewars/uFLUICallback.pas
qmlFrontend/flib.h
qmlFrontend/hwengine.cpp
--- a/hedgewars/CMakeLists.txt	Mon May 18 00:20:09 2015 +0300
+++ b/hedgewars/CMakeLists.txt	Thu May 21 00:19:06 2015 +0300
@@ -109,10 +109,12 @@
     uFLGameConfig.pas
     uFLIPC.pas
     uFLNet.pas
+    uFLNetProtocol.pas
     uFLScripts.pas
     uFLSchemes.pas
     uFLTeams.pas
     uFLTypes.pas
+    uFLUICallback.pas
     uFLUtils.pas
 
     #these interact with everything, so compile last
--- a/hedgewars/hwLibrary.pas	Mon May 18 00:20:09 2015 +0300
+++ b/hedgewars/hwLibrary.pas	Thu May 21 00:19:06 2015 +0300
@@ -48,6 +48,8 @@
     , uFLSchemes
     , uFLAmmo
     , uFLNet
+    , uFLNetProtocol
+    , uFLUICallback
     ;
 
 {$INCLUDE "config.inc"}
@@ -154,7 +156,7 @@
     runQuickGame,
     runLocalGame,
     getPreview,
-    registerGUIMessagesCallback,
+    registerUIMessagesCallback,
     flibInit,
     flibFree,
     //game config
@@ -177,6 +179,7 @@
     changeTeamColor,
     // network
     connectOfficialServer,
+    passNetData,
 
     // dunno what these are
     RunEngine,
--- a/hedgewars/uFLGameConfig.pas	Mon May 18 00:20:09 2015 +0300
+++ b/hedgewars/uFLGameConfig.pas	Thu May 21 00:19:06 2015 +0300
@@ -7,8 +7,6 @@
 procedure runLocalGame; cdecl;
 procedure getPreview; cdecl;
 
-procedure registerGUIMessagesCallback(p: pointer; f: TGUICallback); cdecl;
-
 procedure setSeed(seed: PChar); cdecl;
 function  getSeed: PChar; cdecl;
 procedure setTheme(themeName: PChar); cdecl;
@@ -21,10 +19,7 @@
 procedure changeTeamColor(teamName: PChar; dir: LongInt); cdecl;
 
 implementation
-uses uFLIPC, hwengine, uFLUtils, uFLTeams, uFLData, uFLSChemes, uFLAmmo;
-
-var guiCallbackPointer: pointer;
-    guiCallbackFunction: TGUICallback;
+uses uFLIPC, hwengine, uFLUtils, uFLTeams, uFLData, uFLSChemes, uFLAmmo, uFLUICallback;
 
 const
     MAXCONFIGS = 5;
@@ -200,21 +195,6 @@
     end;
 end;
 
-
-procedure engineMessageCallback(p: pointer; msg: PChar; len: Longword);
-begin
-    if len = 128 * 256 then guiCallbackFunction(guiCallbackPointer, mtPreview, msg, len)
-end;
-
-procedure registerGUIMessagesCallback(p: pointer; f: TGUICallback); cdecl;
-begin
-    guiCallbackPointer:= p;
-    guiCallbackFunction:= f;
-
-    registerIPCCallback(nil, @engineMessageCallback)
-end;
-
-
 procedure tryAddTeam(teamName: PChar); cdecl;
 var msg: ansistring;
     i, hn, hedgehogsNumber: Longword;
@@ -249,13 +229,13 @@
         teams[i].color:= c;
 
         msg:= '0' + #10 + teamName;
-        guiCallbackFunction(guiCallbackPointer, mtAddPlayingTeam, @msg[1], length(msg));
+        sendUI(mtAddPlayingTeam, @msg[1], length(msg));
 
         msg:= teamName + #10 + colorsSet[teams[i].color];
-        guiCallbackFunction(guiCallbackPointer, mtTeamColor, @msg[1], length(msg));
+        sendUI(mtTeamColor, @msg[1], length(msg));
 
         msg:= teamName;
-        guiCallbackFunction(guiCallbackPointer, mtRemoveTeam, @msg[1], length(msg))
+        sendUI(mtRemoveTeam, @msg[1], length(msg))
     end
 end;
 
@@ -286,8 +266,8 @@
 
     msg:= teamName;
 
-    guiCallbackFunction(guiCallbackPointer, mtRemovePlayingTeam, @msg[1], length(msg));
-    guiCallbackFunction(guiCallbackPointer, mtAddTeam, @msg[1], length(msg))
+    sendUI(mtRemovePlayingTeam, @msg[1], length(msg));
+    sendUI(mtAddTeam, @msg[1], length(msg))
 end;
 
 
@@ -309,7 +289,7 @@
         teams[i].color:= (teams[i].color + dc) mod 9;
 
         msg:= tn + #10 + colorsSet[teams[i].color];
-        guiCallbackFunction(guiCallbackPointer, mtTeamColor, @msg[1], length(msg))
+        sendUI(mtTeamColor, @msg[1], length(msg))
     end
 end;
 
--- a/hedgewars/uFLNet.pas	Mon May 18 00:20:09 2015 +0300
+++ b/hedgewars/uFLNet.pas	Thu May 21 00:19:06 2015 +0300
@@ -7,7 +7,7 @@
 procedure freeModule;
 
 implementation
-uses SDLh, uFLIPC;
+uses SDLh, uFLIPC, uFLTypes, uFLUICallback;
 
 const endCmd: string = #10 + #10;
 
@@ -57,6 +57,7 @@
 
 procedure handler_CONNECTED;
 begin
+    sendUI(mtNetData, nil, 0);
 end;
 
 procedure handler_EM;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hedgewars/uFLNetProtocol.pas	Thu May 21 00:19:06 2015 +0300
@@ -0,0 +1,13 @@
+unit uFLNetProtocol;
+interface
+
+procedure passNetData(p: pointer); cdecl;
+
+implementation
+
+procedure passNetData(p: pointer); cdecl;
+begin
+    writeln('meow')
+end;
+
+end.
--- a/hedgewars/uFLTypes.pas	Mon May 18 00:20:09 2015 +0300
+++ b/hedgewars/uFLTypes.pas	Thu May 21 00:19:06 2015 +0300
@@ -3,7 +3,7 @@
 
 type
     TMessageType = (mtPreview, mtAddPlayingTeam, mtRemovePlayingTeam, mtAddTeam, mtRemoveTeam
-                   , mtTeamColor);
+                   , mtTeamColor, mtNetData);
 
     TIPCMessage = record
                    str: shortstring;
@@ -12,7 +12,7 @@
                end;
 
     TIPCCallback = procedure (p: pointer; msg: PChar; len: Longword);
-    TGUICallback = procedure (p: pointer; msgType: TMessageType; msg: PChar; len: Longword); cdecl;
+    TUICallback = procedure (p: pointer; msgType: TMessageType; msg: PChar; len: Longword); cdecl;
 
     TGameType = (gtPreview, gtLocal);
     THedgehog = record
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hedgewars/uFLUICallback.pas	Thu May 21 00:19:06 2015 +0300
@@ -0,0 +1,32 @@
+unit uFLUICallback;
+interface
+uses uFLTypes;
+
+procedure registerUIMessagesCallback(p: pointer; f: TUICallback); cdecl;
+procedure sendUI(msgType: TMessageType; msg: PChar; len: Longword);
+
+implementation
+uses uFLIPC;
+
+var uiCallbackPointer: pointer;
+    uiCallbackFunction: TUICallback;
+
+procedure engineMessageCallback(p: pointer; msg: PChar; len: Longword);
+begin
+    if len = 128 * 256 then uiCallbackFunction(uiCallbackPointer, mtPreview, msg, len)
+end;
+
+procedure registerUIMessagesCallback(p: pointer; f: TUICallback); cdecl;
+begin
+    uiCallbackPointer:= p;
+    uiCallbackFunction:= f;
+
+    registerIPCCallback(nil, @engineMessageCallback)
+end;
+
+procedure sendUI(msgType: TMessageType; msg: PChar; len: Longword);
+begin
+    uiCallbackFunction(uiCallbackPointer, msgType, msg, len)
+end;
+
+end.
--- a/qmlFrontend/flib.h	Mon May 18 00:20:09 2015 +0300
+++ b/qmlFrontend/flib.h	Thu May 21 00:19:06 2015 +0300
@@ -14,6 +14,7 @@
     , MSG_ADDTEAM
     , MSG_REMOVETEAM
     , MSG_TEAMCOLOR
+    , MSG_NETDATA
 };
 
 typedef union string255_
@@ -28,7 +29,7 @@
     } string255;
 
 typedef void RunEngine_t(int argc, const char ** argv);
-typedef void registerGUIMessagesCallback_t(void * context, void (*)(void * context, MessageType mt, const char * msg, uint32_t len));
+typedef void registerUIMessagesCallback_t(void * context, void (*)(void * context, MessageType mt, const char * msg, uint32_t len));
 typedef void getPreview_t();
 typedef void runQuickGame_t();
 typedef void runLocalGame_t();
@@ -41,6 +42,7 @@
 typedef void setAmmo_t(const char * ammoName);
 typedef void flibInit_t(const char * localPrefix, const char * userPrefix);
 typedef void flibFree_t();
+typedef void passNetData_t(const char * data);
 
 typedef char **getThemesList_t();
 typedef void freeThemesList_t(char **list);
--- a/qmlFrontend/hwengine.cpp	Mon May 18 00:20:09 2015 +0300
+++ b/qmlFrontend/hwengine.cpp	Thu May 21 00:19:06 2015 +0300
@@ -10,7 +10,7 @@
 
 extern "C" {
     RunEngine_t *flibRunEngine;
-    registerGUIMessagesCallback_t *flibRegisterGUIMessagesCallback;
+    registerUIMessagesCallback_t *flibRegisterUIMessagesCallback;
     setSeed_t *flibSetSeed;
     getSeed_t *flibGetSeed;
     setTheme_t *flibSetTheme;
@@ -35,6 +35,7 @@
     changeTeamColor_t * flibChangeTeamColor;
 
     connectOfficialServer_t * flibConnectOfficialServer;
+    passNetData_t * flibPassNetData;
 }
 
 Q_DECLARE_METATYPE(MessageType);
@@ -51,7 +52,7 @@
         qWarning() << "Engine library not found" << hwlib.errorString();
 
     flibRunEngine = (RunEngine_t*) hwlib.resolve("RunEngine");
-    flibRegisterGUIMessagesCallback = (registerGUIMessagesCallback_t*) hwlib.resolve("registerGUIMessagesCallback");
+    flibRegisterUIMessagesCallback = (registerUIMessagesCallback_t*) hwlib.resolve("registerUIMessagesCallback");
     flibGetSeed = (getSeed_t*) hwlib.resolve("getSeed");
     flibGetPreview = (getPreview_t*) hwlib.resolve("getPreview");
     flibRunQuickGame = (runQuickGame_t*) hwlib.resolve("runQuickGame");
@@ -80,9 +81,10 @@
     flibChangeTeamColor = (changeTeamColor_t*) hwlib.resolve("changeTeamColor");
 
     flibConnectOfficialServer = (connectOfficialServer_t*) hwlib.resolve("connectOfficialServer");
+    flibPassNetData = (passNetData_t*) hwlib.resolve("passNetData");
 
     flibInit("/usr/home/unC0Rr/Sources/Hedgewars/Hedgewars-GC/share/hedgewars/Data", "/usr/home/unC0Rr/.hedgewars");
-    flibRegisterGUIMessagesCallback(this, &guiMessagesCallback);
+    flibRegisterUIMessagesCallback(this, &guiMessagesCallback);
 
     ThemeIconProvider * themeIcon = (ThemeIconProvider *)m_engine->imageProvider(QLatin1String("theme"));
     themeIcon->setFileContentsFunction(flibGetThemeIcon);
@@ -170,6 +172,9 @@
         emit teamColorChanged(l[0], QColor::fromRgba(l[1].toInt()).name());
         break;
     }
+    case MSG_NETDATA: {
+        flibPassNetData(msg.constData());
+    }
     }
 }