|
From: libvidcap c. <lib...@li...> - 2008-04-28 19:17:51
|
Revision: 94
http://libvidcap.svn.sourceforge.net/libvidcap/?rev=94&view=rev
Author: bcholew
Date: 2008-04-28 12:16:42 -0700 (Mon, 28 Apr 2008)
Log Message:
-----------
fix to detect video device insertion/removal on Vista
Modified Paths:
--------------
trunk/src/directshow/DevMonitor.cpp
trunk/src/directshow/DevMonitor.h
Modified: trunk/src/directshow/DevMonitor.cpp
===================================================================
--- trunk/src/directshow/DevMonitor.cpp 2008-04-04 15:20:27 UTC (rev 93)
+++ trunk/src/directshow/DevMonitor.cpp 2008-04-28 19:16:42 UTC (rev 94)
@@ -26,7 +26,6 @@
#include <dbt.h>
#include <process.h>
#include <string>
-#include "sapi_context.h"
#include "DevMonitor.h"
#include "logging.h"
@@ -148,6 +147,9 @@
WPARAM wParam,
LPARAM lParam)
{
+ static DevMonitor *pDevMon = 0;
+ LPCREATESTRUCT lpcstr = 0;
+
switch (message)
{
case WM_CLOSE:
@@ -168,6 +170,19 @@
PostQuitMessage(0);
break;
+ case WM_CREATE:
+ // hold on to context for use when WM_DEVICECHANGE occurs
+ lpcstr = (LPCREATESTRUCT)lParam;
+ pDevMon = (DevMonitor *)lpcstr->lpCreateParams;
+ break;
+
+ case WM_DEVICECHANGE:
+ if ( !pDevMon )
+ log_error("Got WM_DEVICECHANGE event, but have no context!\n");
+ else
+ pDevMon->handleDeviceChange(wParam);
+ break;
+
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
@@ -175,6 +190,77 @@
return 0;
}
+void
+DevMonitor::handleDeviceChange(WPARAM wParam)
+{
+ std::string str;
+
+ switch (wParam)
+ {
+ case DBT_CONFIGCHANGECANCELED:
+ str.assign("DBT_CONFIGCHANGECANCELED");
+ break;
+ case DBT_CONFIGCHANGED:
+ str.assign("DBT_CONFIGCHANGED");
+ break;
+ case DBT_CUSTOMEVENT:
+ str.assign("DBT_CUSTOMEVENT");
+ break;
+ case DBT_DEVICEARRIVAL:
+ str.assign("DBT_DEVICEARRIVAL");
+ break;
+ case DBT_DEVICEQUERYREMOVE:
+ str.assign("DBT_DEVICEQUERYREMOVE");
+ break;
+ case DBT_DEVICEQUERYREMOVEFAILED:
+ str.assign("DBT_DEVICEQUERYREMOVEFAILED");
+ break;
+ case DBT_DEVICEREMOVECOMPLETE:
+ str.assign("DBT_DEVICEREMOVECOMPLETE");
+ break;
+ case DBT_DEVICEREMOVEPENDING:
+ str.assign("DBT_DEVICEREMOVEPENDING");
+ break;
+ case DBT_DEVICETYPESPECIFIC:
+ str.assign("DBT_DEVICETYPESPECIFIC");
+ break;
+ case DBT_DEVNODES_CHANGED:
+ str.assign("DBT_DEVNODES_CHANGED");
+ break;
+ case DBT_QUERYCHANGECONFIG:
+ str.assign("DBT_QUERYCHANGECONFIG");
+ break;
+ case DBT_USERDEFINED:
+ str.assign("DBT_USERDEFINED");
+ break;
+ default:
+ str.assign("default");
+ break;
+ }
+
+ log_info("[[ Device event: %s ]]\n", str.c_str());
+
+ vc_mutex_lock(®istrationMutex_);
+
+ if ( sapiCtx_ &&
+ sapiCtx_->notify_callback )
+ {
+ // TODO: If the user returns non-zero from
+ // their callback, maybe that should have
+ // some semantic meaning. For example, maybe
+ // that should be a way that the user can
+ // cancel further notification callbacks.
+ sapiCtx_->notify_callback(sapiCtx_, sapiCtx_->notify_data);
+ }
+ else
+ {
+ log_info("[[ no user-defined handler registered ]]\n");
+ }
+
+ vc_mutex_unlock(®istrationMutex_);
+
+}
+
unsigned int
DevMonitor::monitorDevices(void * param)
{
@@ -183,7 +269,8 @@
// extract instance
DevMonitor * pDevMon = (DevMonitor *)param;
- // Create a window, so main thread can communicate with us
+ // Create a window, so we can receive device events.
+ // Pass context for delivery via WM_CREATE event
pDevMon->windowHandle_ = CreateWindow(
pDevMon->szWindowClass_,
pDevMon->szTitle_,
@@ -195,7 +282,7 @@
NULL,
NULL,
(HINSTANCE)0,
- NULL);
+ param);
if ( !pDevMon->windowHandle_ )
{
@@ -225,83 +312,18 @@
// Main message loop
// Breaks out when PostQuitMessage() is called
MSG msg;
- while (GetMessage(&msg, NULL, 0, 0))
+ int ret;
+ while ( (ret = GetMessage(&msg, NULL, 0, 0)) != 0 )
{
- std::string str;
-
- sapi_context * sapiCtx =
- static_cast<sapi_context *>(pDevMon->sapiCtx_);
-
- switch (msg.message)
+ if ( ret == -1 )
{
- case WM_DEVICECHANGE:
- switch(msg.wParam)
- {
- case DBT_CONFIGCHANGECANCELED:
- str.assign("DBT_CONFIGCHANGECANCELED");
+ log_error("[[ GetMessage() returned error. [%d] ]]\n",
+ GetLastError());
break;
- case DBT_CONFIGCHANGED:
- str.assign("DBT_CONFIGCHANGED");
- break;
- case DBT_CUSTOMEVENT:
- str.assign("DBT_CUSTOMEVENT");
- break;
- case DBT_DEVICEARRIVAL:
- str.assign("DBT_DEVICEARRIVAL");
- break;
- case DBT_DEVICEQUERYREMOVE:
- str.assign("DBT_DEVICEQUERYREMOVE");
- break;
- case DBT_DEVICEQUERYREMOVEFAILED:
- str.assign("DBT_DEVICEQUERYREMOVEFAILED");
- break;
- case DBT_DEVICEREMOVECOMPLETE:
- str.assign("DBT_DEVICEREMOVECOMPLETE");
- break;
- case DBT_DEVICEREMOVEPENDING:
- str.assign("DBT_DEVICEREMOVEPENDING");
- break;
- case DBT_DEVICETYPESPECIFIC:
- str.assign("DBT_DEVICETYPESPECIFIC");
- break;
- case DBT_DEVNODES_CHANGED:
- str.assign("DBT_DEVNODES_CHANGED");
- break;
- case DBT_QUERYCHANGECONFIG:
- str.assign("DBT_QUERYCHANGECONFIG");
- break;
- case DBT_USERDEFINED:
- str.assign("DBT_USERDEFINED");
- break;
- default:
- str.assign("default");
- break;
}
- log_info("[[ Device event: %s ]]\n", str.c_str());
-
- vc_mutex_lock(&pDevMon->registrationMutex_);
-
- if ( sapiCtx &&
- sapiCtx->notify_callback )
- {
- // TODO: If the user returns non-zero from
- // their callback, maybe that should have
- // some semantic meaning. For example, maybe
- // that should be a way that the user can
- // cancel further notification callbacks.
- sapiCtx->notify_callback(sapiCtx, sapiCtx->notify_data);
- }
else
{
- log_info("[[ no user-defined handler registered ]]\n");
- }
-
- vc_mutex_unlock(&pDevMon->registrationMutex_);
-
- default:
- str.assign("");
DispatchMessage(&msg);
- break;
}
}
@@ -317,7 +339,7 @@
vc_mutex_lock(®istrationMutex_);
- sapiCtx_ = sapiCtx;
+ sapiCtx_ = static_cast<sapi_context *>(sapiCtx);
vc_mutex_unlock(®istrationMutex_);
Modified: trunk/src/directshow/DevMonitor.h
===================================================================
--- trunk/src/directshow/DevMonitor.h 2008-04-04 15:20:27 UTC (rev 93)
+++ trunk/src/directshow/DevMonitor.h 2008-04-28 19:16:42 UTC (rev 94)
@@ -26,6 +26,7 @@
#define _DEVMONITOR_H_
#include <vidcap/vidcap.h>
+#include "sapi_context.h"
class DevMonitor
{
@@ -38,6 +39,7 @@
private:
static LRESULT __stdcall processWindowsMsgs(HWND, UINT, WPARAM, LPARAM);
static unsigned int STDCALL monitorDevices(void * lpParam);
+ void handleDeviceChange(WPARAM wParam);
private:
HANDLE initDoneEvent_;
@@ -50,7 +52,7 @@
TCHAR * szTitle_;
TCHAR * szWindowClass_;
- void * sapiCtx_;
+ sapi_context * sapiCtx_;
vc_mutex registrationMutex_;
};
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|