drive: win: add hotplug support.

This commit is contained in:
Zhang Zhaolong 2014-02-14 15:09:55 +08:00
parent 4982c03e3d
commit 7205e348eb
4 changed files with 223 additions and 0 deletions

View File

@ -67,6 +67,15 @@ void devman_free(DEVMAN* devman)
free(devman); free(devman);
} }
void devman_unregister_device(DEVMAN* devman, void* key)
{
DEVICE* device;
device = (DEVICE *)ListDictionary_Remove(devman->devices, key);
if (device)
devman_device_free(device);
}
static void devman_register_device(DEVMAN* devman, DEVICE* device) static void devman_register_device(DEVMAN* devman, DEVICE* device)
{ {
void* key = NULL; void* key = NULL;

View File

@ -23,6 +23,7 @@
#include "rdpdr_main.h" #include "rdpdr_main.h"
void devman_unregister_device(DEVMAN* devman, void* key);
BOOL devman_load_device_service(DEVMAN* devman, RDPDR_DEVICE* device); BOOL devman_load_device_service(DEVMAN* devman, RDPDR_DEVICE* device);
DEVICE* devman_get_device_by_id(DEVMAN* devman, UINT32 id); DEVICE* devman_get_device_by_id(DEVMAN* devman, UINT32 id);

View File

@ -33,6 +33,11 @@
#include <freerdp/constants.h> #include <freerdp/constants.h>
#include <freerdp/channels/rdpdr.h> #include <freerdp/channels/rdpdr.h>
#ifdef _WIN32
#include <windows.h>
#include <dbt.h>
#endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
@ -44,6 +49,201 @@
#include "rdpdr_main.h" #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) static void rdpdr_process_connect(rdpdrPlugin* rdpdr)
{ {
int index; int index;
@ -58,6 +258,11 @@ static void rdpdr_process_connect(rdpdrPlugin* rdpdr)
for (index = 0; index < settings->DeviceCount; index++) for (index = 0; index < settings->DeviceCount; index++)
{ {
device = settings->DeviceArray[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); devman_load_device_service(rdpdr->devman, device);
} }
} }
@ -493,6 +698,8 @@ static void rdpdr_virtual_channel_event_terminated(rdpdrPlugin* rdpdr)
MessagePipe_Free(rdpdr->MsgPipe); MessagePipe_Free(rdpdr->MsgPipe);
CloseHandle(rdpdr->thread); CloseHandle(rdpdr->thread);
drive_hotplug_thread_terminate(rdpdr);
rdpdr->channelEntryPoints.pVirtualChannelClose(rdpdr->OpenHandle); rdpdr->channelEntryPoints.pVirtualChannelClose(rdpdr->OpenHandle);
if (rdpdr->data_in) if (rdpdr->data_in)

View File

@ -52,6 +52,12 @@ struct rdpdr_plugin
UINT16 versionMinor; UINT16 versionMinor;
UINT16 clientID; UINT16 clientID;
char computerName[256]; char computerName[256];
/* hotplug support */
#ifdef _WIN32
HWND hotplug_wnd;
#else
#endif
}; };
int rdpdr_send(rdpdrPlugin* rdpdr, wStream* s); int rdpdr_send(rdpdrPlugin* rdpdr, wStream* s);