forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PTYWindow.m
401 lines (343 loc) · 13.2 KB
/
PTYWindow.m
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/* -*- mode:objc -*- */
/* $Id: PTYWindow.m,v 1.17 2008-09-24 22:35:39 yfabian Exp $ */
/* Incorporated into iTerm.app by Ujwal S. Setlur */
/*
** PTYWindow.m
**
** Copyright (c) 2002, 2003
**
** Author: Fabian, Ujwal S. Setlur
** Initial code by Kiichi Kusama
**
** Project: iTerm
**
** Description: NSWindow subclass. Implements transparency.
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#import "iTerm.h"
#import "PTYWindow.h"
#import "PreferencePanel.h"
#import "PseudoTerminal.h"
#import "FutureMethods.h"
#import "iTermController.h"
#import "iTermApplicationDelegate.h"
// This is included because the blurring code uses undocumented APIs to do its thing.
#import <CGSInternal.h>
#define DEBUG_METHOD_ALLOC 0
#define DEBUG_METHOD_TRACE 0
#define DEBUG_WINDOW_LAYOUT 0
#ifdef PSEUDOTERMINAL_VERBOSE_LOGGING
#define PtyLog NSLog
#else
#define PtyLog(args...) \
do { \
if (gDebugLogging) { \
DebugLog([NSString stringWithFormat:args]); \
} \
} while (0)
#endif
@implementation PTYWindow
- (void) dealloc
{
[restoreState_ release];
[super dealloc];
}
- initWithContentRect:(NSRect)contentRect
styleMask:(NSUInteger)aStyle
backing:(NSBackingStoreType)bufferingType
defer:(BOOL)flag;
{
#if DEBUG_METHOD_ALLOC
NSLog(@"%s: 0x%x", __PRETTY_FUNCTION__, self);
#endif
if ((self = [super initWithContentRect:contentRect
styleMask:aStyle
backing:bufferingType
defer:flag]) != nil) {
[self setAlphaValue:0.9999];
blurFilter = 0;
layoutDone = NO;
}
return self;
}
typedef CGError CGSSetWindowBackgroundBlurRadiusFunction(CGSConnectionID cid, CGSWindowID wid, NSUInteger blur);
static void *GetFunctionByName(NSString *library, char *func) {
CFBundleRef bundle;
CFURLRef bundleURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef) library, kCFURLPOSIXPathStyle, true);
CFStringRef functionName = CFStringCreateWithCString(kCFAllocatorDefault, func, kCFStringEncodingASCII);
bundle = CFBundleCreate(kCFAllocatorDefault, bundleURL);
void *f = NULL;
if (bundle) {
f = CFBundleGetFunctionPointerForName(bundle, functionName);
CFRelease(bundle);
}
CFRelease(functionName);
CFRelease(bundleURL);
return f;
}
static CGSSetWindowBackgroundBlurRadiusFunction* GetCGSSetWindowBackgroundBlurRadiusFunction() {
static BOOL tried = NO;
static CGSSetWindowBackgroundBlurRadiusFunction *function = NULL;
if (!tried) {
function = GetFunctionByName(@"/System/Library/Frameworks/ApplicationServices.framework",
"CGSSetWindowBackgroundBlurRadius");
tried = YES;
}
return function;
}
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
// This gives a warning, but this method won't be called except in 10.7 where this
// method does exist in our superclass. The only way to avoid the warning
// is to do some really gnarly stuff. See here for more:
// http://www.cocoabuilder.com/archive/cocoa/214903-using-performselector-on-super.html
[super encodeRestorableStateWithCoder:coder];
[coder encodeObject:restoreState_ forKey:@"ptyarrangement"];
}
- (void)setRestoreState:(NSObject *)restoreState {
[restoreState_ autorelease];
restoreState_ = [restoreState retain];
}
- (void)enableBlur:(double)radius
{
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
// Only works in Leopard (or hopefully later)
if (!OSX_LEOPARDORLATER) return;
const double kEpsilon = 0.001;
if (blurFilter && fabs(blurRadius_ - radius) < kEpsilon) {
return;
}
CGSConnectionID con = CGSMainConnectionID();
if (!con) {
return;
}
CGSSetWindowBackgroundBlurRadiusFunction* function = GetCGSSetWindowBackgroundBlurRadiusFunction();
if (IsLionOrLater() && function) {
// If CGSSetWindowBackgroundBlurRadius() is available (10.6 and up) use it because it works
// right in ExposŽ.
function(con, [self windowNumber], (int)radius);
} else {
// Fall back to 10.5-only method.
if (CGSNewCIFilterByName(con, (CFStringRef)@"CIGaussianBlur", &blurFilter)) {
return;
}
NSDictionary *optionsDict = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:radius] forKey:@"inputRadius"];
CGSSetCIFilterValuesFromDictionary(con, blurFilter, (CFDictionaryRef)optionsDict);
CGSAddWindowFilter(con, [self windowNumber], blurFilter, kCGWindowFilterUnderlay);
}
blurRadius_ = radius;
#endif
}
- (void)disableBlur
{
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
//only works in Leopard (or hopefully later)
if (!OSX_LEOPARDORLATER) return;
CGSConnectionID con = CGSMainConnectionID();
if (!con) {
return;
}
CGSSetWindowBackgroundBlurRadiusFunction* function = GetCGSSetWindowBackgroundBlurRadiusFunction();
if (IsLionOrLater() && function) {
function(con, [self windowNumber], 0);
} else if (blurFilter) {
CGSRemoveWindowFilter(con, (CGSWindowID)[self windowNumber], blurFilter);
CGSReleaseCIFilter(CGSMainConnectionID(), blurFilter);
blurFilter = 0;
}
#endif
}
- (id<PTYWindowDelegateProtocol>)ptyDelegate {
return (id<PTYWindowDelegateProtocol>)[self delegate];
}
- (void)toggleFullScreen:(id)sender
{
if (![[self ptyDelegate] lionFullScreen] &&
![[PreferencePanel sharedInstance] lionStyleFullscreen]) {
// The user must have clicked on the toolbar arrow, but the pref is set
// to use traditional fullscreen.
[[self delegate] performSelector:@selector(toggleTraditionalFullScreenMode)
withObject:nil];
} else {
// This is a way of calling [super toggleFullScreen:] that doesn't give a warning if
// the method doesn't exist (it's new in 10.7) but we build against 10.5 sdk.
IMP functionPointer = [NSWindow instanceMethodForSelector:_cmd];
isTogglingLionFullScreen_ = true;
functionPointer(self, _cmd, sender);
isTogglingLionFullScreen_ = false;
}
}
- (BOOL)isTogglingLionFullScreen
{
return isTogglingLionFullScreen_;
}
- (int)screenNumber
{
return [[[[self screen] deviceDescription] objectForKey:@"NSScreenNumber"] intValue];
}
- (void)smartLayout
{
PtyLog(@"enter smartLayout");
NSEnumerator* iterator;
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
CGSWorkspaceID currentSpace = -1; // Valid only before 10.8 Mountain Lion.
CGSConnectionID con;
if (!IsMountainLionOrLater()) {
con = CGSMainConnectionID();
if (!con) {
PtyLog(@"CGSMainConnectionID failed");
return;
}
CGSGetWorkspace(con, ¤tSpace);
}
#endif
int currentScreen = [self screenNumber];
NSRect screenRect = [[self screen] visibleFrame];
// Get a list of relevant windows, same screen & workspace
NSMutableArray* windows = [[NSMutableArray alloc] init];
iterator = [[[iTermController sharedInstance] terminals] objectEnumerator];
PseudoTerminal* term;
PtyLog(@"Begin iterating over terminals");
while ((term = [iterator nextObject])) {
PTYWindow* otherWindow = (PTYWindow*)[term window];
PtyLog(@"See window %@ at %@", otherWindow, [NSValue valueWithRect:[otherWindow frame]]);
if (otherWindow == self) {
PtyLog(@" skip - is self");
continue;
}
int otherScreen = [otherWindow screenNumber];
if (otherScreen != currentScreen) {
PtyLog(@" skip - screen %d vs my %d", otherScreen, currentScreen);
continue;
}
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
if (IsMountainLionOrLater()) {
// CGSGetWindowWorkspace broke in 10.8.
if (![otherWindow isOnActiveSpace]) {
PtyLog(@" skip - not in active space");
continue;
}
} else {
CGSWorkspaceID otherSpace = -1;
CGSGetWindowWorkspace(con, [otherWindow windowNumber], &otherSpace);
if (otherSpace != currentSpace) {
PtyLog(@" skip - different space %d vs my %d", otherSpace, currentSpace);
continue;
}
}
#endif
PtyLog(@" add window to array of windows");
[windows addObject:otherWindow];
}
// Find the spot on screen with the lowest window intersection
float bestIntersect = INFINITY;
NSRect bestFrame = [self frame];
NSRect placementRect = NSMakeRect(
screenRect.origin.x,
screenRect.origin.y,
MAX(1, screenRect.size.width-[self frame].size.width),
MAX(1, screenRect.size.height-[self frame].size.height)
);
PtyLog(@"PlacementRect is %@", [NSValue valueWithRect:placementRect]);
for(int x = 0; x < placementRect.size.width/2; x += 50) {
for(int y = 0; y < placementRect.size.height/2; y += 50) {
PtyLog(@"Try coord %d,%d", x, y);
NSRect testRects[4] = {[self frame]};
// Top Left
testRects[0].origin.x = placementRect.origin.x + x;
testRects[0].origin.y = placementRect.origin.y + placementRect.size.height - y;
// Top Right
testRects[1] = testRects[0];
testRects[1].origin.x = placementRect.origin.x + placementRect.size.width - x;
// Bottom Left
testRects[2] = testRects[0];
testRects[2].origin.y = placementRect.origin.y + y;
// Bottom Right
testRects[3] = testRects[1];
testRects[3].origin.y = placementRect.origin.y + y;
for (int i = 0; i < sizeof(testRects)/sizeof(NSRect); i++) {
PtyLog(@"compute badness of test rect %d %@", i, [NSValue valueWithRect:testRects[i]]);
iterator = [windows objectEnumerator];
PTYWindow* other;
float badness = 0.0f;
while ((other = [iterator nextObject])) {
NSRect otherFrame = [other frame];
NSRect intersection = NSIntersectionRect(testRects[i], otherFrame);
badness += intersection.size.width * intersection.size.height;
PtyLog(@"badness of %@ is %.2f", other, intersection.size.width * intersection.size.height);
}
const char const * names[] = {"TL", "TR", "BL", "BR"};
PtyLog(@"%s: testRect:%@, bad:%.2f",
names[i], NSStringFromRect(testRects[i]), badness);
if (badness < bestIntersect) {
PtyLog(@"This is the best coordinate found so far");
bestIntersect = badness;
bestFrame = testRects[i];
}
// Shortcut if we've found an empty spot
if (bestIntersect == 0) {
PtyLog(@"zero badness. Done.");
goto end;
}
}
}
}
end:
[windows release];
PtyLog(@"set frame to %@", [NSValue valueWithPoint:bestFrame.origin]);
[super setFrameOrigin:bestFrame.origin];
}
- (void)setLayoutDone
{
PtyLog(@"setLayoutDone %@", [NSThread callStackSymbols]);
layoutDone = YES;
}
- (void)makeKeyAndOrderFront:(id)sender
{
PtyLog(@"PTYWindow makeKeyAndOrderFront: layoutDone=%d %@", (int)layoutDone, [NSThread callStackSymbols]);
if (!layoutDone) {
PtyLog(@"try to call windowWillShowInitial");
[self setLayoutDone];
if ([[self delegate] respondsToSelector:@selector(windowWillShowInitial)]) {
[[self delegate] performSelector:@selector(windowWillShowInitial)];
} else {
PtyLog(@"delegate %@ does not respond", [self delegate]);
}
}
PtyLog(@"PTYWindow - calling makeKeyAndOrderFont, which triggers a window resize");
PtyLog(@"The current window frame is %fx%f", [self frame].size.width, [self frame].size.height);
[super makeKeyAndOrderFront:sender];
}
- (void)toggleToolbarShown:(id)sender
{
#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[PTYWindow toggleToolbarShown]",
__FILE__, __LINE__);
#endif
id delegate = [self delegate];
// Let our delegate know
if([delegate conformsToProtocol: @protocol(PTYWindowDelegateProtocol)])
[delegate windowWillToggleToolbarVisibility: self];
[super toggleToolbarShown: sender];
// Let our delegate know
if([delegate conformsToProtocol: @protocol(PTYWindowDelegateProtocol)])
[delegate windowDidToggleToolbarVisibility: self];
}
- (BOOL)canBecomeKeyWindow
{
return YES;
}
@end