[Freeswitch-svn] [commit] r12233 - freeswitch/branches/ctrix/mod_airpe

FreeSWITCH SVN ctrix at freeswitch.org
Sun Feb 22 04:28:30 PST 2009


Author: ctrix
Date: Sun Feb 22 06:28:30 2009
New Revision: 12233

Log:
something for windows

Modified:
   freeswitch/branches/ctrix/mod_airpe/airpe_if_win32.c

Modified: freeswitch/branches/ctrix/mod_airpe/airpe_if_win32.c
==============================================================================
--- freeswitch/branches/ctrix/mod_airpe/airpe_if_win32.c	(original)
+++ freeswitch/branches/ctrix/mod_airpe/airpe_if_win32.c	Sun Feb 22 06:28:30 2009
@@ -1,19 +1,315 @@
-
-#include "mod_airpe.h"
-
-#ifdef WIN32
-
-#error WIN32 Not yet supported
-
-#endif
-
-/* For Emacs:
- * Local Variables:
- * mode:c
- * indent-tabs-mode:t
- * tab-width:4
- * c-basic-offset:4
- * End:
- * For VIM:
- * vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
- */
+
+#include "mod_airpe.h"
+
+#ifdef WIN32
+
+//#error WIN32 Not yet supported
+
+#include <conio.h>
+#include <stdio.h>
+#include <string.h>
+#include <process.h>
+
+#include <windows.h>
+#include <rpcdce.h>
+
+
+enum {
+	SKYPECONTROLAPI_ATTACH_SUCCESS					= 0,	// Client is successfully attached and API window handle can be found in wParam parameter
+	SKYPECONTROLAPI_ATTACH_PENDING_AUTHORIZATION	= 1,	// Skype has acknowledged connection request and is waiting for confirmation from the user.
+															// The client is not yet attached and should wait for SKYPECONTROLAPI_ATTACH_SUCCESS message
+	SKYPECONTROLAPI_ATTACH_REFUSED					= 2,	// User has explicitly denied access to client
+	SKYPECONTROLAPI_ATTACH_NOT_AVAILABLE			= 3,	// API is not available at the moment. For example, this happens when no user is currently logged in.
+															// Client should wait for SKYPECONTROLAPI_ATTACH_API_AVAILABLE broadcast before making any further
+															// connection attempts.
+	SKYPECONTROLAPI_ATTACH_API_AVAILABLE			= 0x8001
+};
+
+
+
+
+UINT uiGlobal_MsgID_SkypeControlAPIAttach;
+UINT uiGlobal_MsgID_SkypeControlAPIDiscover;
+HINSTANCE hInit_ProcessHandle;
+HWND hInit_MainWindowHandle;
+char acInit_WindowClassName[128];
+HANDLE hGlobal_ThreadShutdownEvent;
+HWND hGlobal_SkypeAPIWindowHandle=NULL;
+
+
+struct skype_window_handler_s {
+    uint8_t			api_connected;
+    int				watchdog;
+};
+
+static switch_memory_pool_t *tech_pool;
+
+
+void airpe_tech_set_pool(switch_memory_pool_t *pool) {
+    if ( !tech_pool )
+		tech_pool = pool;
+
+	ADEBUG("--\n");
+}
+
+switch_status_t airpe_skype_watchdog_reset(skype_window_handler_t *window) {
+    window->watchdog = 0;
+	ADEBUG("--\n");
+    return SWITCH_STATUS_SUCCESS;
+}
+
+switch_status_t airpe_skype_watchdog_increment(skype_window_handler_t *window) {
+    window->watchdog++;
+
+	ADEBUG("--\n");
+    if ( window->watchdog >= 5 ) {
+    }
+
+    return SWITCH_STATUS_SUCCESS;
+}
+
+skype_window_handler_t *skype_window_alloc( switch_memory_pool_t *pool) {
+	ADEBUG("--\n");
+    return (skype_window_handler_t *) switch_core_alloc( pool, sizeof(skype_window_handler_t));
+}
+
+switch_status_t skype_restart( skype_window_handler_t *window, const char *params ) {
+    /* TODO */
+	ADEBUG("--\n");
+    return SWITCH_STATUS_SUCCESS;
+}
+
+switch_status_t airpe_skype_send_message(skype_window_handler_t *window, const char *message_P) {
+	COPYDATASTRUCT oCopyData;
+	ADEBUG("Sending %s \n", message_P);
+
+	oCopyData.dwData=0;
+	oCopyData.lpData=(char*) message_P;
+	oCopyData.cbData=strlen(message_P)+1;
+
+	if( oCopyData.cbData!=1 ) {
+		if( SendMessage( hGlobal_SkypeAPIWindowHandle, WM_COPYDATA, (WPARAM)hInit_MainWindowHandle, (LPARAM)&oCopyData)==FALSE ) {
+			hGlobal_SkypeAPIWindowHandle=NULL;
+			ADEBUG("DISCONNECTED :-( \n");
+		}
+	}
+
+    return SWITCH_STATUS_SUCCESS;
+}
+
+switch_status_t airpe_skype_avalaible(skype_window_handler_t *window)
+{
+	ADEBUG("--\n");
+	return SWITCH_STATUS_SUCCESS;
+}
+
+/*****************************************************************************
+    THREADS
+ *****************************************************************************/
+static int attached = -1;
+
+static LRESULT APIENTRY SkypeReceiveMessage( HWND hWindow, UINT uiMessage, WPARAM uiParam, LPARAM ulParam) {
+	LRESULT lReturnCode;
+	int fIssueDefProc;
+	airpe_interface_t *airpe = NULL;
+
+	ADEBUG("--\n");
+
+	lReturnCode=0;
+	fIssueDefProc=0;
+
+	airpe = (airpe_interface_t *) GetWindowLong(hWindow, GWL_USERDATA);
+
+	switch(uiMessage) {
+		case WM_CREATE:
+			assert(!airpe);
+			airpe = (airpe_interface_t *) ((LPCREATESTRUCT) ulParam)->lpCreateParams;
+			SetWindowLong(hWindow, GWL_USERDATA, (LONG) airpe);
+			break;
+		case WM_DESTROY:
+			hInit_MainWindowHandle=NULL;
+			PostQuitMessage(0);
+			break;
+		case WM_COPYDATA:
+			if( hGlobal_SkypeAPIWindowHandle==(HWND)uiParam ) {
+				char msg[SKYPE_MSG_LEN];
+
+				PCOPYDATASTRUCT poCopyData=(PCOPYDATASTRUCT)ulParam;
+				strncpy(msg, (const char *) poCopyData->lpData, sizeof(msg)-1);
+				ADEBUG("MESSAGE: %s\n", msg);
+				airpe_manage_skype_msg( airpe, msg);
+				//printf( "Message from Skype(%u): %.*s\n", poCopyData->dwData, poCopyData->cbData, poCopyData->lpData);
+				lReturnCode=1;
+			}
+			break;
+		default:
+			if( uiMessage==uiGlobal_MsgID_SkypeControlAPIAttach ) {
+				switch(ulParam) {
+					case SKYPECONTROLAPI_ATTACH_SUCCESS:
+						printf("!!! Connected\n");
+						hGlobal_SkypeAPIWindowHandle=(HWND)uiParam;
+						break;
+					case SKYPECONTROLAPI_ATTACH_PENDING_AUTHORIZATION:
+						printf("!!! Pending authorization\n");
+						break;
+					case SKYPECONTROLAPI_ATTACH_REFUSED:
+						printf("!!! Connection refused\n");
+						break;
+					case SKYPECONTROLAPI_ATTACH_NOT_AVAILABLE:
+						printf("!!! Skype API not available\n");
+						break;
+					case SKYPECONTROLAPI_ATTACH_API_AVAILABLE:
+						printf("!!! Try connect now (API available); issue #connect\n");
+						break;
+				}
+				lReturnCode=1;
+				break;
+			}
+			fIssueDefProc=1;
+			break;
+	}
+
+	if( fIssueDefProc ) {
+		lReturnCode=DefWindowProc( hWindow, uiMessage, uiParam, ulParam);
+	}
+
+	return(lReturnCode);
+}
+
+static switch_status_t Initialize_CreateMainWindow(airpe_interface_t *airpe) {
+	hInit_MainWindowHandle=CreateWindowEx( 
+					WS_EX_APPWINDOW|WS_EX_WINDOWEDGE,
+					acInit_WindowClassName, "", 
+					WS_BORDER|WS_SYSMENU|WS_MINIMIZEBOX,
+					CW_USEDEFAULT, CW_USEDEFAULT, 128, 128, NULL, 0, hInit_ProcessHandle, 
+					airpe);
+
+	if ( hInit_MainWindowHandle!=NULL ) {
+		return SWITCH_STATUS_SUCCESS;
+	}
+	else {
+		return SWITCH_STATUS_FALSE;
+	}
+}
+
+void DeInitialize_DestroyMainWindow(void) {
+	if( hInit_MainWindowHandle!=NULL )
+		DestroyWindow(hInit_MainWindowHandle),hInit_MainWindowHandle=NULL;
+	}
+
+	
+static switch_status_t Initialize_CreateWindowClass(void) {
+	unsigned char *paucUUIDString;
+	RPC_STATUS lUUIDResult;
+	int fReturnStatus;
+	UUID oUUID;
+
+	fReturnStatus=0;
+	lUUIDResult = UuidCreate(&oUUID);
+
+	hInit_ProcessHandle = (HINSTANCE) OpenProcess( PROCESS_DUP_HANDLE, FALSE, GetCurrentProcessId());
+	
+	if( hInit_ProcessHandle!=NULL && (lUUIDResult==RPC_S_OK || lUUIDResult==RPC_S_UUID_LOCAL_ONLY) ) {
+		if( UuidToString( &oUUID, &paucUUIDString)==RPC_S_OK ) {
+			WNDCLASS oWindowClass;
+
+			strcpy( acInit_WindowClassName, "mod_airpe");
+			strcat( acInit_WindowClassName, (char *)paucUUIDString);
+
+			oWindowClass.style=CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;
+			oWindowClass.lpfnWndProc=(WNDPROC)&SkypeReceiveMessage;
+			oWindowClass.cbClsExtra=0;
+			oWindowClass.cbWndExtra=0;
+			oWindowClass.hInstance=hInit_ProcessHandle;
+			oWindowClass.hIcon=NULL;
+			oWindowClass.hCursor=NULL;
+			oWindowClass.hbrBackground=NULL;
+			oWindowClass.lpszMenuName=NULL;
+			oWindowClass.lpszClassName=acInit_WindowClassName;
+
+			if( RegisterClass(&oWindowClass)!=0 )
+				fReturnStatus=1;
+
+			RpcStringFree(&paucUUIDString);
+		}
+	}
+	if( fReturnStatus==0 ) {
+		CloseHandle(hInit_ProcessHandle),hInit_ProcessHandle=NULL;
+		return SWITCH_STATUS_FALSE;
+	}
+	else {	
+		return SWITCH_STATUS_SUCCESS;
+	}
+}
+
+static void DeInitialize_DestroyWindowClass(void) {
+	UnregisterClass( acInit_WindowClassName, hInit_ProcessHandle);
+	CloseHandle(hInit_ProcessHandle),hInit_ProcessHandle=NULL;
+}
+
+void *SWITCH_THREAD_FUNC airpe_skype_thread(switch_thread_t * thread, void *obj) {
+    airpe_interface_t *airpe = NULL;
+
+    assert(obj);
+    airpe = obj;
+
+	uiGlobal_MsgID_SkypeControlAPIAttach   = RegisterWindowMessage("SkypeControlAPIAttach");
+	uiGlobal_MsgID_SkypeControlAPIDiscover = RegisterWindowMessage("SkypeControlAPIDiscover");
+	
+	if( uiGlobal_MsgID_SkypeControlAPIAttach == 0 || uiGlobal_MsgID_SkypeControlAPIDiscover == 0 ) {
+		ADEBUG("Cannot find any running Skype application\n");
+		return NULL;
+	}
+
+	if( Initialize_CreateWindowClass()==SWITCH_STATUS_SUCCESS ) {
+		if( Initialize_CreateMainWindow(airpe)==SWITCH_STATUS_SUCCESS ) {
+			hGlobal_ThreadShutdownEvent = CreateEvent( NULL, TRUE, FALSE, NULL);
+			if( hGlobal_ThreadShutdownEvent!=NULL ) {
+				if( SendMessage( HWND_BROADCAST, uiGlobal_MsgID_SkypeControlAPIDiscover, (WPARAM)hInit_MainWindowHandle, 0)!=0 ) {
+					MSG oMessage;
+
+					airpe->running = 1;
+
+					if ( airpe_on_connect_messages( airpe, 6 ) != SWITCH_STATUS_SUCCESS ) {
+							switch_log_printf(	SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, 
+								"airpe client %s. Failed to send handshake messages\n", 
+								airpe->name);
+							airpe->running = 0;
+					}
+					
+					ADEBUG("BEFORE LOOPING--\n");
+					while( airpe->running && GetMessage( &oMessage, 0, 0, 0)!=FALSE ) {
+						ADEBUG("LOOPING\n");
+						switch_sleep(100000);
+						TranslateMessage(&oMessage);
+						DispatchMessage(&oMessage);
+					}
+//					WaitForSingleObject( hGlobal_ThreadShutdownEvent, INFINITE);
+				}
+				CloseHandle(hGlobal_ThreadShutdownEvent);
+			}
+			DeInitialize_DestroyMainWindow();
+		}
+		DeInitialize_DestroyWindowClass();
+	}
+
+    ADEBUG("Exiting\n");
+    airpe->airpe_thread_skype = NULL;
+
+    return NULL;
+}
+
+
+
+#endif
+
+/* For Emacs:
+ * Local Variables:
+ * mode:c
+ * indent-tabs-mode:t
+ * tab-width:4
+ * c-basic-offset:4
+ * End:
+ * For VIM:
+ * vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
+ */



More information about the Freeswitch-svn mailing list