2003/09/19 05:26:47
[org.ibex.core.git] / src / org / xwt / plat / X11.cc
index 9fd2ac3..2d146e6 100644 (file)
@@ -1,8 +1,8 @@
 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [LGPL]
 // see below for copyright information on the second portion of this file
 
-#include <sys/ipc.h>
-#include <sys/shm.h>
+#include "POSIX.cc"
+
 #include <X11/Xlib.h>
 #include <X11/extensions/XShm.h>
 #include <X11/keysymdef.h>
 #include <X11/Xutil.h>
 #include <X11/Xatom.h>
 #include <X11/Xmu/StdCmap.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <gcj/cni.h>
-#include <signal.h>
 
 #include <java/lang/String.h>
 #include <org/xwt/Surface.h>
@@ -25,7 +19,7 @@
 #include <org/xwt/plat/X11.h>
 #include <org/xwt/plat/X11$X11Surface.h>
 #include <org/xwt/plat/X11$X11Picture.h>
-#include <org/xwt/plat/X11$X11DoubleBuffer.h>
+#include <org/xwt/plat/X11$X11PixelBuffer.h>
 #include <org/xwt/util/Semaphore.h>
 #include <org/xwt/Platform.h>
 #include <java/lang/Long.h>
@@ -41,7 +35,7 @@ static Colormap s_colormap;
 static XStandardColormap* colormap_info;
 static XShmSegmentInfo shm_info;
 static Window selectionWindow;
-static int shm_supported;
+static int shm_supported = 0;
 static int shm_pixmaps_supported;
 static int screen_num;
 static int colorDepth = 0;
@@ -51,7 +45,7 @@ static int shm_size = 0;
 #define min(a, b) ((a) < (b) ? (a) : (b))
 #define max(a, b) ((a) < (b) ? (b) : (a))
 
-// X11DoubleBuffer //////////////////////////////////////////////////////////////////////
+// X11PixelBuffer //////////////////////////////////////////////////////////////////////
 
 // ensures that the shared memory is at least size bytes; if not, allocates 3/2 * size
 static void ensureShmSize(int size) {
@@ -72,7 +66,7 @@ static void ensureShmSize(int size) {
     }
 }
 
-void org::xwt::plat::X11$X11DoubleBuffer::fastDrawPicture(org::xwt::Picture* s,
+void org::xwt::plat::X11$X11PixelBuffer::fastDrawPicture(org::xwt::Picture* s,
                                                             jint dx1, jint dy1, jint dx2, jint dy2, jint sx1, jint sy1, jint sx2, jint sy2) {
     org::xwt::plat::X11$X11Picture* source = (org::xwt::plat::X11$X11Picture*)s;
 
@@ -94,7 +88,7 @@ void org::xwt::plat::X11$X11DoubleBuffer::fastDrawPicture(org::xwt::Picture* s,
     XCopyArea(display, *((Pixmap*)source->doublebuf->pm), (*((Pixmap*)pm)), (*((GC*)clipped_gc)), sx1, sy1, sx2 - sx1, sy2 - sy1, dx1, dy1);
 }
 
-void org::xwt::plat::X11$X11DoubleBuffer::slowDrawPicture(org::xwt::Picture* s,
+void org::xwt::plat::X11$X11PixelBuffer::slowDrawPicture(org::xwt::Picture* s,
                                                             jint dx1, jint dy1, jint dx2, jint dy2, jint sx1, jint sy1, jint sx2, jint sy2) {
 
     org::xwt::plat::X11$X11Picture* source = (org::xwt::plat::X11$X11Picture*)s;
@@ -139,7 +133,7 @@ void org::xwt::plat::X11$X11DoubleBuffer::slowDrawPicture(org::xwt::Picture* s,
                 int targetpixel;
                 switch (xi->bits_per_pixel) {
                 case 8: targetpixel = (int)(*current_pixel); break;
-                case 16: targetpixel = (int)(*((u_int16_t*)current_pixel)); break;
+                case 16: targetpixel = (int)(*((uint16_t*)current_pixel)); break;
                 case 24: targetpixel = (((int)*current_pixel) << 16) | (((int)*(current_pixel + 1)) << 8) | (((int)*(current_pixel + 2))); break;
                 case 32: targetpixel = *((int*)current_pixel); break;
                 default: org::xwt::Platform::criticalAbort(JvNewStringLatin1("ERROR: bpp not a multiple of 8!"));
@@ -162,21 +156,21 @@ void org::xwt::plat::X11$X11DoubleBuffer::slowDrawPicture(org::xwt::Picture* s,
             red = ((source_red * colormap_info->red_max * alpha) + (red * 0xFF * (0xFF - alpha))) / (0xFF * 0xFF);
             green = ((source_green * colormap_info->green_max * alpha) + (green * 0xFF * (0xFF - alpha))) / (0xFF * 0xFF);
             blue = ((source_blue * colormap_info->blue_max * alpha) + (blue * 0xFF * (0xFF - alpha))) / (0xFF * 0xFF);
-            u_int32_t destpixel = red * colormap_info->red_mult + green * colormap_info->green_mult +
+            uint32_t destpixel = red * colormap_info->red_mult + green * colormap_info->green_mult +
                 blue * colormap_info->blue_mult + colormap_info->base_pixel;
 
             switch (xi->bits_per_pixel) {
             case 8: *current_pixel = (char)(destpixel & 0xFF); break;
-            case 16: *((u_int16_t*)current_pixel) = (u_int16_t)destpixel; break;
+            case 16: *((uint16_t*)current_pixel) = (uint16_t)destpixel; break;
             case 24: {
                 int offset = (int)current_pixel & 0x3;
-                u_int64_t dest = ((u_int64_t)destpixel) << (8 * offset);
-                u_int64_t mask = ((u_int64_t)0xffffff) << (8 * offset);
-                u_int64_t* base = (u_int64_t*)(current_pixel - offset);
+                uint64_t dest = ((uint64_t)destpixel) << (8 * offset);
+                uint64_t mask = ((uint64_t)0xffffff) << (8 * offset);
+                uint64_t* base = (uint64_t*)(current_pixel - offset);
                 *base = (*base & ~mask) | dest;
                 break;
             }
-            case 32: *((u_int32_t*)current_pixel) = destpixel; break;
+            case 32: *((uint32_t*)current_pixel) = destpixel; break;
             default: org::xwt::Platform::criticalAbort(JvNewStringLatin1("ERROR: bpp not a multiple of 8!"));
             }
         }
@@ -195,7 +189,7 @@ void org::xwt::plat::X11$X11DoubleBuffer::slowDrawPicture(org::xwt::Picture* s,
     }
 }
 
-void org::xwt::plat::X11$X11DoubleBuffer::finalize() {
+void org::xwt::plat::X11$X11PixelBuffer::finalize() {
     if (shared_pixmap) {
         XShmSegmentInfo *sinfo = (XShmSegmentInfo*)shm_segment;
         XShmDetach(display, sinfo);
@@ -213,7 +207,7 @@ void org::xwt::plat::X11$X11DoubleBuffer::finalize() {
     XFreeGC(display, *((GC*)clipped_gc));
 }
 
-void org::xwt::plat::X11$X11DoubleBuffer::natInit() {
+void org::xwt::plat::X11$X11PixelBuffer::natInit() {
     
     if (width == 0 || height == 0) return;
     shared_pixmap &= shm_supported & shm_pixmaps_supported; // refuse to use shared pixmaps if we don't have shm
@@ -246,7 +240,7 @@ void org::xwt::plat::X11$X11DoubleBuffer::natInit() {
     XChangeGC(display, (*((GC*)clipped_gc)), GCGraphicsExposures, &vm);
 }    
 
-void org::xwt::plat::X11$X11DoubleBuffer::createStipple(org::xwt::plat::X11$X11Picture* xpi) {
+void org::xwt::plat::X11$X11PixelBuffer::createStipple(org::xwt::plat::X11$X11Picture* xpi) {
 
     stipple = (gnu::gcj::RawData*)malloc(sizeof(Pixmap));
     (*((Pixmap*)stipple)) = XCreatePixmap(display, RootWindow(display, screen_num), width, height, 1);
@@ -279,13 +273,13 @@ void org::xwt::plat::X11$X11DoubleBuffer::createStipple(org::xwt::plat::X11$X11P
     XPutImage(display, (*((Pixmap*)stipple)), stipple_gc, &xi, 0, 0, 0, 0, width, height);
 }
 
-void org::xwt::plat::X11$X11Surface::blit(org::xwt::DoubleBuffer* db, jint sx, jint sy, jint dx, jint dy, jint dx2, jint dy2) {
-    org::xwt::plat::X11$X11DoubleBuffer *xdb = (org::xwt::plat::X11$X11DoubleBuffer*)db;
+void org::xwt::plat::X11$X11Surface::blit(org::xwt::PixelBuffer* db, jint sx, jint sy, jint dx, jint dy, jint dx2, jint dy2) {
+    org::xwt::plat::X11$X11PixelBuffer *xdb = (org::xwt::plat::X11$X11DoubleBuffer*)db;
     XCopyArea(display, *((Pixmap*)xdb->pm), *((Window*)window), *((GC*)gc), sx, sy, dx2 - dx, dy2 - dy, dx, dy);
     XFlush(display);
 }
 
-void org::xwt::plat::X11$X11DoubleBuffer::fillRect (jint x, jint y, jint x2, jint y2, jint argb) {
+void org::xwt::plat::X11$X11PixelBuffer::fillRect (jint x, jint y, jint x2, jint y2, jint argb) {
     
     jint w = x2 - x;
     jint h = y2 - y;
@@ -305,7 +299,7 @@ void org::xwt::plat::X11$X11DoubleBuffer::fillRect (jint x, jint y, jint x2, jin
     XFillRectangle(display, (*((Pixmap*)pm)), (*((GC*)gc)), x, y, w, h);
 }
 
-void org::xwt::plat::X11$X11DoubleBuffer::drawString(::java::lang::String* font, ::java::lang::String* text, jint x, jint y, jint argb) {
+void org::xwt::plat::X11$X11PixelBuffer::drawString(::java::lang::String* font, ::java::lang::String* text, jint x, jint y, jint argb) {
     
     XRectangle rect;
     rect.x = clipx, rect.y = clipy; rect.width = clipw; rect.height = cliph;
@@ -340,8 +334,8 @@ void org::xwt::plat::X11$X11DoubleBuffer::drawString(::java::lang::String* font,
 
 void org::xwt::plat::X11$X11Surface::setIcon(org::xwt::Picture* pic) {
     org::xwt::plat::X11$X11Picture* p = ((org::xwt::plat::X11$X11Picture*)pic);
-    org::xwt::plat::X11$X11DoubleBuffer* old_dbuf = p->doublebuf;
-    p->buildDoubleBuffer(1);
+    org::xwt::plat::X11$X11PixelBuffer* old_dbuf = p->doublebuf;
+    p->buildPixelBuffer(1);
     XWMHints xwmh;
     memset(&xwmh, 0, sizeof(XWMHints));
     xwmh.flags |= IconPixmapHint | IconMaskHint;
@@ -583,8 +577,8 @@ void org::xwt::plat::X11$X11Surface::dispatchEvent(gnu::gcj::RawData* ev) {
         int x_out, y_out;
         XConfigureEvent* xce = (XConfigureEvent*)(e);
         XTranslateCoordinates(display, (*((Window*)window)), RootWindow(display, screen_num), 0, 0, &x_out, &y_out, &child);
-        if (xce->width != width || xce->height != height) SizeChange(xce->width, xce->height);
-        if (x_out != root->abs(0) || y_out != root->abs(1)) PosChange(x_out, y_out);
+        if (xce->width != root->width || xce->height != root->height) SizeChange(xce->width, xce->height);
+        if (x_out != root->x || y_out != root->y) PosChange(x_out, y_out);
         
     }
 }
@@ -706,12 +700,19 @@ void org::xwt::plat::X11::natInit() {
     if (!XInitThreads())
         org::xwt::Platform::criticalAbort(JvNewStringLatin1("Your X11 libraries do not support multithreaded programs"));
 
-    display = XOpenDisplay(NULL);
+    char* DISPLAY = getenv("DISPLAY");
+    if (DISPLAY == NULL || strlen(DISPLAY) == 0) DISPLAY = ":0.0";
+    display = XOpenDisplay(DISPLAY);
+
+    if (display == 0)
+        org::xwt::Platform::criticalAbort(JvNewStringLatin1("Unable to connect to X11 display server"));
+
     screen_num = XDefaultScreen(display);
     colorDepth = (jint)(DefaultDepth(((Display*)display), 0));
     shm_info.shmaddr = NULL;
 
-    shm_supported = (XShmQueryExtension(display) == True);
+    // FIXME: SHM doesn't work on Darwin
+    //shm_supported = (XShmQueryExtension(display) == True);
     if (shm_supported) {
         X11ErrorHandler* oldHandler = XSetErrorHandler(errorHandler);
         XShmSegmentInfo sinfo;