Enable or disable Compiz with one click

9 11 2008

Every decent Linux user knows what Compiz is: an excuse to put load on the system by giving you windows that look like Steve Ballmer’s brain out of his skull — that is, if he had a brain — and some other fancy effects like turning your desktop into a fishtank-cube and stuff. But anyways, sometimes this wonderful, indispensable piece of technology makes it hard for us to play the numerous great games Linux has.

Wouldn’t it be nice to have some kind of button to enable or disable Compiz at will? Just one click to disable it, and launch your game. After you finished playing, just click again, and tadaa! Compiz is back, in full glory?

Well, it’s not that difficult. Continue reading for an easy guide on how to make a script that does the work for you.

Part 1

First, gain root permissions and open your favorite text editor, for example:
sudo nano /usr/bin/compizswitch.sh
or:

su
vi /usr/bin/compizswitch.sh

Next, you’ll need to put the actual script in:

#!/bin/bash
if [ `cat $HOME/.compiz-enabled` == "true" ]; then
$DEFAULT_WM --replace &
echo "false" > $HOME/.compiz-enabled
else
compiz --replace &
echo "true" > $HOME/.compiz-enabled
fi

Don’t bother yet wat this script will do, believe me, it will work.

Next, chmod a+x this script. You’ll need to be root for this ( sudo or su )

When that’s done, you’ll need to do some other stuff. Replace nano by your favorite editor:
nano ~/.bash_profile
Append this:
export DEFAULT_WM=mydefaultwm
echo "true" > ~/.compiz-enabled

Replace mydefaultwm with the default Window Manager for your deskop. For Gnome, this is metacity, for KDE it’s kwin.

If Compiz isn’t started automatically after you log in, replace true with false.

Now test if the script works (maybe you made a mistake, but it could also be that I wrote an error in my script — please tell me if this is the case). Logout, and login again. At the command prompt, type (without root rights!)
compizswitch.sh

Now, compiz would normally be disabled if you had it enabled, or vice versa. Type again:

compizswitch.sh

Now compiz would be back enabled/disabled.

Part 2: the button

While we have a nice script now, it’s not a lot easier than just typing kwin –replace &, compiz –replace &, or metacity –replace & in the command prompt. So we’re gonna make a button to do this.

First, create a starter/shortcut to application on your panel. Please refer to the documentation of your desktop environment on how to do this ( hey, I’m typing this on Mac OS X, you know :P ).

Next, when you have to specify the path to the executable, type /usr/bin/compizswitch.sh.

Give it a good icon, like a compiz logo, or something else you like. Heck, you can even use a penguin, or an Apple logo (  — if you don’t see a box or 3 lines, you’re the best), it doesn’t matter, your — actually, my — script will work anyways. Just make sure you’ll remember within three months when you finally boot Linux again — that’s why people discourage dual-boots (j/k).

Click it. It will work as expected. If not, see if you didn’t miss a step. If it still doesn’t work, please give me as much information as possible. I only tested this on KDE 4, so…

Have a lot of fun with 3d gaming!

- SeySayux


Actions

Information

4 responses

26 11 2008
okokokaynow

Hi there. I’m a Linux newbie trying to use your tutorial to make a compiz switch for my Acer Aspire One. It runs Linpus.

I created compizswitch with the following in it:

#!/bin/bash
if [ `cat $HOME/.compiz-enabled` == "true" ]; then
$DEFAULT_WM –replace &
echo “false” > $HOME/.compiz-enabled
else
compiz –replace &
echo “true” > $HOME/.compiz-enabled
fi

Edited bash_profile so it looks like this:

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:/sbin/:/usr/sbin/:$HOME/bin

export PATH
unset USERNAME

if [ `tty` = "/dev/tty1" ];then
while true
do
#xpid=
#xpid=`pidof X`
if [ -e /tmp/.X11-unix/X0 ];then
DISPLAY=:0.0
export DISPLAY

if [ ! -e /usr/share/oobe/oobe.log ];then
sudo modprobe psmouse
sudo /usr/sbin/oobe >/usr/share/oobe/oobe.log 2>&1
fi
while true
do
opid=`pidof oobe`
if [ "X${opid}" = "X" ];then
startxfce4 &
break
fi
done
break
fi
done
fi

export DEFAULT_WM=metacity
echo "true" > ~/.compiz-enabled

But after I restart the computer and try to run compizswitch.sh, I get the following error:

compiz (core) - Error: Screen 0 on display ":0.0" already has a window manager; try using the --replace option to replace the current window manager.
compiz (core) - Fatal: No manageable screens found on display :0.0.

I have compiz autostarting using the method under “Compiz” on this blog post: http://dismantle-it.blogspot.com/2008/09/acer-aspire-one-compiz-cairo-dock.html

What am I doing wrong? Any help would be very much appreciated as again, I am very new to all this!

26 11 2008
okokokaynow

Sorry to comment spam, but I thought I’d mention that I tried using kwin rather than metacity with a slightly different result:

/usr/bin/compizswitch.sh: line 3 -replace: command not found

If I try to run it again I get the same effect as before.

Sorry to bother you like this, I’m just a total Linux newbie and would love to be able to turn compiz off when I want to watch a video (slows the Aspire One down too much for smooth playback).

26 11 2008
okokokaynow

Oh weird, I had typed a long comment a couple hours ago that never showed up but when I tried to enter it again I was told I had already submitted it so I thought you had comment moderation enabled.
ANYWAY!

I’m a big newbie, just got an Acer Aspire One with Linpus on it about a week ago. I have Compiz Fusion starting on startup but want to be able to turn it off at a whim like you. I followed your instructions but can’t get the result. When I try to run the script, after logging off and on, I get the above error. If I try to run it again, I get this error:
compiz (core) -Error: Screen 0 on display ":0.0" already has a window manager; try using the --replace option to replace the current window manager.
compiz (core) -Fatal: No manageable screens found on display :0.0

Any ideas? Am I doing some huge newbie mistake?

29 11 2008
SeySayux

Hello okokokaynow,

Seems that wordpress doesnt save two dashes (–) and puts one dash (-) instead. Both ‘replace’-es should get two dashes (- -replace, without the space). Then it should work :) .

Leave a comment

You must be logged in to post a comment.