#include <freerdp/constants.h>
#include <freerdp/channels/rdpdr.h>
+#ifdef _WIN32
+#include <windows.h>
+#include <dbt.h>
+#endif
+
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "rdpdr_main.h"
+static void rdpdr_send_device_list_announce_request(rdpdrPlugin* rdpdr, BOOL userLoggedOn);
+
+static void rdpdr_send_device_list_remove_request(rdpdrPlugin* rdpdr, UINT32 count, UINT32 ids[])
+{
+ wStream* s;
+ int i;
+
+ s = Stream_New(NULL, 256);
+
+ Stream_Write_UINT16(s, RDPDR_CTYP_CORE);
+ Stream_Write_UINT16(s, PAKID_CORE_DEVICELIST_REMOVE);
+ Stream_Write_UINT32(s, count);
+
+ for (i = 0; i < count; i++)
+ Stream_Write_UINT32(s, ids[i]);
+
+ Stream_SealLength(s);
+
+ rdpdr_send(rdpdr, s);
+}
+
+#ifdef _WIN32
+
+LRESULT CALLBACK hotplug_proc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
+{
+ rdpdrPlugin *rdpdr;
+ PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
+
+ rdpdr = (rdpdrPlugin *)GetWindowLongPtr(hWnd, GWLP_USERDATA);
+
+ switch(Msg)
+ {
+ case WM_DEVICECHANGE:
+ switch (wParam)
+ {
+ case DBT_DEVICEARRIVAL:
+ if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
+ {
+ PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
+ DWORD unitmask = lpdbv->dbcv_unitmask;
+ int i;
+ char drive_path[4] = { 'c', ':', '/', '\0'};
+
+ for (i = 0; i < 26; i++)
+ {
+ if (unitmask & 0x01)
+ {
+ RDPDR_DRIVE* drive;
+
+ drive_path[0] = 'A' + i;
+
+ drive = (RDPDR_DRIVE*) malloc(sizeof(RDPDR_DRIVE));
+ ZeroMemory(drive, sizeof(RDPDR_DRIVE));
+
+ drive->Type = RDPDR_DTYP_FILESYSTEM;
+
+ drive->Path = _strdup(drive_path);
+ drive_path[1] = '\0';
+ drive->Name = _strdup(drive_path);
+ devman_load_device_service(rdpdr->devman, (RDPDR_DEVICE *)drive);
+ rdpdr_send_device_list_announce_request(rdpdr, TRUE);
+ }
+ unitmask = unitmask >> 1;
+ }
+ }
+ break;
+
+ case DBT_DEVICEREMOVECOMPLETE:
+ if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
+ {
+ PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
+ DWORD unitmask = lpdbv->dbcv_unitmask;
+ int i, j, count;
+ char drive_name_upper, drive_name_lower;
+
+ ULONG_PTR *keys;
+ DEVICE *dev;
+ UINT32 ids[1];
+
+ for (i = 0; i < 26; i++)
+ {
+ if (unitmask & 0x01)
+ {
+ drive_name_upper = 'A' + i;
+ drive_name_lower = 'a' + i;
+
+ count = ListDictionary_GetKeys(rdpdr->devman->devices, &keys);
+
+ for (j = 0; j < count; j++)
+ {
+ dev = (DEVICE *)ListDictionary_GetItemValue(rdpdr->devman->devices, (void *)keys[j]);
+ if (dev->name[0] == drive_name_upper || dev->name[0] == drive_name_lower)
+ {
+ devman_unregister_device(rdpdr->devman, (void *)keys[j]);
+ ids[0] = keys[j];
+ rdpdr_send_device_list_remove_request(rdpdr, 1, ids);
+ break;
+ }
+ }
+ }
+ unitmask = unitmask >> 1;
+ }
+ }
+ break;
+
+ default:
+ break;
+ }
+ break;
+
+ default:
+ return DefWindowProc(hWnd, Msg, wParam, lParam);
+ }
+ return DefWindowProc(hWnd, Msg, wParam, lParam);
+}
+
+static void* drive_hotplug_thread_func(void *arg)
+{
+ rdpdrPlugin *rdpdr;
+ WNDCLASSEX wnd_cls;
+ HWND hwnd;
+ MSG msg;
+ BOOL bRet;
+ DEV_BROADCAST_HANDLE NotificationFilter;
+ HDEVNOTIFY hDevNotify;
+
+ rdpdr = (rdpdrPlugin *)arg;
+
+ /* init windows class */
+ wnd_cls.cbSize = sizeof(WNDCLASSEX);
+ wnd_cls.style = CS_HREDRAW | CS_VREDRAW;
+ wnd_cls.lpfnWndProc = hotplug_proc;
+ wnd_cls.cbClsExtra = 0;
+ wnd_cls.cbWndExtra = 0;
+ wnd_cls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
+ wnd_cls.hCursor = NULL;
+ wnd_cls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
+ wnd_cls.lpszMenuName = NULL;
+ wnd_cls.lpszClassName = L"DRIVE_HOTPLUG";
+ wnd_cls.hInstance = NULL;
+ wnd_cls.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
+ RegisterClassEx(&wnd_cls);
+
+ /* create window */
+ hwnd = CreateWindowEx(0, L"DRIVE_HOTPLUG", NULL,
+ 0, 0, 0, 0, 0,
+ NULL, NULL, NULL, NULL);
+ SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)rdpdr);
+
+ rdpdr->hotplug_wnd = hwnd;
+ /* register device interface to hwnd */
+ NotificationFilter.dbch_size = sizeof(DEV_BROADCAST_HANDLE);
+ NotificationFilter.dbch_devicetype = DBT_DEVTYP_HANDLE;
+ hDevNotify = RegisterDeviceNotification(hwnd, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
+
+ /* message loop */
+ while ((bRet = GetMessage(&msg, 0, 0, 0)) != 0)
+ {
+ if (bRet == -1)
+ {
+ break;
+ }
+ else
+ {
+ TranslateMessage(&msg);
+ DispatchMessage(&msg);
+ }
+ }
+
+ UnregisterDeviceNotification(hDevNotify);
+
+ return NULL;
+}
+
+static void drive_hotplug_thread_terminate(rdpdrPlugin* rdpdr)
+{
+ if (rdpdr->hotplug_wnd)
+ PostMessage(rdpdr->hotplug_wnd, WM_QUIT, 0, 0);
+}
+
+#else
+
+static void* drive_hotplug_thread_func(void *arg)
+{
+
+}
+
+static void drive_hotplug_thread_terminate(rdpdrPlugin* rdpdr)
+{
+
+}
+
+#endif
+
+
static void rdpdr_process_connect(rdpdrPlugin* rdpdr)
{
int index;
for (index = 0; index < settings->DeviceCount; index++)
{
device = settings->DeviceArray[index];
+ if (strcmp(device->Name, "*") == 0)
+ {
+ CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)drive_hotplug_thread_func, rdpdr, 0, NULL);
+ continue;
+ }
devman_load_device_service(rdpdr->devman, device);
}
}
MessagePipe_Free(rdpdr->MsgPipe);
CloseHandle(rdpdr->thread);
+ drive_hotplug_thread_terminate(rdpdr);
+
rdpdr->channelEntryPoints.pVirtualChannelClose(rdpdr->OpenHandle);
if (rdpdr->data_in)