This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
user:mgough:autodrawing [2010/05/08 15:00] – mgough | user:mgough:autodrawing [2010/05/08 15:44] (current) – mgough | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== AutoDrawing ====== | ||
+ | Turn your computer into an artist! | ||
+ | |||
+ | ===== Overview ===== | ||
+ | |||
+ | The goals of this project are the following: | ||
+ | 1) Write a script to allow the computer to draw in Xpaint | ||
+ | 2) Turn your computer into an award winning artist. | ||
+ | |||
+ | ===== Components ===== | ||
+ | 1) A unix computer | ||
+ | 2) An x-windows environment | ||
+ | 3) X-paint | ||
+ | |||
+ | ===== Script Breakdown ====== | ||
+ | |||
+ | We will go over the commands that will be used in the drawing program. | ||
+ | |||
+ | ==== Initialization ==== | ||
+ | |||
+ | To start out, we want to kill all instances of xpaint, if this is not done it will lead to script confusion and unpredictable errors. | ||
+ | |||
+ | This calls the pkill command to shut down all instances of x-paint calling SIGTERM to each instance. | ||
+ | echo `pkill xpaint` | ||
+ | |||
+ | We now start a new instance of xpaint with the options of a 1024x768 drawing space without a menu bar. | ||
+ | Sleep for 4 seconds is called as well to allow time for the program to fully initialize. | ||
+ | / | ||
+ | sleep 4 | ||
+ | |||
+ | Let's set up some variables that we can use to call up the ID's of xpaint and the drawing window (the Untitled window.) | ||
+ | toolID=`xwit -all -print | grep XPaint | sed -e ' | ||
+ | winID=`xwit -all -print | grep Untitled | sed -e ' | ||
+ | |||
+ | The following commands use xwit to manpulate a window' | ||
+ | focusWin=`xwit -id $winID -focus -raise -move 0 0` | ||
+ | focusTools=`xwit -id $toolID -focus -raise -move 0 0` | ||
+ | echo $focusTools | ||
+ | echo $focusWin | ||
+ | sleep 2 | ||
+ | |||
+ | ==== Lets draw! ==== | ||
+ | Drawing is pretty strait forward, basically you control the mouse via the computer, performing commands such as clicks, drags, and selection, but without manually interfering. | ||
+ | |||
+ | To begin with, in my script I am drawing random shapes in random positions, so I must set up a random number generation system and position variables. | ||
+ | |||
+ | The random variables are set up as shown. | ||
+ | |||
+ | size=`echo $[($RANDOM % 30)]` | ||
+ | xpos=`echo $[($RANDOM % 1024)]` | ||
+ | ypos=`echo $[($RANDOM % 720 + 60)]` | ||
+ | xwit -id $winID -warp $xpos $ypos | ||
+ | | ||
+ | What happens is a random number limited to 30 is generated and stored in the " | ||
+ | |||
+ | xpos = generates a random number " | ||
+ | |||
+ | ypos = generates a random number for the height of the drawing space, we are adding 60 to push down the drawing space so that it does not include the title bar of the drawing window. | ||
+ | |||
+ | Using xwit, we warp the mouse over the drawing window who's window is selected using our winID variable to the generated xpos and ypos. This brings us to our initial drawing position. | ||
+ | |||
+ | Now we generate a random number to determine the shape we draw, and store it in the variable " | ||
+ | shape=`echo $(($RANDOM % 3))` | ||
+ | |||
+ | The value of shape is ran through a series of selection structures and the commands within are executed if the number is a match. | ||
+ | |||
+ | if [ $shape -eq 0 ] #Draw a square! | ||
+ | then | ||
+ | xte " | ||
+ | xte " | ||
+ | xte " | ||
+ | xte " | ||
+ | xte " | ||
+ | xte " | ||
+ | fi | ||
+ | |||
+ | This time we are using the " | ||
+ | |||
+ | ==== The Xwit Command ==== | ||
+ | The command " | ||
+ | |||
+ | According to xwit's man page: | ||
+ | |||
+ | xwit is an X window interface tool. By default | ||
+ | with no arguments in an xterm it de-iconifies and raises the | ||
+ | | ||
+ | | ||
+ | name begins with one of the given strings, or a particular | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | Therefore, by using the xwit command and grep, we extract the window ID's for our own devious purposes. | ||
+ | Xwit also allows the " | ||
+ | |||
+ | |||
+ | ==== The Xte Command ==== | ||
+ | |||
+ | According to the man page. | ||
+ | xte is a program that generates fake input using the XTest extension, more reliable than xse. | ||
+ | | ||
+ | Short and sweet huh? With xte you can emulate pretty much anything you can do with a mouse, here is a good list of the functions you can perform. | ||
+ | key k | ||
+ | Press and release key k | ||
+ | keydown " | ||
+ | Press key k down | ||
+ | keyup k | ||
+ | Release key k | ||
+ | str string | ||
+ | Do a bunch of key X events for each char in string | ||
+ | mouseclick i | ||
+ | Click mouse button i | ||
+ | mousemove x y | ||
+ | Move mouse to screen position x, y | ||
+ | mousermove x y | ||
+ | Move mouse relative from current location by x, y | ||
+ | mousedown i | ||
+ | Press mouse button i down | ||
+ | mouseup i | ||
+ | Release mouse button i | ||
+ | sleep x | ||
+ | Sleep x seconds | ||
+ | usleep x | ||
+ | Usleep x microseconds | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== The Script ====== | ||
+ | |||
+ | < | ||
+ | #!/bin/bash | ||
+ | |||
+ | ################################## | ||
+ | # | ||
+ | # Automated Drawing Program | ||
+ | # v0.1 | ||
+ | # Michael Gough | ||
+ | # CSIT 1320 | ||
+ | # 2/3/2010 | ||
+ | # | ||
+ | ################################## | ||
+ | echo `pkill xpaint` | ||
+ | / | ||
+ | sleep 4 | ||
+ | xwit -root -rwarp 100 100 | ||
+ | |||
+ | # Grep out the xpaint, get it's ID and bring it to focus | ||
+ | |||
+ | toolID=`xwit -all -print | grep XPaint | sed -e ' | ||
+ | winID=`xwit -all -print | grep Untitled | sed -e ' | ||
+ | |||
+ | focusWin=`xwit -id $winID -focus -raise -move 0 0` | ||
+ | focusTools=`xwit -id $toolID -focus -raise -move 0 0` | ||
+ | echo $focusTools | ||
+ | echo $focusWin | ||
+ | sleep 2 | ||
+ | |||
+ | xwit -id $winID -warp 640 480 | ||
+ | count=1 | ||
+ | |||
+ | xwit -id $winID -focus | ||
+ | |||
+ | while [ $count -le 100 ] | ||
+ | do | ||
+ | size=`echo $[($RANDOM % 30)]` | ||
+ | xpos=`echo $[($RANDOM % 1024)]` | ||
+ | ypos=`echo $[($RANDOM % 720 + 60)]` | ||
+ | xwit -id $winID -warp $xpos $ypos | ||
+ | |||
+ | # Lets draw a bit!! Add more shapes! | ||
+ | |||
+ | shape=`echo $(($RANDOM % 3))` | ||
+ | |||
+ | t_ypos=`echo $(($size/ | ||
+ | if [ $shape -eq 1 ] #Draw a triangle! | ||
+ | then | ||
+ | xte " | ||
+ | xte " | ||
+ | xte " | ||
+ | xte " | ||
+ | xte " | ||
+ | fi | ||
+ | |||
+ | if [ $shape -eq 0 ] #Draw a square! | ||
+ | then | ||
+ | xte " | ||
+ | xte " | ||
+ | xte " | ||
+ | xte " | ||
+ | xte " | ||
+ | xte " | ||
+ | fi | ||
+ | |||
+ | if [ $shape -eq 2 ] #Get lazy and use the xpaint toolbar to draw a circle! | ||
+ | then | ||
+ | xwit -id $toolID -raise -focus | ||
+ | xwit -id $toolID -warp 32 245 | ||
+ | xte " | ||
+ | xte " | ||
+ | echo $focusWin | ||
+ | xwit -id $winID -warp $xpos $ypos | ||
+ | xte " | ||
+ | xte " | ||
+ | xte " | ||
+ | xwit -id $toolID -raise -focus | ||
+ | xwit -id $toolID -warp 33 123 | ||
+ | xte " | ||
+ | xte " | ||
+ | echo $focusWin | ||
+ | xwit -id $winID -warp $xpos $ypos | ||
+ | fi | ||
+ | |||
+ | |||
+ | |||
+ | (( count++ )) | ||
+ | done | ||
+ | |||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |