From: John Hodge Date: Sun, 5 Jun 2011 11:24:57 +0000 (+0800) Subject: AxWin2 - Cleaned out messy code X-Git-Tag: rel0.10~80 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=b43ed8d15c86ff9b7f5abc9f47e1385503dcdf35;p=tpg%2Facess2.git AxWin2 - Cleaned out messy code - Changed to tIPC_Type instead of passing function pointers around --- diff --git a/Usermode/Applications/axwin2_src/WM/common.h b/Usermode/Applications/axwin2_src/WM/common.h index 00c84538..a515bdc0 100644 --- a/Usermode/Applications/axwin2_src/WM/common.h +++ b/Usermode/Applications/axwin2_src/WM/common.h @@ -10,9 +10,17 @@ #include #include // _SysDebug -typedef void tMessages_Handle_Callback(void*, size_t, void*); +// === TYPES === +typedef struct sIPC_Type tIPC_Type; typedef struct sFont tFont; +struct sIPC_Type +{ + int (*GetIdentSize)(void *Ident); + int (*CompareIdent)(void *Ident1, void *Ident2); + void (*SendMessage)(void *Ident, size_t, void *Data); +}; + #include "wm.h" #include "image.h" //#include "font.h" @@ -70,14 +78,17 @@ extern int giMouseFD; extern void memset32(void *ptr, uint32_t val, size_t count); // --- Initialisation --- extern void ParseCommandline(int argc, char *argv[]); +// --- Messages / IPC --- extern void IPC_Init(void); extern void IPC_FillSelect(int *nfds, fd_set *set); extern void IPC_HandleSelect(fd_set *set); +// --- Input --- extern void Input_FillSelect(int *nfds, fd_set *set); extern void Input_HandleSelect(fd_set *set); // --- Local WM --- -extern tApplication *AxWin_RegisterClient(int IdentLen, void *Ident, tMessages_Handle_Callback *Cb, const char *Name); +extern tApplication *AxWin_RegisterClient(tIPC_Type *Method, void *Ident, const char *Name); extern void AxWin_DeregisterClient(tApplication *App); +extern tApplication *AxWin_GetClient(tIPC_Type *Method, void *Ident); // --- Video --- extern void Video_Setup(void); extern void Video_Update(void); diff --git a/Usermode/Applications/axwin2_src/WM/messages.c b/Usermode/Applications/axwin2_src/WM/messages.c index 61ffc530..f876f505 100644 --- a/Usermode/Applications/axwin2_src/WM/messages.c +++ b/Usermode/Applications/axwin2_src/WM/messages.c @@ -18,13 +18,27 @@ void IPC_Init(void); void IPC_FillSelect(int *nfds, fd_set *set); void IPC_HandleSelect(fd_set *set); -void Messages_RespondDatagram(void *Ident, size_t Length, void *Data); -void Messages_RespondIPC(void *Ident, size_t Length, void *Data); -void Messages_Handle(size_t IdentLen, void *Ident, size_t MsgLen, tAxWin_Message *Msg, tMessages_Handle_Callback *Respond); +void IPC_Handle(tIPC_Type *IPCType, void *Ident, size_t MsgLen, tAxWin_Message *Msg); + int IPC_Type_Datagram_GetSize(void *Ident); + int IPC_Type_Datagram_Compare(void *Ident1, void *Ident2); +void IPC_Type_Datagram_Send(void *Ident, size_t Length, void *Data); + int IPC_Type_Sys_GetSize(void *Ident); + int IPC_Type_Sys_Compare(void *Ident1, void *Ident2); +void IPC_Type_Sys_Send(void *Ident, size_t Length, void *Data); // === GLOBALS === int giNetworkFileHandle = -1; int giMessagesFileHandle = -1; +tIPC_Type gIPC_Type_Datagram = { + IPC_Type_Datagram_GetSize, + IPC_Type_Datagram_Compare, + IPC_Type_Datagram_Send +}; +tIPC_Type gIPC_Type_SysMessage = { + IPC_Type_Sys_GetSize, + IPC_Type_Sys_Compare, + IPC_Type_Sys_Send +}; // === CODE === void IPC_Init(void) @@ -55,7 +69,7 @@ void IPC_HandleSelect(fd_set *set) identlen = 4 + Net_GetAddressSize( ((uint16_t*)staticBuf)[1] ); msg = staticBuf + identlen; - Messages_Handle(identlen, staticBuf, readlen - identlen, (void*)msg, Messages_RespondDatagram); + IPC_Handle(&gIPC_Type_Datagram, staticBuf, readlen - identlen, (void*)msg); } while(SysGetMessage(NULL, NULL)) @@ -65,34 +79,24 @@ void IPC_HandleSelect(fd_set *set) char data[len]; SysGetMessage(NULL, data); - Messages_Handle(sizeof(tid), &tid, len, (void*)data, Messages_RespondIPC); + IPC_Handle(&gIPC_Type_SysMessage, &tid, len, (void*)data); } } -void Messages_RespondDatagram(void *Ident, size_t Length, void *Data) -{ - int addrSize = Net_GetAddressSize( ((uint16_t*)Ident)[1] ); - char tmpbuf[ 4 + addrSize + Length ]; - memcpy(tmpbuf, Ident, 4 + addrSize); - memcpy(tmpbuf + 4 + addrSize, Data, Length); - // TODO: Handle fragmented packets - write(giNetworkFileHandle, sizeof(tmpbuf), tmpbuf); -} - -void Messages_RespondIPC(void *Ident, size_t Length, void *Data) -{ - SysSendMessage( *(tid_t*)Ident, Length, Data ); -} - -void Messages_Handle(size_t IdentLen, void *Ident, size_t MsgLen, tAxWin_Message *Msg, tMessages_Handle_Callback *Respond) +void IPC_Handle(tIPC_Type *IPCType, void *Ident, size_t MsgLen, tAxWin_Message *Msg) { + tApplication *app; + if( MsgLen < sizeof(tAxWin_Message) ) return ; if( MsgLen < sizeof(tAxWin_Message) + Msg->Size ) return ; + + app = AxWin_GetClient(IPCType, Ident); - switch(Msg->ID) + switch((enum eAxWin_Messages) Msg->ID) { + // --- Ping message (reset timeout and get server version) case MSG_SREQ_PING: if( MsgLen < sizeof(tAxWin_Message) + 4 ) return; Msg->ID = MSG_SRSP_VERSION; @@ -100,23 +104,77 @@ void Messages_Handle(size_t IdentLen, void *Ident, size_t MsgLen, tAxWin_Message Msg->Data[0] = 0; Msg->Data[1] = 1; *(uint16_t*)&Msg->Data[2] = -1; - Respond(Ident, sizeof(Msg->ID), Msg); + IPCType->SendMessage(Ident, sizeof(Msg->ID), Msg); break; + + // --- Register an application case MSG_SREQ_REGISTER: if( Msg->Data[Msg->Size-1] != '\0' ) { // Invalid message return ; } - AxWin_RegisterClient(IdentLen, Ident, Respond, Msg->Data); - + if( app != NULL ) { + _SysDebug("Notice: Duplicate registration (%s)\n", Msg->Data); + return ; + } + + // TODO: Should this function be implemented here? + AxWin_RegisterClient(IPCType, Ident, Msg->Data); break; - + + // --- Create a window + case MSG_SREQ_ADDWIN: + if( Msg->Data[Msg->Size-1] != '\0' ) { + // Invalid message + return ; + } + + break; + + // --- Unknown message default: - fprintf(stderr, "WARNING: Unknown message %i (%p)\n", Msg->ID, Respond); - _SysDebug("WARNING: Unknown message %i (%p)\n", Msg->ID, Respond); + fprintf(stderr, "WARNING: Unknown message %i (%p)\n", Msg->ID, IPCType); + _SysDebug("WARNING: Unknown message %i (%p)\n", Msg->ID, IPCType); break; } } +int IPC_Type_Datagram_GetSize(void *Ident) +{ + return 4 + Net_GetAddressSize( ((uint16_t*)Ident)[1] ); +} + +int IPC_Type_Datagram_Compare(void *Ident1, void *Ident2) +{ + // Pass the buck :) + // - No need to worry about mis-matching sizes, as the size is computed + // from the 3rd/4th bytes, hence it will differ before the size is hit. + return memcmp(Ident1, Ident2, IPC_Type_Datagram_GetSize(Ident1)); +} + +void IPC_Type_Datagram_Send(void *Ident, size_t Length, void *Data) +{ + int identlen = IPC_Type_Datagram_GetSize(Ident); + char tmpbuf[ identlen + Length ]; + memcpy(tmpbuf, Ident, identlen); // Header + memcpy(tmpbuf + identlen, Data, Length); // Data + // TODO: Handle fragmented packets + write(giNetworkFileHandle, sizeof(tmpbuf), tmpbuf); +} + +int IPC_Type_Sys_GetSize(void *Ident) +{ + return sizeof(pid_t); +} + +int IPC_Type_Sys_Compare(void *Ident1, void *Ident2) +{ + return *(int*)Ident1 - *(int*)Ident2; +} + +void IPC_Type_Sys_Send(void *Ident, size_t Length, void *Data) +{ + SysSendMessage( *(tid_t*)Ident, Length, Data ); +} diff --git a/Usermode/Applications/axwin2_src/WM/wm.c b/Usermode/Applications/axwin2_src/WM/wm.c index cb4fd27e..dc1de6a2 100644 --- a/Usermode/Applications/axwin2_src/WM/wm.c +++ b/Usermode/Applications/axwin2_src/WM/wm.c @@ -14,6 +14,9 @@ extern void Video_GetTextDims(tFont *Font, const char *Text, int *W, int *H); // === PROTOTYPES === +tApplication *AxWin_RegisterClient(tIPC_Type *IPCType, void *Ident, const char *Name); +void AxWin_DeregisterClient(tApplication *App); +tApplication *AxWin_GetClient(tIPC_Type *Method, void *Ident); tElement *AxWin_CreateElement(tElement *Parent, int Type, int Flags, const char *DebugName); void AxWin_DeleteElement(tElement *Element); void AxWin_SetFlags(tElement *Element, int Flags); @@ -25,7 +28,6 @@ void AxWin_SetText(tElement *Element, const char *Text); tElement gWM_RootElement = { .DebugName = "ROOT" }; - tApplication *gWM_Applications; // --- Element type flags @@ -43,19 +45,28 @@ struct { const int ciWM_NumWidgetTypes = sizeof(gaWM_WidgetTypes)/sizeof(gaWM_WidgetTypes[0]); // === CODE === -tApplication *AxWin_RegisterClient(int IdentLen, void *Ident, tMessages_Handle_Callback *Cb, const char *Name) +tApplication *AxWin_RegisterClient(tIPC_Type *Method, void *Ident, const char *Name) { - tApplication *ret = calloc( 1, sizeof(tApplication) + 1 + strlen(Name) + 1 + IdentLen ); + int identlen = Method->GetIdentSize(Ident); + // Structure, empty string, Name, Ident + tApplication *ret = calloc( 1, sizeof(tApplication) + 1 + strlen(Name) + 1 + identlen ); + + // DebugName is empty + // Name/Title ret->Name = &ret->MetaElement.DebugName[1]; strcpy(ret->Name, Name); + // Ident ret->Ident = ret->Name + strlen(Name) + 1; - memcpy(ret->Ident, Ident, IdentLen); - ret->SendMessage = Cb; + memcpy(ret->Ident, Ident, identlen); + // IPC Type + ret->IPCType = Method; + // Element index ret->MaxElementIndex = DEFAULT_ELEMENTS_PER_APP; ret->EleIndex = calloc( 1, ret->MaxElementIndex * sizeof(*ret->EleIndex) ); + // Add to global list ret->Next = gWM_Applications; gWM_Applications = ret; @@ -66,8 +77,8 @@ tApplication *AxWin_RegisterClient(int IdentLen, void *Ident, tMessages_Handle_C void AxWin_DeregisterClient(tApplication *App) { + // TODO: Complete implementing DeregisterClient tElement *win, *next; - // TODO: Implement DeregisterClient for( win = App->MetaElement.FirstChild; win; win = next ) { @@ -76,10 +87,44 @@ void AxWin_DeregisterClient(tApplication *App) } // TODO: Inform listeners of deleted application - // TODO: Remove from list + + // Remove from list + { + tApplication *app, *prev = NULL; + for( app = gWM_Applications; app; app = app->Next ) + { + if( app == App ) break; + prev = app; + } + + if( app ) + { + if(prev) + prev->Next = App->Next; + else + gWM_Applications = App->Next; + } + } + free(App); } +/** + * \brief Get an application handle from a client identifier + */ +tApplication *AxWin_GetClient(tIPC_Type *Method, void *Ident) +{ + // TODO: Faster and smarter technique + tApplication *app; + for( app = gWM_Applications; app; app = app->Next ) + { + if( app->IPCType != Method ) continue; + if( Method->CompareIdent( app->Ident, Ident ) != 0 ) continue; + return app; + } + return NULL; +} + tElement *AxWin_CreateWindow(tApplication *App, const char *Name) { tElement *ret; diff --git a/Usermode/Applications/axwin2_src/WM/wm.h b/Usermode/Applications/axwin2_src/WM/wm.h index d91626ba..ab2a629c 100644 --- a/Usermode/Applications/axwin2_src/WM/wm.h +++ b/Usermode/Applications/axwin2_src/WM/wm.h @@ -8,7 +8,7 @@ #include "common.h" /** - * \brief Number of elements that can be owned by each application) + * \brief Number of elements that can be owned by each application */ // TODO: Fine tune these values #define MAX_ELEMENTS_PER_APP 1024 @@ -60,8 +60,8 @@ struct sApplication { tApplication *Next; + tIPC_Type *IPCType; void *Ident; //!< Client Identifier - tMessages_Handle_Callback *SendMessage; char *Name; //!< Application name