Ejemplo n.º 1
0
Bool
HandleRFBServerMessage()
{
  rfbServerToClientMsg msg;

  if (!ReadFromRFBServer((char *)&msg, 1))
    return False;

  switch (msg.type) {

  case rfbSetColourMapEntries:
  {
    int i;
    CARD16 rgb[3];
    XColor xc;

    if (!ReadFromRFBServer(((char *)&msg) + 1,
			   sz_rfbSetColourMapEntriesMsg - 1))
      return False;

    msg.scme.firstColour = Swap16IfLE(msg.scme.firstColour);
    msg.scme.nColours = Swap16IfLE(msg.scme.nColours);

    for (i = 0; i < msg.scme.nColours; i++) {
      if (!ReadFromRFBServer((char *)rgb, 6))
	return False;
      xc.pixel = msg.scme.firstColour + i;
      xc.red = Swap16IfLE(rgb[0]);
      xc.green = Swap16IfLE(rgb[1]);
      xc.blue = Swap16IfLE(rgb[2]);
      xc.flags = DoRed|DoGreen|DoBlue;
      XStoreColor(dpy, cmap, &xc);
    }

    break;
  }

  case rfbFramebufferUpdate:
  {
    rfbFramebufferUpdateRectHeader rect;
    int linesToRead;
    int bytesPerLine;
    int i;
    int usecs;

    if (!ReadFromRFBServer(((char *)&msg.fu) + 1,
			   sz_rfbFramebufferUpdateMsg - 1))
      return False;

    msg.fu.nRects = Swap16IfLE(msg.fu.nRects);

    for (i = 0; i < msg.fu.nRects; i++) {
      if (!ReadFromRFBServer((char *)&rect, sz_rfbFramebufferUpdateRectHeader))
	return False;

      rect.encoding = Swap32IfLE(rect.encoding);
      if (rect.encoding == rfbEncodingLastRect)
	break;

      rect.r.x = Swap16IfLE(rect.r.x);
      rect.r.y = Swap16IfLE(rect.r.y);
      rect.r.w = Swap16IfLE(rect.r.w);
      rect.r.h = Swap16IfLE(rect.r.h);

      if (rect.encoding == rfbEncodingXCursor ||
	  rect.encoding == rfbEncodingRichCursor) {
	if (!HandleCursorShape(rect.r.x, rect.r.y, rect.r.w, rect.r.h,
			      rect.encoding)) {
	  return False;
	}
	continue;
      }

      if (rect.encoding == rfbEncodingPointerPos) {
	if (!HandleCursorPos(rect.r.x, rect.r.y)) {
	  return False;
	}
	continue;
      }

      if ((rect.r.x + rect.r.w > si.framebufferWidth) ||
	  (rect.r.y + rect.r.h > si.framebufferHeight))
	{
	  fprintf(stderr,"Rect too large: %dx%d at (%d, %d)\n",
		  rect.r.w, rect.r.h, rect.r.x, rect.r.y);
	  return False;
	}

      if (rect.r.h * rect.r.w == 0) {
	fprintf(stderr,"Zero size rect - ignoring\n");
	continue;
      }

      /* If RichCursor encoding is used, we should prevent collisions
	 between framebuffer updates and cursor drawing operations. */
      SoftCursorLockArea(rect.r.x, rect.r.y, rect.r.w, rect.r.h);

      switch (rect.encoding) {

      case rfbEncodingRaw:

	bytesPerLine = rect.r.w * myFormat.bitsPerPixel / 8;
	linesToRead = BUFFER_SIZE / bytesPerLine;

	while (rect.r.h > 0) {
	  if (linesToRead > rect.r.h)
	    linesToRead = rect.r.h;

	  if (!ReadFromRFBServer(buffer,bytesPerLine * linesToRead))
	    return False;

	  CopyDataToScreen(buffer, rect.r.x, rect.r.y, rect.r.w,
			   linesToRead);

	  rect.r.h -= linesToRead;
	  rect.r.y += linesToRead;

	}
	break;

      case rfbEncodingCopyRect:
      {
	rfbCopyRect cr;

	if (!ReadFromRFBServer((char *)&cr, sz_rfbCopyRect))
	  return False;

	cr.srcX = Swap16IfLE(cr.srcX);
	cr.srcY = Swap16IfLE(cr.srcY);

	/* If RichCursor encoding is used, we should extend our
	   "cursor lock area" (previously set to destination
	   rectangle) to the source rectangle as well. */
	SoftCursorLockArea(cr.srcX, cr.srcY, rect.r.w, rect.r.h);

	if (appData.copyRectDelay != 0) {
	  XFillRectangle(dpy, desktopWin, srcGC, cr.srcX, cr.srcY,
			 rect.r.w, rect.r.h);
#if VNC_CAPTURE
      myxfillrec(dpy, srcGC, cr.srcX,  cr.srcY, rect.r.w, rect.r.h);
#endif
	  XFillRectangle(dpy, desktopWin, dstGC, rect.r.x, rect.r.y,
			 rect.r.w, rect.r.h);
#if VNC_CAPTURE
      myxfillrec(dpy, dstGC, cr.srcX,  cr.srcY, rect.r.w, rect.r.h);
#endif
	  XSync(dpy,False);
	  usleep(appData.copyRectDelay * 1000);
	  XFillRectangle(dpy, desktopWin, dstGC, rect.r.x, rect.r.y,
			 rect.r.w, rect.r.h);
#if VNC_CAPTURE
      myxfillrec(dpy, dstGC, rect.r.x,  rect.r.y, rect.r.w, rect.r.h);
#endif
	  XFillRectangle(dpy, desktopWin, srcGC, cr.srcX, cr.srcY,
			 rect.r.w, rect.r.h);
#if VNC_CAPTURE
      myxfillrec(dpy, srcGC, cr.srcX,  cr.srcY, rect.r.w, rect.r.h);
#endif
	}

	XCopyArea(dpy, desktopWin, desktopWin, gc, cr.srcX, cr.srcY,
		  rect.r.w, rect.r.h, rect.r.x, rect.r.y);
#if VNC_CAPTURE
    myxcparea(dpy, gc, cr.srcX,  cr.srcY, rect.r.w, rect.r.h,rect.r.x,rect.r.y);
#endif

	break;
      }

      case rfbEncodingRRE:
      {
	switch (myFormat.bitsPerPixel) {
	case 8:
	  if (!HandleRRE8(rect.r.x,rect.r.y,rect.r.w,rect.r.h))
	    return False;
	  break;
	case 16:
	  if (!HandleRRE16(rect.r.x,rect.r.y,rect.r.w,rect.r.h))
	    return False;
	  break;
	case 32:
	  if (!HandleRRE32(rect.r.x,rect.r.y,rect.r.w,rect.r.h))
	    return False;
	  break;
	}
	break;
      }

      case rfbEncodingCoRRE:
      {
	switch (myFormat.bitsPerPixel) {
	case 8:
	  if (!HandleCoRRE8(rect.r.x,rect.r.y,rect.r.w,rect.r.h))
	    return False;
	  break;
	case 16:
	  if (!HandleCoRRE16(rect.r.x,rect.r.y,rect.r.w,rect.r.h))
	    return False;
	  break;
	case 32:
	  if (!HandleCoRRE32(rect.r.x,rect.r.y,rect.r.w,rect.r.h))
	    return False;
	  break;
	}
	break;
      }

      case rfbEncodingHextile:
      {
	switch (myFormat.bitsPerPixel) {
	case 8:
	  if (!HandleHextile8(rect.r.x,rect.r.y,rect.r.w,rect.r.h))
	    return False;
	  break;
	case 16:
	  if (!HandleHextile16(rect.r.x,rect.r.y,rect.r.w,rect.r.h))
	    return False;
	  break;
	case 32:
	  if (!HandleHextile32(rect.r.x,rect.r.y,rect.r.w,rect.r.h))
	    return False;
	  break;
	}
	break;
      }

      case rfbEncodingZlib:
      {
	switch (myFormat.bitsPerPixel) {
	case 8:
	  if (!HandleZlib8(rect.r.x,rect.r.y,rect.r.w,rect.r.h))
	    return False;
	  break;
	case 16:
	  if (!HandleZlib16(rect.r.x,rect.r.y,rect.r.w,rect.r.h))
	    return False;
	  break;
	case 32:
	  if (!HandleZlib32(rect.r.x,rect.r.y,rect.r.w,rect.r.h))
	    return False;
	  break;
	}
	break;
     }

      case rfbEncodingTight:
      {
	switch (myFormat.bitsPerPixel) {
	case 8:
	  if (!HandleTight8(rect.r.x,rect.r.y,rect.r.w,rect.r.h))
	    return False;
	  break;
	case 16:
	  if (!HandleTight16(rect.r.x,rect.r.y,rect.r.w,rect.r.h))
	    return False;
	  break;
	case 32:
	  if (!HandleTight32(rect.r.x,rect.r.y,rect.r.w,rect.r.h))
	    return False;
	  break;
	}
	break;
      }

      default:
	fprintf(stderr,"Unknown rect encoding %d\n",
		(int)rect.encoding);
	return False;
      }

      /* Now we may discard "soft cursor locks". */
      SoftCursorUnlockScreen();
    }

#ifdef MITSHM
    /* if using shared memory PutImage, make sure that the X server has
       updated its framebuffer before we reuse the shared memory.  This is
       mainly to avoid copyrect using invalid screen contents - not sure
       if we'd need it otherwise. */

    if (appData.useShm)
      XSync(dpy, False);
#endif

    if (!SendIncrementalFramebufferUpdateRequest())
      return False;

    break;
  }

  case rfbBell:
  {
    Window toplevelWin;

    XBell(dpy, 0);

    if (appData.raiseOnBeep) {
      toplevelWin = XtWindow(toplevel);
      XMapRaised(dpy, toplevelWin);
    }

    break;
  }

  case rfbServerCutText:
  {
    if (!ReadFromRFBServer(((char *)&msg) + 1,
			   sz_rfbServerCutTextMsg - 1))
      return False;

    msg.sct.length = Swap32IfLE(msg.sct.length);

    if (serverCutText)
      free(serverCutText);

    serverCutText = malloc(msg.sct.length+1);

    if (!ReadFromRFBServer(serverCutText, msg.sct.length))
      return False;

    serverCutText[msg.sct.length] = 0;

    newServerCutText = True;

    break;
  }

  default:
    fprintf(stderr,"Unknown message type %d from VNC server\n",msg.type);
    return False;
  }

  return True;
}
Ejemplo n.º 2
0
uint QGLContext::colorIndex(const QColor& c) const
{
    Q_D(const QGLContext);
    int screen = ((XVisualInfo *)d->vi)->screen;
    QColormap colmap = QColormap::instance(screen);
    if (isValid()) {
        if (format().plane() && c == Qt::transparent) {
            return qt_transparent_pixel(((XVisualInfo *)d->vi)->visualid,
                                        ((XVisualInfo *)d->vi)->screen);
        }
        if (((XVisualInfo*)d->vi)->visualid ==
             XVisualIDFromVisual((Visual *) QX11Info::appVisual(screen)))
            return colmap.pixel(c);                // We're using QColor's cmap

        XVisualInfo *info = (XVisualInfo *) d->vi;
        CMapEntryHash *hash = cmap_handler()->cmap_hash;
        CMapEntryHash::ConstIterator it = hash->constFind(long(info->visualid)
                + (info->screen * 256));
        QCMapEntry *x = 0;
        if (it != hash->constEnd())
            x = it.value();
        if (x && !x->alloc) {                // It's a standard colormap
            int rf = (int)(((float)c.red() * (x->scmap.red_max+1))/256.0);
            int gf = (int)(((float)c.green() * (x->scmap.green_max+1))/256.0);
            int bf = (int)(((float)c.blue() * (x->scmap.blue_max+1))/256.0);
            uint p = x->scmap.base_pixel
                     + (rf * x->scmap.red_mult)
                     + (gf * x->scmap.green_mult)
                     + (bf * x->scmap.blue_mult);
            return p;
        } else {
            QMap<int, QRgb> &cmap = (*cmap_handler()->qglcmap_hash)[(long)info->visualid];

            // already in the map?
            QRgb target = c.rgb();
            QMap<int, QRgb>::Iterator it = cmap.begin();
            for (; it != cmap.end(); ++it) {
                if ((*it) == target)
                    return it.key();
            }

            // need to alloc color
            unsigned long plane_mask[2];
            unsigned long color_map_entry;
            if (!XAllocColorCells (QX11Info::display(), x->cmap, true, plane_mask, 0,
                                   &color_map_entry, 1))
                return colmap.pixel(c);

            XColor col;
            col.flags = DoRed | DoGreen | DoBlue;
            col.pixel = color_map_entry;
            col.red   = (ushort)((qRed(c.rgb()) / 255.0) * 65535.0 + 0.5);
            col.green = (ushort)((qGreen(c.rgb()) / 255.0) * 65535.0 + 0.5);
            col.blue  = (ushort)((qBlue(c.rgb()) / 255.0) * 65535.0 + 0.5);
            XStoreColor(QX11Info::display(), x->cmap, &col);

            cmap.insert(color_map_entry, target);
            return color_map_entry;
        }
    }
    return 0;
}
Ejemplo n.º 3
0
uint QGLContext::colorIndex( const QColor& c ) const
{
    int screen = ((XVisualInfo *)vi)->screen;
    if ( isValid() ) {
	if ( format().plane()
	     && c.pixel( screen ) == overlayTransparentColor().pixel( screen ) )
	    return c.pixel( screen );		// Special; don't look-up
	if ( ((XVisualInfo*)vi)->visualid ==
	     XVisualIDFromVisual( (Visual*)QPaintDevice::x11AppVisual( screen ) ) )
	    return c.pixel( screen );		// We're using QColor's cmap

	XVisualInfo *info = (XVisualInfo *) vi;
	CMapEntry *x = cmap_dict->find( (long) info->visualid + ( info->screen * 256 ) );
	if ( x && !x->alloc) {		// It's a standard colormap
	    int rf = (int)(((float)c.red() * (x->scmap.red_max+1))/256.0);
	    int gf = (int)(((float)c.green() * (x->scmap.green_max+1))/256.0);
	    int bf = (int)(((float)c.blue() * (x->scmap.blue_max+1))/256.0);
	    uint p = x->scmap.base_pixel
		     + ( rf * x->scmap.red_mult )
		     + ( gf * x->scmap.green_mult )
		     + ( bf * x->scmap.blue_mult );
	    return p;
	} else {
	    if (!qglcmap_dict) {
		qglcmap_dict = new QIntDict< QMap<int, QRgb> >;
	    }
	    QMap<int, QRgb> *cmap;
	    if ((cmap = qglcmap_dict->find((long) info->visualid)) == 0) {
		cmap = new QMap<int, QRgb>;
		qglcmap_dict->insert((long) info->visualid, cmap);
	    }

	    // already in the map?
	    QRgb target = c.rgb();
	    QMap<int, QRgb>::Iterator it = cmap->begin();
	    for (; it != cmap->end(); ++it) {
		if ((*it) == target)
		    return it.key();
	    }

	    // need to alloc color
	    unsigned long plane_mask[2];
	    unsigned long color_map_entry;
	    if (!XAllocColorCells (QPaintDevice::x11AppDisplay(), x->cmap, TRUE, plane_mask, 0,
				   &color_map_entry, 1))
		return c.pixel(screen);

	    XColor col;
	    col.flags = DoRed | DoGreen | DoBlue;
	    col.pixel = color_map_entry;
	    col.red   = (ushort)((qRed(c.rgb()) / 255.0) * 65535.0 + 0.5);
	    col.green = (ushort)((qGreen(c.rgb()) / 255.0) * 65535.0 + 0.5);
	    col.blue  = (ushort)((qBlue(c.rgb()) / 255.0) * 65535.0 + 0.5);
	    XStoreColor(QPaintDevice::x11AppDisplay(), x->cmap, &col);

	    cmap->insert(color_map_entry, target);
	    return color_map_entry;
	}
    }
    return 0;
}
Ejemplo n.º 4
0
PetscErrorCode PetscDrawSetUpColormap_Private(Display *display,int screen,Visual *visual,Colormap colormap)
{
  int            i,found;
  Colormap       defaultmap = DefaultColormap(display,screen);
  XColor         colordef;
  PetscBool      fast = PETSC_FALSE;
  PetscErrorCode ierr;

  PetscFunctionBegin;
  if (colormap) gColormap = colormap;
  else          gColormap = XCreateColormap(display,RootWindow(display,screen),visual,AllocAll);

  cmap_base = 0;

  ierr = PetscMemzero(cmap_pixvalues_used,256*sizeof(PetscBool));CHKERRQ(ierr);

  /* set the basic colors into the color map */
  for (i=0; i<PETSC_DRAW_BASIC_COLORS; i++) {
    XParseColor(display,gColormap,colornames[i],&colordef);
    /* try to allocate the color in the default-map */
    found = XAllocColor(display,defaultmap,&colordef);
    /* use it, if it it exists and is not already used in the new colormap */
    if (found && colordef.pixel < 256  && !cmap_pixvalues_used[colordef.pixel]) {
      cmap_pixvalues_used[colordef.pixel] = PETSC_TRUE;
      /* otherwise search for the next available slot */
    } else {
      while (cmap_pixvalues_used[cmap_base]) cmap_base++;
      colordef.pixel                   = cmap_base;
      cmap_pixvalues_used[cmap_base++] = PETSC_TRUE;
    }
    XStoreColor(display,gColormap,&colordef);
    gCmapping[i] = colordef.pixel;
  }

  /* set the contour colors into the colormap */
  ierr = PetscOptionsGetBool(NULL,NULL,"-draw_fast",&fast,NULL);CHKERRQ(ierr);
  if (!fast) {
    int ncolors = 256-PETSC_DRAW_BASIC_COLORS;
    unsigned char  *red,*green,*blue;
    ierr = PetscMalloc3(ncolors,&red,ncolors,&green,ncolors,&blue);CHKERRQ(ierr);
    ierr = PetscDrawUtilitySetCmap(NULL,ncolors,red,green,blue);CHKERRQ(ierr);
    for (i=0; i<ncolors; i++) {
      colordef.red   = (unsigned short)(red[i]   << 8);
      colordef.green = (unsigned short)(green[i] << 8);
      colordef.blue  = (unsigned short)(blue[i]  << 8);
      colordef.flags = DoRed|DoGreen|DoBlue;
      /* try to allocate the color in the default-map */
      found = XAllocColor(display,defaultmap,&colordef);
      /* use it, if it it exists and is not already used in the new colormap */
      if (found && colordef.pixel < 256  && !cmap_pixvalues_used[colordef.pixel]) {
        cmap_pixvalues_used[colordef.pixel] = PETSC_TRUE;
        /* otherwise search for the next available slot */
      } else {
        while (cmap_pixvalues_used[cmap_base]) cmap_base++;
        colordef.pixel                   = cmap_base;
        cmap_pixvalues_used[cmap_base++] = PETSC_TRUE;
      }
      XStoreColor(display,gColormap,&colordef);
      gCmapping[PETSC_DRAW_BASIC_COLORS + i] = colordef.pixel;
    }
    ierr = PetscFree3(red,green,blue);CHKERRQ(ierr);
  }

  ierr = PetscInfo(0,"Successfully allocated colors\n");CHKERRQ(ierr);
  PetscFunctionReturn(0);
}
Ejemplo n.º 5
0
static void graphics_subinit (void)
{
    XSetWindowAttributes wattr;
    XClassHint classhint;
    XWMHints *hints;
    unsigned long valuemask;

    dgamode = screen_is_picasso ? currprefs.gfx_pfullscreen : currprefs.gfx_afullscreen;
    dgamode = dgamode && dgaavail;

    wattr.background_pixel = /*black.pixel*/0;
    wattr.backing_store = Always;
    wattr.backing_planes = bitdepth;
    wattr.border_pixmap = None;
    wattr.border_pixel = /*black.pixel*/0;
    wattr.colormap = cmap;
    valuemask = (CWEventMask | CWBackPixel | CWBorderPixel
		 | CWBackingStore | CWBackingPlanes | CWColormap);

    if (dgamode) {
	wattr.event_mask = DGA_EVENTMASK;
	wattr.override_redirect = 1;
	valuemask |= CWOverrideRedirect;
    } else
	wattr.event_mask = EVENTMASK;

    XSync (display, 0);

    delete_win = XInternAtom(display, "WM_DELETE_WINDOW", False);
    mywin = XCreateWindow (display, rootwin, 0, 0, current_width, current_height,
			   0, bitdepth, InputOutput, vis, valuemask, &wattr);
    XSetWMProtocols (display, mywin, &delete_win, 1);
    XSync (display, 0);
    XStoreName (display, mywin, PACKAGE_NAME);
    XSetIconName (display, mywin, PACKAGE_NAME);

    /* set class hint */
    classhint.res_name  = (char *)"UAE";
    classhint.res_class = (char *)"UAEScreen";
    XSetClassHint(display, mywin, &classhint);

    hints = XAllocWMHints();
    /* Set window group leader to self to become an application
     * that can be hidden by e.g. WindowMaker.
     * Would be more useful if we could find out what the
     * (optional) GTK+ window ID is :-/ */
    hints->window_group = mywin;
    hints->flags = WindowGroupHint;
    XSetWMHints(display, mywin, hints);

    XMapRaised (display, mywin);
    XSync (display, 0);
    mygc = XCreateGC (display, mywin, 0, 0);

    if (dgamode) {
#ifdef USE_DGA_EXTENSION
	enter_dga_mode ();
	/*setuid(getuid());*/
	picasso_vidinfo.rowbytes = fb_width * picasso_vidinfo.pixbytes;
#endif
    } else {
	get_image (current_width, current_height, &ami_dinfo);
	if (screen_is_picasso) {
	    get_image (current_width, current_height, &pic_dinfo);
	    picasso_vidinfo.rowbytes = pic_dinfo.ximg->bytes_per_line;
	}
    }

    picasso_vidinfo.extra_mem = 1;

    gfxvidinfo.flush_screen = x11_flush_screen;
    gfxvidinfo.lockscr      = x11_lock;
    gfxvidinfo.unlockscr    = x11_unlock;
    

    if (need_dither) {
	gfxvidinfo.maxblocklines = 0;
	gfxvidinfo.rowbytes = gfxvidinfo.pixbytes * currprefs.gfx_width_win;
	gfxvidinfo.linemem = malloc (gfxvidinfo.rowbytes);
        gfxvidinfo.flush_line  = x11_flush_line_dither;
    } else if (! dgamode) {
	gfxvidinfo.emergmem = 0;
	gfxvidinfo.linemem = 0;
	gfxvidinfo.bufmem = (uae_u8 *)ami_dinfo.image_mem;
	gfxvidinfo.rowbytes = ami_dinfo.ximg->bytes_per_line;
	if (currprefs.x11_use_low_bandwidth) {
	    write_log ("Doing low-bandwidth output.\n");
	    gfxvidinfo.maxblocklines = 0;
	    gfxvidinfo.rowbytes = ami_dinfo.ximg->bytes_per_line;
	    gfxvidinfo.linemem = malloc (gfxvidinfo.rowbytes);

	    if (shmavail && currprefs.x11_use_mitshm) {
		switch (gfxvidinfo.pixbytes) {
		    case 4  : gfxvidinfo.flush_line = x11_flush_line_lbw_32bit_mitshm; break;
		    case 2  : gfxvidinfo.flush_line = x11_flush_line_lbw_16bit_mitshm; break;
		    default : gfxvidinfo.flush_line = x11_flush_line_lbw_8bit_mitshm;  break;
		}
	    } else {
		switch (gfxvidinfo.pixbytes) {
		    case 4  : gfxvidinfo.flush_line = x11_flush_line_lbw_32bit; break;
		    case 2  : gfxvidinfo.flush_line = x11_flush_line_lbw_16bit; break;
		    default : gfxvidinfo.flush_line = x11_flush_line_lbw_8bit;	break;
		}
	    }
	} else {
	    gfxvidinfo.maxblocklines = MAXBLOCKLINES_MAX;

	    if (shmavail && currprefs.x11_use_mitshm)
		gfxvidinfo.flush_block  = x11_flush_block_mitshm;
	    else
		gfxvidinfo.flush_block  = x11_flush_block;
	}
    }

    if (visualInfo.VI_CLASS != TrueColor && ! screen_is_picasso) {
	int i;
	for (i = 0; i < 256; i++)
	    XStoreColor (display, cmap, parsed_xcolors + i);
    }

#ifdef USE_DGA_EXTENSION
    if (dgamode) {
	dga_colormap_installed = 0;
	XF86DGAInstallColormap (display, screen, cmap2);
	XF86DGAInstallColormap (display, screen, cmap);
    }
#endif

    if (! dgamode) {
	if (!currprefs.hide_cursor)
	    XDefineCursor (display, mywin, xhairCursor);
	else
	    XDefineCursor (display, mywin, blankCursor);
	cursorOn = 1;
    }

    mousehack = !dgamode;

    if (screen_is_picasso) {
	picasso_has_invalid_lines = 0;
	picasso_invalid_start = picasso_vidinfo.height + 1;
	picasso_invalid_stop = -1;
	memset (picasso_invalid_lines, 0, sizeof picasso_invalid_lines);
    } else
	reset_drawing ();

    inwindow = 0;
    inputdevice_release_all_keys ();
    reset_hotkeys ();
}
Ejemplo n.º 6
0
Archivo: clr.c Proyecto: satish2/Learn
void
fillUpColorCube ( Rgb2Pseudo* map, Colormap cm, int nAvail, unsigned long *pixels,
				  unsigned char (*mp) [8][8][8] )
{
  int            i, j, k, pix, l;
  int            nMis = 0, maxMis = nAvail;
  int            r, g, b;
  Mismatch       *mm = alloca( maxMis* sizeof( Mismatch));
  unsigned char  d;
  XColor         xclr;

  memset( *mp, 0, 8*8*8);

  /*
   * Find the nearest values for not yet initialized cells. Note that we
   * cannot set these values directly in map->pix because it would cause
   * interference with other unset cells (the way nearestColor works), and
   * pixel values should just be computed from direct match cells
   */
  for ( i=0; i<8; i++ ){
	for ( j=0; j<8; j++ ) {
	  for ( k=0; k<8; k++ ){
		if ( (map->pix[i][j][k] == 0) && (i | j | k) ){
		  pix = nearestColor( map, i, j, k, 1);
		  (*mp)[i][j][k] = pix;

		  /*
		   * If we still have available color cells, build a sorted list of the
		   * worst mismatches (but skip dark values)
		   */
		  if ( (nAvail > 0) && (i|j|k) > 2) {
			if ( (d = (unsigned char) colorDiff( rgb8[i], rgb8[j], rgb8[k],
								map->rgb[pix].r, map->rgb[pix].g, map->rgb[pix].b)) > 50 ){
			  for ( l=0; l<nMis && mm[l].d > d ; l++ );
			  if ( l < nMis )
				memmove( mm + l+1, mm+l, (nMis - l)*sizeof( Mismatch));
			  mm[l].d = d; mm[l].i = i; mm[l].j = j; mm[l].k = k;
			  if ( nMis < maxMis )
				nMis++;
			}
		  }
		}
	  }
	}
  }

  /* if there is a mismatch list, resolve it */
  for ( l=0; l< nMis-1; l++ ) {
	r = rgb8[ mm[l].i ];
	g = rgb8[ mm[l].j ];
	b = rgb8[ mm[l].k ];

	xclr.pixel = pixels[l];
	xclr.flags = DoRed | DoGreen | DoBlue;
	xclr.red   = r << 8;
	xclr.green = g << 8;
	xclr.blue  = b << 8;
    XStoreColor( Tlk->dsp, cm, &xclr);

	map->pix [mm[l].i] [mm[l].j] [mm[l].k] = xclr.pixel;
	map->rgb[xclr.pixel].r = r;
	map->rgb[xclr.pixel].g = g;
	map->rgb[xclr.pixel].b = b;

	/* mark this cell as satisifed */
	(*mp) [mm[l].i] [mm[l].j] [mm[l].k] = 0;
  }

  /* store all still uninitialized cube cells from our temp cube of nearest values */
  for ( i=0; i<8; i++ ){
	for ( j=0; j<8; j++ ) {
	  for ( k=0; k<8; k++ ){
		if ( (pix = (*mp) [i][j][k]) )
		  map->pix[i][j][k] = pix;
	  }
	}
  }
}
Ejemplo n.º 7
0
Archivo: clr.c Proyecto: satish2/Learn
void
initColormap ( JNIEnv* env, Toolkit* Tlk, Colormap cm, Rgb2Pseudo* map )
{
  jclass   clazz;
  jfieldID fid;
  jarray   rgbRequests = 0;
  jboolean isCopy;
  jint     *jrgbs = 0;
  int      nReq = 0;
  unsigned long  pixels[MAX_REQUESTS];
  jint     req[MAX_REQUESTS];
  unsigned long  planeMasks[1];
  int      n, i, j, k, l, m, pix;
  Visual   *v = DefaultVisualOfScreen( DefaultScreenOfDisplay( Tlk->dsp));
  XColor   xclr;
  int      r, g, b;
  char     blackSeen = 0;
  unsigned char (*mp)[8][8][8] = alloca( 8*8*8 * sizeof( char));

  memset( *mp, 0, 8*8*8);

  /* get the java.awt.DefaultsRGB.RgbRequests field */
  if ( (clazz = (*env)->FindClass( env, "java/awt/Defaults")) ){
	if ( (fid = (*env)->GetStaticFieldID( env, clazz, "RgbRequests", "[I")) ){
	  if ( (rgbRequests = (*env)->GetStaticObjectField( env, clazz, fid)) ){
		jrgbs = (*env)->GetIntArrayElements( env, rgbRequests, &isCopy);
		nReq = (*env)->GetArrayLength( env, rgbRequests);

		if ( nReq > MAX_REQUESTS )
		  nReq = MAX_REQUESTS;

		memcpy( req, jrgbs, nReq * sizeof( jint));
		(*env)->ReleaseIntArrayElements( env, rgbRequests, jrgbs, JNI_ABORT);
	  }
	}
  }
  
  /*
   * Determine how many RW cells there are available. Don't try to grab
   * too many cells, since this might disturb other apps and could end up
   * in even worse results
   */
  for ( n= 10; n; n-- ) {
	if ( XAllocColorCells( Tlk->dsp, cm, False, planeMasks, 0, pixels, n) )
	  break;
  }

  xclr.red = 0; xclr.green = 0; xclr.blue = 0;
  xclr.flags = DoRed | DoGreen | DoBlue;

  /* mark all of our cells (so that we don't rely on their current values) */
  for ( i=0; i<n; i++ ){
	xclr.pixel = pixels[i];
	XStoreColor( Tlk->dsp, cm, &xclr);
  }

  /* check which of our rgb requests are already in the colormap */
  for ( l=0; l<v->map_entries; l++ ) {
	xclr.pixel = l;
	XQueryColor( Tlk->dsp, cm, &xclr);
	r = xclr.red >> 8;
	g = xclr.green >> 8;
	b = xclr.blue >> 8;

	i = JI8(r);
	j = JI8(g);
	k = JI8(b);

	if ( r | g | b ) {
	  for ( m=0; m<nReq; m++ ) {
		if ( req[m] &&
			 colorDiff( JRED(req[m]), JGREEN(req[m]), JBLUE(req[m]), r, g, b) < CLR_DIST ) {
           req[m] = 0;                 /* mark color request as satisfied */
		   (*mp)[i][j][k] = 1;         /* mark cube cell (i,j,k) as requested */
		   break;
		}
	  }
	}

	/*
	 * we start to populate the color cube as we go (XQueryColor might be expensive),
	 * but we don't overwrite an already set cell with a worse match (if we don't
	 * have a standard system colormap, there is a good chance that several
	 * colormap cells will map to the same indices of our 3/3/3 color cube). We also
	 * shouldn't overwrite requested colors with better cube-cell matches (hence the
	 * 'mp' check)
	 */
	if ( !(i|j|k) && blackSeen++ ) /* don't overwrite real black with avail cell */
	  continue;
	if ( ((*mp)[i][j][k] < 2) && (!(pix = map->pix [i][j][k]) || 
						  (colorDiff( r, g, b, rgb8[i], rgb8[j], rgb8[k]) <
						   colorDiff( map->rgb[pix].r, map->rgb[pix].g, map->rgb[pix].b,
									  rgb8[i], rgb8[j], rgb8[k]))) ) {
	  if ( (*mp)[i][j][k] ) /* prevent cube cell from being overwritten, again */
		(*mp)[i][j][k] ++;

	  map->pix [i][j][k] = l;
	  map->rgb[l].r = r;
	  map->rgb[l].g = g;
	  map->rgb[l].b = b;
	}
  }

  /* set cells of not-yet satisfied rgb requests */
  for ( i=0, j=0; (i<nReq) && (i<n); i++ ) {
	if ( req[i] ){
	  r = JRED( req[i]);
	  g = JGREEN( req[i]);
	  b = JBLUE( req[i]);

	  xclr.pixel = pixels[j++];
	  xclr.red   = r << 8;
	  xclr.green = g << 8;
	  xclr.blue  = b << 8;
	  XStoreColor( Tlk->dsp, cm, &xclr);

	  map->pix [JI8(r)] [JI8(g)] [JI8(b)] = xclr.pixel;
	  map->rgb[xclr.pixel].r = r;
	  map->rgb[xclr.pixel].g = g;
	  map->rgb[xclr.pixel].b = b;
	}
  }

  /*
   * initalize rest of cube cells by computing their nearest rgb value, optionally
   * allocating new cells for the worst mismatches (if there still are colors avail)
   */
  fillUpColorCube( map, cm, (n - j), pixels+j, mp );
}
Ejemplo n.º 8
0
/*
*+
*  Name:
*     Foreground
*
*  Purpose:
*     Demonstrate how to change the foreground of a GWM window.
*
*  Language:
*     C
*
*  Invocation:
*     foreground <window name> <colour>
*
*  Description:
*     The foreground window property is updated and the colour table
*     updated either buy changing the colour representation of entry
*     1 (if the colour table is writable) or changing the value of
*     entry 1 to point to the closest available colour.
*
*  Arguments:
*
*  Copyright:
*     Copyright (C) 1992 Science & Engineering Research Council.
*     All Rights Reserved.

*  Licence:
*     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., 51 Franklin Street,Fifth Floor, Boston, MA
*     02110-1301, USA

*  Authors:
*     DLT: David Terrett (Starlink RAL)
*     {enter_new_authors_here}
*
*  History:
*      3-APR-1992 (DLT):
*        Orignal version
*     {enter_changes_here}
*
*  Bugs:
*     The error handling is somewhat brutal!
*     {note_any_bugs_here}
*-
*/
int main(int argc, char *argv[])
{
    Display *display;                             /* display id              */
    Window win;                                   /* window id               */
    Pixmap pix;                                   /* pixmap id               */
    int status;                                   /* status                  */
    unsigned int width, height, border, depth;    /*    "        "           */
    unsigned long *table, size;                   /* colour table array      */
    int visual_class;				  /* visual class of display */
    XColor fg_color;				  /* X colour structure */

/*
**  Open the default X display
*/
    display = XOpenDisplay( NULL );

/*
**  Get the id of the specified window
*/
    status = GWM_FindWindow( display, argv[1], &win);
    if (status) GWM_Error(status);

/*
**  Get the associated colour table array
*/
    status = GWM_GetColTable( display, win, &table, &size);
    if (status) GWM_Error(status);

/*
**  Set the new foreground window property
*/
    status = GWM_SetFgCol( display, win, argv[2]);
    if (status) GWM_Error(status);

/*
**  Convert the colour specification to an XColor structure (the
**  specification must be valid because SetFgCol succeeded).
*/
    XParseColor( display, DefaultColormap( display, DefaultScreen( display )),
	argv[2], &fg_color);

/*
**  Get the visual class of the display
*/
    visual_class = DefaultVisual( display, DefaultScreen( display ) )-> class;

    if (visual_class == StaticGray || visual_class == StaticColor ||
            visual_class == TrueColor )
    {

/*
**     Colour table is static so find nearest available colour
*/
        XAllocColor( display, DefaultColormap( display,
            DefaultScreen( display )), &fg_color);
        table[1] = fg_color.pixel;

/*
**      Write the revised colour table back to the window
*/
	status = GWM_SetColTable( display, win, table, size);
    }
    else
    {

/*
**      Colour table is dynamic so store the new colour in the server's
**      colourmap.
*/
	fg_color.pixel = table[1];
	XStoreColor( display, DefaultColormap( display,
            DefaultScreen( display )), &fg_color);
     }

/*
**  Close the display
*/
    XCloseDisplay( display);
}
Ejemplo n.º 9
0
static BOOL CreateSystemPalette()
{
	int i, j,
	    SystemPaletteLoIndex0, SystemPaletteLoIndex1,
	    SystemPaletteHiIndex0, SystemPaletteHiIndex1;
	XColor xc;

	/* compute system palette index limits */
	SystemPaletteSize = 1 << pixel_numplanes;
	SystemPaletteLoStaticUse = DefaultPaletteLoStaticUse;
	SystemPaletteHiStaticUse = DefaultPaletteHiStaticUse;
	SystemPaletteLoNoStaticUse = DefaultPaletteLoNoStaticUse;
	SystemPaletteHiNoStaticUse = DefaultPaletteHiNoStaticUse;
	SystemPaletteLoStaticIndex = DefaultPaletteLoStaticIndex;
	SystemPaletteHiStaticIndex = SystemPaletteSize
		- DefaultPaletteHiStaticUse;
	SystemPaletteLoNoStaticIndex = DefaultPaletteLoNoStaticIndex;
	SystemPaletteHiNoStaticIndex = SystemPaletteSize
		- DefaultPaletteHiNoStaticUse;

	/* create system palette */
	if (!(SystemPalette = (LPPALETTEENTRY) WinMalloc(SystemPaletteSize *
		sizeof(PALETTEENTRY))))
	{
		ERRSTR((LF_WARNING,"%s: Unable to create system color palette.\n",
			"CreateSystemPalette"));
		return (FALSE);
	}

	/* initialize system palette */
	for (i = 0, j = pixel0; i < SystemPaletteSize; i++, j += pixel_mask0)
	{
		SystemPalette[i].peRed =
			xxxx_to_xx(DeviceColormapValues[j].red);
		SystemPalette[i].peGreen =
			xxxx_to_xx(DeviceColormapValues[j].green);
		SystemPalette[i].peBlue =
			xxxx_to_xx(DeviceColormapValues[j].blue);
		SystemPalette[i].peFlags = 0;
	}

	/* create system palette mapper */
	if (!CreateSystemPaletteMapper())
		return (FALSE);

	/* create system palette pixel use counter */
	if (!CreateSystemPalettePixelUse())
		return (FALSE);

	/* get system palette index limits */
	SystemPaletteLoIndex0 = SystemPaletteLoStaticIndex;
	SystemPaletteLoIndex1 = SystemPaletteLoStaticIndex
		+ SystemPaletteLoStaticUse;
	SystemPaletteHiIndex0 = SystemPaletteHiStaticIndex;
	SystemPaletteHiIndex1 = SystemPaletteHiStaticIndex
		+ SystemPaletteHiStaticUse;

	/* set system palette low static colors */
	for (i = SystemPaletteLoIndex0; i < SystemPaletteLoIndex1; i++)
	{
		/* low static colors cannot use system palette high half */
		if (i >= SystemPaletteSize / 2)
		{
			continue;
		}
		/* set system palette color */
		SystemPalette[i] =
			DefaultPalette[DefaultPaletteLoStaticIndex + i];
		/* set hardware color */
		xc.pixel = SystemPaletteMapper[i];
		xc.red = xx_to_xxxx(SystemPalette[i].peRed);
		xc.green = xx_to_xxxx(SystemPalette[i].peGreen);
		xc.blue = xx_to_xxxx(SystemPalette[i].peBlue);
		xc.flags = DoRed | DoGreen | DoBlue;
		XStoreColor(display, DeviceColormap, &xc);
	}

	/* set system palette unused colors */
	for (i = SystemPaletteLoIndex1; i < SystemPaletteHiIndex0; i++)
	{
		float rainbow;
		BYTE r, g, b;
		/* set system palette color */
		rainbow = i - SystemPaletteLoIndex1 + 1.0;
		rainbow /= SystemPaletteHiIndex0 - SystemPaletteLoIndex1 + 1.0;
		r = (BYTE) (((long)(rainbow * 0x00000100L)) % 0x0100L);
		g = (BYTE) (((long)(rainbow * 0x00010000L)) % 0x0100L);
		b = (BYTE) (((long)(rainbow * 0x01000000L)) % 0x0100L);
		SystemPalette[i].peRed = r;
		SystemPalette[i].peGreen = g;
		SystemPalette[i].peBlue = b;
		/* set hardware color */
		xc.pixel = SystemPaletteMapper[i];
		xc.red = xx_to_xxxx(SystemPalette[i].peRed);
		xc.green = xx_to_xxxx(SystemPalette[i].peGreen);
		xc.blue = xx_to_xxxx(SystemPalette[i].peBlue);
		xc.flags = DoRed | DoGreen | DoBlue;
		XStoreColor(display, DeviceColormap, &xc);
	}

	/* set system palette high static colors */
	for (i = SystemPaletteHiIndex0; i < SystemPaletteHiIndex1; i++)
	{
		/* high static colors cannot use system palette low half */
		if (i < SystemPaletteSize / 2)
		{
			continue;
		}
		/* set system palette color */
		SystemPalette[i] =
			DefaultPalette[DefaultPaletteHiStaticIndex + i
				- SystemPaletteHiIndex0];
		/* set hardware color */
		xc.pixel = SystemPaletteMapper[i];
		xc.red = xx_to_xxxx(SystemPalette[i].peRed);
		xc.green = xx_to_xxxx(SystemPalette[i].peGreen);
		xc.blue = xx_to_xxxx(SystemPalette[i].peBlue);
		xc.flags = DoRed | DoGreen | DoBlue;
		XStoreColor(display, DeviceColormap, &xc);
	}

	/* success */
	return (TRUE);

}
Ejemplo n.º 10
0
Colormap XtcwpCreateHueColormap (Display *dpy, Window win,
	float fhue, float lhue, float sat, float bright)
/*****************************************************************************
create a colormap with varying hues (user-specified) in contiguous cells
******************************************************************************
Input:
dpy		display
win		window
fhue		first hue in colormap
lhue		last hue in colormap
sat		saturation
bright		brightness
******************************************************************************
Notes:
The returned colormap is only created; the window's colormap attribute
is not changed, and the colormap is not installed by this function.
The returned colormap is a copy of the window's current colormap, but 
with varying hues (blue to red) allocated in the range of contiguous
cells determined by XA_RGB_DEFAULT_MAP.  If it does not already exist,
XA_RGB_DEFAULT_MAP will be created.
******************************************************************************
Author:  Dave Hale, Colorado School of Mines, 09/29/90
Modified:  Craig Artley, Colorado School of Mines, 11/22/93
	   Saturation, brightness, and range of hues now user-specified.
*****************************************************************************/
{
	Screen *scr=XDefaultScreenOfDisplay(dpy);
	Colormap cmap,wcmap;
	XColor color;
	XWindowAttributes wa;
	unsigned long i,ncells,npixels;
	unsigned long bpixel,epixel,pixel[4096];
	float red=0,green=0,blue=0;

	/* determine beginning and ending pixels in contiguous range */
	bpixel = XtcwpGetFirstPixel(dpy);
	epixel = XtcwpGetLastPixel(dpy);
	if (epixel<=bpixel) return None;

	/* determine window's current colormap */
	XGetWindowAttributes(dpy,win,&wa);
	wcmap = wa.colormap;

	/* create new colormap and allocate all cells read/write */
	cmap = XCreateColormap(dpy,win,DefaultVisualOfScreen(scr),AllocNone);
	ncells = CellsOfScreen(scr);
	XAllocColorCells(dpy,cmap,True,NULL,0,pixel,(unsigned int) ncells);

	/* copy color cells from window's colormap to new colormap */
	for (i=0; i<ncells; ++i) {
		if (i<bpixel || i>epixel) {
			color.pixel = i;
			XQueryColor(dpy,wcmap,&color);
			XFreeColors(dpy,cmap,&i,1,0);
			XAllocColor(dpy,cmap,&color);
		}
	}

	/* build hues in contiguous cells in new colormap */
	npixels = epixel-bpixel+1;
	for (i=0; i<npixels; ++i) {
		color.pixel = bpixel+i;
		hsvrgb(fhue+(lhue-fhue)*((float)i)/((float)(npixels-1)),sat,bright,
			&red,&green,&blue);
		color.red = 65535*red;
		color.green = 65535*green;
		color.blue = 65535*blue;
		color.flags = DoRed|DoGreen|DoBlue;
		XStoreColor(dpy,cmap,&color);
	}

	/* return colormap */
	return cmap;
}
Ejemplo n.º 11
0
Colormap XtcwpCreateSatColormap (Display *dpy, Window win,
	float fhue, float lhue, float wfrac, float bright)
/*****************************************************************************
create a colormap with varying saturations in contiguous cells
******************************************************************************
Input:
dpy		display
win		window
fhue		first hue in colormap (saturation=1)
lhue		last hue in colormap (saturation=1)
wfrac		fractional position of white within the colormap (saturation=0)
bright		brightness
******************************************************************************
Notes:
The returned colormap is only created; the window's colormap attribute
is not changed, and the colormap is not installed by this function.
The returned colormap is a copy of the window's current colormap, but 
with varying hues (blue to red) allocated in the range of contiguous
cells determined by XA_RGB_DEFAULT_MAP.  If it does not already exist,
XA_RGB_DEFAULT_MAP will be created.
******************************************************************************
Author:  Craig Artley, Colorado School of Mines, 11/22/93
*****************************************************************************/
{
	Screen *scr=XDefaultScreenOfDisplay(dpy);
	Colormap cmap,wcmap;
	XColor color;
	XWindowAttributes wa;
	unsigned long i,j,ncells,npixels,nfpixels,nlpixels;
	unsigned long bpixel,epixel,pixel[4096];
	long  ltemp;
	float red=0,green=0,blue=0;

	/* determine beginning and ending pixels in contiguous range */
	bpixel = XtcwpGetFirstPixel(dpy);
	epixel = XtcwpGetLastPixel(dpy);
	if (epixel<=bpixel) return None;

	/* determine window's current colormap */
	XGetWindowAttributes(dpy,win,&wa);
	wcmap = wa.colormap;

	/* create new colormap and allocate all cells read/write */
	cmap = XCreateColormap(dpy,win,DefaultVisualOfScreen(scr),AllocNone);
	ncells = CellsOfScreen(scr);
	XAllocColorCells(dpy,cmap,True,NULL,0,pixel,(unsigned int) ncells);

	/* copy color cells from window's colormap to new colormap */
	for (i=0; i<ncells; ++i) {
		if (i<bpixel || i>epixel) {
			color.pixel = i;
			XQueryColor(dpy,wcmap,&color);
			XFreeColors(dpy,cmap,&i,1,0);
			XAllocColor(dpy,cmap,&color);
		}
	}

	/* devide colormap into 3 regions: fhues, white, and lhues */
	npixels = epixel-bpixel+1;
	ltemp = (long) (wfrac*((float) npixels));
	nfpixels = (ltemp<0) ? 0: ltemp;
	if (nfpixels>npixels-1) nfpixels = npixels-1;
	nlpixels = npixels-nfpixels-1;

	/* pixels from fhue to just under white */
	for (i=0; i<nfpixels; ++i) {
		color.pixel = bpixel+i;
		hsvrgb(fhue,((float)(nfpixels-i))/((float) nfpixels),bright,
			&red,&green,&blue);
		color.red = 65535*red;
		color.green = 65535*green;
		color.blue = 65535*blue;
		color.flags = DoRed|DoGreen|DoBlue;
		XStoreColor(dpy,cmap,&color);
	}

	/* white pixel */
	color.pixel = bpixel+i;
	hsvrgb(fhue,0.0,bright,&red,&green,&blue);
	color.red = 65535*red;
	color.green = 65535*green;
	color.blue = 65535*blue;
	color.flags = DoRed|DoGreen|DoBlue;
	XStoreColor(dpy,cmap,&color);
	++i;

	/* pixels from just under white to lhue */
	for (j=0; j<nlpixels; ++i,++j) {
		color.pixel = bpixel+i;
		hsvrgb(lhue,((float)(j+1))/((float) nlpixels),bright,&red,&green,&blue);
		color.red = 65535*red;
		color.green = 65535*green;
		color.blue = 65535*blue;
		color.flags = DoRed|DoGreen|DoBlue;
		XStoreColor(dpy,cmap,&color);
	}

	/* return colormap */
	return cmap;
}
Ejemplo n.º 12
0
Colormap XtcwpCreateGrayColormap (Display *dpy, Window win)
/*****************************************************************************
create a colormap with a gray scale in contiguous cells
******************************************************************************
Input:
dpy		display
win		window
******************************************************************************
Notes:
The returned colormap is only created; the window's colormap attribute
is not changed, and the colormap is not installed by this function.
The returned colormap is a copy of the window's current colormap, but 
with a gray scale (black to white) allocated in the range of contiguous
cells determined by XA_RGB_DEFAULT_MAP.  If it does not already exist,
XA_RGB_DEFAULT_MAP will be created.
******************************************************************************
Author:  Dave Hale, Colorado School of Mines, 09/29/90
*****************************************************************************/
{
	Screen *scr=XDefaultScreenOfDisplay(dpy);
	Colormap cmap,wcmap;
	XColor color;
	XWindowAttributes wa;
	unsigned long i,ncells,npixels;
	unsigned long bpixel,epixel,pixel[4096];
	
	/* determine beginning and ending pixels in contiguous range */
	bpixel = XtcwpGetFirstPixel(dpy);
	epixel = XtcwpGetLastPixel(dpy);
	if (epixel<=bpixel) return None;
	
	/* determine window's current colormap */
	XGetWindowAttributes(dpy,win,&wa);
	wcmap = wa.colormap;
	
	/* create new colormap and allocate all cells read/write */
	cmap = XCreateColormap(dpy,win,DefaultVisualOfScreen(scr),AllocNone);
	ncells = CellsOfScreen(scr);
	XAllocColorCells(dpy,cmap,True,NULL,0,pixel,(unsigned int)ncells);
	
	/* copy color cells from window's colormap to new colormap */
	for (i=0; i<ncells; ++i) {
		if (i<bpixel || i>epixel) {
			color.pixel = i;
			XQueryColor(dpy,wcmap,&color);
			XFreeColors(dpy,cmap,&i,1,0);
			XAllocColor(dpy,cmap,&color);
		}
	}
	
	/* build gray scale in contiguous cells in new colormap */
	npixels = epixel-bpixel+1;
	for (i=0; i<npixels; ++i) {
		color.pixel = bpixel+i;
		color.red = (unsigned short) (65535*i/(npixels-1));
		color.green = color.red;
		color.blue = color.red;
		color.flags = DoRed|DoGreen|DoBlue;
		XStoreColor(dpy,cmap,&color);
	}
	
	/* return colormap */
	return cmap;
}
Ejemplo n.º 13
0
Colormap XtcwpCreateRGBColormap (Display *dpy, Window win)
/*****************************************************************************
create a colormap with an RGB color scale in contiguous cells
******************************************************************************
Input:
dpy		display
win		window
******************************************************************************
Notes:
The returned colormap is only created; the window's colormap attribute
is not changed, and the colormap is not installed by this function.
The returned colormap is a copy of the window's current colormap, but 
with an RGB color scale allocated in the range of contiguous cells
determined by XA_RGB_DEFAULT_MAP.  If it does not already exist,
XA_RGB_DEFAULT_MAP will be created.
******************************************************************************
Author:  Dave Hale, Colorado School of Mines, 09/29/90
*****************************************************************************/
{
	Screen *scr=XDefaultScreenOfDisplay(dpy);
	Window root=XRootWindowOfScreen(scr);
	Colormap cmap,wcmap;
	XStandardColormap scmap;
	XColor color;
	XWindowAttributes wa;
	unsigned long i,ncells,npixels;
	unsigned long bpixel,epixel,pixel[4096];
	
	/* determine beginning and ending pixels in contiguous range */
	bpixel = XtcwpGetFirstPixel(dpy);
	epixel = XtcwpGetLastPixel(dpy);
	if (epixel<=bpixel) return None;
	
	/* get standard colormap XA_RGB_DEFAULT_MAP */
	if (!XGetStandardColormap(dpy,root,&scmap,XA_RGB_DEFAULT_MAP))
		if (!XtcwpCreateRGBDefaultMap(dpy,&scmap))
			return None;
	
	/* determine window's current colormap */
	XGetWindowAttributes(dpy,win,&wa);
	wcmap = wa.colormap;
	
	/* create new colormap and allocate all cells read/write */
	cmap = XCreateColormap(dpy,win,DefaultVisualOfScreen(scr),AllocNone);
	ncells = CellsOfScreen(scr);
	XAllocColorCells(dpy,cmap,True,NULL,0,pixel,(unsigned int)ncells);
	
	/* copy color cells from window's colormap to new colormap */
	for (i=0; i<ncells; ++i) {
		if (i<bpixel || i>epixel) {
			color.pixel = i;
			XQueryColor(dpy,wcmap,&color);
			XFreeColors(dpy,cmap,&i,1,0);
			XAllocColor(dpy,cmap,&color);
		}
	}
	
	/* copy RGB color scale from XA_RGB_DEFAULT_MAP to new colormap */
	npixels = epixel-bpixel+1;
	for (i=0; i<npixels; ++i) {
		color.pixel = bpixel+i;
		XQueryColor(dpy,scmap.colormap,&color);
		XStoreColor(dpy,cmap,&color);
	}
	
	/* return colormap */
	return cmap;
}
Ejemplo n.º 14
0
Status XtcwpCreateRGBDefaultMap (Display *dpy, XStandardColormap *scmap)
/*****************************************************************************
create XA_RGB_DEFAULT_MAP property of root window if it does not already exist
******************************************************************************
Input:
dpy		display

Output:
scmap		the standard colormap structure
******************************************************************************
Notes:
This function returns 0 if the XA_RGB_DEFAULT_MAP property does not exist
and cannot be created.  At least 8 contiguous color cells must be free
in the default colormap to create the XA_RGB_DEFAULT_MAP.  If created, the
red_max, green_max, and blue_max values returned in scmap will be equal.
******************************************************************************
Author:  Dave Hale, Colorado School of Mines, 09/29/90
*****************************************************************************/
{
	Screen *scr=XDefaultScreenOfDisplay(dpy);
	Window root=XRootWindowOfScreen(scr);
	Colormap cmap;
	XColor color;
	int i,ncells;
	unsigned long npixels;
	unsigned long bpixel,epixel,pixel1,pixel2,imax,rmult,gmult,bmult;
	unsigned long pixel[4096];

	/* grab the server */
	XGrabServer(dpy);

	/* if XA_RGB_DEFAULT_MAP does not exist, then */
	if (!XGetStandardColormap(dpy,root,scmap,XA_RGB_DEFAULT_MAP)) {
		
		/* use default colormap */
		cmap = DefaultColormapOfScreen(scr);

		/* determine largest number of contiguous free color cells */
		ncells = CellsOfScreen(scr);
		while(ncells && 
			!XAllocColorCells(dpy,cmap,True,NULL,0,pixel,ncells))
			ncells--;
		
		/* determine beginning and ending pixel of contiguous cells */
		for (i=1,bpixel=epixel=pixel1=pixel2=pixel[0]; i<ncells; i++) {
			if (pixel[i]==pixel[i-1]+1)
				pixel2 = pixel[i];
			else
				pixel1 = pixel2 = pixel[i];
			if (pixel2-pixel1>=epixel-bpixel) {
				bpixel = pixel1;
				epixel = pixel2;
			}
		}
		
		/* number of pixels must be at least 8 */
		npixels = epixel-bpixel+1;
		if (npixels<8) {
			XUngrabServer(dpy);
			return 0;
		}
		
		/* force number of contiguous cells to be an integer cubed */
		for (i=2,imax=0; i*i*i<=npixels; i++,imax++);
		npixels = (imax+1)*(imax+1)*(imax+1);
		bpixel = epixel-npixels+1;
		
		/* free cells not in contiguous range */
		for (i=0; i<ncells; i++)
			if (pixel[i]<bpixel || pixel[i]>epixel)
				XFreeColors(dpy,cmap,&pixel[i],1,0);

		/* store colors in contiguous range of allocated cells */
		rmult = (imax+1)*(imax+1);
		gmult = imax+1;
		bmult = 1;
		for (i=0; i<npixels; i++) {
			color.pixel = bpixel+i;
			color.red = (unsigned short) (i/rmult);
			color.green = (unsigned short) ((i-color.red*rmult)/gmult);
			color.blue = (unsigned short) (i-color.red*rmult-color.green*gmult);
			color.red *= 65535/imax;
			color.green *= 65535/imax;
			color.blue *= 65535/imax;
			color.flags = DoRed|DoGreen|DoBlue;
			XStoreColor(dpy,cmap,&color);
		}
		
		/* set standard colormap */
		scmap->colormap = cmap;
		scmap->red_max = imax;
		scmap->green_max = imax;
		scmap->blue_max = imax;
		scmap->red_mult = rmult;
		scmap->green_mult = gmult;
		scmap->blue_mult = bmult;
		scmap->base_pixel = bpixel;
		XSetStandardColormap(dpy,root,scmap,XA_RGB_DEFAULT_MAP);
	}

	/* ungrab the server before returning */
	XUngrabServer(dpy);
	return 1;
}
Ejemplo n.º 15
0
int ph_window_modify_pen (int pen, unsigned char red, unsigned char green,
   unsigned char blue)
{
#if 0
   PgColor_t color;

   /* Translate 0-255 values of new color to X 0-65535 values. */
   color.flags = (DoRed | DoGreen | DoBlue);
   color.red = (int) red << 8;
   color.green = (int) green << 8;
   color.blue = (int) blue << 8;
   color.pixel = pseudo_color_lookup[pen];

   if (pseudo_color_use_rw_palette)
   {
      XStoreColor (display, colormap, &color);
   }
   else
   {
      /* free previously allocated color */
      if (pseudo_color_allocated[pen])
      {
         XFreeColors (display, colormap, &pseudo_color_lookup[pen], 1, 0);
         pseudo_color_allocated[pen] = FALSE;
      }

      /* allocate new color and assign it to pen index */
      if (XAllocColor (display, colormap, &color))
      {
         if (pseudo_color_lookup[pen] != color.pixel)
            pseudo_color_lookup_dirty = TRUE;
         pseudo_color_lookup[pen] = color.pixel;
         pseudo_color_allocated[pen] = TRUE;
      }
      else /* try again with the closest match */
      {
         int i;
         XColor colors[256];
         int my_red   = (int)red << 8;
         int my_green = (int)green << 8;
         int my_blue  = (int)blue << 8;
         int best_pixel = black_pen;
         float best_diff = FLT_MAX;
         
         for(i=0;i<256;i++)
            colors[i].pixel = i;
         
         XQueryColors(display, colormap, colors, 256);
         for(i=0;i<256;i++)
         {
            #define SQRT(x) ((float)(x)*(x))
            float diff = SQRT(my_red - colors[i].red) + 
               SQRT(my_green - colors[i].green) +
               SQRT(my_blue - colors[i].blue);
            if (diff < best_diff)
            {
               best_pixel = colors[i].pixel;
               best_diff  = diff;
            }
         }
         
         color = colors[best_pixel];
         
         if (XAllocColor (display, colormap, &color))
         {
            if (pseudo_color_lookup[pen] != color.pixel)
               pseudo_color_lookup_dirty = TRUE;
            pseudo_color_lookup[pen] = color.pixel;
            pseudo_color_allocated[pen] = TRUE;
         }
         else
         {
            if (pseudo_color_warn_low_on_colors)
            {
               pseudo_color_warn_low_on_colors = 0;
               fprintf (stderr_file,
                  "warning: Closest color match alloc failed\n"
                  "Couldn't allocate all colors, some parts of the emulation may be black\n"
                  "Try running mame with the -privatecmap option\n");
            }
            
            /* If color allocation failed, use black to ensure the
               pen is not left set to an invalid color */
            pseudo_color_lookup[pen] = black_pen;
            return -1;
         }
      }
   }
#endif
   return 0;
}
Ejemplo n.º 16
0
static void create_colorform(char *resourcestring, char *valuestring, 
                             int item_to_adjust)

{
    ColorDataPtr colordata;
    ColorDataPtr colordata2;
    Arg arg[5];
    unsigned long plane_masks_return[1];
    unsigned long pixels_return[4];
    Status status;
    Boolean false = 0;
    XColor color;
    XColor color2;

    fetched = (Widget) 0;
    colordata = (ColorDataPtr) XtCalloc(1,sizeof(ColorData));
    colordata2 = (ColorDataPtr) XtCalloc(1,sizeof(ColorData));
    colordata->item_to_adjust = item_to_adjust;
    colordata2->item_to_adjust = item_to_adjust;
    status = XAllocColorCells(
			  dpy,
			  cmap,
			  false,
			  plane_masks_return,
			  1,
			  pixels_return,
			  4);
    if(!status){
      no_colors_available = 1;
      return;
    }

    /* Allocate colors for color change P.B. */

    status = XAllocColorCells(
			  dpy,
			  cmap,
			  false,
			  plane_masks_return2,
			  1,
			  pixels_return2,
			  5);
    if(!status){
      no_colors_available = 1;
      return;
      }

    colordata->color.pixel = pixels_return[0];
    colordata->color.flags = DoRed | DoGreen | DoBlue;
    colordata->fg_pixel = pixels_return[0];
    colordata->bg_pixel = pixels_return[1];
    colordata->ts_pixel = pixels_return[2];
    colordata->bs_pixel = pixels_return[3];

    initialize_coords();

    fetch("color_form",&fetched);

    /* store userData */
	if ( fetched == NULL ) {
		exit(1);
	} else {
    XtSetArg(arg[0],XmNuserData,colordata);
    XtSetValues(fetched,arg,1);
    setcolor_colorform(fetched,valuestring);
	}

    /* set the background (and other fields) of the color patch */
    /* to the allocated color and associated colors */

    color.pixel = pixels_return[0];
    XQueryColor(dpy,cmap,&color);
    color2.pixel = pixels_return[0];
    XQueryColor(dpy,cmap,&color2);
    color.pixel = pixels_return[2];
    XStoreColor(dpy,cmap,&color);
    color.pixel = pixels_return[3];
    XStoreColor(dpy,cmap,&color);

    /* note that foreground is initialized to contrast with background */
    color.red = 65535 - color.red;
    color.green = 65535 - color.green;
    color.blue = 65535 - color.blue;
    color.pixel = pixels_return[1];
    XStoreColor(dpy,cmap,&color);

    color2.red = 0;
    color2.green = 0;
    color2.blue = 0;
    color2.pixel = pixels_return2[1];
    XStoreColor(dpy,cmap,&color2);

    color2.red = 0;
    color2.green = 0;
    color2.blue = 0;
    color2.pixel = pixels_return2[2];
    XStoreColor(dpy,cmap,&color2);

    color2.red = 0;
    color2.green = 0;
    color2.blue = 0;
    color2.pixel = pixels_return2[3];
    XStoreColor(dpy,cmap,&color2);

    color2.red = 0;
    color2.green = 0;
    color2.blue = 0;
    color2.pixel = pixels_return2[4];
    XStoreColor(dpy,cmap,&color2);

    color2.red = 65535;
    color2.green = 65535;
    color2.blue = 65535;
    color2.pixel = pixels_return2[0];
    XStoreColor(dpy,cmap,&color2);

    XtSetArg(arg[0],XmNbackground,pixels_return[0]);
    XtSetArg(arg[1],XmNforeground,pixels_return[1]);
    XtSetArg(arg[2],XmNtopShadowColor,pixels_return[2]);
    XtSetArg(arg[3],XmNbottomShadowColor,pixels_return[3]);
    XtSetValues(XtNameToWidget(fetched,"color_patch"),arg,4);

    XtSetArg(arg[0],XmNbackground,pixels_return2[0]);
    XtSetArg(arg[1],XmNforeground,pixels_return2[1]);
    XtSetArg(arg[2],XmNtopShadowColor,pixels_return2[2]);
    XtSetArg(arg[3],XmNbottomShadowColor,pixels_return2[3]);
    XtSetArg(arg[4],XmNarmColor,pixels_return2[4]);
    XtSetValues(XtNameToWidget(fetched,"color_change_BB.color_change"),arg,5);

    XtSetArg(arg[0],XmNbackground,pixels_return2[0]);
    XtSetValues(XtNameToWidget(fetched,"color_change_BB"),arg,1);

    XtManageChild(fetched);
}