Usermode/libaxwin4 - Handle demarshal failure
[tpg/acess2.git] / Usermode / Applications / axwin3_src / libaxwin3.so_src / wm.c
index 412f583..f1de00b 100644 (file)
@@ -54,6 +54,20 @@ tWindow *AxWin3_int_GetWindowFromID(uint32_t ServerID)
        return block->Windows[ServerID];
 }
 
+void AxWin3_int_DelWindowByID(uint32_t ServerID)
+{
+        int    orig_id = ServerID;
+       tWindowBlock    *block = &gAxWin3_WindowList;
+       while(block && ServerID > WINDOWS_PER_ALLOC) {
+               block = block->Next;
+               ServerID -= WINDOWS_PER_ALLOC;
+       }
+       if( !block || !block->Windows[ServerID] )
+               _SysDebug("AxWin3_int_DelWindowByID - Id %i out of range", orig_id);
+       else
+               block->Windows[ServerID] = NULL;
+}
+
 tWindow *AxWin3_int_AllocateWindowInfo(int DataBytes, int *WinID)
 {
         int    idx, newWinID;
@@ -127,7 +141,7 @@ int AxWin3_GetDisplayDims(int Display, int *X, int *Y, int *Width, int *Height)
        ret = (void*)msg->Data;
        
        if(X)   *X = ret->X;
-       if(X)   *X = ret->Y;
+       if(Y)   *Y = ret->Y;
        if(Width)       *Width = ret->W;
        if(Height)      *Height = ret->H;
        
@@ -161,6 +175,8 @@ tHWND AxWin3_CreateWindow(
        AxWin3_int_SendIPCMessage(msg);
        free(msg);
 
+       _SysDebug("AxWin3_CreateWindow: %i :: '%s'", newWinID, Renderer);
+
        // TODO: Detect and handle possible errors
 
        // Return success
@@ -181,6 +197,38 @@ void *AxWin3_int_GetDataPtr(tHWND Window)
        return Window->Data;
 }
 
+int AxWin3_int_DefaultMessageHandler(tWindow *Win, int ID, size_t Len, const void *Data)
+{
+       switch(ID)
+       {
+       case WNDMSG_HOTKEY: {
+               const struct sWndMsg_Hotkey *mi = Data;
+               if( Len < sizeof(*mi) )
+                       return -1;
+
+               if( mi->ID >= MAX_HOTKEYS ) 
+                       _SysDebug("--- Out of range hotkey %i fired", mi->ID);
+               else if( gAxWin3_Hotkeys[mi->ID] == 0 )
+                       _SysDebug("--- Unmapped hotkey ID %i fired", mi->ID);
+               else
+                       gAxWin3_Hotkeys[mi->ID]();
+               }
+               return 1;
+       // Honour a close message by default
+       case WNDMSG_CLOSE:
+               AxWin3_DestroyWindow(Win);
+               return 1;
+       // Zero fucks given?
+       case WNDMSG_DESTROY:
+               _SysDebug("TODO: Check that WNDMSG_DESTROY was from us calling _DestroyWindow");
+               // TODO: Finalise cleanup of window, this will be the last message sent to this window
+               AxWin3_int_DelWindowByID(Win->ServerID);
+               return 1;
+       default:
+               return 0;
+       }
+}
+
 void AxWin3_int_HandleMessage(tAxWin_IPCMessage *Msg)
 {
        tWindow *dest;
@@ -204,24 +252,14 @@ void AxWin3_int_HandleMessage(tAxWin_IPCMessage *Msg)
 
                if( dest->Handler(dest, info->ID, info->Length, info->Data) )
                        ;
-               else {
-                       switch( info->ID )
-                       {
-                       case WNDMSG_HOTKEY: {
-                               const struct sWndMsg_Hotkey *mi = (void*)info->Data;
-                               if( mi->ID >= MAX_HOTKEYS ) 
-                                       ;       // TODO: Error when hotkey is out of range
-                               else if( gAxWin3_Hotkeys[mi->ID] == 0 )
-                                       _SysDebug("--- Unmapped hotkey ID %i fired", mi->ID);
-                               else
-                                       gAxWin3_Hotkeys[mi->ID]();
-                               }break;
-                       default:
-                               _SysDebug("--- Unhandled SENDMSG %i", info->ID);
-                               break;
-                       }
-               }
+               else if( AxWin3_int_DefaultMessageHandler(dest, info->ID, info->Length, info->Data) )
+                       ;
+               else
+                       _SysDebug("--- Unhandled SENDMSG 0x%x win %i", info->ID, Msg->Window);
                break; }
+       case IPCMSG_DESTROYWIN:
+               // Clean up resources associated with this window
+               break;
        default:
                _SysDebug("Unknow message ID %i", Msg->ID);
                break;
@@ -285,6 +323,34 @@ void *AxWin3_WaitMessage(tHWND Window, int MessageID, size_t *Length)
        }
 }
 
+void AxWin3_SendIPC(tHWND Window, int Message, size_t Length, const void *Data)
+{
+       tAxWin_IPCMessage *msg = AxWin3_int_AllocateIPCMessage(Window, Message, IPCMSG_FLAG_RENDERER, Length);
+       memcpy(msg->Data, Data, Length);
+       AxWin3_int_SendIPCMessage(msg);
+       free(msg);
+}
+
+void *AxWin3_WaitIPCMessage(tHWND Window, int MessageID, size_t *Length)
+{
+       tAxWin_IPCMessage       *msg;
+       for( ;; )
+       {
+               msg = AxWin3_int_WaitIPCMessage(MessageID);
+               if( !(msg->Flags & IPCMSG_FLAG_RENDERER) || msg->Window != AxWin3_int_GetWindowID(Window) ) {
+                       AxWin3_int_HandleMessage(msg);
+                       continue ;
+               }
+
+               *Length = msg->Size;
+               void    *ret = malloc(msg->Size);
+               memcpy(ret, msg->Data, msg->Size);
+               free(msg);
+       
+               return ret;
+       }
+}
+
 void AxWin3_FocusWindow(tHWND Window)
 {
        tAxWin_IPCMessage       *msg;

UCC git Repository :: git.ucc.asn.au