Code:
#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
//#include <cstdlib>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdlib.h>
#include <getopt.h>
#include <vector>
#include </usr/include/Imlib2.h>
#include "files.h"
#include "img.h"
#include "options.h"
options opts;
enum
{
T, TH, TV, TD, THV, ST, BUTT
};
const char *tile_opts[] =
{
[T] = "n",
[TH] = "h",
[TV] = "v",
[TD] = "d",
[THV] = "hv",
[ST] = "st",
[BUTT] = NULL
};
enum
{
FD, FH, FV, FAH, FAV, FATH, FATV, BUTT2
};
const char *flip_opts [] =
{
[FD] = "d",
[FH] = "h",
[FV] = "v",
[FAH] = "ah",
[FAV] = "av",
[FATH] = "ath",
[FATV] = "atv",
[BUTT2] = NULL
};
enum
{
RC, RA, RD, SOR, BUTT3
};
const char *rand_opts [] =
{
[RC] = "rc", // random 2 colors
[RA] = "ra", // random angle
[RD] = "rd", // random distance
[SOR] = "sor", // system over ride - randomizes everything.
[BUTT3] = NULL
};
void init_options(int argc, char **argv)
{
std::memset(&opts, 0, sizeof(options));
std::memset(&img, 0, sizeof(image_data));
parse_options(argc, argv);
}
void parse_options(int argc, char **argv)
{
static char string_options [] = "rFfoh::a:T:s:n:i:d:c::z:";
// no_argument 0
// required_argument 1 (‘:’) to indicate that it takes a required argument
// optional_argument 2 (‘::’), its argument is optional
static struct option long_options[] =
{
// a b c d e f g h i j k l m n o p q r s t u v w x y z
{"fullscreen", 0, 0, 'f'}, // fills screen streaching image
{"fill", 0, 0, 'F'}, // fills image using aspect ratio
{"center", 2, 0, 'c'}, // Center and resize-center
{"tile", 1, 0, 'T'}, // arg n=normal v = vertical etc.. SUBOPTS
{"angle", 1, 0, 'a'}, //angle between 2 colors
{"solid", 1, 0, 's'}, // solid color
{"distance", 1, 0, 'd'}, // distance between 2 colors
{"randomize", 0, 0, 'r'}, // sets random files
{"delay", 1, 0, 'z'}, // sets delay timer
{"file-number", 1, 0, 'n'}, // gets user specifed file by number
{"help", 0, 0, 'h'}, // help
{"version", 0, 0, 'v'}, // version
{"add", 1, 0, 200}, // for adding two colors
{"rc", 2, 0, 201}, // sets two random colors plus more
{"ra", 0, 0, 202}, // random angle
{"rd", 0, 0, 203}, //random distance
{"over-ride", 0, 0, 204}, // over rides everything and sets random everything
{"ordered", 0, 0, 'o'}, //sets images to show in alphabit order
{"sc", 0, 0, 205}, // single random color
{"st", 2, 0, 207}, // space tile
{"fi", 1, 0, 208}, // flip image
{"printlist", 0, 0, 209}, // prints files on intake
{"over-drive", 0, 0, 210}, //over drive does not do just colors, only random images
{"p", 0, 0, 211}, // print list file
{"rotate-angle", 1, 0, 212}, // gets degrees of rotation
{"m", 0, 0, 213}, // moves image down and across screen
{0, 0, 0, 0}
}; // end options
int ol = 0;
unsigned int w,h;
int option_index = 0;
char *value;
char oopsX[10];
char *subopts;
extern char *optarg;
extern int optind;
while (( ol = getopt_long_only(argc, argv, string_options, long_options,&option_index) ) != -1)
{
switch (ol)
{
case 'f': //fullscreen
img.mode = FULLSCREEN;
break;
case 'F': // fill
img.mode = FILL;
break;
case 213:
img.mode = MOVEIMAGE;
mvi.start();
break;
case 'c': // center / resize center
//check ahead by 1 to see if = was forgotten to be added
// if user is trying to resize the image. Retruns 0
// if it is a proper format for size using an x.
if (optarg == NULL)
{
img.mode = CENTER;
}
else
{
strcpy(img.findX, optarg);
strcpy(oopsX, img.findX);
if (gettingWidthHeight(img.findX, &w,&h) == -1)
{
BadFindX(oopsX);
exit(1);
}
else
{
img.set_newimg_w = w;
img.set_newimg_h = h;
img.mode = DCENTER;
}
}
break;
case 207: //space tile
if (optarg == NULL)
{
img.mode = SPACE_TILE;
}
else
{
img.flip = atoi(optarg);
if (img.flip > 7)
{
printf("Value < %d > sets between 1 ... 6\n7 is for random modes\n", img.flip);
exit(1);
}
}
if(img.flip == 7)
img.flips_mode = 1;
img.mode = SPACE_TILE;
break; // ############ COLORS ########
case 's': //solid
img.color_flag = 1;
img.solidColor = strdup(optarg);
break;
case 200:
//--add with or without distance
img.color_flag = 2;
img.set_2_colors += 1;
img.color1 = strdup(optarg);
//get them both for later.
switch (img.set_2_colors)
{
case 1:
img.color2 = img.color1;
break;
default:
break;
}
break;
case 205: // random solid color
img.color_flag = 3;
break;
case 201: // Random Colors
img.color_flag = 4;
if (optarg == NULL)
{
img.set_random_color_num = 1;
}
else if (atoi(optarg) > MAX_SHOW_COLORS)
{
BeyondMaxColors(optarg);
exit(1);
}
else
{
img.set_random_color_num = atoi(optarg);
}
break;
case 'd': // distance
img.distance = atoi(optarg);
if (img.distance == 0)
{
printf("Missing Distance < %d >\n", img.distance);
exit(1);
}
break;
case 'a': //angle
// sets a angle to two colors gradients
img.angle = atoi(optarg);
break;
case 'r': // random files
img.set_randomize = 1;
break;
case 'z': //sets timer time
if (optarg == NULL)
printf("< %s >\n",optarg);
else
if (seconds_or_minutes(optarg) != -1)
{
img.time = seconds_or_minutes(optarg);
}
else
{
set_oops_color();
warning_message();
}
break;
case 'n': //number file to display
img.get_file_number = 1;
img.file_number = atoi(optarg);
break;
case 'h':
printf("Help: You're telling me?\n");
usage();
exit(0);
break;
case 202: //random angle
img.angle_flag = 1;
get_randoms();
break;
case 203: // random distance
img.angle_flag = 2;
get_randoms();
break;
case 204: // -over-ride-system
img.set_over_ride_system = 1;
break;
case 'o': // order list
img.set_ordered_list = 1;
break;
case 209:
img.printlistintake = 1;
break;
case 210:
img.set_over_drive = 1;
break;
case 211:
img.printlistintake = 3;
break;
case 212:
img.rotate_angle_flag = 1;
img.rotate_angle_num = rotate_angle(atoi(optarg));
break;
case 'v':
// version_message();
break;
case '?':
printf("What did you forget? Think about it.\n");
exit(1);
break;
case 'T': // sub img one tile
subopts = optarg;
while (*subopts != '\0')
{
switch (getsubopt (&subopts, (char **)tile_opts, &value))
{
case T:
if (value == NULL)
{
img.mode = TILE;
}
else
{
strcpy(img.findX, value);
strcpy(oopsX, img.findX);
if (gettingWidthHeight(img.findX, &w,&h) == -1)
{
BadFindX(oopsX);
exit(1);
}
else
{
img.set_newimg_w = w;
img.set_newimg_h = h;
img.mode = DTILE;
}
}
break;
case TH:
if (value == NULL)
{
img.mode = TILEH;
}
else
{
strcpy(img.findX, value);
strcpy(oopsX, img.findX);
if (gettingWidthHeight(img.findX, &w,&h) == -1)
{
BadFindX(oopsX);
exit(1);
}
else
{
img.set_newimg_w = w;
img.set_newimg_h = h;
img.mode = DTILEH;
}
}
break;
case TV:
if (value == NULL)
{
img.mode = TILEV;
}
else
{
strcpy(img.findX, value);
strcpy(oopsX, img.findX);
if (gettingWidthHeight(img.findX, &w,&h) == -1)
{
BadFindX(oopsX);
exit(1);
}
else
{
img.set_newimg_w = w;
img.set_newimg_h = h;
img.mode = DTILEV;
}
}
break;
case TD:
if (value == NULL)
{
img.mode = TILED;
}
else
{
strcpy(img.findX, value);
strcpy(oopsX, img.findX);
if (gettingWidthHeight(img.findX, &w,&h) == -1)
{
BadFindX(oopsX);
exit(1);
}
else
{
img.set_newimg_w = w;
img.set_newimg_h = h;
img.mode = DTILED;
}
}
break;
case THV:
if (value == NULL)
{
img.mode = TILEHV;
}
else
{
strcpy(img.findX, value);
strcpy(oopsX, img.findX);
if (gettingWidthHeight(img.findX, &w,&h) == -1)
{
BadFindX(oopsX);
exit(1);
}
else
{
img.set_newimg_w = w;
img.set_newimg_h = h;
img.mode = DTILEHV;
}
}
break;
default:
printf ("Unknown Option < %s >\n", value);
exit(1);
break;
}
} // end off while
break;
case 208: //flip image
subopts = optarg;
while (*subopts != '\0')
{
switch (getsubopt (&subopts, (char **)flip_opts, &value))
{
case FD:
if (value == NULL)
{
img.mode = FLIPIMGD;
}
else
{
strcpy(img.findX, value);
strcpy(oopsX, img.findX);
if (gettingWidthHeight(img.findX, &w,&h) == -1)
{
BadFindX(oopsX);
exit(1);
}
else
{
img.set_newimg_w = w;
img.set_newimg_h = h;
img.mode = DFLIPIMGD;
}
}
break;
case FH:
if (value == NULL)
{
img.mode = FLIPIMGH;
}
else
{
strcpy(img.findX, value);
strcpy(oopsX, img.findX);
if (gettingWidthHeight(img.findX, &w,&h) == -1)
{
BadFindX(oopsX);
exit(1);
}
else
{
img.set_newimg_w = w;
img.set_newimg_h = h;
img.mode = DFLIPIMGH;
}
}
break;
case FV:
if (value == NULL)
{
img.mode = FLIPIMGV;
}
else
{
strcpy(img.findX, value);
strcpy(oopsX, img.findX);
if (gettingWidthHeight(img.findX, &w,&h) == -1)
{
BadFindX(oopsX);
exit(1);
}
else
{
img.set_newimg_w = w;
img.set_newimg_h = h;
img.mode = DFLIPIMGV;
}
}
break;
case FAH:
img.mode = FLIPAH;
break;
case FAV:
img.mode = FLIPAV;
break;
case FATH:
img.mode = FLIPAVTH;
break;
case FATV:
img.mode = FLIPAVTV;
break;
default:
//Second suboption.
printf ("Unknown Option < %s >\n", value);
exit(1);
break;
} // end second switch subopts
} // end second while off subopts
break; // break off of inner subopts switch
default:
printf("Unknown Option < %s >\n", optarg);
exit(1);
break;
} // end switch
} // end
if ( img.set_over_ride_system != 1 && img.mode == 0 && img.set_over_drive != 1 &&
img.set_random_color_num < 1 && img.color_flag == 0 &&
argv[argc-1] != NULL )
{
set_oops_color();
warning_message();
}
if(optind == argc)
{
if ( ( (img.set_ordered_list == 1 ) || ( img.set_randomize == 1) ) && (argv[argc] == NULL))
{
std::cout<<"1: File Source is (NULL)"<<std::endl;
exit(1);
}
if( ((img.set_over_ride_system == 1 ) || (img.set_over_drive == 1) ) && (argv[argc] == NULL))
{
std::cout<<"Just so you know. You're not "<<"\n"<<"going to get any images like that."<<std::endl;
exit(1);
} // if image mode selected and no path to file
if(img.mode > 0 && argv[argc] == NULL)
{
set_oops_color();
warning_message();
}
}
if ( optind < argc)
{
for(; optind < argc ; optind++)
{
read_files(argv[optind++], FILELIST_FIRST);
}
}
optind = 0;
} //end function
void check_options(void)
{
if(img.set_randomize == 1 && img.set_ordered_list == 1)
{
img.set_randomize = 0;
}
if (img.set_over_drive == 1)
{
// if over ride is selected, make sure it over rides everything.
img.set_randomize = 1;
img.set_random_color_num = 0;
img.set_ordered_list = 0;
img.file_number = 0;
img.color_flag = 0;
img.mode = 0;
}
}