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
|
/*
* NSView subclass for Mac OS X rootless X server
*/
/* $XFree86: xc/programs/Xserver/hw/darwin/bundle/XView.m,v 1.5 2001/11/09 00:12:38 torrey Exp $ */
#include <ApplicationServices/ApplicationServices.h>
#import "XView.h"
#include "fakeBoxRec.h"
static const void *infobytes(void *info)
{
return info;
}
static unsigned long *shapeBits = NULL;
static int shapeWidth = 0;
static int shapeHeight = 0;
static void reallocShapeBits(NSSize minSize)
{
if (shapeWidth < minSize.width || shapeHeight < minSize.height) {
shapeWidth = minSize.width;
shapeHeight = minSize.height;
if (shapeBits) free(shapeBits);
shapeBits = (unsigned long *) malloc(4 * shapeWidth * shapeHeight);
}
}
@implementation XView
- (id)initWithFrame:(NSRect)aRect
{
self = [super initWithFrame:aRect];
if (!self) return nil;
mShaped = NO;
mBitsPerSample = 8;
mSamplesPerPixel = 3;
mDepth = mBitsPerSample * mSamplesPerPixel;
mBitsPerPixel = 32;
mBits = nil;
[self allocBitsForSize:aRect.size];
return self;
}
- (void)dealloc
{
if (mBits) free(mBits);
[super dealloc];
}
- (void)drawRect:(NSRect)aRect
{
// Never draw here.
}
- (BOOL)isFlipped
{
return NO;
}
- (BOOL)isOpaque
{
return !mShaped;
}
- (BOOL)acceptsFirstResponder
{
return YES;
}
- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
{
return YES;
}
- (BOOL)shouldDelayWindowOrderingForEvent:(NSEvent *)theEvent
{
return YES;
}
- (void)mouseDown:(NSEvent *)anEvent
{
// Only X is allowed to restack windows.
[NSApp preventWindowOrdering];
[[self nextResponder] mouseDown:anEvent];
}
- (void)mouseUp:(NSEvent *)anEvent
{
// Bring app to front if necessary
// Don't bring app to front in mouseDown; mousedown-mouseup is too
// long and X gets what looks like a mouse drag.
if (! [NSApp isActive]) {
[NSApp activateIgnoringOtherApps:YES];
[NSApp arrangeInFront:nil]; // fixme only bring some windows forward?
}
[[self nextResponder] mouseDown:anEvent];
}
// Reallocate bits.
// setFrame goes through here too.
- (void)setFrameSize:(NSSize)newSize
{
[self allocBitsForSize:newSize];
[super setFrameSize:newSize];
}
- (void)allocBitsForSize:(NSSize)newSize
{
if (mBits) free(mBits);
mBytesPerRow = newSize.width * mBitsPerPixel / 8;
mBits = calloc(mBytesPerRow * newSize.height, 1);
}
- (char *)bits
{
return mBits;
}
- (void)getBits:(char **)bits
rowBytes:(int *)rowBytes
depth:(int *)depth
bitsPerPixel:(int *)bpp
{
*bits = mBits;
*rowBytes = mBytesPerRow;
*depth = mDepth;
*bpp = mBitsPerPixel;
}
- (void)refreshRects:(fakeBoxRec *)rectList count:(int)count
{
if (!mShaped) {
[self lockFocus];
[self copyToScreen:rectList count:count fromTemp:NO];
} else {
[self copyToShapeBits:rectList count:count];
[self lockFocus];
[self copyToScreen:rectList count:count fromTemp:YES];
}
[[NSGraphicsContext currentContext] flushGraphics];
[self unlockFocus];
}
// eraseRects are OUTSIDE the new shape
- (void)reshapeRects:(fakeBoxRec *)eraseRects count:(int)count
{
fakeBoxRec bounds = {0, 0, [self frame].size.width,
[self frame].size.height};
if (count == 0 && !mShaped) {
[self refreshRects:&bounds count:1];
} else {
// View is shaped, or used to be shaped.
// Shaped windows never become unshaped.
// (Mac OS X 10.0.4 does not allow transparent windows
// to become opaque.)
mShaped = YES;
// Magic. 10.0.4 and 10.1 both require the alpha channel to be
// cleared explicitly. 10.0.4 additionally requires the view to
// be unlocked between this and the drawing code below.
[self lockFocus];
[[NSColor clearColor] set];
NSRectFill([self frame]);
[self unlockFocus];
// copy everything from X11 to temp
// erase eraseRects from temp
// copy everything from temp to screen
[self lockFocus];
[self copyToShapeBits:&bounds count:1];
[self eraseFromShapeBits:eraseRects count:count];
[self copyToScreen:&bounds count:1 fromTemp:YES];
[[NSGraphicsContext currentContext] flushGraphics];
[self unlockFocus];
}
}
- (void)eraseFromShapeBits:(fakeBoxRec *)rectList count:(int)count
{
unsigned long *dst = NULL; // don't assign yet
int dstWidth = 0; // don't assign yet
fakeBoxRec *r;
fakeBoxRec *end;
assert(mBitsPerPixel == 32);
reallocShapeBits([self frame].size);
dst = shapeBits;
dstWidth = shapeWidth;
for (r = rectList, end = rectList + count; r < end; r++) {
unsigned long *dstline = dst + dstWidth*r->y1 + r->x1;
int h = r->y2 - r->y1;
while (h--) {
unsigned long *dstp = dstline;
int w = r->x2 - r->x1;
while (w--) {
*dstp++ = 0x00000000;
}
dstline += dstWidth;
}
}
}
// assumes X11 bits and temp bits are 32-bit
- (void)copyToShapeBits:(fakeBoxRec *)rectList count:(int)count
{
unsigned long *src = (unsigned long *) mBits;
unsigned long *dst = NULL; // don't assign yet
int srcWidth = mBytesPerRow / 4;
int dstWidth = 0; // don't assign yet
fakeBoxRec *r;
fakeBoxRec *end;
assert(mBitsPerPixel == 32);
reallocShapeBits([self frame].size);
dst = shapeBits;
dstWidth = shapeWidth;
for (r = rectList, end = rectList + count; r < end; r++) {
unsigned long *srcline = src + srcWidth*r->y1 + r->x1;
unsigned long *dstline = dst + dstWidth*r->y1 + r->x1;
int h = r->y2 - r->y1;
while (h--) {
unsigned long *srcp = srcline;
unsigned long *dstp = dstline;
int w = r->x2 - r->x1;
while (w--) {
*dstp++ = *srcp++ | 0xff000000;
}
srcline += srcWidth;
dstline += dstWidth;
}
}
}
// Copy boxes to the screen from the per-window pixmaps where X draws.
// rectList is in local, X-flipped coordinates.
- (void)copyToScreen:(fakeBoxRec *)rectList count:(int)count
fromTemp:(BOOL)copyFromTemp
{
unsigned char *offsetbits;
fakeBoxRec *r;
fakeBoxRec *end;
NSRect bounds;
char *srcBits;
int bytesPerRow;
int bitsPerPixel;
int bitsPerSample;
CGImageAlphaInfo alpha;
CGContextRef destCtx = (CGContextRef)[[NSGraphicsContext currentContext]
graphicsPort];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// fixme colorspace leaks?
const CGDataProviderDirectAccessCallbacks cb = {
infobytes, NULL, NULL, NULL
};
if (copyFromTemp) {
// shapeBits assumed to be 32-bit
srcBits = (char *)shapeBits;
bytesPerRow = 4 * shapeWidth;
bitsPerPixel = 32;
bitsPerSample = 8;
alpha = kCGImageAlphaPremultipliedFirst; // premultiplied ARGB
} else {
srcBits = mBits;
bytesPerRow = mBytesPerRow;
bitsPerPixel = mBitsPerPixel;
bitsPerSample = mBitsPerSample;
alpha = kCGImageAlphaNoneSkipFirst; // xRGB
}
bounds = [self frame];
bounds.origin.x = bounds.origin.y = 0;
for (r = rectList, end = rectList + count; r < end; r++) {
NSRect nsr = {{r->x1, r->y1}, {r->x2-r->x1, r->y2-r->y1}};
CGRect destRect;
CGDataProviderRef dataProviderRef;
CGImageRef imageRef;
// Clip to window
// (bounds origin is (0,0) so it can be used in either flip)
// fixme is this necessary with pixmap-per-window?
nsr = NSIntersectionRect(nsr, bounds);
// Disallow empty rects
if (nsr.size.width <= 0 || nsr.size.height <= 0) continue;
offsetbits = srcBits + (int)(nsr.origin.y * bytesPerRow +
nsr.origin.x * bitsPerPixel/8);
// Flip r to Cocoa-flipped
nsr.origin.y = bounds.size.height - nsr.origin.y - nsr.size.height;
destRect = CGRectMake(nsr.origin.x, nsr.origin.y,
nsr.size.width, nsr.size.height);
dataProviderRef = CGDataProviderCreateDirectAccess(offsetbits,
destRect.size.height * bytesPerRow, &cb);
imageRef = CGImageCreate(destRect.size.width, destRect.size.height,
bitsPerSample, bitsPerPixel, bytesPerRow,
colorSpace, alpha, dataProviderRef, NULL,
1, kCGRenderingIntentDefault);
CGContextDrawImage(destCtx, destRect, imageRef);
CGImageRelease(imageRef);
CGDataProviderRelease(dataProviderRef);
}
}
@end
|