wfreerdp-server: get rid of wfInfoSingleton
authorMarc-André Moreau <marcandre.moreau@gmail.com>
Sun, 2 Sep 2012 21:09:36 +0000 (17:09 -0400)
committerMarc-André Moreau <marcandre.moreau@gmail.com>
Sun, 2 Sep 2012 21:09:36 +0000 (17:09 -0400)
server/Windows/wf_info.c
server/Windows/wf_info.h
server/Windows/wf_mirage.c
server/Windows/wf_peer.c
server/Windows/wf_peer.h
server/Windows/wfreerdp.c
server/Windows/wfreerdp.h

index c4bf73b..903d1a7 100644 (file)
 \r
 extern wfInfo * wfInfoSingleton;\r
 \r
-int wf_info_lock(DWORD ms)\r
+static wfInfo* wfInfoInstance = NULL;\r
+\r
+int wf_info_lock(wfInfo* wfi, DWORD ms)\r
 {\r
        DWORD dRes;\r
 \r
-       dRes = WaitForSingleObject(wfInfoSingleton->mutex, ms);\r
+       dRes = WaitForSingleObject(wfi->mutex, ms);\r
 \r
-       switch(dRes)\r
+       switch (dRes)\r
        {\r
-       case WAIT_ABANDONED:\r
-               //complain and proceed as normal\r
-               printf("Got ownership of abandoned mutex... resuming...\n");\r
-\r
-       case WAIT_OBJECT_0:\r
-               break;\r
-\r
-       case WAIT_TIMEOUT:\r
-               return 1;\r
-               break;\r
-       case WAIT_FAILED:\r
-               printf("WAIT FAILED code %#X\n", GetLastError());\r
-               return -1;\r
-               break;\r
+               case WAIT_ABANDONED:\r
+                       printf("Got ownership of abandoned mutex... resuming...\n");\r
+                       break;\r
+\r
+               case WAIT_OBJECT_0:\r
+                       break;\r
+\r
+               case WAIT_TIMEOUT:\r
+                       return 1;\r
+                       break;\r
+\r
+               case WAIT_FAILED:\r
+                       printf("WAIT FAILED code %#X\n", GetLastError());\r
+                       return -1;\r
+                       break;\r
        }\r
-               \r
+\r
        return 0;\r
 }\r
 \r
-\r
-\r
-int wf_info_unlock()\r
+int wf_info_unlock(wfInfo* wfi)\r
 {\r
-       if(ReleaseMutex(wfInfoSingleton->mutex) == 0)\r
+       if (ReleaseMutex(wfi->mutex) == 0)\r
                return 0;\r
+\r
        return 1;\r
 }\r
 \r
-\r
-wfInfo* wf_info_init(wfInfo * wfi)\r
+wfInfo* wf_info_init(wfInfo* wfi)\r
 {\r
        if (!wfi)\r
        {\r
@@ -100,13 +101,21 @@ wfInfo* wf_info_init(wfInfo * wfi)
        return wfi;\r
 }\r
 \r
+wfInfo* wf_info_get_instance()\r
+{\r
+       if (wfInfoInstance == NULL)\r
+               wfInfoInstance = wf_info_init(NULL);\r
+\r
+       return wfInfoInstance;\r
+}\r
+\r
 void wf_info_mirror_init(wfInfo* wfi, wfPeerContext* context)\r
 {\r
        DWORD dRes;\r
 \r
        dRes = WaitForSingleObject(wfi->mutex, INFINITE);\r
 \r
-       switch(dRes)\r
+       switch (dRes)\r
        {\r
                case WAIT_OBJECT_0:\r
 \r
@@ -114,21 +123,21 @@ void wf_info_mirror_init(wfInfo* wfi, wfPeerContext* context)
                        {\r
                                /* only the first peer needs to call this. */\r
 \r
-                               context->wfInfo = wfi;\r
-                               wf_check_disp_devices(context->wfInfo);\r
-                               wf_disp_device_set_attatch(context->wfInfo, 1);\r
-                               wf_update_mirror_drv(context->wfInfo, 0);\r
-                               wf_map_mirror_mem(context->wfInfo);\r
+                               context->info = wfi;\r
+                               wf_check_disp_devices(context->info);\r
+                               wf_disp_device_set_attatch(context->info, 1);\r
+                               wf_update_mirror_drv(context->info, 0);\r
+                               wf_map_mirror_mem(context->info);\r
 \r
                                context->rfx_context = rfx_context_new();\r
                                context->rfx_context->mode = RLGR3;\r
-                               context->rfx_context->width = context->wfInfo->width;\r
-                               context->rfx_context->height = context->wfInfo->height;\r
+                               context->rfx_context->width = context->info->width;\r
+                               context->rfx_context->height = context->info->height;\r
 \r
                                rfx_context_set_pixel_format(context->rfx_context, RDP_PIXEL_FORMAT_B8G8R8A8);\r
                                context->s = stream_new(65536);\r
 \r
-                               context->wfInfo->roflbuffer = (BYTE*)malloc( (context->wfInfo->width) * (context->wfInfo->height) * 4);\r
+                               context->info->roflbuffer = (BYTE*)malloc( (context->info->width) * (context->info->height) * 4);\r
 \r
                                printf("Start Encoder\n");\r
                                ReleaseMutex(wfi->encodeMutex);\r
@@ -171,14 +180,14 @@ void wf_info_subscriber_release(wfInfo* wfi, wfPeerContext* context)
                        case WAIT_OBJECT_0: \r
                                --wfi->subscribers;\r
                                /* only the last peer needs to call this */\r
-                               wf_mirror_cleanup(context->wfInfo);\r
-                               wf_disp_device_set_attatch(context->wfInfo, 0);\r
-                               wf_update_mirror_drv(context->wfInfo, 1);\r
+                               wf_mirror_cleanup(context->info);\r
+                               wf_disp_device_set_attatch(context->info, 0);\r
+                               wf_update_mirror_drv(context->info, 1);\r
 \r
                                stream_free(context->s);\r
                                rfx_context_free(context->rfx_context);\r
 \r
-                               free(context->wfInfo->roflbuffer);\r
+                               free(context->info->roflbuffer);\r
                                break; \r
 \r
                        /**\r
@@ -222,9 +231,7 @@ BOOL wf_info_have_updates(wfInfo* wfi)
 \r
 void wf_info_updated(wfInfo* wfi)\r
 {\r
-\r
-       wfi->lastUpdate = wfi->nextUpdate;\r
-       \r
+       wfi->lastUpdate = wfi->nextUpdate;      \r
 }\r
 \r
 void wf_info_update_changes(wfInfo* wfi)\r
@@ -233,7 +240,6 @@ void wf_info_update_changes(wfInfo* wfi)
 \r
        buf = (GETCHANGESBUF*) wfi->changeBuffer;\r
        wfi->nextUpdate = buf->buffer->counter;\r
-       \r
 }\r
 \r
 void wf_info_find_invalid_region(wfInfo* wfi)\r
@@ -270,7 +276,6 @@ void wf_info_find_invalid_region(wfInfo* wfi)
 \r
        if (wfi->invalid_y2 >= wfi->height)\r
                wfi->invalid_y2 = wfi->height - 1;\r
-\r
 }\r
 \r
 void wf_info_clear_invalid_region(wfInfo* wfi)\r
index 92201e7..cbe8597 100644 (file)
@@ -53,9 +53,10 @@ struct wf_info
 };\r
 typedef struct wf_info wfInfo;\r
 \r
-int wf_info_lock(DWORD ms);\r
-int wf_info_unlock();\r
+int wf_info_lock(wfInfo* wfi, DWORD ms);\r
+int wf_info_unlock(wfInfo* wfi);\r
 \r
+wfInfo* wf_info_get_instance();\r
 wfInfo* wf_info_init(wfInfo* wfi);\r
 void wf_info_mirror_init(wfInfo* wfi, wfPeerContext* context);\r
 void wf_info_subscriber_release(wfInfo* wfi, wfPeerContext* context);\r
index 8c0c6f4..057508c 100644 (file)
@@ -243,7 +243,6 @@ BOOL wf_map_mirror_mem(wfInfo* context)
 {\r
        int status;\r
        GETCHANGESBUF* b;\r
-       _tprintf(_T("\n\nCreating a device context...\n"));\r
 \r
        context->driverDC = CreateDC(context->deviceName, NULL, NULL, NULL);\r
 \r
index 52c314e..9f27c79 100644 (file)
 void wf_peer_context_new(freerdp_peer* client, wfPeerContext* context)\r
 {\r
 #ifndef WITH_WIN8\r
-       wfInfoSingleton = wf_info_init(wfInfoSingleton);\r
-       wf_info_mirror_init(wfInfoSingleton, context);\r
+       context->info = wf_info_get_instance();\r
+       wf_info_mirror_init(context->info, context);\r
 #endif\r
 }\r
 \r
 void wf_peer_context_free(freerdp_peer* client, wfPeerContext* context)\r
 {\r
 #ifndef WITH_WIN8\r
-       wf_info_subscriber_release(wfInfoSingleton, context);\r
+       wf_info_subscriber_release(context->info, context);\r
 #endif\r
 }\r
 \r
 static DWORD WINAPI wf_peer_mirror_monitor(LPVOID lpParam)\r
 {\r
        DWORD fps;\r
+       wfInfo* wfi;\r
        DWORD beg, end;\r
        DWORD diff, rate;\r
        freerdp_peer* client;\r
+       wfPeerContext* context;\r
 \r
        fps = 24;\r
        rate = 1000 / fps;\r
        client = (freerdp_peer*) lpParam;\r
+       context = (wfPeerContext*) client->context;\r
+       wfi = context->info;\r
        \r
        while (1)\r
        {\r
                beg = GetTickCount();\r
 \r
-               wf_info_lock(INFINITE);\r
+               wf_info_lock(wfi, INFINITE);\r
 \r
-               if (wf_info_has_subscribers(wfInfoSingleton))\r
+               if (wf_info_has_subscribers(wfi))\r
                {\r
+                       wf_info_update_changes(wfi);\r
 \r
-                       wf_info_update_changes(wfInfoSingleton);\r
-                       if (wf_info_have_updates(wfInfoSingleton))\r
-                       {\r
+                       if (wf_info_have_updates(wfi))\r
                                wf_rfx_encode(client);\r
-                       }\r
                }\r
                else\r
                {\r
-                       wf_info_unlock();\r
+                       wf_info_unlock(wfi);\r
                }\r
 \r
-               wf_info_unlock();\r
+               wf_info_unlock(wfi);\r
 \r
                end = GetTickCount();\r
                diff = end - beg;\r
@@ -92,7 +94,7 @@ static DWORD WINAPI wf_peer_mirror_monitor(LPVOID lpParam)
        \r
 \r
        _tprintf(_T("monitor thread terminating...\n"));\r
-       wf_info_set_thread_count(wfInfoSingleton, wf_info_get_thread_count(wfInfoSingleton) - 1);\r
+       wf_info_set_thread_count(wfi, wf_info_get_thread_count(wfi) - 1);\r
 \r
        return 0;\r
 }\r
@@ -118,36 +120,34 @@ void wf_rfx_encode(freerdp_peer* client)
        BYTE* dstp;\r
 #endif\r
 \r
-       if(client->activated == FALSE)\r
+       if (client->activated == FALSE)\r
                return;\r
+       \r
        wfp = (wfPeerContext*) client->context;\r
+       wfi = wfp->info;\r
 \r
-       dRes = WaitForSingleObject(wfInfoSingleton->encodeMutex, INFINITE);\r
+       dRes = WaitForSingleObject(wfi->encodeMutex, INFINITE);\r
 \r
        switch (dRes)\r
        {\r
-\r
                case WAIT_ABANDONED:\r
-\r
-                       printf("\n\nwf_rfx_encode: Got ownership of abandoned mutex... resuming...\n");                 \r
-                       //no break\r
+                       printf("\n\nwf_rfx_encode: Got ownership of abandoned mutex... resuming...\n");\r
 \r
                case WAIT_OBJECT_0:\r
 \r
-                       wf_info_find_invalid_region(wfInfoSingleton);\r
+                       wf_info_find_invalid_region(wfi);\r
 \r
                        if( (wfp->activated == false) ||\r
-                               (wf_info_has_subscribers(wfInfoSingleton) == false) ||\r
-                               !wf_info_have_invalid_region(wfInfoSingleton) ||\r
-                               (wfInfoSingleton->enc_data == true) )\r
+                               (wf_info_has_subscribers(wfi) == false) ||\r
+                               !wf_info_have_invalid_region(wfi) ||\r
+                               (wfi->enc_data == true) )\r
                        {\r
-                               ReleaseMutex(wfInfoSingleton->encodeMutex);\r
+                               ReleaseMutex(wfi->encodeMutex);\r
                                break;\r
                        }\r
 \r
                        update = client->update;\r
                        cmd = &update->surface_bits_command;\r
-                       wfi = wfp->wfInfo;\r
                        buf = (GETCHANGESBUF*) wfi->changeBuffer;\r
 \r
                        width = (wfi->invalid_x2 - wfi->invalid_x1) + 1;\r
@@ -216,12 +216,12 @@ void wf_rfx_encode(freerdp_peer* client)
                        cmd->bitmapData = stream_get_head(s);\r
 \r
                        wfi->enc_data = true;\r
-                       ReleaseMutex(wfInfoSingleton->encodeMutex);\r
+                       ReleaseMutex(wfi->encodeMutex);\r
                        break;\r
 \r
                case WAIT_TIMEOUT:\r
 \r
-                       ReleaseMutex(wfInfoSingleton->encodeMutex);\r
+                       ReleaseMutex(wfi->encodeMutex);\r
                        break;\r
 \r
                default:\r
@@ -233,17 +233,22 @@ void wf_rfx_encode(freerdp_peer* client)
 \r
 void wf_peer_init(freerdp_peer* client)\r
 {\r
+       wfInfo* wfi;\r
+\r
        client->context_size = sizeof(wfPeerContext);\r
        client->ContextNew = (psPeerContextNew) wf_peer_context_new;\r
        client->ContextFree = (psPeerContextFree) wf_peer_context_free;\r
+       \r
        freerdp_peer_context_new(client);\r
 \r
+       wfi = ((wfPeerContext*) client->context)->info;\r
+\r
 #ifndef WITH_WIN8\r
-       if (!wf_info_get_thread_count(wfInfoSingleton))\r
+       if (!wf_info_get_thread_count(wfi))\r
        {\r
                if (CreateThread(NULL, 0, wf_peer_mirror_monitor, client, 0, NULL) != 0)\r
                {\r
-                       wf_info_set_thread_count(wfInfoSingleton, wf_info_get_thread_count(wfInfoSingleton) + 1);\r
+                       wf_info_set_thread_count(wfi, wf_info_get_thread_count(wfi) + 1);\r
                }\r
                else\r
                {\r
@@ -255,8 +260,11 @@ void wf_peer_init(freerdp_peer* client)
 \r
 boolean wf_peer_post_connect(freerdp_peer* client)\r
 {\r
+       wfInfo* wfi;\r
        wfPeerContext* context = (wfPeerContext*) client->context;\r
 \r
+       wfi = context->info;\r
+\r
        /**\r
         * This callback is called when the entire connection sequence is done, i.e. we've received the\r
         * Font List PDU from the client and sent out the Font Map PDU.\r
@@ -280,10 +288,10 @@ boolean wf_peer_post_connect(freerdp_peer* client)
        printf("Client requested desktop: %dx%dx%d\n",\r
                client->settings->width, client->settings->height, client->settings->color_depth);\r
 \r
-       printf("But we will try resizing to %dx%d\n", wfInfoSingleton->width, wfInfoSingleton->height);\r
+       printf("But we will try resizing to %dx%d\n", wfi->width, wfi->height);\r
 \r
-       client->settings->width = wfInfoSingleton->width;\r
-       client->settings->height = wfInfoSingleton->height;\r
+       client->settings->width = wfi->width;\r
+       client->settings->height = wfi->height;\r
 \r
        client->update->DesktopResize(client->update->context);\r
 \r
@@ -304,42 +312,46 @@ void wf_peer_synchronize_event(rdpInput* input, uint32 flags)
 \r
 }\r
 \r
-void wf_peer_send_changes(rdpUpdate* update)\r
+void wf_peer_send_changes(freerdp_peer* client)\r
 {\r
        int dRes;\r
+       wfInfo* wfi;\r
+       wfPeerContext* context = (wfPeerContext*) client->context;\r
+\r
+       wfi = context->info;\r
 \r
        /* are we currently encoding? */\r
-       dRes = WaitForSingleObject(wfInfoSingleton->encodeMutex, 0);\r
+       dRes = WaitForSingleObject(wfi->encodeMutex, 0);\r
 \r
        switch(dRes)\r
        {\r
                case WAIT_ABANDONED:\r
 \r
                        printf("\n\nwf_peer_send_changes: Got ownership of abandoned mutex... resuming...\n");\r
-                       //no break;\r
+                       /* no break; */\r
 \r
                case WAIT_OBJECT_0:\r
 \r
                        /* are there changes to send? */\r
 \r
-                       if (    ((wf_info_lock(0) != 0)) ||\r
-                               !wf_info_have_updates(wfInfoSingleton) ||\r
-                               !wf_info_have_invalid_region(wfInfoSingleton) ||\r
-                               (wfInfoSingleton->enc_data == FALSE))\r
+                       if (    ((wf_info_lock(wfi, 0) != 0)) ||\r
+                               !wf_info_have_updates(wfi) ||\r
+                               !wf_info_have_invalid_region(wfi) ||\r
+                               (wfi->enc_data == FALSE))\r
                        {\r
-                               //we dont send\r
-                               wf_info_unlock();\r
-                               ReleaseMutex(wfInfoSingleton->encodeMutex);\r
+                               /* we do not send */\r
+                               wf_info_unlock(wfi);\r
+                               ReleaseMutex(wfi->encodeMutex);\r
                                break;\r
                        }\r
 \r
-                       wf_info_updated(wfInfoSingleton);\r
+                       wf_info_updated(wfi);\r
 \r
-                       update->SurfaceBits(update->context, &update->surface_bits_command);\r
+                       client->update->SurfaceBits(client->update->context, &client->update->surface_bits_command);\r
 \r
-                       wfInfoSingleton->enc_data = FALSE;\r
-                       wf_info_unlock();\r
-                       ReleaseMutex(wfInfoSingleton->encodeMutex);\r
+                       wfi->enc_data = FALSE;\r
+                       wf_info_unlock(wfi);\r
+                       ReleaseMutex(wfi->encodeMutex);\r
                        break;\r
 \r
                case WAIT_TIMEOUT:\r
index 833126f..d85777e 100644 (file)
@@ -35,8 +35,6 @@ boolean wf_peer_activate(freerdp_peer* client);
 \r
 void wf_peer_synchronize_event(rdpInput* input, uint32 flags);\r
 \r
-void wf_peer_send_changes(rdpUpdate* update);\r
-\r
-wfInfo* wfInfoSingleton;\r
+void wf_peer_send_changes(freerdp_peer* client);\r
 \r
 #endif /* WF_PEER_H */\r
index cb214b7..99ecfbe 100644 (file)
@@ -93,7 +93,7 @@ static DWORD WINAPI wf_peer_main_loop(LPVOID lpParam)
 \r
 #ifndef WITH_WIN8\r
                if(client->activated)\r
-                       wf_peer_send_changes(client->update);\r
+                       wf_peer_send_changes(client);\r
 #endif\r
        }\r
 \r
index f1cb2f1..021d0e3 100644 (file)
@@ -30,14 +30,11 @@ struct wf_peer_context
 {\r
        rdpContext _p;\r
 \r
-       wfInfo* wfInfo;\r
+       STREAM* s;\r
+       wfInfo* info;\r
        boolean activated;\r
        RFX_CONTEXT* rfx_context;\r
-       STREAM* s;\r
-\r
 };\r
 typedef struct wf_peer_context wfPeerContext;\r
 \r
-extern wfInfo * wfInfoSingleton;\r
-\r
 #endif /* WFREERDP_H */\r