Sign In

    Enjoy FOSSwire's content? Have it delivered! Subscribe

    Suspending Compiz

    There are times when you find a game that for some reason just doesn't work well with Compiz. It may flicker over other windows, become distorted, or it may crash. The real fix for these comes in new X.Org drivers in the works, but for now the best solution is to suspend Compiz while you run a game.

    Let's think of a way to do this. First, we'll obviously want to start Metacity and replace Compiz. Then, we want the game to run. And when we're done, Compiz should start up again. The most logical script would be this:

    #!/bin/bash
    metacity --replace
    game_name
    compiz --replace

    If you try to run the script above, you'd notice that Metacity would start, but nothing else would happen. This is because the commands are run synchronously, that is, when one finishes the next starts. Metacity will never finish unless you stop it. So, let's make it run in the background:

    #!/bin/bash
    metacity --replace &
    game_name
    compiz --replace

    The ampersand makes Metacity run in the background, allowing other commands to run. We then can run the game. The game shouldn't be run in the background, however, because Compiz would immediately replace Metacity again.

    At the end of the script, we start up Compiz again. This is where you don't want to run the script in a terminal for once, because as soon as you close the terminal Compiz will quit. So, let's make a menu item for it. I'm a big fan of StepMania, so I'm going to run the version I have installed in my home directory:

    #!/bin/bash
    metacity --replace &
    cd ~/bin/stepmania
    ./stepmania
    compiz --replace

    If you run a command that requires a path, make sure to put the full path name in or your script may not run. We then add a menu entry in the Games menu:

    sm-launcher.png

    Now whenever I decide to play StepMania, all desktop effects will be shut off. When I am finished, they will be switched back on again. This can be applied to almost any situation where Compiz needs to be off in your own scripts.


    Do a MySQL backup from a PHP script

    In today's FOSSwire programming special, I'm going to be showing you how to build a script in PHP that will allow you to back up your MySQL database remotely, using just a web browser.

    Why might you need such a script? Well, remote backup through a web browser could be very useful, as you will be able to do it from any machine with a web browser, where you might not have the ability to or want to log in via SSH.

    First of all, I need to point out an important point. This example will have no authentication in it. That means anyone that can find this script can perform a backup of your MySQL databases should they know a valid password, or could try knocking at the door to see if they can get in. I strongly advise you restrict access to the script we're about to write using some form of authentication, be it through .htpasswd or similar, or by building a custom login system on top of this.

    A few more notes:

    • This script is basic. It's not designed to be super-duper, and it's probably best as a starting point for your own script. Take it with a pinch of salt.
    • It needs a directory writable to the web server to store its files. This should most definitely be outside of the web root - i.e. impossible for anyone to download your raw database dumps in their browser.
    • Any errors that happen in the mysqldump process will get thrown right back onto the browser. Making that cleaner and better is outside the scope of this script.
    • It's ugly, but making it pretty isn't my job, or my expertise. ;)
    • It is designed for servers where MySQL runs on localhost (i.e. the same box as the web server). It can be easily adapted though, just by changing the mysqldump command string.
    • The mysqldump program must be in the PATH, or else you must supply the full path to it in $command.

    And that is it. Properly in place, that script is a very simple, down and dirty interface to remotely back up your MySQL databases from a web browser.


    C++: Variables and Functions

    If you haven't read the previous tutorial, we highly recommend you do so. Otherwise, you might miss important concepts used here.

    Go ahead and fire up a text editor, and begin reading and typing. The meat of this tutorial is in the comments. Remember, try typing it out instead of copying and pasting. This way you will be able to remember by doing instead of by reading.


    #include

    /* First off: let's make a basic function that prints "Hello" on the screen.
    * The syntax for a basic function is as follows:
    ** returnType functionName()
    * Like with main(), you can return an int if you want, but with normal
    * functions it is possible to return nothing (void).
    */
    void sayHello(){
    std::cout << "Hello from a function!" << std::endl;
    /* Here is something new. std::endl (or just endl if you used the namespace
    * mentioned in the previous lesson) inserts a new line into the console.
    * Get into the habit of adding it to cout statements for readibility.
    */
    } /* Notice how using void as a return type means we don't have to return
    * anything. For a quick example, let's make a function that does return
    * something.
    */

    int threeTimesThree(){
    int result;
    /* Woah, we haven't seen this before. This is simply known as a variable.
    * Basically, it stores data. But before you can store anything, you need
    * to tell the compiler what type of data you want to store. This is
    * specified here with "int". So, we just made an integer named "result".
    */

    result = 3 * 3;
    /* To set a variable to something, just use the syntax
    ** variable = value;
    * Value can be a single value, a calculation, or the result of another
    * function. But, it has to be of the same type that you defined it as
    * above. result should equal 9 right now.
    */

    return result;
    /* As with main(), if we set a return type we need to actually return
    * something. Here, we are returning the variable result.
    */
    }

    /* What if you want to pass a variable to your function? That is when arguments
    * come in handy. They aren't much more difficult to use than a simple
    * variable:
    */
    int multiply(int number1, int number2){
    /* You can have as many arguments as you want, and name them whatever you want.
    * They can be used in your function as a simple variable.
    */

    int result = number1 * number2;
    // This is an example of defining and setting a variable at the same time.

    return result;
    }

    // Of course, we need our usual main() function.
    int main(){
    /* To call a function, use the same syntax you did for defining it, except
    * without the type before it. If a function returns a value, you can
    * assign it to a variable.
    */

    // Print out hello
    sayHello();

    int answer = threeTimesThree();
    std::cout << answer << std::endl;
    // You can use variables in a cout statement as well.

    // If you like, you can even use a function directly.
    std::cout << multiply(4, 5) << std::endl;

    // Finally, finish up our main() function with a return.
    return 0;
    }

    Compile it (if you forget how, see the previous tutorial) and run. You should get something like:

    ./test
    Hello from a function!
    9 20

    If not, or you got an error, check your code and see if you mistyped anything. If you are still having trouble, post a comment and I'll try to help you out.

    We hope you enjoyed this tutorial. Stay tuned for more!


    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6