/* X-specific Lisp objects. Copyright (C) 1993, 1994 Free Software Foundation, Inc. Copyright (C) 1995 Board of Trustees, University of Illinois. Copyright (C) 1995 Tinker Systems Copyright (C) 1995, 1996 Ben Wing Copyright (C) 1995 Sun MicrosystemsThis file is part of XEmacs.XEmacs is free software; you can redistribute it and/or modify itunder the terms of the GNU General Public License as published by theFree Software Foundation; either version 2, or (at your option) anylater version.XEmacs is distributed in the hope that it will be useful, but WITHOUTANY WARRANTY; without even the implied warranty of MERCHANTABILITY orFITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public Licensefor more details.You should have received a copy of the GNU General Public Licensealong with XEmacs; see the file COPYING. If not, write tothe Free Software Foundation, Inc., 59 Temple Place - Suite 330,Boston, MA 02111-1307, USA. *//* Synched up with: Not in FSF. *//* Original author: Jamie Zawinski for 19.8 font-truename stuff added by Jamie Zawinski for 19.10 subwindow support added by Chuck Thompson additional XPM support added by Chuck Thompson initial X-Face support added by Stig rewritten/restructured by Ben Wing for 19.12/19.13 GIF/JPEG support added by Ben Wing for 19.14 PNG support added by Bill Perry for 19.14 Improved GIF/JPEG support added by Bill Perry for 19.14 Cleanup/simplification of error handling by Ben Wing for 19.14 Pointer/icon overhaul, more restructuring by Ben Wing for 19.14 GIF support changed to external GIFlib 3.1 by Jareth Hein for 21.0 Many changes for color work and optimizations by Jareth Hein for 21.0 Switch of GIF/JPEG/PNG to new EImage intermediate code by Jareth Hein for 21.0 TIFF code by Jareth Hein for 21.0 GIF/JPEG/PNG/TIFF code moved to new glyph-eimage.c for 21.0 TODO: Convert images.el to C and stick it in here? */#include <config.h>#include "lisp.h"#include "lstream.h"#include "console-x.h"#include "glyphs-x.h"#include "objects-x.h"#include "xmu.h"#include "buffer.h"#include "frame.h"#include "insdel.h"#include "opaque.h"#include "imgproc.h"#include "sysfile.h"#include <setjmp.h>#ifdef FILE_CODING#include "file-coding.h"#endif#if INTBITS == 32# define FOUR_BYTE_TYPE unsigned int#elif LONGBITS == 32# define FOUR_BYTE_TYPE unsigned long#elif SHORTBITS == 32# define FOUR_BYTE_TYPE unsigned short#else#error What kind of strange-ass system are we running on?#endif#define LISP_DEVICE_TO_X_SCREEN(dev) XDefaultScreenOfDisplay (DEVICE_X_DISPLAY (XDEVICE (dev)))#ifdef HAVE_XFACEDEFINE_IMAGE_INSTANTIATOR_FORMAT(xface);Lisp_ObjectQxface;#endifDEFINE_IMAGE_INSTANTIATOR_FORMAT(cursor_font);Lisp_ObjectQcursor_font;DEFINE_IMAGE_INSTANTIATOR_FORMAT(font);DEFINE_IMAGE_INSTANTIATOR_FORMAT(autodetect);staticvoidcursor_font_instantiate(Lisp_Objectimage_instance,Lisp_Objectinstantiator,Lisp_Objectpointer_fg,Lisp_Objectpointer_bg,intdest_mask,Lisp_Objectdomain);#include "bitmaps.h"/************************************************************************//* image instance methods *//************************************************************************//************************************************************************//* convert from a series of RGB triples to an XImage formated for the *//* proper display *//************************************************************************/staticXImage*convert_EImage_to_XImage(Lisp_Objectdevice,intwidth,intheight,unsignedchar*pic,unsignedlong**pixtbl,int*npixels){Display*dpy;Colormapcmap;Visual*vis;XImage*outimg;intdepth,bitmap_pad,byte_cnt,i,j;intrd,gr,bl,q;unsignedchar*data,*ip,*dp;quant_table*qtable;union{FOUR_BYTE_TYPEval;charcp[4];}conv;dpy=DEVICE_X_DISPLAY(XDEVICE(device));cmap=DEVICE_X_COLORMAP(XDEVICE(device));vis=DEVICE_X_VISUAL(XDEVICE(device));depth=DEVICE_X_DEPTH(XDEVICE(device));if(vis->class==PseudoColor){/* Quantize the image and get a histogram while we're at it. Do this first to save memory */qtable=build_EImage_quantable(pic,width,height,256);if(qtable==NULL)returnNULL;}bitmap_pad=((depth>16)?32:(depth>8)?16:8);outimg=XCreateImage(dpy,vis,depth,ZPixmap,0,0,width,height,bitmap_pad,0);if(!outimg)returnNULL;byte_cnt=outimg->bits_per_pixel>>3;data=(unsignedchar*)xmalloc(outimg->bytes_per_line*height);if(!data){XDestroyImage(outimg);returnNULL;}outimg->data=(char*)data;if(vis->class==PseudoColor){unsignedlongpixarray[256];intpixcount,n;/* use our quantize table to allocate the colors */pixcount=32;*pixtbl=xnew_array(unsignedlong,pixcount);*npixels=0;/* ### should implement a sort by popularity to assure proper allocation */n=*npixels;for(i=0;i<qtable->num_active_colors;i++){XColorcolor;intres;color.red=qtable->rm[i]?qtable->rm[i]<<8:0;color.green=qtable->gm[i]?qtable->gm[i]<<8:0;color.blue=qtable->bm[i]?qtable->bm[i]<<8:0;color.flags=DoRed|DoGreen|DoBlue;res=allocate_nearest_color(dpy,cmap,vis,&color);if(res>0&&res<3){DO_REALLOC(*pixtbl,pixcount,n+1,unsignedlong);(*pixtbl)[n]=color.pixel;n++;}pixarray[i]=color.pixel;}*npixels=n;ip=pic;for(i=0;i<height;i++){dp=data+(i*outimg->bytes_per_line);for(j=0;j<width;j++){rd=*ip++;gr=*ip++;bl=*ip++;conv.val=pixarray[QUANT_GET_COLOR(qtable,rd,gr,bl)];#if WORDS_BIGENDIANif(outimg->byte_order==MSBFirst)for(q=4-byte_cnt;q<4;q++)*dp++=conv.cp[q];elsefor(q=3;q>=4-byte_cnt;q--)*dp++=conv.cp[q];#elseif(outimg->byte_order==MSBFirst)for(q=byte_cnt-1;q>=0;q--)*dp++=conv.cp[q];elsefor(q=0;q<byte_cnt;q++)*dp++=conv.cp[q];#endif}}xfree(qtable);}else{unsignedlongrshift,gshift,bshift,rbits,gbits,bbits,junk;junk=vis->red_mask;rshift=0;while((junk&0x1)==0){junk=junk>>1;rshift++;}rbits=0;while(junk!=0){junk=junk>>1;rbits++;}junk=vis->green_mask;gshift=0;while((junk&0x1)==0){junk=junk>>1;gshift++;}gbits=0;while(junk!=0){junk=junk>>1;gbits++;}junk=vis->blue_mask;bshift=0;while((junk&0x1)==0){junk=junk>>1;bshift++;}bbits=0;while(junk!=0){junk=junk>>1;bbits++;}ip=pic;for(i=0;i<height;i++){dp=data+(i*outimg->bytes_per_line);for(j=0;j<width;j++){if(rbits>8)rd=*ip++<<(rbits-8);elserd=*ip++>>(8-rbits);if(gbits>8)gr=*ip++<<(gbits-8);elsegr=*ip++>>(8-gbits);if(bbits>8)bl=*ip++<<(bbits-8);elsebl=*ip++>>(8-bbits);conv.val=(rd<<rshift)|(gr<<gshift)|(bl<<bshift);#if WORDS_BIGENDIANif(outimg->byte_order==MSBFirst)for(q=4-byte_cnt;q<4;q++)*dp++=conv.cp[q];elsefor(q=3;q>=4-byte_cnt;q--)*dp++=conv.cp[q];#elseif(outimg->byte_order==MSBFirst)for(q=byte_cnt-1;q>=0;q--)*dp++=conv.cp[q];elsefor(q=0;q<byte_cnt;q++)*dp++=conv.cp[q];#endif}}}returnoutimg;}staticvoidx_print_image_instance(structLisp_Image_Instance*p,Lisp_Objectprintcharfun,intescapeflag){charbuf[100];switch(IMAGE_INSTANCE_TYPE(p)){caseIMAGE_MONO_PIXMAP:caseIMAGE_COLOR_PIXMAP:caseIMAGE_POINTER:sprintf(buf," (0x%lx",(unsignedlong)IMAGE_INSTANCE_X_PIXMAP(p));write_c_string(buf,printcharfun);if(IMAGE_INSTANCE_X_MASK(p)){sprintf(buf,"/0x%lx",(unsignedlong)IMAGE_INSTANCE_X_MASK(p));write_c_string(buf,printcharfun);}write_c_string(")",printcharfun);break;#if HAVE_SUBWINDOWScaseIMAGE_SUBWINDOW:/* #### implement me */#endifdefault:break;}}staticvoidx_finalize_image_instance(structLisp_Image_Instance*p){if(!p->data)return;if(DEVICE_LIVE_P(XDEVICE(p->device))){Display*dpy=DEVICE_X_DISPLAY(XDEVICE(p->device));if(IMAGE_INSTANCE_X_PIXMAP(p))XFreePixmap(dpy,IMAGE_INSTANCE_X_PIXMAP(p));if(IMAGE_INSTANCE_X_MASK(p)&&IMAGE_INSTANCE_X_MASK(p)!=IMAGE_INSTANCE_X_PIXMAP(p))XFreePixmap(dpy,IMAGE_INSTANCE_X_MASK(p));IMAGE_INSTANCE_X_PIXMAP(p)=0;IMAGE_INSTANCE_X_MASK(p)=0;if(IMAGE_INSTANCE_X_CURSOR(p)){XFreeCursor(dpy,IMAGE_INSTANCE_X_CURSOR(p));IMAGE_INSTANCE_X_CURSOR(p)=0;}if(IMAGE_INSTANCE_X_NPIXELS(p)!=0){XFreeColors(dpy,IMAGE_INSTANCE_X_COLORMAP(p),IMAGE_INSTANCE_X_PIXELS(p),IMAGE_INSTANCE_X_NPIXELS(p),0);IMAGE_INSTANCE_X_NPIXELS(p)=0;}}if(IMAGE_INSTANCE_X_PIXELS(p)){xfree(IMAGE_INSTANCE_X_PIXELS(p));IMAGE_INSTANCE_X_PIXELS(p)=0;}xfree(p->data);p->data=0;}staticintx_image_instance_equal(structLisp_Image_Instance*p1,structLisp_Image_Instance*p2,intdepth){switch(IMAGE_INSTANCE_TYPE(p1)){caseIMAGE_MONO_PIXMAP:caseIMAGE_COLOR_PIXMAP:caseIMAGE_POINTER:if(IMAGE_INSTANCE_X_COLORMAP(p1)!=IMAGE_INSTANCE_X_COLORMAP(p2)||IMAGE_INSTANCE_X_NPIXELS(p1)!=IMAGE_INSTANCE_X_NPIXELS(p2))return0;#if HAVE_SUBWINDOWScaseIMAGE_SUBWINDOW:/* #### implement me */#endifbreak;default:break;}return1;}staticunsignedlongx_image_instance_hash(structLisp_Image_Instance*p,intdepth){switch(IMAGE_INSTANCE_TYPE(p)){caseIMAGE_MONO_PIXMAP:caseIMAGE_COLOR_PIXMAP:caseIMAGE_POINTER:returnIMAGE_INSTANCE_X_NPIXELS(p);#if HAVE_SUBWINDOWScaseIMAGE_SUBWINDOW:/* #### implement me */return0;#endifdefault:return0;}}/* Set all the slots in an image instance structure to reasonable default values. This is used somewhere within an instantiate method. It is assumed that the device slot within the image instance is already set -- this is the case when instantiate methods are called. */staticvoidx_initialize_pixmap_image_instance(structLisp_Image_Instance*ii,enumimage_instance_typetype){ii->data=xnew_and_zero(structx_image_instance_data);IMAGE_INSTANCE_TYPE(ii)=type;IMAGE_INSTANCE_PIXMAP_FILENAME(ii)=Qnil;IMAGE_INSTANCE_PIXMAP_MASK_FILENAME(ii)=Qnil;IMAGE_INSTANCE_PIXMAP_HOTSPOT_X(ii)=Qnil;IMAGE_INSTANCE_PIXMAP_HOTSPOT_Y(ii)=Qnil;IMAGE_INSTANCE_PIXMAP_FG(ii)=Qnil;IMAGE_INSTANCE_PIXMAP_BG(ii)=Qnil;}/************************************************************************//* pixmap file functions *//************************************************************************//* Where bitmaps are; initialized from resource database */Lisp_ObjectVx_bitmap_file_path;#ifndef BITMAPDIR#define BITMAPDIR "/usr/include/X11/bitmaps"#endif#define USE_XBMLANGPATH/* Given a pixmap filename, look through all of the "standard" places where the file might be located. Return a full pathname if found; otherwise, return Qnil. */staticLisp_Objectx_locate_pixmap_file(Lisp_Objectname){/* This function can GC if IN_REDISPLAY is false */Display*display;/* Check non-absolute pathnames with a directory component relative to the search path; that's the way Xt does it. *//* #### Unix-specific */if(XSTRING_BYTE(name,0)=='/'||(XSTRING_BYTE(name,0)=='.'&&(XSTRING_BYTE(name,1)=='/'||(XSTRING_BYTE(name,1)=='.'&&(XSTRING_BYTE(name,2)=='/'))))){if(!NILP(Ffile_readable_p(name)))returnname;elsereturnQnil;}if(NILP(Vdefault_x_device))/* This may occur during intialization. */returnQnil;else/* We only check the bitmapFilePath resource on the original X device. */display=DEVICE_X_DISPLAY(XDEVICE(Vdefault_x_device));#ifdef USE_XBMLANGPATH{char*path=egetenv("XBMLANGPATH");SubstitutionRecsubs[1];subs[0].match='B';subs[0].substitution=(char*)XSTRING_DATA(name);/* #### Motif uses a big hairy default if $XBMLANGPATH isn't set. We don't. If you want it used, set it. */if(path&&(path=XtResolvePathname(display,"bitmaps",0,0,path,subs,XtNumber(subs),0))){name=build_string(path);XtFree(path);return(name);}}#endifif(NILP(Vx_bitmap_file_path)){char*type=0;XrmValuevalue;if(XrmGetResource(XtDatabase(display),"bitmapFilePath","BitmapFilePath",&type,&value)&&!strcmp(type,"String"))Vx_bitmap_file_path=decode_env_path(0,(char*)value.addr);Vx_bitmap_file_path=nconc2(Vx_bitmap_file_path,(decode_path(BITMAPDIR)));}{Lisp_Objectfound;if(locate_file(Vx_bitmap_file_path,name,"",&found,R_OK)<0){Lisp_Objecttemp=list1(Vdata_directory);structgcprogcpro1;GCPRO1(temp);locate_file(temp,name,"",&found,R_OK);UNGCPRO;}returnfound;}}staticLisp_Objectlocate_pixmap_file(Lisp_Objectname){returnx_locate_pixmap_file(name);}#if 0static voidwrite_lisp_string_to_temp_file (Lisp_Object string, char *filename_out){ Lisp_Object instream, outstream; Lstream *istr, *ostr; char tempbuf[1024]; /* some random amount */ int fubar = 0; FILE *tmpfil; static Extbyte_dynarr *conversion_out_dynarr; Bytecount bstart, bend; struct gcpro gcpro1, gcpro2;#ifdef FILE_CODING Lisp_Object conv_out_stream; Lstream *costr; struct gcpro gcpro3;#endif /* This function can GC */ if (!conversion_out_dynarr) conversion_out_dynarr = Dynarr_new (Extbyte); else Dynarr_reset (conversion_out_dynarr); /* Create the temporary file ... */ sprintf (filename_out, "/tmp/emacs%d.XXXXXX", (int) getpid ()); mktemp (filename_out); tmpfil = fopen (filename_out, "w"); if (!tmpfil) { if (tmpfil) { int old_errno = errno; fclose (tmpfil); unlink (filename_out); errno = old_errno; } report_file_error ("Creating temp file", list1 (build_string (filename_out))); } CHECK_STRING (string); get_string_range_byte (string, Qnil, Qnil, &bstart, &bend, GB_HISTORICAL_STRING_BEHAVIOR); instream = make_lisp_string_input_stream (string, bstart, bend); istr = XLSTREAM (instream); /* setup the out stream */ outstream = make_dynarr_output_stream((unsigned_char_dynarr *)conversion_out_dynarr); ostr = XLSTREAM (outstream);#ifdef FILE_CODING /* setup the conversion stream */ conv_out_stream = make_encoding_output_stream (ostr, Fget_coding_system(Qbinary)); costr = XLSTREAM (conv_out_stream); GCPRO3 (instream, outstream, conv_out_stream);#else GCPRO2 (instream, outstream);#endif/* Get the data while doing the conversion */while(1){intsize_in_bytes=Lstream_read(istr,tempbuf,sizeof(tempbuf));if(!size_in_bytes)break;/* It does seem the flushes are necessary... */#ifdef FILE_CODINGLstream_write(costr,tempbuf,size_in_bytes);Lstream_flush(costr);#elseLstream_write(ostr,tempbuf,size_in_bytes);#endifLstream_flush(ostr);if(fwrite((unsignedchar*)Dynarr_atp(conversion_out_dynarr,0),Dynarr_length(conversion_out_dynarr),1,tmpfil)!=1){fubar=1;break;}/* reset the dynarr */Lstream_rewind(ostr);}if(fclose(tmpfil)!=0)fubar=1;Lstream_close(istr);#ifdef FILE_CODINGLstream_close(costr);#endifLstream_close(ostr);UNGCPRO;Lstream_delete(istr);Lstream_delete(ostr);#ifdef FILE_CODINGLstream_delete(costr);#endifif(fubar)report_file_error("Writing temp file",list1(build_string(filename_out)));}#endif /* 0 *//************************************************************************//* cursor functions *//************************************************************************//* Check that this server supports cursors of size WIDTH * HEIGHT. If not, signal an error. INSTANTIATOR is only used in the error message. */staticvoidcheck_pointer_sizes(Screen*xs,unsignedintwidth,unsignedintheight,Lisp_Objectinstantiator){unsignedintbest_width,best_height;if(!XQueryBestCursor(DisplayOfScreen(xs),RootWindowOfScreen(xs),width,height,&best_width,&best_height))/* this means that an X error of some sort occurred (we trap these so they're not fatal). */signal_simple_error("XQueryBestCursor() failed?",instantiator);if(width>best_width||height>best_height)error_with_frob(instantiator,"pointer too large (%dx%d): ""server requires %dx%d or smaller",width,height,best_width,best_height);}staticvoidgenerate_cursor_fg_bg(Lisp_Objectdevice,Lisp_Object*foreground,Lisp_Object*background,XColor*xfg,XColor*xbg){if(!NILP(*foreground)&&!COLOR_INSTANCEP(*foreground))*foreground=Fmake_color_instance(*foreground,device,encode_error_behavior_flag(ERROR_ME));if(COLOR_INSTANCEP(*foreground))*xfg=COLOR_INSTANCE_X_COLOR(XCOLOR_INSTANCE(*foreground));else{xfg->pixel=0;xfg->red=xfg->green=xfg->blue=0;}if(!NILP(*background)&&!COLOR_INSTANCEP(*background))*background=Fmake_color_instance(*background,device,encode_error_behavior_flag(ERROR_ME));if(COLOR_INSTANCEP(*background))*xbg=COLOR_INSTANCE_X_COLOR(XCOLOR_INSTANCE(*background));else{xbg->pixel=0;xbg->red=xbg->green=xbg->blue=~0;}}staticvoidmaybe_recolor_cursor(Lisp_Objectimage_instance,Lisp_Objectforeground,Lisp_Objectbackground){Lisp_Objectdevice=XIMAGE_INSTANCE_DEVICE(image_instance);XColorxfg,xbg;generate_cursor_fg_bg(device,&foreground,&background,&xfg,&xbg);if(!NILP(foreground)||!NILP(background)){XRecolorCursor(DEVICE_X_DISPLAY(XDEVICE(device)),XIMAGE_INSTANCE_X_CURSOR(image_instance),&xfg,&xbg);XIMAGE_INSTANCE_PIXMAP_FG(image_instance)=foreground;XIMAGE_INSTANCE_PIXMAP_BG(image_instance)=background;}}/************************************************************************//* color pixmap functions *//************************************************************************//* Initialize an image instance from an XImage. DEST_MASK specifies the mask of allowed image types. PIXELS and NPIXELS specify an array of pixels that are used in the image. These need to be kept around for the duration of the image. When the image instance is freed, XFreeColors() will automatically be called on all the pixels specified here; thus, you should have allocated the pixels yourself using XAllocColor() or the like. The array passed in is used directly without being copied, so it should be heap data created with xmalloc(). It will be freed using xfree() when the image instance is destroyed. If this fails, signal an error. INSTANTIATOR is only used in the error message. #### This should be able to handle conversion into `pointer'. Use the same code as for `xpm'. */staticvoidinit_image_instance_from_x_image(structLisp_Image_Instance*ii,XImage*ximage,intdest_mask,Colormapcmap,unsignedlong*pixels,intnpixels,Lisp_Objectinstantiator){Lisp_Objectdevice=IMAGE_INSTANCE_DEVICE(ii);Display*dpy;GCgc;Drawabled;Pixmappixmap;if(!DEVICE_X_P(XDEVICE(device)))signal_simple_error("Not an X device",device);dpy=DEVICE_X_DISPLAY(XDEVICE(device));d=XtWindow(DEVICE_XT_APP_SHELL(XDEVICE(device)));if(!(dest_mask&IMAGE_COLOR_PIXMAP_MASK))incompatible_image_types(instantiator,dest_mask,IMAGE_COLOR_PIXMAP_MASK);pixmap=XCreatePixmap(dpy,d,ximage->width,ximage->height,ximage->depth);if(!pixmap)signal_simple_error("Unable to create pixmap",instantiator);gc=XCreateGC(dpy,pixmap,0,NULL);if(!gc){XFreePixmap(dpy,pixmap);signal_simple_error("Unable to create GC",instantiator);}XPutImage(dpy,pixmap,gc,ximage,0,0,0,0,ximage->width,ximage->height);XFreeGC(dpy,gc);x_initialize_pixmap_image_instance(ii,IMAGE_COLOR_PIXMAP);IMAGE_INSTANCE_PIXMAP_FILENAME(ii)=find_keyword_in_vector(instantiator,Q_file);IMAGE_INSTANCE_X_PIXMAP(ii)=pixmap;IMAGE_INSTANCE_X_MASK(ii)=0;IMAGE_INSTANCE_PIXMAP_WIDTH(ii)=ximage->width;IMAGE_INSTANCE_PIXMAP_HEIGHT(ii)=ximage->height;IMAGE_INSTANCE_PIXMAP_DEPTH(ii)=ximage->depth;IMAGE_INSTANCE_X_COLORMAP(ii)=cmap;IMAGE_INSTANCE_X_PIXELS(ii)=pixels;IMAGE_INSTANCE_X_NPIXELS(ii)=npixels;}staticvoidx_init_image_instance_from_eimage(structLisp_Image_Instance*ii,intwidth,intheight,unsignedchar*eimage,intdest_mask,Lisp_Objectinstantiator,Lisp_Objectdomain){Lisp_Objectdevice=IMAGE_INSTANCE_DEVICE(ii);Colormapcmap=DEVICE_X_COLORMAP(XDEVICE(device));unsignedlong*pixtbl=NULL;intnpixels=0;XImage*ximage;ximage=convert_EImage_to_XImage(device,width,height,eimage,&pixtbl,&npixels);if(!ximage){if(pixtbl)xfree(pixtbl);signal_image_error("EImage to XImage conversion failed",instantiator);}/* Now create the pixmap and set up the image instance */init_image_instance_from_x_image(ii,ximage,dest_mask,cmap,pixtbl,npixels,instantiator);if(ximage){if(ximage->data){xfree(ximage->data);ximage->data=0;}XDestroyImage(ximage);}}intread_bitmap_data_from_file(CONSTchar*filename,unsignedint*width,unsignedint*height,unsignedchar**datap,int*x_hot,int*y_hot){returnXmuReadBitmapDataFromFile(filename,width,height,datap,x_hot,y_hot);}/* Given inline data for a mono pixmap, create and return the corresponding X object. */staticPixmappixmap_from_xbm_inline(Lisp_Objectdevice,intwidth,intheight,/* Note that data is in ext-format! */CONSTExtbyte*bits){returnXCreatePixmapFromBitmapData(DEVICE_X_DISPLAY(XDEVICE(device)),XtWindow(DEVICE_XT_APP_SHELL(XDEVICE(device))),(char*)bits,width,height,1,0,1);}/* Given inline data for a mono pixmap, initialize the given image instance accordingly. */staticvoidinit_image_instance_from_xbm_inline(structLisp_Image_Instance*ii,intwidth,intheight,/* Note that data is in ext-format! */CONSTchar*bits,Lisp_Objectinstantiator,Lisp_Objectpointer_fg,Lisp_Objectpointer_bg,intdest_mask,Pixmapmask,Lisp_Objectmask_filename){Lisp_Objectdevice=IMAGE_INSTANCE_DEVICE(ii);Lisp_Objectforeground=find_keyword_in_vector(instantiator,Q_foreground);Lisp_Objectbackground=find_keyword_in_vector(instantiator,Q_background);Display*dpy;Screen*scr;Drawabledraw;enumimage_instance_typetype;if(!DEVICE_X_P(XDEVICE(device)))signal_simple_error("Not an X device",device);dpy=DEVICE_X_DISPLAY(XDEVICE(device));draw=XtWindow(DEVICE_XT_APP_SHELL(XDEVICE(device)));scr=DefaultScreenOfDisplay(dpy);if((dest_mask&IMAGE_MONO_PIXMAP_MASK)&&(dest_mask&IMAGE_COLOR_PIXMAP_MASK)){if(!NILP(foreground)||!NILP(background))type=IMAGE_COLOR_PIXMAP;elsetype=IMAGE_MONO_PIXMAP;}elseif(dest_mask&IMAGE_MONO_PIXMAP_MASK)type=IMAGE_MONO_PIXMAP;elseif(dest_mask&IMAGE_COLOR_PIXMAP_MASK)type=IMAGE_COLOR_PIXMAP;elseif(dest_mask&IMAGE_POINTER_MASK)type=IMAGE_POINTER;elseincompatible_image_types(instantiator,dest_mask,IMAGE_MONO_PIXMAP_MASK|IMAGE_COLOR_PIXMAP_MASK|IMAGE_POINTER_MASK);x_initialize_pixmap_image_instance(ii,type);IMAGE_INSTANCE_PIXMAP_WIDTH(ii)=width;IMAGE_INSTANCE_PIXMAP_HEIGHT(ii)=height;IMAGE_INSTANCE_PIXMAP_FILENAME(ii)=find_keyword_in_vector(instantiator,Q_file);switch(type){caseIMAGE_MONO_PIXMAP:{IMAGE_INSTANCE_X_PIXMAP(ii)=pixmap_from_xbm_inline(device,width,height,(Extbyte*)bits);}break;caseIMAGE_COLOR_PIXMAP:{Dimensiond=DEVICE_X_DEPTH(XDEVICE(device));unsignedlongfg=BlackPixelOfScreen(scr);unsignedlongbg=WhitePixelOfScreen(scr);if(!NILP(foreground)&&!COLOR_INSTANCEP(foreground))foreground=Fmake_color_instance(foreground,device,encode_error_behavior_flag(ERROR_ME));if(COLOR_INSTANCEP(foreground))fg=COLOR_INSTANCE_X_COLOR(XCOLOR_INSTANCE(foreground)).pixel;if(!NILP(background)&&!COLOR_INSTANCEP(background))background=Fmake_color_instance(background,device,encode_error_behavior_flag(ERROR_ME));if(COLOR_INSTANCEP(background))bg=COLOR_INSTANCE_X_COLOR(XCOLOR_INSTANCE(background)).pixel;/* We used to duplicate the pixels using XAllocColor(), to protect against their getting freed. Just as easy to just store the color instances here and GC-protect them, so this doesn't happen. */IMAGE_INSTANCE_PIXMAP_FG(ii)=foreground;IMAGE_INSTANCE_PIXMAP_BG(ii)=background;IMAGE_INSTANCE_X_PIXMAP(ii)=XCreatePixmapFromBitmapData(dpy,draw,(char*)bits,width,height,fg,bg,d);IMAGE_INSTANCE_PIXMAP_DEPTH(ii)=d;}break;caseIMAGE_POINTER:{XColorfg_color,bg_color;Pixmapsource;check_pointer_sizes(scr,width,height,instantiator);source=XCreatePixmapFromBitmapData(dpy,draw,(char*)bits,width,height,1,0,1);if(NILP(foreground))foreground=pointer_fg;if(NILP(background))background=pointer_bg;generate_cursor_fg_bg(device,&foreground,&background,&fg_color,&bg_color);IMAGE_INSTANCE_PIXMAP_FG(ii)=foreground;IMAGE_INSTANCE_PIXMAP_BG(ii)=background;IMAGE_INSTANCE_PIXMAP_HOTSPOT_X(ii)=find_keyword_in_vector(instantiator,Q_hotspot_x);IMAGE_INSTANCE_PIXMAP_HOTSPOT_Y(ii)=find_keyword_in_vector(instantiator,Q_hotspot_y);IMAGE_INSTANCE_X_CURSOR(ii)=XCreatePixmapCursor(dpy,source,mask,&fg_color,&bg_color,!NILP(IMAGE_INSTANCE_PIXMAP_HOTSPOT_X(ii))?XINT(IMAGE_INSTANCE_PIXMAP_HOTSPOT_X(ii)):0,!NILP(IMAGE_INSTANCE_PIXMAP_HOTSPOT_Y(ii))?XINT(IMAGE_INSTANCE_PIXMAP_HOTSPOT_Y(ii)):0);}break;default:abort();}}staticvoidxbm_instantiate_1(Lisp_Objectimage_instance,Lisp_Objectinstantiator,Lisp_Objectpointer_fg,Lisp_Objectpointer_bg,intdest_mask,intwidth,intheight,/* Note that data is in ext-format! */CONSTchar*bits){Lisp_Objectmask_data=find_keyword_in_vector(instantiator,Q_mask_data);Lisp_Objectmask_file=find_keyword_in_vector(instantiator,Q_mask_file);structLisp_Image_Instance*ii=XIMAGE_INSTANCE(image_instance);Pixmapmask=0;CONSTchar*gcc_may_you_rot_in_hell;if(!NILP(mask_data)){GET_C_STRING_BINARY_DATA_ALLOCA(XCAR(XCDR(XCDR(mask_data))),gcc_may_you_rot_in_hell);mask=pixmap_from_xbm_inline(IMAGE_INSTANCE_DEVICE(ii),XINT(XCAR(mask_data)),XINT(XCAR(XCDR(mask_data))),(CONSTunsignedchar*)gcc_may_you_rot_in_hell);}init_image_instance_from_xbm_inline(ii,width,height,bits,instantiator,pointer_fg,pointer_bg,dest_mask,mask,mask_file);}/* Instantiate method for XBM's. */staticvoidx_xbm_instantiate(Lisp_Objectimage_instance,Lisp_Objectinstantiator,Lisp_Objectpointer_fg,Lisp_Objectpointer_bg,intdest_mask,Lisp_Objectdomain){Lisp_Objectdata=find_keyword_in_vector(instantiator,Q_data);CONSTchar*gcc_go_home;assert(!NILP(data));GET_C_STRING_BINARY_DATA_ALLOCA(XCAR(XCDR(XCDR(data))),gcc_go_home);xbm_instantiate_1(image_instance,instantiator,pointer_fg,pointer_bg,dest_mask,XINT(XCAR(data)),XINT(XCAR(XCDR(data))),gcc_go_home);}#ifdef HAVE_XPM/********************************************************************** * XPM * **********************************************************************//* xpm 3.2g and better has XpmCreatePixmapFromBuffer()... There was no version number in xpm.h before 3.3, but this should do. */#if (XpmVersion >= 3) || defined(XpmExactColors)# define XPM_DOES_BUFFERS#endif#ifndef XPM_DOES_BUFFERSYourversionofXPMistooold.Youcannotcompilewithit.Upgradetoversion3.2gorbetterorcompilewith--with-xpm=no.#endif /* !XPM_DOES_BUFFERS */staticXpmColorSymbol*extract_xpm_color_names(XpmAttributes*xpmattrs,Lisp_Objectdevice,Lisp_Objectdomain,Lisp_Objectcolor_symbol_alist){/* This function can GC */Display*dpy=DEVICE_X_DISPLAY(XDEVICE(device));Colormapcmap=DEVICE_X_COLORMAP(XDEVICE(device));XColorcolor;Lisp_Objectrest;Lisp_Objectresults=Qnil;inti;XpmColorSymbol*symbols;structgcprogcpro1,gcpro2;GCPRO2(results,device);/* We built up results to be (("name" . #<color>) ...) so that if an error happens we don't lose any malloc()ed data, or more importantly, leave any pixels allocated in the server. */i=0;LIST_LOOP(rest,color_symbol_alist){Lisp_Objectcons=XCAR(rest);Lisp_Objectname=XCAR(cons);Lisp_Objectvalue=XCDR(cons);if(NILP(value))continue;if(STRINGP(value))value=Fmake_color_instance(value,device,encode_error_behavior_flag(ERROR_ME_NOT));else{assert(COLOR_SPECIFIERP(value));value=Fspecifier_instance(value,domain,Qnil,Qnil);}if(NILP(value))continue;results=noseeum_cons(noseeum_cons(name,value),results);i++;}UNGCPRO;/* no more evaluation */if(i==0)return0;symbols=xnew_array(XpmColorSymbol,i);xpmattrs->valuemask|=XpmColorSymbols;xpmattrs->colorsymbols=symbols;xpmattrs->numsymbols=i;while(--i>=0){Lisp_Objectcons=XCAR(results);color=COLOR_INSTANCE_X_COLOR(XCOLOR_INSTANCE(XCDR(cons)));/* Duplicate the pixel value so that we still have a lock on it if the pixel we were passed is later freed. */if(!XAllocColor(dpy,cmap,&color))abort();/* it must be allocable since we're just duplicating it */symbols[i].name=(char*)XSTRING_DATA(XCAR(cons));symbols[i].pixel=color.pixel;symbols[i].value=0;free_cons(XCONS(cons));cons=results;results=XCDR(results);free_cons(XCONS(cons));}returnsymbols;}staticvoidxpm_free(XpmAttributes*xpmattrs){/* Could conceivably lose if XpmXXX returned an error without first initializing this structure, if we didn't know that initializing it to all zeros was ok (and also that it's ok to call XpmFreeAttributes() multiple times, since it zeros slots as it frees them...) */XpmFreeAttributes(xpmattrs);}staticvoidx_xpm_instantiate(Lisp_Objectimage_instance,Lisp_Objectinstantiator,Lisp_Objectpointer_fg,Lisp_Objectpointer_bg,intdest_mask,Lisp_Objectdomain){/* This function can GC */structLisp_Image_Instance*ii=XIMAGE_INSTANCE(image_instance);Lisp_Objectdevice=IMAGE_INSTANCE_DEVICE(ii);Lisp_Objectdata=find_keyword_in_vector(instantiator,Q_data);Display*dpy;Screen*xs;Colormapcmap;intdepth;Visual*visual;Pixmappixmap;Pixmapmask=0;XpmAttributesxpmattrs;intresult;XpmColorSymbol*color_symbols;Lisp_Objectcolor_symbol_alist=find_keyword_in_vector(instantiator,Q_color_symbols);enumimage_instance_typetype;intforce_mono;unsignedintw,h;if(!DEVICE_X_P(XDEVICE(device)))signal_simple_error("Not an X device",device);dpy=DEVICE_X_DISPLAY(XDEVICE(device));xs=DefaultScreenOfDisplay(dpy);if(dest_mask&IMAGE_COLOR_PIXMAP_MASK)type=IMAGE_COLOR_PIXMAP;elseif(dest_mask&IMAGE_MONO_PIXMAP_MASK)type=IMAGE_MONO_PIXMAP;elseif(dest_mask&IMAGE_POINTER_MASK)type=IMAGE_POINTER;elseincompatible_image_types(instantiator,dest_mask,IMAGE_MONO_PIXMAP_MASK|IMAGE_COLOR_PIXMAP_MASK|IMAGE_POINTER_MASK);force_mono=(type!=IMAGE_COLOR_PIXMAP);#if 1/* Although I haven't found it documented yet, it appears that pointers are always colored via the default window colormap... Sigh. */if(type==IMAGE_POINTER){cmap=DefaultColormap(dpy,DefaultScreen(dpy));depth=DefaultDepthOfScreen(xs);visual=DefaultVisualOfScreen(xs);}else{cmap=DEVICE_X_COLORMAP(XDEVICE(device));depth=DEVICE_X_DEPTH(XDEVICE(device));visual=DEVICE_X_VISUAL(XDEVICE(device));}#elsecmap=DEVICE_X_COLORMAP(XDEVICE(device));depth=DEVICE_X_DEPTH(XDEVICE(device));visual=DEVICE_X_VISUAL(XDEVICE(device));#endifx_initialize_pixmap_image_instance(ii,type);assert(!NILP(data));retry:xzero(xpmattrs);/* want XpmInitAttributes() */xpmattrs.valuemask=XpmReturnPixels;if(force_mono){/* Without this, we get a 1-bit version of the color image, which isn't quite right. With this, we get the mono image, which might be very different looking. */xpmattrs.valuemask|=XpmColorKey;xpmattrs.color_key=XPM_MONO;xpmattrs.depth=1;xpmattrs.valuemask|=XpmDepth;}else{xpmattrs.closeness=65535;xpmattrs.valuemask|=XpmCloseness;xpmattrs.depth=depth;xpmattrs.valuemask|=XpmDepth;xpmattrs.visual=visual;xpmattrs.valuemask|=XpmVisual;xpmattrs.colormap=cmap;xpmattrs.valuemask|=XpmColormap;}color_symbols=extract_xpm_color_names(&xpmattrs,device,domain,color_symbol_alist);result=XpmCreatePixmapFromBuffer(dpy,XtWindow(DEVICE_XT_APP_SHELL(XDEVICE(device))),(char*)XSTRING_DATA(data),&pixmap,&mask,&xpmattrs);if(color_symbols){xfree(color_symbols);xpmattrs.colorsymbols=0;/* in case XpmFreeAttr is too smart... */xpmattrs.numsymbols=0;}switch(result){caseXpmSuccess:break;caseXpmFileInvalid:{xpm_free(&xpmattrs);signal_image_error("invalid XPM data",data);}caseXpmColorFailed:caseXpmColorError:{xpm_free(&xpmattrs);if(force_mono){/* second time; blow out. */signal_double_file_error("Reading pixmap data","color allocation failed",data);}else{if(!(dest_mask&IMAGE_MONO_PIXMAP_MASK)){/* second time; blow out. */signal_double_file_error("Reading pixmap data","color allocation failed",data);}force_mono=1;IMAGE_INSTANCE_TYPE(ii)=IMAGE_MONO_PIXMAP;gotoretry;}}caseXpmNoMemory:{xpm_free(&xpmattrs);signal_double_file_error("Parsing pixmap data","out of memory",data);}default:{xpm_free(&xpmattrs);signal_double_file_error_2("Parsing pixmap data","unknown error code",make_int(result),data);}}w=xpmattrs.width;h=xpmattrs.height;{intnpixels=xpmattrs.npixels;Pixel*pixels;if(npixels!=0){pixels=xnew_array(Pixel,npixels);memcpy(pixels,xpmattrs.pixels,npixels*sizeof(Pixel));}elsepixels=NULL;IMAGE_INSTANCE_X_PIXMAP(ii)=pixmap;IMAGE_INSTANCE_X_MASK(ii)=mask;IMAGE_INSTANCE_X_COLORMAP(ii)=cmap;IMAGE_INSTANCE_X_PIXELS(ii)=pixels;IMAGE_INSTANCE_X_NPIXELS(ii)=npixels;IMAGE_INSTANCE_PIXMAP_WIDTH(ii)=w;IMAGE_INSTANCE_PIXMAP_HEIGHT(ii)=h;IMAGE_INSTANCE_PIXMAP_FILENAME(ii)=find_keyword_in_vector(instantiator,Q_file);}switch(type){caseIMAGE_MONO_PIXMAP:break;caseIMAGE_COLOR_PIXMAP:{IMAGE_INSTANCE_PIXMAP_DEPTH(ii)=depth;}break;caseIMAGE_POINTER:{intnpixels=xpmattrs.npixels;Pixel*pixels=xpmattrs.pixels;XColorfg,bg;inti;intxhot=0,yhot=0;if(xpmattrs.valuemask&XpmHotspot){xhot=xpmattrs.x_hotspot;XSETINT(IMAGE_INSTANCE_PIXMAP_HOTSPOT_X(ii),xpmattrs.x_hotspot);}if(xpmattrs.valuemask&XpmHotspot){yhot=xpmattrs.y_hotspot;XSETINT(IMAGE_INSTANCE_PIXMAP_HOTSPOT_Y(ii),xpmattrs.y_hotspot);}check_pointer_sizes(xs,w,h,instantiator);/* If the loaded pixmap has colors allocated (meaning it came from an XPM file), then use those as the default colors for the cursor we create. Otherwise, default to pointer_fg and pointer_bg. */if(npixels>=2){/* With an XBM file, it's obvious which bit is foreground and which is background, or rather, it's implicit: in an XBM file, a 1 bit is foreground, and a 0 bit is background. XCreatePixmapCursor() assumes this property of the pixmap it is called with as well; the `foreground' color argument is used for the 1 bits. With an XPM file, it's tricker, since the elements of the pixmap don't represent FG and BG, but are actual pixel values. So we need to figure out which of those pixels is the foreground color and which is the background. We do it by comparing RGB and assuming that the darker color is the foreground. This works with the result of xbmtopbm|ppmtoxpm, at least. It might be nice if there was some way to tag the colors in the XPM file with whether they are the foreground - perhaps with logical color names somehow? Once we have decided which color is the foreground, we need to ensure that that color corresponds to a `1' bit in the Pixmap. The XPM library wrote into the (1-bit) pixmap with XPutPixel, which will ignore all but the least significant bit. This means that a 1 bit in the image corresponds to `fg' only if `fg.pixel' is odd. (This also means that the image will be all the same color if both `fg' and `bg' are odd or even, but we can safely assume that that won't happen if the XPM file is sensible I think.) The desired result is that the image use `1' to represent the foreground color, and `0' to represent the background color. So, we may need to invert the image to accomplish this; we invert if fg is odd. (Remember that WhitePixel and BlackPixel are not necessarily 1 and 0 respectively, though I think it might be safe to assume that one of them is always 1 and the other is always 0. We also pretty much need to assume that one is even and the other is odd.) */fg.pixel=pixels[0];/* pick a pixel at random. */bg.pixel=fg.pixel;for(i=1;i<npixels;i++)/* Look for an "other" pixel value.*/{bg.pixel=pixels[i];if(fg.pixel!=bg.pixel)break;}/* If (fg.pixel == bg.pixel) then probably something has gone wrong, but I don't think signalling an error would be appropriate. */XQueryColor(dpy,cmap,&fg);XQueryColor(dpy,cmap,&bg);/* If the foreground is lighter than the background, swap them. (This occurs semi-randomly, depending on the ordering of the color list in the XPM file.) */{unsignedshortfg_total=((fg.red/3)+(fg.green/3)+(fg.blue/3));unsignedshortbg_total=((bg.red/3)+(bg.green/3)+(bg.blue/3));if(fg_total>bg_total){XColorswap;swap=fg;fg=bg;bg=swap;}}/* If the fg pixel corresponds to a `0' in the bitmap, invert it. (This occurs (only?) on servers with Black=0, White=1.) */if((fg.pixel&1)==0){XGCValuesgcv;GCgc;gcv.function=GXxor;gcv.foreground=1;gc=XCreateGC(dpy,pixmap,(GCFunction|GCForeground),&gcv);XFillRectangle(dpy,pixmap,gc,0,0,w,h);XFreeGC(dpy,gc);}}else{generate_cursor_fg_bg(device,&pointer_fg,&pointer_bg,&fg,&bg);IMAGE_INSTANCE_PIXMAP_FG(ii)=pointer_fg;IMAGE_INSTANCE_PIXMAP_BG(ii)=pointer_bg;}IMAGE_INSTANCE_X_CURSOR(ii)=XCreatePixmapCursor(dpy,pixmap,mask,&fg,&bg,xhot,yhot);}break;default:abort();}xpm_free(&xpmattrs);/* after we've read pixels and hotspot */}#endif /* HAVE_XPM */#ifdef HAVE_XFACE/********************************************************************** * X-Face * **********************************************************************/staticvoidxface_validate(Lisp_Objectinstantiator){file_or_data_must_be_present(instantiator);}staticLisp_Objectxface_normalize(Lisp_Objectinst,Lisp_Objectconsole_type){/* This funcation can call lisp */Lisp_Objectfile=Qnil,mask_file=Qnil;structgcprogcpro1,gcpro2,gcpro3;Lisp_Objectalist=Qnil;GCPRO3(file,mask_file,alist);/* Now, convert any file data into inline data for both the regular data and the mask data. At the end of this, `data' will contain the inline data (if any) or Qnil, and `file' will contain the name this data was derived from (if known) or Qnil. Likewise for `mask_file' and `mask_data'. Note that if we cannot generate any regular inline data, we skip out. */file=potential_pixmap_file_instantiator(inst,Q_file,Q_data,console_type);mask_file=potential_pixmap_file_instantiator(inst,Q_mask_file,Q_mask_data,console_type);if(CONSP(file))/* failure locating filename */signal_double_file_error("Opening bitmap file","no such file or directory",Fcar(file));if(NILP(file)&&NILP(mask_file))/* no conversion necessary */RETURN_UNGCPRO(inst);alist=tagged_vector_to_alist(inst);{Lisp_Objectdata=make_string_from_file(file);alist=remassq_no_quit(Q_file,alist);/* there can't be a :data at this point. */alist=Fcons(Fcons(Q_file,file),Fcons(Fcons(Q_data,data),alist));}alist=xbm_mask_file_munging(alist,file,mask_file,console_type);{Lisp_Objectresult=alist_to_tagged_vector(Qxface,alist);free_alist(alist);RETURN_UNGCPRO(result);}}staticintxface_possible_dest_types(void){returnIMAGE_MONO_PIXMAP_MASK|IMAGE_COLOR_PIXMAP_MASK|IMAGE_POINTER_MASK;}#if defined(EXTERN)/* This is about to get redefined! */#undef EXTERN#endif/* We have to define SYSV32 so that compface.h includes string.h instead of strings.h. */#define SYSV32#ifdef __cplusplusextern"C"{#endif#include <compface.h>#ifdef __cplusplus}#endif/* JMP_BUF cannot be used here because if it doesn't get defined to jmp_buf we end up with a conflicting type error with the definition in compface.h */externjmp_bufcomp_env;#undef SYSV32staticvoidxface_instantiate(Lisp_Objectimage_instance,Lisp_Objectinstantiator,Lisp_Objectpointer_fg,Lisp_Objectpointer_bg,intdest_mask,Lisp_Objectdomain){Lisp_Objectdata=find_keyword_in_vector(instantiator,Q_data);inti,stattis;char*p,*bits,*bp;CONSTchar*volatileemsg=0;CONSTchar*volatiledstring;assert(!NILP(data));GET_C_STRING_BINARY_DATA_ALLOCA(data,dstring);if((p=strchr(dstring,':'))){dstring=p+1;}/* Must use setjmp not SETJMP because we used jmp_buf above not JMP_BUF */if(!(stattis=setjmp(comp_env))){UnCompAll((char*)dstring);UnGenFace();}switch(stattis){case-2:emsg="uncompface: internal error";break;case-1:emsg="uncompface: insufficient or invalid data";break;case1:emsg="uncompface: excess data ignored";break;}if(emsg)signal_simple_error_2(emsg,data,Qimage);bp=bits=(char*)alloca(PIXELS/8);/* the compface library exports char F[], which uses a single byte per pixel to represent a 48x48 bitmap. Yuck. */for(i=0,p=F;i<(PIXELS/8);++i){intn,b;/* reverse the bit order of each byte... */for(b=n=0;b<8;++b){n|=((*p++)<<b);}*bp++=(char)n;}xbm_instantiate_1(image_instance,instantiator,pointer_fg,pointer_bg,dest_mask,48,48,bits);}#endif /* HAVE_XFACE *//********************************************************************** * Autodetect * **********************************************************************/staticvoidautodetect_validate(Lisp_Objectinstantiator){data_must_be_present(instantiator);}staticLisp_Objectautodetect_normalize(Lisp_Objectinstantiator,Lisp_Objectconsole_type){Lisp_Objectfile=find_keyword_in_vector(instantiator,Q_data);Lisp_Objectfilename=Qnil;Lisp_Objectdata=Qnil;structgcprogcpro1,gcpro2,gcpro3;Lisp_Objectalist=Qnil;GCPRO3(filename,data,alist);if(NILP(file))/* no conversion necessary */RETURN_UNGCPRO(instantiator);alist=tagged_vector_to_alist(instantiator);filename=locate_pixmap_file(file);if(!NILP(filename)){intxhot,yhot;/* #### Apparently some versions of XpmReadFileToData, which is called by pixmap_to_lisp_data, don't return an error value if the given file is not a valid XPM file. Instead, they just seg fault. It is definitely caused by passing a bitmap. To try and avoid this we check for bitmaps first. */data=bitmap_to_lisp_data(filename,&xhot,&yhot,1);if(!EQ(data,Qt)){alist=remassq_no_quit(Q_data,alist);alist=Fcons(Fcons(Q_file,filename),Fcons(Fcons(Q_data,data),alist));if(xhot!=-1)alist=Fcons(Fcons(Q_hotspot_x,make_int(xhot)),alist);if(yhot!=-1)alist=Fcons(Fcons(Q_hotspot_y,make_int(yhot)),alist);alist=xbm_mask_file_munging(alist,filename,Qnil,console_type);{Lisp_Objectresult=alist_to_tagged_vector(Qxbm,alist);free_alist(alist);RETURN_UNGCPRO(result);}}#ifdef HAVE_XPMdata=pixmap_to_lisp_data(filename,1);if(!EQ(data,Qt)){alist=remassq_no_quit(Q_data,alist);alist=Fcons(Fcons(Q_file,filename),Fcons(Fcons(Q_data,data),alist));alist=Fcons(Fcons(Q_color_symbols,evaluate_xpm_color_symbols()),alist);{Lisp_Objectresult=alist_to_tagged_vector(Qxpm,alist);free_alist(alist);RETURN_UNGCPRO(result);}}#endif}/* If we couldn't convert it, just put it back as it is. We might try to further frob it later as a cursor-font specification. (We can't do that now because we don't know what dest-types it's going to be instantiated into.) */{Lisp_Objectresult=alist_to_tagged_vector(Qautodetect,alist);free_alist(alist);RETURN_UNGCPRO(result);}}staticintautodetect_possible_dest_types(void){returnIMAGE_MONO_PIXMAP_MASK|IMAGE_COLOR_PIXMAP_MASK|IMAGE_POINTER_MASK|IMAGE_TEXT_MASK;}staticvoidautodetect_instantiate(Lisp_Objectimage_instance,Lisp_Objectinstantiator,Lisp_Objectpointer_fg,Lisp_Objectpointer_bg,intdest_mask,Lisp_Objectdomain){Lisp_Objectdata=find_keyword_in_vector(instantiator,Q_data);structgcprogcpro1,gcpro2,gcpro3;Lisp_Objectalist=Qnil;Lisp_Objectresult=Qnil;intis_cursor_font=0;GCPRO3(data,alist,result);alist=tagged_vector_to_alist(instantiator);if(dest_mask&IMAGE_POINTER_MASK){CONSTchar*name_ext;GET_C_STRING_FILENAME_DATA_ALLOCA(data,name_ext);if(XmuCursorNameToIndex(name_ext)!=-1){result=alist_to_tagged_vector(Qcursor_font,alist);is_cursor_font=1;}}if(!is_cursor_font)result=alist_to_tagged_vector(Qstring,alist);free_alist(alist);if(is_cursor_font)cursor_font_instantiate(image_instance,result,pointer_fg,pointer_bg,dest_mask,domain);elsestring_instantiate(image_instance,result,pointer_fg,pointer_bg,dest_mask,domain);UNGCPRO;}/********************************************************************** * Font * **********************************************************************/staticvoidfont_validate(Lisp_Objectinstantiator){data_must_be_present(instantiator);}/* XmuCvtStringToCursor is bogus in the following ways: - When it can't convert the given string to a real cursor, it will sometimes return a "success" value, after triggering a BadPixmap error. It then gives you a cursor that will itself generate BadCursor errors. So we install this error handler to catch/notice the X error and take that as meaning "couldn't convert." - When you tell it to find a cursor file that doesn't exist, it prints an error message on stderr. You can't make it not do that. - Also, using Xmu means we can't properly hack Lisp_Image_Instance objects, or XPM files, or $XBMLANGPATH. *//* Duplicate the behavior of XmuCvtStringToCursor() to bypass its bogusness. */staticintXLoadFont_got_error;staticintXLoadFont_error_handler(Display*dpy,XErrorEvent*xerror){XLoadFont_got_error=1;return0;}staticFontsafe_XLoadFont(Display*dpy,char*name){Fontfont;int(*old_handler)(Display*,XErrorEvent*);XLoadFont_got_error=0;XSync(dpy,0);old_handler=XSetErrorHandler(XLoadFont_error_handler);font=XLoadFont(dpy,name);XSync(dpy,0);XSetErrorHandler(old_handler);if(XLoadFont_got_error)return0;returnfont;}staticintfont_possible_dest_types(void){returnIMAGE_POINTER_MASK;}staticvoidfont_instantiate(Lisp_Objectimage_instance,Lisp_Objectinstantiator,Lisp_Objectpointer_fg,Lisp_Objectpointer_bg,intdest_mask,Lisp_Objectdomain){/* This function can GC */Lisp_Objectdata=find_keyword_in_vector(instantiator,Q_data);structLisp_Image_Instance*ii=XIMAGE_INSTANCE(image_instance);Lisp_Objectdevice=IMAGE_INSTANCE_DEVICE(ii);Display*dpy;XColorfg,bg;Fontsource,mask;charsource_name[MAXPATHLEN],mask_name[MAXPATHLEN],dummy;intsource_char,mask_char;intcount;Lisp_Objectforeground,background;if(!DEVICE_X_P(XDEVICE(device)))signal_simple_error("Not an X device",device);dpy=DEVICE_X_DISPLAY(XDEVICE(device));if(!STRINGP(data)||strncmp("FONT ",(char*)XSTRING_DATA(data),5))signal_simple_error("Invalid font-glyph instantiator",instantiator);if(!(dest_mask&IMAGE_POINTER_MASK))incompatible_image_types(instantiator,dest_mask,IMAGE_POINTER_MASK);foreground=find_keyword_in_vector(instantiator,Q_foreground);if(NILP(foreground))foreground=pointer_fg;background=find_keyword_in_vector(instantiator,Q_background);if(NILP(background))background=pointer_bg;generate_cursor_fg_bg(device,&foreground,&background,&fg,&bg);count=sscanf((char*)XSTRING_DATA(data),"FONT %s %d %s %d %c",source_name,&source_char,mask_name,&mask_char,&dummy);/* Allow "%s %d %d" as well... */if(count==3&&(1==sscanf(mask_name,"%d %c",&mask_char,&dummy)))count=4,mask_name[0]=0;if(count!=2&&count!=4)signal_simple_error("invalid cursor specification",data);source=safe_XLoadFont(dpy,source_name);if(!source)signal_simple_error_2("couldn't load font",build_string(source_name),data);if(count==2)mask=0;elseif(!mask_name[0])mask=source;else{mask=safe_XLoadFont(dpy,mask_name);if(!mask)/* continuable */Fsignal(Qerror,list3(build_string("couldn't load font"),build_string(mask_name),data));}if(!mask)mask_char=0;/* #### call XQueryTextExtents() and check_pointer_sizes() here. */x_initialize_pixmap_image_instance(ii,IMAGE_POINTER);IMAGE_INSTANCE_X_CURSOR(ii)=XCreateGlyphCursor(dpy,source,mask,source_char,mask_char,&fg,&bg);XIMAGE_INSTANCE_PIXMAP_FG(image_instance)=foreground;XIMAGE_INSTANCE_PIXMAP_BG(image_instance)=background;XUnloadFont(dpy,source);if(mask&&mask!=source)XUnloadFont(dpy,mask);}/********************************************************************** * Cursor-Font * **********************************************************************/staticvoidcursor_font_validate(Lisp_Objectinstantiator){data_must_be_present(instantiator);}staticintcursor_font_possible_dest_types(void){returnIMAGE_POINTER_MASK;}staticvoidcursor_font_instantiate(Lisp_Objectimage_instance,Lisp_Objectinstantiator,Lisp_Objectpointer_fg,Lisp_Objectpointer_bg,intdest_mask,Lisp_Objectdomain){/* This function can GC */Lisp_Objectdata=find_keyword_in_vector(instantiator,Q_data);structLisp_Image_Instance*ii=XIMAGE_INSTANCE(image_instance);Lisp_Objectdevice=IMAGE_INSTANCE_DEVICE(ii);Display*dpy;inti;CONSTchar*name_ext;Lisp_Objectforeground,background;if(!DEVICE_X_P(XDEVICE(device)))signal_simple_error("Not an X device",device);dpy=DEVICE_X_DISPLAY(XDEVICE(device));if(!(dest_mask&IMAGE_POINTER_MASK))incompatible_image_types(instantiator,dest_mask,IMAGE_POINTER_MASK);GET_C_STRING_FILENAME_DATA_ALLOCA(data,name_ext);if((i=XmuCursorNameToIndex(name_ext))==-1)signal_simple_error("Unrecognized cursor-font name",data);x_initialize_pixmap_image_instance(ii,IMAGE_POINTER);IMAGE_INSTANCE_X_CURSOR(ii)=XCreateFontCursor(dpy,i);foreground=find_keyword_in_vector(instantiator,Q_foreground);if(NILP(foreground))foreground=pointer_fg;background=find_keyword_in_vector(instantiator,Q_background);if(NILP(background))background=pointer_bg;maybe_recolor_cursor(image_instance,foreground,background);}staticintx_colorize_image_instance(Lisp_Objectimage_instance,Lisp_Objectforeground,Lisp_Objectbackground){structLisp_Image_Instance*p;p=XIMAGE_INSTANCE(image_instance);switch(IMAGE_INSTANCE_TYPE(p)){caseIMAGE_MONO_PIXMAP:IMAGE_INSTANCE_TYPE(p)=IMAGE_COLOR_PIXMAP;/* Make sure there aren't two pointers to the same mask, causing it to get freed twice. */IMAGE_INSTANCE_X_MASK(p)=0;break;default:return0;}{Display*dpy=DEVICE_X_DISPLAY(XDEVICE(IMAGE_INSTANCE_DEVICE(p)));Drawabledraw=XtWindow(DEVICE_XT_APP_SHELL(XDEVICE(IMAGE_INSTANCE_DEVICE(p))));Dimensiond=DEVICE_X_DEPTH(XDEVICE(IMAGE_INSTANCE_DEVICE(p)));Pixmapnew=XCreatePixmap(dpy,draw,IMAGE_INSTANCE_PIXMAP_WIDTH(p),IMAGE_INSTANCE_PIXMAP_HEIGHT(p),d);XColorcolor;XGCValuesgcv;GCgc;color=COLOR_INSTANCE_X_COLOR(XCOLOR_INSTANCE(foreground));gcv.foreground=color.pixel;color=COLOR_INSTANCE_X_COLOR(XCOLOR_INSTANCE(background));gcv.background=color.pixel;gc=XCreateGC(dpy,new,GCBackground|GCForeground,&gcv);XCopyPlane(dpy,IMAGE_INSTANCE_X_PIXMAP(p),new,gc,0,0,IMAGE_INSTANCE_PIXMAP_WIDTH(p),IMAGE_INSTANCE_PIXMAP_HEIGHT(p),0,0,1);XFreeGC(dpy,gc);IMAGE_INSTANCE_X_PIXMAP(p)=new;IMAGE_INSTANCE_PIXMAP_DEPTH(p)=d;IMAGE_INSTANCE_PIXMAP_FG(p)=foreground;IMAGE_INSTANCE_PIXMAP_BG(p)=background;return1;}}#if HAVE_SUBWINDOWS/************************************************************************//* subwindows *//************************************************************************/Lisp_ObjectQsubwindowp;staticLisp_Objectmark_subwindow(Lisp_Objectobj,void(*markobj)(Lisp_Object)){structLisp_Subwindow*sw=XSUBWINDOW(obj);returnsw->frame;}staticvoidprint_subwindow(Lisp_Objectobj,Lisp_Objectprintcharfun,intescapeflag){charbuf[100];structLisp_Subwindow*sw=XSUBWINDOW(obj);structframe*frm=XFRAME(sw->frame);if(print_readably)error("printing unreadable object #<subwindow 0x%x>",sw->header.uid);write_c_string("#<subwindow",printcharfun);sprintf(buf," %dx%d",sw->width,sw->height);write_c_string(buf,printcharfun);/* This is stolen from frame.c. Subwindows are strange in that they are specific to a particular frame so we want to print in their description what that frame is. */write_c_string(" on #<",printcharfun);if(!FRAME_LIVE_P(frm))write_c_string("dead",printcharfun);elseif(FRAME_TTY_P(frm))write_c_string("tty",printcharfun);elseif(FRAME_X_P(frm))write_c_string("x",printcharfun);elsewrite_c_string("UNKNOWN",printcharfun);write_c_string("-frame ",printcharfun);print_internal(frm->name,printcharfun,1);sprintf(buf," 0x%x>",frm->header.uid);write_c_string(buf,printcharfun);sprintf(buf,") 0x%x>",sw->header.uid);write_c_string(buf,printcharfun);}staticvoidfinalize_subwindow(void*header,intfor_disksave){structLisp_Subwindow*sw=(structLisp_Subwindow*)header;if(for_disksave)finalose(sw);if(sw->subwindow){XDestroyWindow(DisplayOfScreen(sw->xscreen),sw->subwindow);sw->subwindow=0;}}/* subwindows are equal iff they have the same window XID */staticintsubwindow_equal(Lisp_Objecto1,Lisp_Objecto2,intdepth){return(XSUBWINDOW(o1)->subwindow==XSUBWINDOW(o2)->subwindow);}staticunsignedlongsubwindow_hash(Lisp_Objectobj,intdepth){returnXSUBWINDOW(obj)->subwindow;}DEFINE_LRECORD_IMPLEMENTATION("subwindow",subwindow,mark_subwindow,print_subwindow,finalize_subwindow,subwindow_equal,subwindow_hash,structLisp_Subwindow);/* #### PROBLEM: The display routines assume that the glyph is only being displayed in one buffer. If it is in two different buffers which are both being displayed simultaneously you will lose big time. This can be dealt with in the new redisplay. *//* #### These are completely un-re-implemented in 19.14. Get it done for 19.15. */DEFUN("make-subwindow",Fmake_subwindow,0,3,0,/*Creates a new `subwindow' object of size WIDTH x HEIGHT.The default is a window of size 1x1, which is also the minimum allowedwindow size. Subwindows are per-frame. A buffer being shown in twodifferent frames will only display a subwindow glyph in the frame inwhich it was actually created. If two windows on the same frame aredisplaying the buffer then the most recently used window will actuallydisplay the window. If the frame is not specified, the selected frameis used.Subwindows are not currently implemented.*/(width,height,frame)){Display*dpy;Screen*xs;Windowpw;structframe*f;unsignedintiw,ih;XSetWindowAttributesxswa;MaskvalueMask=0;error("subwindows are not functional in 20.2; they may be again someday");f=decode_x_frame(frame);xs=LISP_DEVICE_TO_X_SCREEN(FRAME_DEVICE(f));dpy=DisplayOfScreen(xs);pw=XtWindow(FRAME_X_TEXT_WIDGET(f));if(NILP(width))iw=1;else{CHECK_INT(width);iw=XINT(width);if(iw<1)iw=1;}if(NILP(height))ih=1;else{CHECK_INT(height);ih=XINT(height);if(ih<1)ih=1;}{structLisp_Subwindow*sw=alloc_lcrecord_type(structLisp_Subwindow,lrecord_subwindow);Lisp_Objectval;sw->frame=frame;sw->xscreen=xs;sw->parent_window=pw;sw->height=ih;sw->width=iw;xswa.backing_store=Always;valueMask|=CWBackingStore;xswa.colormap=DefaultColormapOfScreen(xs);valueMask|=CWColormap;sw->subwindow=XCreateWindow(dpy,pw,0,0,iw,ih,0,CopyFromParent,InputOutput,CopyFromParent,valueMask,&xswa);XSETSUBWINDOW(val,sw);returnval;}}/* #### Should this function exist? */DEFUN("change-subwindow-property",Fchange_subwindow_property,3,3,0,/*For the given SUBWINDOW, set PROPERTY to DATA, which is a string.Subwindows are not currently implemented.*/(subwindow,property,data)){Atomproperty_atom;structLisp_Subwindow*sw;Display*dpy;CHECK_SUBWINDOW(subwindow);CHECK_STRING(property);CHECK_STRING(data);sw=XSUBWINDOW(subwindow);dpy=DisplayOfScreen(LISP_DEVICE_TO_X_SCREEN(FRAME_DEVICE(XFRAME(sw->frame))));property_atom=XInternAtom(dpy,(char*)XSTRING_DATA(property),False);XChangeProperty(dpy,sw->subwindow,property_atom,XA_STRING,8,PropModeReplace,XSTRING_DATA(data),XSTRING_LENGTH(data));returnproperty;}DEFUN("subwindowp",Fsubwindowp,1,1,0,/*Return non-nil if OBJECT is a subwindow.Subwindows are not currently implemented.*/(object)){returnSUBWINDOWP(object)?Qt:Qnil;}DEFUN("subwindow-width",Fsubwindow_width,1,1,0,/*Width of SUBWINDOW.Subwindows are not currently implemented.*/(subwindow)){CHECK_SUBWINDOW(subwindow);returnmake_int(XSUBWINDOW(subwindow)->width);}DEFUN("subwindow-height",Fsubwindow_height,1,1,0,/*Height of SUBWINDOW.Subwindows are not currently implemented.*/(subwindow)){CHECK_SUBWINDOW(subwindow);returnmake_int(XSUBWINDOW(subwindow)->height);}DEFUN("subwindow-xid",Fsubwindow_xid,1,1,0,/*Return the xid of SUBWINDOW as a number.Subwindows are not currently implemented.*/(subwindow)){CHECK_SUBWINDOW(subwindow);returnmake_int(XSUBWINDOW(subwindow)->subwindow);}DEFUN("resize-subwindow",Fresize_subwindow,1,3,0,/*Resize SUBWINDOW to WIDTH x HEIGHT.If a value is nil that parameter is not changed.Subwindows are not currently implemented.*/(subwindow,width,height)){intneww,newh;structLisp_Subwindow*sw;CHECK_SUBWINDOW(subwindow);sw=XSUBWINDOW(subwindow);if(NILP(width))neww=sw->width;elseneww=XINT(width);if(NILP(height))newh=sw->height;elsenewh=XINT(height);XResizeWindow(DisplayOfScreen(sw->xscreen),sw->subwindow,neww,newh);sw->height=newh;sw->width=neww;returnsubwindow;}DEFUN("force-subwindow-map",Fforce_subwindow_map,1,1,0,/*Generate a Map event for SUBWINDOW.Subwindows are not currently implemented.*/(subwindow)){CHECK_SUBWINDOW(subwindow);XMapWindow(DisplayOfScreen(XSUBWINDOW(subwindow)->xscreen),XSUBWINDOW(subwindow)->subwindow);returnsubwindow;}#endif/************************************************************************//* initialization *//************************************************************************/voidsyms_of_glyphs_x(void){#if HAVE_SUBWINDOWSdefsymbol(&Qsubwindowp,"subwindowp");DEFSUBR(Fmake_subwindow);DEFSUBR(Fchange_subwindow_property);DEFSUBR(Fsubwindowp);DEFSUBR(Fsubwindow_width);DEFSUBR(Fsubwindow_height);DEFSUBR(Fsubwindow_xid);DEFSUBR(Fresize_subwindow);DEFSUBR(Fforce_subwindow_map);#endif}voidconsole_type_create_glyphs_x(void){/* image methods */CONSOLE_HAS_METHOD(x,print_image_instance);CONSOLE_HAS_METHOD(x,finalize_image_instance);CONSOLE_HAS_METHOD(x,image_instance_equal);CONSOLE_HAS_METHOD(x,image_instance_hash);CONSOLE_HAS_METHOD(x,colorize_image_instance);CONSOLE_HAS_METHOD(x,init_image_instance_from_eimage);CONSOLE_HAS_METHOD(x,locate_pixmap_file);#ifdef HAVE_XPMCONSOLE_HAS_METHOD(x,xpm_instantiate);#endifCONSOLE_HAS_METHOD(x,xbm_instantiate);}voidimage_instantiator_format_create_glyphs_x(void){INITIALIZE_IMAGE_INSTANTIATOR_FORMAT(cursor_font,"cursor-font");IIFORMAT_HAS_METHOD(cursor_font,validate);IIFORMAT_HAS_METHOD(cursor_font,possible_dest_types);IIFORMAT_HAS_METHOD(cursor_font,instantiate);IIFORMAT_VALID_KEYWORD(cursor_font,Q_data,check_valid_string);IIFORMAT_VALID_KEYWORD(cursor_font,Q_foreground,check_valid_string);IIFORMAT_VALID_KEYWORD(cursor_font,Q_background,check_valid_string);INITIALIZE_IMAGE_INSTANTIATOR_FORMAT(font,"font");IIFORMAT_HAS_METHOD(font,validate);IIFORMAT_HAS_METHOD(font,possible_dest_types);IIFORMAT_HAS_METHOD(font,instantiate);IIFORMAT_VALID_KEYWORD(font,Q_data,check_valid_string);IIFORMAT_VALID_KEYWORD(font,Q_foreground,check_valid_string);IIFORMAT_VALID_KEYWORD(font,Q_background,check_valid_string);#ifdef HAVE_XFACEINITIALIZE_IMAGE_INSTANTIATOR_FORMAT(xface,"xface");IIFORMAT_HAS_METHOD(xface,validate);IIFORMAT_HAS_METHOD(xface,normalize);IIFORMAT_HAS_METHOD(xface,possible_dest_types);IIFORMAT_HAS_METHOD(xface,instantiate);IIFORMAT_VALID_KEYWORD(xface,Q_data,check_valid_string);IIFORMAT_VALID_KEYWORD(xface,Q_file,check_valid_string);IIFORMAT_VALID_KEYWORD(xface,Q_hotspot_x,check_valid_int);IIFORMAT_VALID_KEYWORD(xface,Q_hotspot_y,check_valid_int);IIFORMAT_VALID_KEYWORD(xface,Q_foreground,check_valid_string);IIFORMAT_VALID_KEYWORD(xface,Q_background,check_valid_string);#endifINITIALIZE_IMAGE_INSTANTIATOR_FORMAT(autodetect,"autodetect");IIFORMAT_HAS_METHOD(autodetect,validate);IIFORMAT_HAS_METHOD(autodetect,normalize);IIFORMAT_HAS_METHOD(autodetect,possible_dest_types);IIFORMAT_HAS_METHOD(autodetect,instantiate);IIFORMAT_VALID_KEYWORD(autodetect,Q_data,check_valid_string);}voidvars_of_glyphs_x(void){#ifdef HAVE_XFACEFprovide(Qxface);#endifDEFVAR_LISP("x-bitmap-file-path",&Vx_bitmap_file_path/*A list of the directories in which X bitmap files may be found.If nil, this is initialized from the "*bitmapFilePath" resource.This is used by the `make-image-instance' function (however, note that ifthe environment variable XBMLANGPATH is set, it is consulted first).*/);Vx_bitmap_file_path=Qnil;}voidcomplex_vars_of_glyphs_x(void){#define BUILD_GLYPH_INST(variable, name) \ Fadd_spec_to_specifier \ (GLYPH_IMAGE (XGLYPH (variable)), \ vector3 (Qxbm, Q_data, \ list3 (make_int (name##_width), \ make_int (name##_height), \ make_ext_string (name##_bits, \ sizeof (name##_bits), \ FORMAT_BINARY))), \ Qglobal, Qx, Qnil)BUILD_GLYPH_INST(Vtruncation_glyph,truncator);BUILD_GLYPH_INST(Vcontinuation_glyph,continuer);BUILD_GLYPH_INST(Vxemacs_logo,xemacs);BUILD_GLYPH_INST(Vhscroll_glyph,hscroll);#undef BUILD_GLYPH_INST}