2004-08-02 21:50:17 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2002 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Author: Banlu Kemiyatorn <object at gmail dot com>
|
|
|
|
|
|
|
|
This file is part of GNUstep.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
2007-10-29 23:25:10 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
2004-08-02 21:50:17 +00:00
|
|
|
License as published by the Free Software Foundation; either
|
2008-06-10 04:12:46 +00:00
|
|
|
version 2 of the License, or (at your option) any later version.
|
2007-10-29 23:25:10 +00:00
|
|
|
|
2004-08-02 21:50:17 +00:00
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2007-10-29 23:25:10 +00:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; see the file COPYING.LIB.
|
|
|
|
If not, see <http://www.gnu.org/licenses/> or write to the
|
|
|
|
Free Software Foundation, 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02110-1301, USA.
|
2004-08-02 21:50:17 +00:00
|
|
|
*/
|
|
|
|
|
2011-07-03 23:45:22 +00:00
|
|
|
#include <AppKit/NSImage.h>
|
|
|
|
#include <AppKit/NSGraphics.h>
|
2007-04-27 12:23:28 +00:00
|
|
|
#include "x11/XGServer.h"
|
|
|
|
#include "x11/XGServerWindow.h"
|
2004-08-02 21:50:17 +00:00
|
|
|
#include "cairo/XGCairoSurface.h"
|
2005-07-27 23:25:32 +00:00
|
|
|
#include <cairo-xlib.h>
|
2004-08-02 21:50:17 +00:00
|
|
|
|
|
|
|
#define GSWINDEVICE ((gswindow_device_t *)gsDevice)
|
|
|
|
|
|
|
|
@implementation XGCairoSurface
|
|
|
|
|
|
|
|
- (id) initWithDevice: (void *)device
|
|
|
|
{
|
2005-08-28 00:48:12 +00:00
|
|
|
Display *dpy;
|
|
|
|
Drawable drawable;
|
2007-09-02 14:23:03 +00:00
|
|
|
Visual* visual;
|
|
|
|
XWindowAttributes attributes;
|
2005-08-28 00:48:12 +00:00
|
|
|
|
2004-08-02 21:50:17 +00:00
|
|
|
gsDevice = device;
|
2005-07-27 23:25:32 +00:00
|
|
|
|
2005-08-28 00:48:12 +00:00
|
|
|
dpy = GSWINDEVICE->display;
|
|
|
|
if (GSWINDEVICE->type != NSBackingStoreNonretained)
|
|
|
|
{
|
|
|
|
drawable = GSWINDEVICE->buffer;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
drawable = GSWINDEVICE->ident;
|
|
|
|
}
|
|
|
|
|
2004-08-02 21:50:17 +00:00
|
|
|
/*
|
|
|
|
if (GSWINDEVICE->type != NSBackingStoreNonretained)
|
|
|
|
{
|
|
|
|
GSWINDEVICE->gdriverProtocol |= GDriverHandlesExpose;
|
|
|
|
XSetWindowBackgroundPixmap(GSWINDEVICE->display,
|
|
|
|
GSWINDEVICE->ident,
|
|
|
|
GSWINDEVICE->buffer);
|
|
|
|
}
|
|
|
|
*/
|
2007-09-02 14:23:03 +00:00
|
|
|
|
|
|
|
if (!XGetWindowAttributes (dpy, GSWINDEVICE->ident, &attributes))
|
|
|
|
{
|
|
|
|
visual = DefaultVisual (dpy, DefaultScreen (dpy));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
visual = attributes.visual;
|
|
|
|
}
|
|
|
|
|
2005-08-28 00:48:12 +00:00
|
|
|
_surface = cairo_xlib_surface_create(dpy,
|
2007-09-02 14:23:03 +00:00
|
|
|
drawable,
|
|
|
|
visual,
|
|
|
|
GSWINDEVICE->xframe.size.width,
|
|
|
|
GSWINDEVICE->xframe.size.height);
|
|
|
|
|
2004-08-02 21:50:17 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSSize) size
|
|
|
|
{
|
|
|
|
return GSWINDEVICE->xframe.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
2011-07-03 23:45:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
@implementation XGServer (ScreenCapture)
|
|
|
|
|
|
|
|
- (NSImage *) contentsOfScreen: (int)screen inRect: (NSRect)rect
|
|
|
|
{
|
|
|
|
Window win;
|
|
|
|
XWindowAttributes attrs;
|
|
|
|
|
|
|
|
win = [self xDisplayRootWindowForScreen: screen];
|
|
|
|
|
|
|
|
if (XGetWindowAttributes(dpy, win, &attrs))
|
|
|
|
{
|
2011-07-03 23:59:35 +00:00
|
|
|
NSInteger width = rect.size.width;
|
|
|
|
NSInteger height = rect.size.height;
|
2011-07-03 23:45:22 +00:00
|
|
|
NSImage *result;
|
|
|
|
NSBitmapImageRep *bmp;
|
|
|
|
cairo_surface_t *src, *dest;
|
|
|
|
|
2011-07-03 23:59:35 +00:00
|
|
|
// Convert rect to flipped coordinates
|
|
|
|
rect.origin.y = attrs.height - NSMaxY(rect);
|
|
|
|
|
2011-07-03 23:45:22 +00:00
|
|
|
bmp = [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
|
2011-07-03 23:59:35 +00:00
|
|
|
pixelsWide: width
|
|
|
|
pixelsHigh: height
|
2011-07-03 23:45:22 +00:00
|
|
|
bitsPerSample: 8
|
|
|
|
samplesPerPixel: 4
|
|
|
|
hasAlpha: YES
|
|
|
|
isPlanar: NO
|
|
|
|
colorSpaceName: NSDeviceRGBColorSpace
|
|
|
|
bitmapFormat: 0
|
|
|
|
bytesPerRow: 0
|
|
|
|
bitsPerPixel: 32] autorelease];
|
|
|
|
|
|
|
|
src = cairo_xlib_surface_create(dpy, win, attrs.visual, attrs.width, attrs.height);
|
2011-07-03 23:59:35 +00:00
|
|
|
dest = cairo_image_surface_create_for_data([bmp bitmapData], CAIRO_FORMAT_ARGB32, width, height, [bmp bytesPerRow]);
|
2011-07-03 23:45:22 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
cairo_t *cr = cairo_create(dest);
|
2011-07-03 23:59:35 +00:00
|
|
|
cairo_set_source_surface(cr, src, -1 * rect.origin.x, -1 * rect.origin.y);
|
2011-07-03 23:45:22 +00:00
|
|
|
cairo_paint(cr);
|
|
|
|
cairo_destroy(cr);
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_surface_destroy(src);
|
|
|
|
cairo_surface_destroy(dest);
|
|
|
|
|
|
|
|
// Convert BGRA to RGBA
|
|
|
|
{
|
|
|
|
NSInteger stride;
|
|
|
|
NSInteger x, y;
|
|
|
|
unsigned char *cdata;
|
|
|
|
|
|
|
|
stride = [bmp bytesPerRow];
|
|
|
|
cdata = [bmp bitmapData];
|
|
|
|
|
2011-07-03 23:59:35 +00:00
|
|
|
for (y = 0; y < height; y++)
|
2011-07-03 23:45:22 +00:00
|
|
|
{
|
2011-07-03 23:59:35 +00:00
|
|
|
for (x = 0; x < width; x++)
|
2011-07-03 23:45:22 +00:00
|
|
|
{
|
|
|
|
NSInteger i = (y * stride) + (x * 4);
|
|
|
|
unsigned char d = cdata[i];
|
|
|
|
|
|
|
|
#if GS_WORDS_BIGENDIAN
|
|
|
|
cdata[i] = cdata[i + 1];
|
|
|
|
cdata[i + 1] = cdata[i + 2];
|
|
|
|
cdata[i + 2] = cdata[i + 3];
|
|
|
|
cdata[i + 3] = d;
|
|
|
|
#else
|
|
|
|
cdata[i] = cdata[i + 2];
|
|
|
|
//cdata[i + 1] = cdata[i + 1];
|
|
|
|
cdata[i + 2] = d;
|
|
|
|
//cdata[i + 3] = cdata[i + 3];
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-03 23:59:35 +00:00
|
|
|
result = [[[NSImage alloc] initWithSize: NSMakeSize(width, height)] autorelease];
|
2011-07-03 23:45:22 +00:00
|
|
|
[result addRepresentation: bmp];
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|