/* -*-mode:java; c-basic-offset:2; -*- */ /* WeirdX - Guess. * * Copyright (C) 1999-2004 JCraft, Inc. * * 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., 675 Mass Ave, Cambridge, MA 02139, USA. */ package com.jcraft.weirdx; import java.io.*; final class GC extends Resource { static final byte GXclear=0; static final byte GXand=1; static final byte GXandReverse=2; static final byte GXcopy=3; static final byte GXandInverted=4; static final byte GXnoop=5; static final byte GXxor=6; static final byte GXor=7; static final byte GXnor=8; static final byte GXequiv=9; static final byte GXinvert=10; static final byte GXorReverse=11; static final byte GXcopyInverted=12; static final byte GXorInverted=13; static final byte GXnand=14; static final byte GXset=15; static final int lineStyleOffset=0; static final int lineStyle=(3<0){ io.readPad(n); } gc.time=0; } static void reqSetClipRectangles(Client c) throws IOException{ int foo, ordering; int n; IO io=c.client; ordering=c.data; n=c.length; foo=io.readInt(); c.length-=2; GC gc=c.lookupGC(foo); if(gc==null){ c.errorValue=foo; c.errorReason=13; // GC return; } gc.clip_x_origin=io.readShort(); gc.clip_y_origin=io.readShort(); c.length--; short x, y; int w, h; n=n-3; ClipRectangles cr=null; if(gc.clip_mask!=null){ if(gc.clip_mask instanceof ClipPixmap){ try{((Pixmap)(gc.clip_mask.getMask())).delete();} catch(Exception e){} gc.clip_mask=null; } } if(cr==null){ cr=new ClipRectangles(ordering, n/2); gc.clip_mask=cr; } while(n!=0){ x=(short)io.readShort(); y=(short)io.readShort(); w=io.readShort(); h=io.readShort(); n-=2; cr.add(x, y, w, h); } cr.validate(); c.length=0; } static void reqFreeGC(Client c) throws IOException{ int foo; IO io=c.client; foo=io.readInt(); c.length-=2; GC gc=c.lookupGC(foo); if(gc==null){ c.errorValue=foo; c.errorReason=13; // GC return; } Resource.freeResource(foo, Resource.RT_NONE); } void delete() throws IOException{ dash=null; if(stipple!=null){ try{stipple.delete();}catch(Exception e){} stipple=null; } if(clip_mask!=null){ try{ if(clip_mask instanceof ClipPixmap){ ((Pixmap)(clip_mask.getMask())).delete(); } }catch(Exception e){} clip_mask=null; } if(tile.pixmap!=null){ try{tile.pixmap.delete();}catch(Exception e){} tile.pixmap=null; tile=null; } put(this); } static void reqCopyGC(Client c) throws IOException{ int foo; IO io=c.client; foo=io.readInt(); c.length-=2; GC srcgc=c.lookupGC(foo); if(srcgc==null){ c.errorValue=foo; c.errorReason=13; // GC return; } foo=io.readInt(); c.length--; GC dstgc=c.lookupGC(foo); if(dstgc==null){ c.errorValue=foo; c.errorReason=13; // GC return; } int msk=io.readInt(); c.length--; msk&=0x7fffff; dstgc.copyAttr(c, msk, srcgc); } static void reqChangeGC(Client c) throws IOException{ int foo; int n; IO io=c.client; n=c.length; foo=io.readInt(); c.length-=2; GC gc=c.lookupGC(foo); if(gc==null){ c.errorValue=foo; c.errorReason=13; // GC return; } foo=io.readInt(); c.length--; foo&=0x7fffff; if(foo!=0){ gc.changeAttr(c, foo); } } static void reqCreateGC(Client c) throws IOException{ int foo; IO io=c.client; int cid=io.readInt(); foo=io.readInt(); c.length-=3; Drawable d=c.lookupDrawable(foo); if(d==null){ c.errorValue=foo; c.errorReason=9; // Drawable return; } int mask=io.readInt(); c.length--; GC gc=getGC(cid, d); mask&=0x7fffff; if(mask!=0){ gc.changeAttr(c, mask); } Resource.add(gc); } private void copyAttr(Client c, int vmask, GC srcgc) throws IOException{ int index=0; int foo; int mask=vmask; while(mask!=0){ index=lowbit(mask); mask&=~index; switch(index){ case GCFunction: function=(byte)srcgc.function; break; case GCPlaneMask: break; case GCForeground: fgPixel=srcgc.fgPixel; if((attr&tileIsPixel)==0 && tile.pixmap==null){ attr|=tileIsPixel; tile.pixel=fgPixel; } break; case GCBackground: bgPixel=srcgc.bgPixel; break; case GCLineWidth: lineWidth=srcgc.lineWidth; break; case GCLineStyle: attr&=~lineStyle; attr|=(srcgc.attr&lineStyle); break; case GCCapStyle: attr&=~capStyle; attr|=(srcgc.attr&capStyle); break; case GCJoinStyle: attr&=~joinStyle; attr|=(srcgc.attr&joinStyle); break; case GCFillStyle: attr&=~fillStyle; attr|=(srcgc.attr&fillStyle); break; case GCFillRule: attr&=~fillRule; attr|=(srcgc.attr&fillRule); break; case GCTile: // tile=srcgc.tile; if((srcgc.attr&tileIsPixel)!=0){ tile.pixel=srcgc.tile.pixel; } else{ tile.pixmap=srcgc.tile.pixmap; } break; case GCStipple: if(stipple!=null){ try{stipple.delete();}catch(Exception e){} stipple=null; } stipple=srcgc.stipple; if(stipple!=null){ stipple.ref(); } break; case GCTileStipXOrigin: tile_stipple_x_origin=srcgc.tile_stipple_x_origin; break; case GCTileStipYOrigin: tile_stipple_y_origin=srcgc.tile_stipple_y_origin; break; case GCFont: font=srcgc.font; break; case GCSubwindowMode: attr&=~subWindowMode; attr|=(srcgc.attr&subWindowMode); break; case GCGraphicsExposures: attr&=~graphicsExposures; attr|=(srcgc.attr&graphicsExposures); break; case GCClipXOrigin: clip_x_origin=srcgc.clip_x_origin; break; case GCClipYOrigin: clip_y_origin=srcgc.clip_y_origin; break; case GCClipMask: if(clip_mask!=null){ try{ if(clip_mask instanceof ClipPixmap){ ((Pixmap)(clip_mask.getMask())).delete(); } }catch(Exception e){} clip_mask=null; } clip_mask=srcgc.clip_mask; if(clip_mask!=null){ if(clip_mask instanceof ClipPixmap){ ((Pixmap)(clip_mask.getMask())).ref(); } } break; case GCDashOffset: dash_phase=srcgc.dash_phase; break; case GCDashList: dash=srcgc.dash; if(dash!=null){ float[] bar=new float[dash.length]; System.arraycopy(dash, 0, bar, 0, bar.length); dash=bar; } break; case GCArcMode: // not implemented break; default: c.errorValue=vmask; c.errorReason=2; // BadValue; } if(c.errorReason!=0){ return; } } if(c.length!=0){ c.errorValue=vmask; c.errorReason=2; // BadValue; } else{ time=c.seq; } } private void changeAttr(Client c, int vmask) throws IOException{ int index=0; int foo; int mask=vmask; IO io=c.client; while(mask!=0){ index=lowbit(mask); mask&=~index; c.length--; switch(index){ case GCFunction: foo=io.readInt(); foo&=0xff; function=(byte)foo; break; case GCPlaneMask: foo=io.readInt(); break; case GCForeground: foo=io.readInt(); fgPixel=foo; if((attr&tileIsPixel)==0 && tile.pixmap==null){ attr|=tileIsPixel; tile.pixel=fgPixel; } // fgPixel&=0xff; // depth 8 break; case GCBackground: bgPixel=io.readInt(); // bgPixel&=0xff; // depth 8 break; case GCLineWidth: foo=io.readInt(); foo&=0xffff; if(foo==0) foo=1; lineWidth=(short)foo; break; case GCLineStyle: foo=io.readInt(); foo&=0xff; foo=(foo<>lineStyleOffset; c.errorReason=2; // BadValue break; } attr&=~lineStyle; attr|=foo; break; case GCCapStyle: foo=io.readInt(); foo&=0xff; foo=(foo<>capStyleOffset; c.errorReason=2; // BadValue break; } attr|=foo; break; case GCJoinStyle: foo=io.readInt(); foo&=0xff; foo=(foo<>joinStyleOffset; c.errorReason=2; // BadValue break; } attr|=foo; break; case GCFillStyle: foo=io.readInt(); foo&=0xff; attr=(attr&~fillStyle)|(foo<>1; } return result; } static private GC getGC(int id, Drawable d){ GC gc=get(); if(gc==null){ return new GC(id, d);} gc.id=id; gc.drawable=d; gc.init(); return gc; } static private GC pool[]=new GC[32]; static private boolean full=false; static private synchronized GC get(){ GC tmp=null; if(full) full=false; for(int i=0; i<32; i++){ if(pool[i]!=null){ tmp=pool[i]; pool[i]=null; break;} } return tmp; } static private synchronized void put(GC gc){ if(full) return; for(int i=0; i<32; i++){ if(pool[i]==null){ pool[i]=gc; return; } } full=true; } }