1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
/************************************************************************
*
* Purpose: Program to randomly change the root window colour on X Displays.
*
* Description.
* The program randomly selects a target colour and then
* performs a basic interpolation to move from the current
* colour to the target colour. When the target has been
* reached, a new target is selected and the process is
* repeated.
*
* Notes: 1) No attempt is made to see if the client supports colour.
* 2) Other applications may cause problems if they have allocated
* all the available colour cells in the colour map.
* 3) Use the following command to compile.
*
* gcc root_col.c -lX11
* 4) Tested on Linux running XFree386 and FVWM
* and SunOS 4.1.3 running X11R5 and Motif.
*
* Author: M.J. Leslie
*
* Date: 26-Mar-95
*
************************************************************************/
/****************** Includes ********************************************/
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <stdio.h>
#include <stdlib.h>
/************************************************************************/
#define INC 512 /* Increments between colours. */
#define MAX_COL 65536 /* Max number of colours. */
#define SECONDS 2 /* # of seconds between updates */
/****************** Functions *******************************************/
void help(char * proganme); /* Give some help on syntax. */
/* Used to see if a colour gun has
* reached its required intensity
*/
int reached_target( unsigned short colour, unsigned short target_colour);
void root_colour(int seconds); /* Where the real work starts. */
/* Change the colour of the
* root window */
void change_colour(Display * display,
Window root,
Colormap colormap,
XColor colour);
/************************************************************************/
main(int argc, char * argv[])
{
int seconds=SECONDS;
/* Have we got cmd line parms? */
if (argc > 1 )
{
/* Yes. Is Help Required? */
if ( !strcmp(argv[1], "-h"))
{
help(argv[0]); /* Yes. */
exit(0);
}
/* Do we have an integer. */
if (!sscanf(argv[1], "%u", &seconds))
{
help(argv[0]); /* No. */
exit(0);
}
}
root_colour(seconds); /* Start colouring the
* root window.
* 'seconds' is the # of seconds
* between updates. */
}
/************************************************************************/
void root_colour(int seconds)
{
Display * display;
int screen;
char * display_name=NULL;
Window root;
Colormap colormap;
XColor colour, target_colour;
/****************************************
*
* Initalise the X environment.
*
****************************************/
/* Connect to X display server. */
display=XOpenDisplay(display_name);
/* Get screen ID */
screen=DefaultScreen(display);
root=RootWindow(display, screen);
/* Load the default colour map */
colormap = DefaultColormap(display, screen);
/* Starting colour - Grey */
colour.red=MAX_COL/2; colour.green=MAX_COL/2; colour.blue=MAX_COL/2;
colour.flags = DoRed|DoGreen|DoBlue;
/****************************************
*
* Loop forever.
*
****************************************/
while(1)
{
/* Increments used to go from the current
* colour to the target colour. */
int red_inc, green_inc, blue_inc;
/* Flags used to show when the target
* colour has been reached.
*
* 0 = Arrived at the target colour.
* 1 = Not arrived at the target colour.
* */
int red=1, green=1, blue=1;
/* Random selection of a new target
* colour */
target_colour.red=rand()%MAX_COL;
target_colour.green=rand()%MAX_COL;
target_colour.blue=rand()%MAX_COL;
/* Calculate the direction to go in
* to reach the new target colour. */
red_inc = ( target_colour.red > colour.red ) ? INC : INC * -1;
green_inc = ( target_colour.green > colour.green) ? INC : INC * -1;
blue_inc = ( target_colour.blue > colour.blue ) ? INC : INC * -1;
/* Loop until all three colours have
* reached their required value. */
while(red || green || blue)
{
/* Change the colour of the root window */
change_colour(display, root, colormap, colour);
/* Increment onto the next colour. */
if ( red=reached_target(colour.red, target_colour.red))
colour.red += red_inc;
if (green=reached_target(colour.green, target_colour.green))
colour.green += green_inc;
if ( blue=reached_target(colour.blue, target_colour.blue))
colour.blue += blue_inc;
sleep(seconds); /* Pause...... */
}
}
XCloseDisplay(display);
}
/************************************************************************/
int reached_target( unsigned short colour, unsigned short target_colour)
{
return (( colour >= target_colour-INC && colour <= target_colour+INC) ?0:1);
}
/************************************************************************/
void change_colour(Display * display,
Window root,
Colormap colormap,
XColor colour)
{
/* Reserve a cell in the colour map */
XAllocColorCells(display, colormap, False, NULL, 0, &colour.pixel, 1);
/* Place the required colour in the map.*/
XStoreColor(display, colormap, &colour);
/* Free the cell so it can be reused. */
XFreeColors(display, colormap, &colour.pixel, 1, 0);
/* Set the root window colour. */
XSetWindowBackground(display, root, colour.pixel);
/* Refresh the root window (with the new
* colour). */
XClearWindow(display, root);
}
/************************************************************************/
void help(char * progname)
{
printf("\n%s information\n\n", progname);
puts(" This program randomly changes the color of the screen background.");
puts("\n Syntax:\n");
printf("\t%s \tRun the program with with a default update time\n", progname);
printf("\t\t \tof %d seconds.\n", SECONDS);
printf("\t%s 3\tRun the program specifing the delay between updates.\n", progname);
printf("\t%s -h\tHelp. you are reading it.\n\n", progname);
}
|