File: CocoaEventBridgeHelpers.c

package info (click to toggle)
psychtoolbox-3 3.0.14.20170103%2Bgit6-g605ff5c.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 103,044 kB
  • ctags: 69,483
  • sloc: ansic: 167,371; cpp: 11,232; objc: 4,708; sh: 1,875; python: 383; php: 344; makefile: 207; java: 113
file content (158 lines) | stat: -rw-r--r-- 6,837 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
	PsychSourceGL/Source/Common/CocoaEventBridge/CocoaEventBridgeHelpers.c		
  
	PROJECTS: 
  
		CocoaEventBridge only
  
	AUTHORS:
  
		Allen.Ingling@nyu.edu		awi 
  
	PLATFORMS:	
  
		Only OS X.
    
	HISTORY:

		9/14/05		awi		Wrote it .


*/


#include "CocoaEventBridgeHelpers.h"

void GetPathToBundleString(char **fPath);


InitializeCocoaProc				InitializeCocoa = NULL;
OpenGetCharWindowProc			OpenGetCharWindow= NULL;
CloseGetCharWindowProc			CloseGetCharWindow = NULL;
MakeGetCharWindowVisibleProc			MakeGetCharWindowVisible = NULL;
MakeGetCharWindowInvisibleProc	MakeGetCharWindowInvisible = NULL;
StartKeyGatheringProc			StartKeyGathering = NULL;
StopKeyGatheringProc			StopKeyGathering = NULL;
MakeKeyWindowProc				MakeKeyWindow = NULL;
RevertKeyWindowProc				RevertKeyWindow = NULL;
CopyReadKeypressListProc		CopyReadKeypressList = NULL;
CopyPeekKeypressListProc		CopyPeekKeypressList = NULL;
CopyReadNextKeypressProc		CopyReadNextKeypress = NULL;
CopyPeekNextKeypressProc		CopyPeekNextKeypress = NULL;
ClearKeypressListProc			ClearKeypressList = NULL;
GetNumKeypressesProc			GetNumKeypresses = NULL;
IsKeyWindowProc					IsKeyWindow=NULL;

static void	LoadPrivateFrameworkBundle( CFStringRef framework, CFBundleRef *bundlePtr );
static	CFBundleRef	gStoreBitLibBundle=NULL;		//	"StoreBitLib.bundle" 

psych_bool LoadCocoaBundle(void) 
{
	static psych_bool				firstTime=TRUE;
	psych_bool						failed;
	static	psych_bool				foundAllFunctions;
	char						*localPathToCocoaBundleStr;
	CFStringRef					localPathToCocoaBundle;

	if(firstTime){
		firstTime=FALSE;
		GetPathToBundleString(&localPathToCocoaBundleStr);
		if(!strcmp(localPathToCocoaBundleStr, "")) {
                        mexPrintf("CEB: GetPathToBundleString() failed!");
			return(TRUE);
                }
		localPathToCocoaBundle= CFStringCreateWithBytes(kCFAllocatorDefault, 
														localPathToCocoaBundleStr, 
														strlen(localPathToCocoaBundleStr), 
														kCFStringEncodingUTF8,
														FALSE);
		LoadPrivateFrameworkBundle( localPathToCocoaBundle, &gStoreBitLibBundle );
		CFRelease(localPathToCocoaBundle);
//		LoadPrivateFrameworkBundle( CFSTR("/GetCharWindow.bundle"), &gStoreBitLibBundle );
		if ( gStoreBitLibBundle != NULL ) {
			InitializeCocoa	= (InitializeCocoaProc) CFBundleGetFunctionPointerForName(gStoreBitLibBundle, CFSTR("InitializeCocoa") );
                    if(InitializeCocoa != NULL)
				InitializeCocoa();
                    else {
                        mexPrintf("CEB: CFBundleGetFunctionPointerForName('InitializeCocoa') failed!");
                        return(TRUE);
                    }
			OpenGetCharWindow= (OpenGetCharWindowProc)CFBundleGetFunctionPointerForName(gStoreBitLibBundle, CFSTR("OpenGetCharWindow"));
			CloseGetCharWindow= (CloseGetCharWindowProc)CFBundleGetFunctionPointerForName(gStoreBitLibBundle, CFSTR("CloseGetCharWindow"));
			MakeGetCharWindowVisible= (MakeGetCharWindowVisibleProc)CFBundleGetFunctionPointerForName(gStoreBitLibBundle, CFSTR("MakeGetCharWindowVisible"));
			MakeGetCharWindowInvisible= (MakeGetCharWindowInvisibleProc)CFBundleGetFunctionPointerForName(gStoreBitLibBundle, CFSTR("MakeGetCharWindowInvisible"));
			StartKeyGathering= (StartKeyGatheringProc)CFBundleGetFunctionPointerForName(gStoreBitLibBundle, CFSTR("StartKeyGathering"));
			StopKeyGathering= (StopKeyGatheringProc)CFBundleGetFunctionPointerForName(gStoreBitLibBundle, CFSTR("StopKeyGathering"));
			MakeKeyWindow= (MakeKeyWindowProc)CFBundleGetFunctionPointerForName(gStoreBitLibBundle, CFSTR("MakeKeyWindow"));
			RevertKeyWindow= (RevertKeyWindowProc)CFBundleGetFunctionPointerForName(gStoreBitLibBundle, CFSTR("RevertKeyWindow"));
			CopyReadKeypressList= (CopyReadKeypressListProc)CFBundleGetFunctionPointerForName(gStoreBitLibBundle, CFSTR("CopyReadKeypressList"));
			CopyPeekKeypressList= (CopyPeekKeypressListProc)CFBundleGetFunctionPointerForName(gStoreBitLibBundle, CFSTR("CopyPeekKeypressList"));
			CopyReadNextKeypress= (CopyReadNextKeypressProc)CFBundleGetFunctionPointerForName(gStoreBitLibBundle, CFSTR("CopyReadNextKeypress"));
			CopyPeekNextKeypress= (CopyPeekNextKeypressProc)CFBundleGetFunctionPointerForName(gStoreBitLibBundle, CFSTR("CopyPeekNextKeypress"));
			ClearKeypressList= (ClearKeypressListProc)CFBundleGetFunctionPointerForName(gStoreBitLibBundle, CFSTR("ClearKeypressList"));
			GetNumKeypresses= (GetNumKeypressesProc)CFBundleGetFunctionPointerForName(gStoreBitLibBundle, CFSTR("GetNumKeypresses"));
			IsKeyWindow= (IsKeyWindowProc)CFBundleGetFunctionPointerForName(gStoreBitLibBundle, CFSTR("IsKeyWindow"));
		}else {
                    mexPrintf("CEB: LoadPrivateFrameworkBundle(%s) failed!", localPathToCocoaBundleStr);
                    return(TRUE);
                }
                
		foundAllFunctions= OpenGetCharWindow && CloseGetCharWindow && MakeGetCharWindowVisible && MakeGetCharWindowInvisible && StartKeyGathering && StopKeyGathering 
							&& MakeKeyWindow && CopyReadKeypressList && CopyPeekKeypressList && CopyReadNextKeypress 
							&& CopyPeekNextKeypress && ClearKeypressList && GetNumKeypresses && IsKeyWindow && RevertKeyWindow;
                if (!foundAllFunctions) mexPrintf("CEB: Could not retrieve function pointers to all bundle-functions!");
	}
	return(!foundAllFunctions);
}

		


/*
	LoadPrivateFrameworkBundle
	
	Modified from Apple's version to accept an absolute path in posix style.  Apple's version assumed
	that the private bundled framework resided within the calling application's bundle.  We want this to
	work with MATLAB which is not distributed as an application bundle.
*/ 
static void	LoadPrivateFrameworkBundle( CFStringRef framework, CFBundleRef *bundlePtr )
{
	CFURLRef	baseURL			= NULL;
	CFURLRef	bundleURL		= NULL;
	CFBundleRef	myAppsBundle	= NULL;
	
	if ( bundlePtr == NULL )	goto Bail;
	*bundlePtr = NULL;
	
	myAppsBundle	= CFBundleGetMainBundle();					//	Get our application's main bundle from Core Foundation
	if ( myAppsBundle == NULL )	goto Bail;
	
	baseURL	= CFBundleCopyPrivateFrameworksURL( myAppsBundle );
	if ( baseURL == NULL )	goto Bail;

/*
	bundleURL = CFURLCreateCopyAppendingPathComponent( kCFAllocatorSystemDefault, baseURL, framework, false );
	if ( bundleURL == NULL )	goto Bail;
*/
	//replaces commented out section above.  This function now accepts a posix path to bundle.  
	bundleURL = CFURLCreateWithFileSystemPath (kCFAllocatorSystemDefault, framework, kCFURLPOSIXPathStyle, FALSE);
	if ( bundleURL == NULL )	goto Bail;

	*bundlePtr = CFBundleCreate( kCFAllocatorSystemDefault, bundleURL );
	if ( *bundlePtr == NULL )	goto Bail;

	if ( ! CFBundleLoadExecutable( *bundlePtr ) )
	{
		CFRelease( *bundlePtr );
		*bundlePtr	= NULL;
	}

Bail:															// Clean up.
	if ( bundleURL != NULL )	CFRelease( bundleURL );
	if ( baseURL != NULL )		CFRelease( baseURL );
}