All pastes #476656 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-05-08 07:55 UTC
rendered paste body
From 6a6c9449fe72c529562ad25198a7d6bc334cfb37 Mon Sep 17 00:00:00 2001
From: Roi Cohen <roico@roico-desktop.(none)>
Date: Tue, 8 May 2007 10:52:49 +0300
Subject: [PATCH] transparent cube

---
 metadata/cube.xml.in |   29 +++++++
 plugins/cube.c       |  200 +++++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 204 insertions(+), 25 deletions(-)

diff --git a/metadata/cube.xml.in b/metadata/cube.xml.in
index f128f09..ea17ebd 100644
--- a/metadata/cube.xml.in
+++ b/metadata/cube.xml.in
@@ -123,6 +123,35 @@
 		<_long>Adjust top face image to rotation</_long>
 		<default>false</default>
 	    </option>
+	    <option name="active_opacity" type="float">
+		<_short>Opacity During Rotation</_short>
+		<_long>Opacity of desktop window during rotation.</_long>
+		<default>30.0</default>
+		<min>0.0</min>
+		<max>100.0</max>
+		<precision>1.0</precision>
+	    </option>
+	    <option name="inactive_opacity" type="float">
+		<_short>Opacity When Not Rotating</_short>
+		<_long>Opacity of desktop window when not rotating.</_long>
+		<default>100.0</default>
+		<min>0.0</min>
+		<max>100.0</max>
+		<precision>1.0</precision>
+	    </option>
+	    <option name="fade_time" type="float">
+		<_short>Fade Time</_short>
+		<_long>Desktop Window Opacity Fade Time.</_long>
+		<default>1.0</default>
+		<min>0.0</min>
+		<max>10.0</max>
+		<precision>0.1</precision>
+	    </option>
+	    <option name="transparent_manual_only" type="bool">
+		<_short>Transparency Only on Mouse Rotate</_short>
+		<_long>Initiates Cube transparency only if rotation is mouse driven.</_long>
+		<default>true</default>
+	    </option>
 	</screen>
     </plugin>
 </compiz>
diff --git a/plugins/cube.c b/plugins/cube.c
index 5617c82..ac8b073 100644
--- a/plugins/cube.c
+++ b/plugins/cube.c
@@ -70,22 +70,26 @@ typedef struct _CubeDisplay {
     HandleCompizEventProc handleCompizEvent;
 } CubeDisplay;
 
-#define CUBE_SCREEN_OPTION_COLOR	      0
-#define CUBE_SCREEN_OPTION_IN		      1
-#define CUBE_SCREEN_OPTION_SCALE_IMAGE	      2
-#define CUBE_SCREEN_OPTION_IMAGES	      3
-#define CUBE_SCREEN_OPTION_SKYDOME	      4
-#define CUBE_SCREEN_OPTION_SKYDOME_IMG	      5
-#define CUBE_SCREEN_OPTION_SKYDOME_ANIM	      6
-#define CUBE_SCREEN_OPTION_SKYDOME_GRAD_START 7
-#define CUBE_SCREEN_OPTION_SKYDOME_GRAD_END   8
-#define CUBE_SCREEN_OPTION_ACCELERATION	      9
-#define CUBE_SCREEN_OPTION_SPEED	      10
-#define CUBE_SCREEN_OPTION_TIMESTEP	      11
-#define CUBE_SCREEN_OPTION_MIPMAP	      12
-#define CUBE_SCREEN_OPTION_BACKGROUNDS	      13
-#define CUBE_SCREEN_OPTION_ADJUST_IMAGE	      14
-#define CUBE_SCREEN_OPTION_NUM                15
+#define CUBE_SCREEN_OPTION_COLOR	           0
+#define CUBE_SCREEN_OPTION_IN		           1
+#define CUBE_SCREEN_OPTION_SCALE_IMAGE	           2
+#define CUBE_SCREEN_OPTION_IMAGES	           3
+#define CUBE_SCREEN_OPTION_SKYDOME	           4
+#define CUBE_SCREEN_OPTION_SKYDOME_IMG	           5
+#define CUBE_SCREEN_OPTION_SKYDOME_ANIM	           6
+#define CUBE_SCREEN_OPTION_SKYDOME_GRAD_START      7
+#define CUBE_SCREEN_OPTION_SKYDOME_GRAD_END        8
+#define CUBE_SCREEN_OPTION_ACCELERATION	           9
+#define CUBE_SCREEN_OPTION_SPEED	           10
+#define CUBE_SCREEN_OPTION_TIMESTEP	           11
+#define CUBE_SCREEN_OPTION_MIPMAP	           12
+#define CUBE_SCREEN_OPTION_BACKGROUNDS	           13
+#define CUBE_SCREEN_OPTION_ADJUST_IMAGE	           14
+#define CUBE_SCREEN_OPTION_ACTIVE_OPACITY          15
+#define CUBE_SCREEN_OPTION_INACTIVE_OPACITY        16
+#define CUBE_SCREEN_OPTION_FADE_TIME               17
+#define CUBE_SCREEN_OPTION_TRANSPARENT_MANUAL_ONLY 18
+#define CUBE_SCREEN_OPTION_NUM                     19
 
 #define MULTM(x, y, z) \
 z[0] = x[0] * y[0] + x[4] * y[1] + x[8] * y[2] + x[12] * y[3]; \
@@ -130,6 +134,7 @@ typedef struct _CubeScreen {
     PaintScreenProc	       paintScreen;
     PaintTransformedScreenProc paintTransformedScreen;
     PaintBackgroundProc        paintBackground;
+    PaintWindowProc            paintWindow;
     ApplyScreenTransformProc   applyScreenTransform;
     SetScreenOptionProc	       setScreenOption;
     OutputChangeNotifyProc     outputChangeNotify;
@@ -180,6 +185,9 @@ typedef struct _CubeScreen {
     
     Bool disableCaps;
     Bool paintAllViewports;
+    
+    float desktopOpacity;
+    float toOpacity;
 
     CompTexture *bg;
     int		nBg;
@@ -1091,6 +1099,39 @@ cubePreparePaintScreen (CompScreen *s,
     }
 
     memset (cs->cleared, 0, sizeof (Bool) * s->nOutputDev);
+    
+    //transparency, mostly copied/ported from beryl's cube.c, orig. by onestone
+    if (cs->rotationMode != NoRotation && ((cs->rotationMode == RotationManual) || 
+		!(cs->opt[CUBE_SCREEN_OPTION_TRANSPARENT_MANUAL_ONLY].value.b)))
+    {
+	cs->toOpacity = 
+	      (cs->opt[CUBE_SCREEN_OPTION_ACTIVE_OPACITY].value.f / 100.0f) * OPAQUE;
+    }
+    else
+	cs->toOpacity =
+	    (cs->opt[CUBE_SCREEN_OPTION_INACTIVE_OPACITY].value.f / 100.0f) * OPAQUE;
+
+    if (cs->opt[CUBE_SCREEN_OPTION_FADE_TIME].value.f == 0.0f)
+	cs->desktopOpacity = cs->toOpacity;
+
+    else if (cs->desktopOpacity != cs->toOpacity)
+    {
+	float steps = (msSinceLastPaint * OPAQUE / 1000.0) /
+	    cs->opt[CUBE_SCREEN_OPTION_FADE_TIME].value.f;
+	if (steps < 12)
+	    steps = 12;
+
+	if (cs->toOpacity > cs->desktopOpacity)
+	{
+	    cs->desktopOpacity += steps;
+	    cs->desktopOpacity = MIN(cs->toOpacity, cs->desktopOpacity);
+	}
+	if (cs->toOpacity < cs->desktopOpacity)
+	{
+	    cs->desktopOpacity -= steps;
+	    cs->desktopOpacity = MAX(cs->toOpacity, cs->desktopOpacity);
+	}
+    }
 
     UNWRAP (cs, s, preparePaintScreen);
     (*s->preparePaintScreen) (s, msSinceLastPaint);
@@ -1110,7 +1151,7 @@ cubePaintScreen (CompScreen		 *s,
 
     CUBE_SCREEN (s);
 
-    if (cs->grabIndex)
+    if (cs->grabIndex || cs->desktopOpacity != OPAQUE)
     {
 	mask &= ~PAINT_SCREEN_REGION_MASK;
 	mask |= PAINT_SCREEN_TRANSFORMED_MASK;
@@ -1130,7 +1171,7 @@ cubeDonePaintScreen (CompScreen *s)
 {
     CUBE_SCREEN (s);
 
-    if (cs->grabIndex)
+    if (cs->grabIndex || cs->desktopOpacity != cs->toOpacity)
 	damageScreen (s);
 
     UNWRAP (cs, s, donePaintScreen);
@@ -1389,14 +1430,14 @@ cubePaintCaps (CompScreen          *s,
     CUBE_SCREEN(s);
 
     if (!clear && cs->grabIndex == 0 && hsize > 2 &&
-	(cs->invert != 1 || sAttrib->vRotate != 0.0f || sAttrib->yTranslate != 0.0f))
+	(cs->invert != 1 || sAttrib->vRotate != 0.0f || sAttrib->yTranslate != 0.0f || cs->desktopOpacity != OPAQUE))
     {
 	CompTransform sTransform = *transform;
 	float	      yRotate = (360.0f / hsize) * (cs->xrotations + 1);
 
 	screenLighting (s, TRUE);
 
-	glColor3usv (cs->color);
+	glColor4us (cs->color[0], cs->color[1], cs->color[2], cs->desktopOpacity);
 
 	glPushMatrix ();
 
@@ -1411,6 +1452,14 @@ cubePaintCaps (CompScreen          *s,
 	glScalef (cs->outputXScale, cs->outputYScale, 1.0f);
 
 	sAttrib->yRotate -= yRotate;
+	
+	
+	if (cs->desktopOpacity != OPAQUE)
+	{
+	    screenTexEnvMode (s, GL_MODULATE);
+	    glEnable(GL_BLEND);
+	    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+	}
 
 	glVertexPointer (3, GL_FLOAT, 0, cs->vertices);
 	
@@ -1449,6 +1498,10 @@ cubePaintCaps (CompScreen          *s,
 	
 	glColor4usv (defaultColor);
 	glEnableClientState (GL_TEXTURE_COORD_ARRAY);
+	
+	screenTexEnvMode(s, GL_REPLACE);
+	glDisable(GL_BLEND);
+	glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
     }
 }
 
@@ -1466,6 +1519,7 @@ cubePaintTransformedScreen (CompScreen		    *s,
     GLenum            filter = s->display->textureFilter;
     PaintOrder        paintOrder;
     Bool              clear;
+    Bool              wasCulled = FALSE;
     
     CUBE_SCREEN (s);
     
@@ -1474,6 +1528,13 @@ cubePaintTransformedScreen (CompScreen		    *s,
 
     hsize = s->hsize * cs->nOutput;
     size  = hsize;
+    
+    if (cs->desktopOpacity != OPAQUE)
+    {
+	wasCulled = glIsEnabled(GL_CULL_FACE);
+	if (wasCulled)
+	    glDisable(GL_CULL_FACE);
+    }
 
     if (!cs->fullscreenOutput)
     {
@@ -1557,7 +1618,7 @@ cubePaintTransformedScreen (CompScreen		    *s,
     else
 	paintOrder = BTF;	// Inside cube -  start with BTF faces.
     
-    if (cs->paintAllViewports || cs->invert == -1)
+    if (cs->paintAllViewports || cs->desktopOpacity != OPAQUE || cs->invert == -1)
 	cubePaintAllViewports(s, &sa, transform, region, output, mask, xMove, size, hsize, paintOrder);
     
     cubePaintCaps (s, &sa, transform, clear, output, hsize);	// Paint caps between BTF faces and FTB faces.
@@ -1567,10 +1628,13 @@ cubePaintTransformedScreen (CompScreen		    *s,
     else
 	paintOrder = FTB;	// Inside cube -  continue with FTB faces.	
     
-    if (cs->paintAllViewports || cs->invert == 1)
+    if (cs->paintAllViewports || cs->desktopOpacity != OPAQUE || cs->invert == 1)
 	cubePaintAllViewports(s, &sa, transform, region, output, mask, xMove, size, hsize, paintOrder);
     
     s->display->textureFilter = filter;
+    
+    if (cs->desktopOpacity != OPAQUE && wasCulled)
+	glEnable(GL_CULL_FACE);
 
     WRAP (cs, s, paintTransformedScreen, cubePaintTransformedScreen);
 }
@@ -1642,6 +1706,50 @@ cubeStackPrev (CompWindow* window)
 }
 
 static void
+cubeSetBackgroundOpacity(CompScreen* s)
+{
+    CUBE_SCREEN(s);
+
+    if (cs->desktopOpacity != OPAQUE)
+    {
+	if (s->desktopWindowCount)
+	{
+	    glColor4us(0, 0, 0, 0);
+	    glEnable(GL_BLEND);
+	}
+	else
+	{
+	    glColor4us(0xffff, 0xffff, 0xffff, cs->desktopOpacity);
+	    glEnable(GL_BLEND);
+	    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+	    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
+	}
+    }
+}
+
+static void
+cubeUnSetBackgroundOpacity(CompScreen* s)
+{
+    CUBE_SCREEN(s);
+
+    if (cs->desktopOpacity != OPAQUE)
+    {
+	if (s->desktopWindowCount)
+	{
+	    glColor3usv(defaultColor);
+	    glDisable(GL_BLEND);
+	}
+	else
+	{
+	    glColor3usv(defaultColor);
+	    glDisable(GL_BLEND);
+	    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
+	    screenTexEnvMode(s, GL_REPLACE);
+	}
+    }
+}
+
+static void
 cubePaintBackground (CompScreen   *s,
 		     Region	  region,
 		     unsigned int mask)
@@ -1650,6 +1758,8 @@ cubePaintBackground (CompScreen   *s,
 
     CUBE_SCREEN (s);
 
+    cubeSetBackgroundOpacity(s);
+
     n = cs->opt[CUBE_SCREEN_OPTION_BACKGROUNDS].value.list.nValue;
     if (n)
     {
@@ -1659,13 +1769,17 @@ cubePaintBackground (CompScreen   *s,
 	GLfloat     *d, *data;
 
 	if (!nBox)
-	    return;
+	{
+	    cubeUnSetBackgroundOpacity(s);
+ 	    return;
+	}
 
 	n = s->x % n;
 
 	if (s->desktopWindowCount)
 	{
 	    cubeUnloadBackgrounds (s);
+	    cubeUnSetBackgroundOpacity(s);
 	    return;
 	}
 	else
@@ -1678,7 +1792,10 @@ cubePaintBackground (CompScreen   *s,
 
 	data = malloc (sizeof (GLfloat) * nBox * 16);
 	if (!data)
-	    return;
+	{
+	    cubeUnSetBackgroundOpacity(s);
+ 	    return;
+	}
 
 	d = data;
 	n = nBox;
@@ -1735,6 +1852,31 @@ cubePaintBackground (CompScreen   *s,
 	(*s->paintBackground) (s, region, mask);
 	WRAP (cs, s, paintBackground, cubePaintBackground);
     }
+    
+    cubeUnSetBackgroundOpacity(s);
+}
+
+static Bool
+cubePaintWindow (CompWindow		  *w,
+		 const WindowPaintAttrib  *attrib,
+		 const CompTransform	  *transform,
+		 Region			  region,
+		 unsigned int		  mask)
+{
+    Bool status;
+    CompScreen* s = w->screen;
+    CUBE_SCREEN(s);
+    
+    WindowPaintAttrib wa = *attrib;
+
+    if (w->type & CompWindowTypeDesktopMask)
+	wa.opacity = cs->desktopOpacity;
+
+    UNWRAP (cs, s, paintWindow);
+    status = (*s->paintWindow) (w, &wa, transform, region, mask);
+    WRAP (cs, s, paintWindow, cubePaintWindow);
+    
+    return status;
 }
 
 static void cubeSetClipPlanes (CompScreen *s, Region region, int output)
@@ -2146,7 +2288,11 @@ static const CompMetadataOptionInfo cubeScreenOptionInfo[] = {
     { "timestep", "float", "<min>0.1</min>", 0, 0 },
     { "mipmap", "bool", 0, 0, 0 },
     { "backgrounds", "list", "<type>string</type>", 0, 0 },
-    { "adjust_image", "bool", 0, 0, 0 }
+    { "adjust_image", "bool", 0, 0, 0 },
+    { "active_opacity", "float", "<min>0.0</min><max>100.0</max>", 0, 0 },
+    { "inactive_opacity", "float", "<min>0.0</min><max>100.0</max>", 0, 0 },
+    { "fade_time", "float", "<min>0.0</min>", 0, 0 },
+    { "transparent_manual_only", "bool", 0, 0, 0 }
 };
 
 static Bool
@@ -2208,6 +2354,8 @@ cubeInitScreen (CompPlugin *p,
     cs->disableCaps = FALSE;
     cs->paintAllViewports = FALSE;
     cs->rotationMode = NoRotation;
+    
+    cs->desktopOpacity = OPAQUE;
 
     memset (cs->cleared, 0, sizeof (cs->cleared));
 
@@ -2231,6 +2379,7 @@ cubeInitScreen (CompPlugin *p,
     WRAP (cs, s, paintScreen, cubePaintScreen);
     WRAP (cs, s, paintTransformedScreen, cubePaintTransformedScreen);
     WRAP (cs, s, paintBackground, cubePaintBackground);
+    WRAP (cs, s, paintWindow, cubePaintWindow);
     WRAP (cs, s, applyScreenTransform, cubeApplyScreenTransform);
     WRAP (cs, s, setScreenOption, cubeSetGlobalScreenOption);
     WRAP (cs, s, outputChangeNotify, cubeOutputChangeNotify);
@@ -2257,6 +2406,7 @@ cubeFiniScreen (CompPlugin *p,
     UNWRAP (cs, s, paintScreen);
     UNWRAP (cs, s, paintTransformedScreen);
     UNWRAP (cs, s, paintBackground);
+    UNWRAP (cs, s, paintWindow);
     UNWRAP (cs, s, applyScreenTransform);
     UNWRAP (cs, s, setScreenOption);
     UNWRAP (cs, s, outputChangeNotify);
-- 
1.4.4.2