diff options
author | das <das> | 2009-06-29 14:35:01 (GMT) |
---|---|---|
committer | das <das> | 2009-06-29 14:35:01 (GMT) |
commit | 48e7eef4f3d5e1fc60e9f9a16f38d611ee607d9e (patch) | |
tree | 81b5743d2ff5d45f2ee4992153e4f34949ea8f8a /macosx/tkMacOSXXStubs.c | |
parent | 5761930634125f376eb3221b265c4f4267944e0d (diff) | |
download | tk-48e7eef4f3d5e1fc60e9f9a16f38d611ee607d9e.zip tk-48e7eef4f3d5e1fc60e9f9a16f38d611ee607d9e.tar.gz tk-48e7eef4f3d5e1fc60e9f9a16f38d611ee607d9e.tar.bz2 |
Merge of TkAqua Cocoa port
Diffstat (limited to 'macosx/tkMacOSXXStubs.c')
-rw-r--r-- | macosx/tkMacOSXXStubs.c | 380 |
1 files changed, 219 insertions, 161 deletions
diff --git a/macosx/tkMacOSXXStubs.c b/macosx/tkMacOSXXStubs.c index e188b16..f3b3cc1 100644 --- a/macosx/tkMacOSXXStubs.c +++ b/macosx/tkMacOSXXStubs.c @@ -7,19 +7,20 @@ * eventually be moved into other files. * * Copyright (c) 1995-1997 Sun Microsystems, Inc. - * Copyright 2001, Apple Computer, Inc. - * Copyright (c) 2005-2007 Daniel A. Steffen <das@users.sourceforge.net> + * Copyright 2001-2009, Apple Inc. + * Copyright (c) 2005-2009 Daniel A. Steffen <das@users.sourceforge.net> * - * See the file "license.terms" for information on usage and redistribution of - * this file, and for a DISCLAIMER OF ALL WARRANTIES. + * See the file "license.terms" for information on usage and redistribution + * of this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tkMacOSXXStubs.c,v 1.31 2008/12/20 01:33:09 das Exp $ + * RCS: @(#) $Id: tkMacOSXXStubs.c,v 1.32 2009/06/29 14:35:01 das Exp $ */ #include "tkMacOSXPrivate.h" #include "tkMacOSXEvent.h" #include <IOKit/IOKitLib.h> +#include <IOKit/hidsystem/IOHIDShared.h> /* * Because this file is still under major development Debugger statements are @@ -33,6 +34,9 @@ #define ROOT_ID 10 +CGFloat tkMacOSXZeroScreenHeight = 0; +CGFloat tkMacOSXZeroScreenTop = 0; + /* * Declarations of static variables used in this file. */ @@ -56,13 +60,8 @@ static int DefaultErrorHandler(Display *display, static int DestroyImage(XImage *image); static unsigned long ImageGetPixel(XImage *image, int x, int y); -static int PutPixel(XImage *image, int x, int y, +static int ImagePutPixel(XImage *image, int x, int y, unsigned long pixel); -#if 0 -static XImage * SubImage(XImage *image, int x, int y, - unsigned int width, unsigned int height); -static int AddPixel(XImage *image, long value); -#endif /* *---------------------------------------------------------------------- @@ -85,38 +84,35 @@ void TkMacOSXDisplayChanged( Display *display) { - GDHandle graphicsDevice; Screen *screen; - Rect bounds = {0, 0, 0, 0}, *maxBounds; + NSArray *nsScreens; + if (display == NULL || display->screens == NULL) { return; } screen = display->screens; - graphicsDevice = GetMainDevice(); - screen->root_depth = (*(*graphicsDevice)->gdPMap)->cmpSize * - (*(*graphicsDevice)->gdPMap)->cmpCount; - screen->height = (*graphicsDevice)->gdRect.bottom - - (*graphicsDevice)->gdRect.top; - screen->width = (*graphicsDevice)->gdRect.right - - (*graphicsDevice)->gdRect.left; - - screen->mwidth = (screen->width * 254 + 360) / 720; - screen->mheight = (screen->height * 254 + 360) / 720; - - maxBounds = (Rect *) screen->ext_data; - *maxBounds = bounds; - graphicsDevice = GetDeviceList(); - while (graphicsDevice) { - OSStatus err; - - err = ChkErr(GetAvailableWindowPositioningBounds, graphicsDevice, - &bounds); - if (err == noErr) { - UnionRect(&bounds, maxBounds, maxBounds); + nsScreens = [NSScreen screens]; + if (nsScreens && [nsScreens count]) { + NSScreen *s = [nsScreens objectAtIndex:0]; + NSRect bounds = [s frame], visible = [s visibleFrame]; + NSRect maxBounds = NSZeroRect; + + tkMacOSXZeroScreenHeight = bounds.size.height; + tkMacOSXZeroScreenTop = tkMacOSXZeroScreenHeight - + (visible.origin.y + visible.size.height); + + screen->root_depth = NSBitsPerPixelFromDepth([s depth]); + screen->width = bounds.size.width; + screen->height = bounds.size.height; + screen->mwidth = (bounds.size.width * 254 + 360) / 720; + screen->mheight = (bounds.size.height * 254 + 360) / 720; + + for (s in nsScreens) { + maxBounds = NSUnionRect(maxBounds, [s visibleFrame]); } - graphicsDevice = GetNextDevice(graphicsDevice); + *((NSRect *)screen->ext_data) = maxBounds; } } @@ -144,7 +140,10 @@ TkpOpenDisplay( Display *display; Screen *screen; int fd = 0; - static Rect maxBounds = {0, 0, 0, 0}; + static NSRect maxBounds = {{0, 0}, {0, 0}}; + static char vendor[25] = ""; + NSArray *cgVers; + NSAutoreleasePool *pool; if (gMacDisplay != NULL) { if (strcmp(gMacDisplay->display->display_name, display_name) == 0) { @@ -168,11 +167,23 @@ TkpOpenDisplay( display->default_screen = 0; display->display_name = (char *) macScreenName; - Gestalt(gestaltQuickdrawVersion, (long *) &display->proto_minor_version); - display->proto_major_version = 10; - display->proto_minor_version -= gestaltMacOSXQD; - display->vendor = (char*) "Apple"; - Gestalt(gestaltSystemVersion, (long *) &display->release); + pool = [NSAutoreleasePool new]; + cgVers = [[[NSBundle bundleWithIdentifier:@"com.apple.CoreGraphics"] + objectForInfoDictionaryKey:@"CFBundleShortVersionString"] + componentsSeparatedByString:@"."]; + if ([cgVers count] >= 2) { + display->proto_major_version = [[cgVers objectAtIndex:1] integerValue]; + } + if ([cgVers count] >= 3) { + display->proto_minor_version = [[cgVers objectAtIndex:2] integerValue]; + } + if (!vendor[0]) { + snprintf(vendor, sizeof(vendor), "Apple AppKit %s %g", + ([NSGarbageCollector defaultCollector] ? "GC" : "RR"), + NSAppKitVersionNumber); + } + display->vendor = vendor; + Gestalt(gestaltSystemVersion, (SInt32 *) &display->release); /* * These screen bits never change @@ -207,6 +218,7 @@ TkpOpenDisplay( bzero(gMacDisplay, sizeof(TkDisplay)); gMacDisplay->display = display; + [pool drain]; return gMacDisplay; } @@ -274,7 +286,7 @@ TkClipCleanup( * needed. */ - TkSuspendClipboard(); + [NSApp tkProvidePasteboard:dispPtr]; if (dispPtr->clipWindow != NULL) { Tk_DeleteSelHandler(dispPtr->clipWindow, dispPtr->clipboardAtom, @@ -407,14 +419,11 @@ XGetGeometry( *border_width_return = winPtr->changes.border_width; *depth_return = Tk_Depth(winPtr); } else { - Rect boundsRect; - CGrafPtr destPort = TkMacOSXGetDrawablePort(d); - - GetPortBounds(destPort, &boundsRect); - *x_return = boundsRect.left; - *y_return = boundsRect.top; - *width_return = boundsRect.right - boundsRect.left; - *height_return = boundsRect.bottom - boundsRect.top; + CGSize size = ((MacDrawable *) d)->size; + *x_return = 0; + *y_return = 0; + *width_return = size.width; + *height_return = size.height; *border_width_return = 0; *depth_return = 32; } @@ -449,7 +458,7 @@ XBell( Display* display, int percent) { - SysBeep(percent); + NSBeep(); } #if 0 @@ -692,7 +701,7 @@ XSetClipRectangles( while (n--) { XRectangle rect = *rectangles; - + rect.x += clip_x_origin; rect.y += clip_y_origin; TkUnionRectWithRegion(&rect, clipRgn, clipRgn); @@ -729,17 +738,20 @@ TkGetServerInfo( Tk_Window tkwin) /* Token for window; this selects a particular * display and server. */ { - char buffer[8 + TCL_INTEGER_SPACE * 2]; - char buffer2[TCL_INTEGER_SPACE]; + char buffer[5 + TCL_INTEGER_SPACE * 2]; + char buffer2[11 + TCL_INTEGER_SPACE]; - sprintf(buffer, "QD%dR%x ", ProtocolVersion(Tk_Display(tkwin)), + snprintf(buffer, sizeof(buffer), "CG%d.%d ", + ProtocolVersion(Tk_Display(tkwin)), ProtocolRevision(Tk_Display(tkwin))); - sprintf(buffer2, " %x", VendorRelease(Tk_Display(tkwin))); + snprintf(buffer2, sizeof(buffer2), " Mac OS X %x", + VendorRelease(Tk_Display(tkwin))); Tcl_AppendResult(interp, buffer, ServerVendor(Tk_Display(tkwin)), buffer2, NULL); } #pragma mark XImage handling + /* *---------------------------------------------------------------------- * @@ -815,7 +827,7 @@ XCreateImage( ximage->f.create_image = NULL; ximage->f.destroy_image = DestroyImage; ximage->f.get_pixel = ImageGetPixel; - ximage->f.put_pixel = PutPixel; + ximage->f.put_pixel = ImagePutPixel; ximage->f.sub_image = NULL; ximage->f.add_pixel = NULL; @@ -850,37 +862,45 @@ XGetImage( unsigned long plane_mask, int format) { + MacDrawable *macDraw = (MacDrawable *) d; XImage * imagePtr = NULL; Pixmap pixmap = (Pixmap) NULL; - Tk_Window win = (Tk_Window) ((MacDrawable *) d)->winPtr; + Tk_Window win = (Tk_Window) macDraw->winPtr; GC gc; + char * data = NULL; int depth = 32; int offset = 0; - int bitmap_pad = 32; + int bitmap_pad = 0; int bytes_per_line = 0; - if (TkMacOSXGetDrawablePort(d)) { - if (format == ZPixmap) { - if (width > 0 && height > 0) { - /* - * Tk_GetPixmap fails for zero width or height. - */ + if (format == ZPixmap) { + if (width > 0 && height > 0) { + /* + * Tk_GetPixmap fails for zero width or height. + */ - pixmap = Tk_GetPixmap(display, d, width, height, depth); - } - if (win) { - XGCValues values; + pixmap = Tk_GetPixmap(display, d, width, height, depth); + } + if (win) { + XGCValues values; - gc = Tk_GetGC(win, 0, &values); - } else { - gc = XCreateGC(display, pixmap, 0, NULL); - } - if (pixmap) { - XCopyArea(display, d, pixmap, gc, x, y, width, height, 0, 0); + gc = Tk_GetGC(win, 0, &values); + } else { + gc = XCreateGC(display, pixmap, 0, NULL); + } + if (pixmap) { + CGContextRef context; + + XCopyArea(display, d, pixmap, gc, x, y, width, height, 0, 0); + context = ((MacDrawable *) pixmap)->context; + if (context) { + data = CGBitmapContextGetData(context); + bytes_per_line = CGBitmapContextGetBytesPerRow(context); } + } + if (data) { imagePtr = XCreateImage(display, NULL, depth, format, offset, - (char *) TkMacOSXGetDrawablePort(pixmap), - width, height, bitmap_pad, bytes_per_line); + data, width, height, bitmap_pad, bytes_per_line); /* * Track Pixmap underlying the XImage in the unused obdata field @@ -888,14 +908,14 @@ XGetImage( */ imagePtr->obdata = (XPointer) pixmap; - if (!win) { - XFreeGC(display, gc); - } - } else { - TkpDisplayWarning( - "XGetImage: only ZPixmap types are implemented", - "XGetImage Failure"); + } else if (pixmap) { + Tk_FreePixmap(display, pixmap); } + if (!win) { + XFreeGC(display, gc); + } + } else { + TkMacOSXDbgMsg("Invalid image format"); } return imagePtr; } @@ -953,38 +973,57 @@ ImageGetPixel( int x, int y) { - CGrafPtr destPort, savePort; - Boolean portChanged; - RGBColor cPix; - unsigned long r, g, b, c; - - destPort = (CGrafPtr) image->data; - portChanged = QDSwapPort(destPort, &savePort); - GetCPixel(x, y, &cPix); - if (image->obdata) { - /* - * Image from XGetImage, 16 bit color values. - */ - - r = (cPix . red) >> 8; - g = (cPix . green) >> 8; - b = (cPix . blue) >> 8; - } else { - r = cPix . red; - g = cPix . green; - b = cPix . blue; - } - c = (r<<16)|(g<<8)|(b); - if (portChanged) { - QDSwapPort(savePort, NULL); + unsigned char r = 0, g = 0, b = 0; + + if (image && image->data) { + unsigned char *srcPtr = ((unsigned char*) image->data) + + (y * image->bytes_per_line) + + (((image->xoffset + x) * image->bits_per_pixel) / NBBY); + + switch (image->bits_per_pixel) { + case 32: { + r = (*((unsigned int*) srcPtr) >> 16) & 0xff; + g = (*((unsigned int*) srcPtr) >> 8) & 0xff; + b = (*((unsigned int*) srcPtr) ) & 0xff; + /*if (image->byte_order == LSBFirst) { + r = srcPtr[2]; g = srcPtr[1]; b = srcPtr[0]; + } else { + r = srcPtr[1]; g = srcPtr[2]; b = srcPtr[3]; + }*/ + break; + } + case 16: + r = (*((unsigned short*) srcPtr) >> 7) & 0xf8; + g = (*((unsigned short*) srcPtr) >> 2) & 0xf8; + b = (*((unsigned short*) srcPtr) << 3) & 0xf8; + break; + case 8: + r = (*srcPtr << 2) & 0xc0; + g = (*srcPtr << 4) & 0xc0; + b = (*srcPtr << 6) & 0xc0; + r |= r >> 2 | r >> 4 | r >> 6; + g |= g >> 2 | g >> 4 | g >> 6; + b |= b >> 2 | b >> 4 | b >> 6; + break; + case 4: { + unsigned char c = (x % 2) ? *srcPtr : (*srcPtr >> 4); + r = (c & 0x04) ? 0xff : 0; + g = (c & 0x02) ? 0xff : 0; + b = (c & 0x01) ? 0xff : 0; + break; + } + case 1: + r = g = b = ((*srcPtr) & (0x80 >> (x % 8))) ? 0xff : 0; + break; + } } - return c; + return (PIXEL_MAGIC << 24) | (r << 16) | (g << 8) | b; } /* *---------------------------------------------------------------------- * - * PutPixel -- + * ImagePutPixel -- * * Set a single pixel in an image. * @@ -998,64 +1037,53 @@ ImageGetPixel( */ static int -PutPixel( +ImagePutPixel( XImage *image, int x, int y, unsigned long pixel) { - CGrafPtr destPort, savePort; - Boolean portChanged; - RGBColor cPix; - unsigned long r, g, b; - - destPort = (CGrafPtr)image->data; - portChanged = QDSwapPort(destPort, &savePort); - r = (pixel & image->red_mask)>>16; - g = (pixel & image->green_mask)>>8; - b = (pixel & image->blue_mask); - if (image->obdata) { - /* - * Image from XGetImage, 16 bit color values. - */ - - cPix.red = r << 8; - cPix.green = g << 8; - cPix.blue = b << 8; - } else { - cPix.red = r; - cPix.green = g; - cPix.blue = b; - } - SetCPixel(x, y, &cPix); - if (portChanged) { - QDSwapPort(savePort, NULL); + if (image && image->data) { + unsigned char r = ((pixel & image->red_mask) >> 16) & 0xff; + unsigned char g = ((pixel & image->green_mask) >> 8) & 0xff; + unsigned char b = ((pixel & image->blue_mask) ) & 0xff; + unsigned char *dstPtr = ((unsigned char*) image->data) + + (y * image->bytes_per_line) + + (((image->xoffset + x) * image->bits_per_pixel) / NBBY); + + switch (image->bits_per_pixel) { + case 32: + *((unsigned int*) dstPtr) = (0xff << 24) | (r << 16) | + (g << 8) | b; + /*if (image->byte_order == LSBFirst) { + dstPtr[3] = 0xff; dstPtr[2] = r; dstPtr[1] = g; dstPtr[0] = b; + } else { + dstPtr[0] = 0xff; dstPtr[1] = r; dstPtr[2] = g; dstPtr[3] = b; + }*/ + break; + case 16: + *((unsigned short*) dstPtr) = ((r & 0xf8) << 7) | + ((g & 0xf8) << 2) | ((b & 0xf8) >> 3); + break; + case 8: + *dstPtr = ((r & 0xc0) >> 2) | ((g & 0xc0) >> 4) | + ((b & 0xc0) >> 6); + break; + case 4: { + unsigned char c = ((r & 0x80) >> 5) | ((g & 0x80) >> 6) | + ((b & 0x80) >> 7); + *dstPtr = (x % 2) ? ((*dstPtr & 0xf0) | (c & 0x0f)) : + ((*dstPtr & 0x0f) | ((c << 4) & 0xf0)); + break; + } + case 1: + *dstPtr = ((r|g|b) & 0x80) ? (*dstPtr | (0x80 >> (x % 8))) : + (*dstPtr & ~(0x80 >> (x % 8))); + break; + } } return 0; } - -#if 0 -static XImage * -SubImage( - XImage *image, - int x, - int y, - unsigned int width, - unsigned int height) -{ - Debugger(); - return NULL; -} - -static int -AddPixel( - XImage *image, - long value) -{ - Debugger(); - return 0; -} -#endif /* *---------------------------------------------------------------------- @@ -1281,5 +1309,35 @@ void Tk_ResetUserInactiveTime( Display *dpy) { - UpdateSystemActivity(UsrActivity); + IOGPoint loc; + kern_return_t kr; + NXEvent nullEvent = {NX_NULLEVENT, {0, 0}, 0, -1, 0}; + enum { kNULLEventPostThrottle = 10 }; + static io_connect_t io_connection = MACH_PORT_NULL; + + if (io_connection == MACH_PORT_NULL) { + io_service_t service = IOServiceGetMatchingService( + kIOMasterPortDefault, IOServiceMatching(kIOHIDSystemClass)); + + if (service == MACH_PORT_NULL) { + return; + } + kr = IOServiceOpen(service, mach_task_self(), kIOHIDParamConnectType, + &io_connection); + IOObjectRelease(service); + if (kr != KERN_SUCCESS) { + return; + } + } + kr = IOHIDPostEvent(io_connection, NX_NULLEVENT, loc, &nullEvent.data, + FALSE, 0, FALSE); } + +/* + * Local Variables: + * mode: c + * c-basic-offset: 4 + * fill-column: 79 + * coding: utf-8 + * End: + */ |