aboutsummaryrefslogtreecommitdiffstats
path: root/LibOVR/Src/Displays/OVR_OSX_FocusObserver.mm
blob: 3dd77a8dbd4de05d58835ad92bb2c8e1dc66a5ba (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/************************************************************************************

Filename    :   OVR_OSX_FocusObserver.mm
Content     :   Observer for app focus on OSX
Created     :   August 5, 2014
Authors     :   Jordan Tritell

Copyright   :   Copyright 2014 Oculus VR, LLC All Rights reserved.

Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
you may not use the Oculus VR Rift SDK except in compliance with the License,
which is provided at the time of installation or download, or which
otherwise accompanies this software in either electronic or hard copy form.

You may obtain a copy of the License at

http://www.oculusvr.com/licenses/LICENSE-3.2

Unless required by applicable law or agreed to in writing, the Oculus VR SDK
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*************************************************************************************/

OVR_PRIVATE_FILE

#include "OVR_OSX_FocusObserver.h"

#include "../Service/Service_NetServer.h"
#include "OVR_DisplayEnumerator.h"

#include <Cocoa/Cocoa.h>

OVR_DEFINE_SINGLETON(OVR::OSX::AppFocusObserver);

extern bool ServiceRunningFlag;

@interface FocusNotifier : NSObject <NSApplicationDelegate>{
    NSWindow *window;
}

- (void)start;

@property (assign) IBOutlet NSWindow *window;

@end


@implementation FocusNotifier

@synthesize window;

- (void) addActivationObserver
{
    //subscribe to focus notifications from the workspace
	[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
                                                           selector:@selector(activated:)
                                                               name:NSWorkspaceDidActivateApplicationNotification
                                                             object:nil];
    
    //subscribe to termination notifications from the workspace
	[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
                                                           selector:@selector(terminated:)
                                                               name:NSWorkspaceDidTerminateApplicationNotification
                                                             object:nil];
}

-(void) activated:(NSNotification *)notification
{
    NSRunningApplication *app = [[notification userInfo] objectForKey:@"NSWorkspaceApplicationKey"];
    pid_t pid = [app processIdentifier];
    OVR::OSX::AppFocusObserver::GetInstance()->OnProcessFocus(pid);
    //    NSLog(@"Activated: %@", [activatedApp bundleIdentifier]);
}

-(void) terminated:(NSNotification *)notification
{
    NSRunningApplication *app = [[notification userInfo] objectForKey:@"NSWorkspaceApplicationKey"];
    pid_t pid = [app processIdentifier];
    OVR::OSX::AppFocusObserver::GetInstance()->RemoveProcess(pid);
    //    NSLog(@"Activated: %@", [activatedApp bundleIdentifier]);
}

- (void)start
{
    //initialize with relevant variables
    [self addActivationObserver];
}

@end


namespace OVR { namespace OSX{

struct FocusNotifierImpl
{
    FocusNotifier* wrapped;
};
    
AppFocusObserver::AppFocusObserver():
    impl(new FocusNotifierImpl),
    listener(NULL)
{
    //initialize with correct values
    impl->wrapped = [[FocusNotifier alloc] init];
    [impl->wrapped start];
    ActiveProcessId = 0;
}

AppFocusObserver::~AppFocusObserver()
{
    [impl->wrapped dealloc];
    delete impl;
}

void AppFocusObserver::SetListener(Service::NetServerListener *_listener)
{
    listener = _listener;
}

void AppFocusObserver::OnSystemDestroy()
{
}

void AppFocusObserver::OnProcessFocus(pid_t pid)
{
    // If the process changed,
    if (pid != LastProcessId)
    {
        LastProcessId = pid;
        
        Lock::Locker locker(&ListLock);
        
        // Find the process id in the list
        const int count = AppList.GetSizeI();
        for (int i = 0; i < count; ++i)
        {
            // If it is a rift process,
            if (AppList[i] == pid)
            {
                onAppFocus(pid);
                OVR_DEBUG_LOG(("[AppFocusObserver] Oculus Process getting focus: pid=%d", pid));
                return;
           }
        }
        
        OVR_DEBUG_LOG(("[AppFocusObserver] Focus change: %d (non-Oculus process)", pid));
    }
}

void AppFocusObserver::AddProcess(pid_t pid)
{
    Lock::Locker locker(&ListLock);
    
    // If it already exists in the array,
    const int count = AppList.GetSizeI();
    for (int i = 0; i < count; ++i)
    {
        // If we found it,
        if (AppList[i] == pid)
        {
            return;
        }
    }
    
    // If the process being added is already in focus,
    if (pid == LastProcessId)
    {
        // Set the active process
        OVR_DEBUG_LOG(("[AppFocusObserver] AddProcess: Recognizing the newly added process as in-focus pid=%d", pid));
    }
    
    // Add it to the list
    AppList.PushBack(pid);
}

void AppFocusObserver::nextProcess()
{
    Lock::Locker locker(&ListLock);
    
    int count = AppList.GetSizeI();
    
    // Pick the next available rift process
    if (count > 0)
    {
        ActiveProcessId = AppList[0];
        OVR_DEBUG_LOG(("[AppFocusObserver] NextProcess: Switching active rift process to pid=%d", ActiveProcessId));
        onAppFocus(ActiveProcessId);
        return;
    }
    
    // No process to use
    onAppFocus(ActiveProcessId);
    OVR_DEBUG_LOG(("[AppFocusObserver] NextProcess: No remaining rift processes"));
}


void AppFocusObserver::onAppFocus(pid_t pid)
{
    // Note: This is not necessarily the same as the FocusState->ActiveProcessId.
    
    ActiveProcessId = pid;
    
    if (ActiveProcessId == 0) return;
    
    if (pid != LastAppFocus)
    {
        LastAppFocus = pid;
        if(listener){
            listener->onFocusChange(pid);
        }
//        FocusSubject->Call(pid);
    }
}

void AppFocusObserver::RemoveProcess(pid_t pid)
{
    Lock::Locker locker(&ListLock);
    
    // Find the pid in the app list:
    const int count = AppList.GetSizeI();
    for (int i = 0; i < count; ++i)
    {
        // If the app was found,
        if (AppList[i] == pid)
        {            
            // Remove from list here
            AppList.RemoveAtUnordered(i);
            
            // If the removed process is the active one,
            if (ActiveProcessId == pid)
            {
                OVR_DEBUG_LOG(("[AppFocusObserver] RemoveProcess: Active process going away"));
                // Find a new active process
                nextProcess();
            }
            
            break;
        }
    }
}
}} //namespace OVR::OSX