1 /*
2  * Memory mit Scaleable Fonts und mehreren geoeffneten Bildschirmen
3  */
4 
5 #ifndef __global_h
6 #	include "global.H"
7 #endif
8 #ifndef __port_h
9 #	include "port.H"
10 #endif
11 #ifndef __board_h
12 #	include "board.H"
13 #endif
14 #ifndef __game_h
15 #	include "game.H"
16 #endif
17 #ifndef __mem_image_h
18 #	include "mem_image.H"
19 #endif
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <X11/Xutil.h>
25 #include <X11/Xos.h>
26 // #include <sys/time.h>
27 #include <sys/types.h>
28 #ifndef WIN32
29 #	include <dirent.h>
30 #endif
31 
32 #ifndef DATA_DIRECTORY
33 #	define DATA_DIRECTORY "."
34 #endif
35 #define	_DoubleString
36 #define	DoubleDepth
37 
38 #define	_TRACE_COLORS
39 
40 Board *board_p;
41 
42 #ifndef FONT_DEFAULT
43 #	define	FONT_DEFAULT "-bitstream-charter-bold-r-*"
44 #endif
45 
46 Parameter param[] = {
47 
48 // parameters for the 'letter' game
49 
50 { FONT_DEFAULT, 0, {
51 	{  4,  5, 44, 100, 100 }, 		// small
52 	{  6,  7, 44, 100, 100 }, 		// normal
53 	{ 8,  9, 44, 100, 100 }		// big
54 }},
55 
56 // parameters for the 'smiley' game
57 
58 { FONT_DEFAULT, 1, {
59 	{  6,  3, 44, 100, 270 }, 		// small
60 	{  8,  3, 44, 100, 270 }, 		// normal
61 	{ 10,  4, 44, 100, 270 }		// big
62 }},
63 
64 // parameters for the 'picture' game
65 { FONT_DEFAULT, 0, {
66 	{  5,  4, 100, 100, 150 }, 		// small
67 	{  7,  4, 100, 100, 150 }, 		// normal
68 	{  10, 6, 75, 100, 150 }			// big
69 }}
70 
71 
72 #ifdef HUBSYM
73 , // to append to last entry
74 
75 // parameter for a symbol game (requires special symbol font)
76 
77 { "-hub-symbol-normal-r-normal-*", 0, {
78 	{  4,  5, 44, 100, 70 },
79 	{  6,  9, 44, 100, 70 },
80 	{  8, 11, 44, 100, 70 }
81 }},
82 
83 // parameters for the 'cyrillic' game
84 
85 { "-hub-textbook-bold-r-normal-*", 0, {
86 	{  4,  5, 44, 100, 100 }, 		// small
87 	{  8,  9, 44, 100, 100 }, 		// normal
88 	{ 11, 12, 44, 100, 100 }		// big
89 }}
90 #endif
91 };
92 
93 
94 //////////////////////////////////////////////////////////////////////////////
95 
find_file(const char * filename)96 char *find_file( const char *filename ) {
97 char path[1024];
98 static char *lookup[] = {
99 	"./",
100 #ifdef HUBSYM
101 	"/home/helmut/src/xmemory-3.X/",
102 #endif
103 	 DATA_DIRECTORY "/" };
104 FILE *fp;
105 
106 	for(int i=0;i<(int)(sizeof(lookup)/sizeof(char*));i++) {
107 		strcpy(path,lookup[i]);
108 		strcat(path,filename);
109 		fp=fopen(path,"r");
110 		if (fp)	{ fclose(fp); return strdup(path); }
111 
112 		strcat(path,".mem");
113 		fp=fopen(path,"r");
114 		if (fp)	{ fclose(fp); return strdup(path); }
115 	}
116 	return 0;
117 }
118 
119 #ifndef WIN32
scan_dir(const char * dirname)120 void scan_dir( const char *dirname ) {
121 DIR	*dir;
122 struct dirent *entry;
123 
124 	dir=opendir(dirname);
125 	while( (entry=readdir( dir )) != NULL ) {
126 		if (!strcmp(".mem",&entry->d_name[strlen(entry->d_name)-4])) {
127 			char *help=strdup(entry->d_name);		// cut extention
128 			help[strlen(help)-4]=0;
129 			if (strcmp(help,"default"))				// if not default:
130 				printf( "       -%s\n", help );			// print name as option
131 			free(help);
132 		}
133 	}
134 	closedir(dir);
135 }
136 #endif
137 
usage()138 void usage() {
139 	printf("usage: xmemory <options>\n");
140 	printf("options:\n");
141 	printf("    tile selection:\n" );
142 	printf("       -smiley      - select smiley tiles (default)\n" );
143 	printf("       -letter      - select letter tiles\n" );
144 #ifdef HUBSYM
145 	printf("       -symbol      - select Contronic S symbol font\n" );
146 	printf("       -cyrillic    - select Contronic S cyrillic font\n" );
147 #endif
148 	printf("       -pictures    - select default picture package\n" );
149 	printf("       -file <name> - use picture file <name>\n" );
150 	printf("    installed picture packages:\n");
151 #ifndef WIN32
152 	scan_dir( "." );
153 #ifdef HUBSYM
154 	scan_dir( "/home/helmut/src/xmemory-3.X" );
155 #endif
156 	scan_dir( DATA_DIRECTORY );
157 #endif
158 	printf("\n    game selection:\n" );
159 	printf("       -small        - small amount of tiles\n" );
160 	printf("       -normal       - normal amount of tiles (default)\n" );
161 	printf("       -big          - large amount of tiles\n" );
162 	printf("       -remain       - tiles remain on the screen after match\n" );
163 	printf("       -sync         - play the traditional synchronous games\n" );
164 	printf("       -tile <w>x<h> - number of tile (total must be even)\n" );
165 	printf("       -lock <n>     - lock picture size to <n> percent of original\n" );
166 	printf("\n    player selection:\n" );
167 	printf("       <display>        - select display for player <n>\n" );
168 	printf("       <display>/<name> - select display and color <name> for player <n>\n" );
169 	printf("       -color<n> <name> - select color <name> for player <n>\n" );
170 
171 	printf("\nAuthor:  Helmut Hoenig, July-17-96 (V3.7)   (Helmut.Hoenig@hub.de)\n" );
172 
173 	printf("\nsmail (for gifts :-) \n");
174 	printf("        Helmut Hoenig\n");
175 	printf("        Hopfenstrasse 8a\n");
176 	printf("        65520 Bad Camberg\n");
177 	printf("        GERMANY\n");
178 	exit(-1);
179 }
180 
181 #define MAX_PLAYERS	10
182 
183 char	*def_col[MAX_PLAYERS] = {		// predefined colors for the first players
184 	"red",
185 	"DodgerBlue",
186 	"OliveDrab",
187 	"gold",
188 	"DeepPink",
189 	"tomato",
190 	"aquamarine",
191 	"brown",
192 	"bisque",
193 	"sienna" };
194 
195 char *def_disp[MAX_PLAYERS] = {
196 	"", "", "", "", "", "", "", "", "", ""
197 };
198 
199 int
main(int argc,char ** argv)200 main( int argc, char **argv )
201 {
202 int i;
203 time_t	timefield;
204 char	main_display[30];
205 char	*main_p;
206 char *filename;
207 int	xmax=0, ymax=0;
208 
209 	if ( (filename=find_file( "default" ))==0 ) {
210 		filename="default.mem";
211 	}
212 
213 	main_p = getenv("DISPLAY");
214 	if (main_p) {
215 		strcpy(main_display,main_p);
216 		main_p=strstr(main_display,"_XT");
217 		if (main_p)		main_p+=3;
218 	}
219 
220 	time(&timefield);					// preset random generator
221 	srand((int)timefield);
222 
223 	Port::nplayers = 0;
224 	Port::def_size = -1;
225 
226 	for (i=1;i<argc;i++) {
227 		     if (!strcmp(argv[i],"-small"   )) { Port::def_size=0; }
228 		else if (!strcmp(argv[i],"-normal"  )) { Port::def_size=1; }
229 		else if (!strcmp(argv[i],"-big"     )) { Port::def_size=2; }
230 
231 		else if (!strcmp(argv[i],"-smiley"  )) { Port::def_mode=Smiley; }
232 		else if (!strcmp(argv[i],"-pictures" )) { Port::def_mode=Photo; }
233 		else if (!strcmp(argv[i],"-letter"  )) { Port::def_mode=Normal; }
234 #ifdef HUBSYM
235 		else if (!strcmp(argv[i],"-cyrillic")) { Port::def_mode=Cyrillic; }
236 		else if (!strcmp(argv[i],"-symbol"  )) { Port::def_mode=Symbol; }
237 #endif
238 		else if (!strcmp(argv[i],"-file")) {
239 			Port::def_mode=Photo;
240 			filename=argv[++i];
241 		}
242 		else if (!strcmp(argv[i],"-font")) {
243 			param[Normal].font = argv[++i];
244 			param[Smiley].font = argv[  i];
245 		}
246 		else if (!strcmp(argv[i],"-remain")) {
247 			Port::remove_flag=0;
248 		}
249 		else if (!strcmp(argv[i],"-sync")) {
250 			Port::sync_flag=1;
251 		}
252 		else if (!strcmp(argv[i],"-lock")) {
253 			Port::resize_lock = atoi(argv[++i]);
254 		}
255 		else if (!strcmp(argv[i],"-tile")) {
256 			if (sscanf( argv[++i], "%dx%d", &xmax, &ymax )==2) {
257 				if ( (xmax*ymax)&1 )		xmax--;
258 			}
259 			else	xmax=ymax=0;
260 		}
261 		else if (!strncmp(argv[i],"-color",6)) {
262 			int id=atoi(&argv[i][6]);
263 			if ((id>=0)&&(id<MAX_PLAYERS)) {
264 				def_col[id] = argv[++i];
265 			}
266 		}
267 		else if (strrchr(argv[i],':')) {
268 			char *col=strchr(argv[i],'/');			// compatibility with old params
269 			if (col) {
270 				if (col[1])	def_col[Port::nplayers]=col+1;	// color stored
271 				*col='\0';												// color cut
272 			}
273 			def_disp[Port::nplayers++]=argv[i];		// store next display
274 		}
275 		else if (argv[i][0]=='/') {					// compatibility with old params
276 			if (argv[i][1]) {
277 				def_col[Port::nplayers]=&argv[i][1];
278 			}
279 			Port::nplayers++;
280 		}
281 		else if (argv[i][0]=='-') {
282 			if ( (filename=find_file( &argv[i][1] ))!=0 ) {
283 				Port::def_mode=Photo;
284 			}
285 			else	usage();
286 		}
287 		else usage();
288 	}
289 
290 	if (!Port::nplayers) {
291 		Port::nplayers++;
292 	}
293 
294 	if (Port::def_mode==Photo) {
295 		int mwidth, mheight;
296 		Port::gif=new MemImage(filename);
297 		Port::gif->ShowInfoMsg();
298 
299 		if (Port::def_size==-1&&xmax==0&&ymax==0) {
300 			const char *opt=Port::gif->GetExtensionData( OPTION_EXTENSION );
301 
302 			if (!opt || sscanf( opt, "-tile %dx%d", &xmax, &ymax )!=2) {
303 				xmax=ymax=0;
304 			}
305 		}
306 
307 		Display *dpy=XOpenDisplay(def_disp[0]);
308 		if (dpy) {
309 			mwidth  = WidthOfScreen(DefaultScreenOfDisplay(dpy));
310 			mheight = HeightOfScreen(DefaultScreenOfDisplay(dpy));
311 			XCloseDisplay(dpy);
312 		}
313 		else {
314 			mwidth  = 1024;
315 			mheight = 768;
316 		}
317 		Port::gif->SetupParams( &param[Photo], mwidth, mheight );
318 	}
319 	else {
320 		Port::gif=0;
321 	}
322 
323 	if (Port::def_size==-1)		Port::def_size=1;
324 
325 	Port::dwidth  = param[Port::def_mode].dim[Port::def_size].dwidth;
326 	Port::dheight = param[Port::def_mode].dim[Port::def_size].dheight;
327 	Port::dsize   = param[Port::def_mode].dim[Port::def_size].dsize;
328 	Port::wsizex  = param[Port::def_mode].dim[Port::def_size].wsizex;
329 	Port::wsizey  = param[Port::def_mode].dim[Port::def_size].wsizey;
330 
331 	if (xmax) {
332 		Port::dwidth  = xmax;
333 		Port::dheight = ymax;
334 	}
335 	board_p = new Board( Port::dwidth, Port::dheight );
336 
337 	for (i=0;i<Port::nplayers;i++) {
338 		new Port( def_disp[i], def_col[i] );
339 	}
340 
341 #if (0)
342 	if (argc<2)		new Port( main_display, def_col[0] );
343 
344 	for (;i<argc;i++) {
345 		if ( color_name=strchr(argv[i],'/') ) {
346 			*color_name++ = '\0';
347 			if (argv[i][0]=='\0'&&main_p) {
348 				*main_p = '0'+Port::color_id;
349 				new Port( main_display, *color_name?color_name:def_col[Port::color_id-1] );
350 			}
351 			else {
352 				new Port( argv[i], *color_name?color_name:def_col[Port::color_id-1] );
353 			}
354 		}
355 		else {
356 			new Port( argv[i], def_col[Port::color_id-1] );
357 		}
358 	}
359 #endif
360 	board_p->reset();
361 	Port::activate_any_player();
362 
363 #if (1)
364 	while(!Port::wait_event());
365 #else
366 	int	n=10;
367 	while(--n)							Port::wait_event();
368 #endif
369 
370 	Port::statistic();
371 	Port::close_all();
372 	delete board_p;
373 
374 	/* sleep(5); */
375 	return 0;
376 }
377