1 /*
2  *	xtrojka (c) 1994,1995,1996 Maarten Los
3  *
4  *	#include "COPYRIGHT"
5  *
6  *	created:	26.xi.1995
7  *	modified:	27.xii.1995
8  *
9  *	The main program
10  */
11 
12 
13 #include <stdio.h>
14 #include <X11/Intrinsic.h>
15 #include <X11/StringDefs.h>
16 #include <X11/Shell.h>
17 
18 #include "xtrojka.h"
19 #include "scores.h"
20 #include "options.h"
21 #include "preferences.h"
22 #include "sh_slist.h"
23 #include "sh_stat.h"
24 #include "game.h"
25 #include "menu_tool.h"
26 #include "menu.h"
27 #include "window.h"
28 #include "pics.h"
29 #include "slist.h"
30 #include "actions.h"
31 
32 #include "debug.h"
33 #include "tr_core.h"
34 #include "_strdefs.h"
35 
36 extern Widget screen;
37 extern Widget slist_shell;
38 
39 extern Widget main_screen;	/* the mother of all widgets */
40 
41 XtAppContext	app_context;
42 Colormap	the_colormap;	/* the colormap of the application */
43 int		the_depth;	/* the colormap of the application */
44 Visual		*the_visual;
45 Colormap	my_colormap;
46 
47 GAME_STATE game_state;
48 
49 static char *COPYRIGHT = "@(#) xtrojka (c) 1994,1995,1996 Maarten Los";
50 
51 
52 flag	is_color;
53 flag	is_NOcolor;
54 flag	is_debug_info;
55 flag	is_wizard;
56 flag	is_slick;
57 
58 int 	speed;
59 int	starting_speed;
60 
61 
62 static String fallback_resources[] = {
63 
64 #include "_resdefs.h"
65 
66 };
67 
68 static XtResource resources[] = {
69 	{ "color0", "Color0", XtRPixel, sizeof(Pixel),
70 	  XtOffsetOf(AppData, color[0]), XtRString, (caddr_t)"black" },
71 	{ "color1", "Color1", XtRPixel, sizeof(Pixel),
72 	  XtOffsetOf(AppData, color[1]), XtRString, (caddr_t)"magenta" },
73 	{ "color2", "Color2", XtRPixel, sizeof(Pixel),
74 	  XtOffsetOf(AppData, color[2]), XtRString, (caddr_t)"blue" },
75 	{ "color3", "Color3", XtRPixel, sizeof(Pixel),
76 	  XtOffsetOf(AppData, color[3]), XtRString, (caddr_t)"cyan" },
77 	{ "color4", "Color4", XtRPixel, sizeof(Pixel),
78 	  XtOffsetOf(AppData, color[4]), XtRString, (caddr_t)"yellow" },
79 	{ "color5", "Color5", XtRPixel, sizeof(Pixel),
80 	  XtOffsetOf(AppData, color[5]), XtRString, (caddr_t)"green" },
81 	{ "color6", "Color6", XtRPixel, sizeof(Pixel),
82 	  XtOffsetOf(AppData, color[6]), XtRString, (caddr_t)"red" },
83 	{ "color7", "Color7", XtRPixel, sizeof(Pixel),
84 	  XtOffsetOf(AppData, color[7]), XtRString, (caddr_t)"purple" },
85 	{ "color8", "Color8", XtRPixel, sizeof(Pixel),
86 	  XtOffsetOf(AppData, color[8]), XtRString, (caddr_t)"white" },
87 
88 	{ "slist_font", "Slist_font", XtRString, sizeof(String),
89 	  XtOffsetOf(AppData, slist_font), XtRString, (caddr_t)"7x13" },
90 
91 	{ "game_font", "Game_font", XtRString, sizeof(String),
92 	  XtOffsetOf(AppData, game_font), XtRString, (caddr_t)"7x13bold" },
93 
94 	{ "str_best_players", "Str_best_players", XtRString, sizeof(String),
95 	  XtOffsetOf(AppData, str_best_players), XtRString, (caddr_t)"<undef>" },
96 
97 	{ "wstr_stat", "Wstr_stat", XtRString, sizeof(String),
98 	  XtOffsetOf(AppData, wstr_stat), XtRString, (caddr_t)"<undef>" },
99 
100 	{ "wstr_open_prefs", "Wstr_open_prefs", XtRString, sizeof(String),
101 	  XtOffsetOf(AppData, wstr_open_prefs), XtRString, (caddr_t)"<undef>" },
102 
103 	{ "wstr_create_prefs", "Wstr_create_prefs", XtRString, sizeof(String),
104 	  XtOffsetOf(AppData, wstr_create_prefs), XtRString, (caddr_t)"<undef>" },
105 
106 	{ "wstr_write_prefs", "Wstr_write_prefs", XtRString, sizeof(String),
107 	  XtOffsetOf(AppData, wstr_write_prefs), XtRString, (caddr_t)"<undef>" },
108 
109 	{ "wstr_read_prefs", "Wstr_read_prefs", XtRString, sizeof(String),
110 	  XtOffsetOf(AppData, wstr_read_prefs), XtRString, (caddr_t)"<undef>" },
111 };
112 
113 AppData app_data;
114 
115 
main(argc,argv)116 main(argc, argv)
117 int argc;
118 char **argv;
119 {
120 	/*
121 	 *	set default values
122 	 */
123 	is_debug_info = False;
124 	is_NOcolor = False;
125 
126 	/*
127 	 *	get the command line options
128 	 */
129 	get_options(argc, argv);
130 	if(is_debug_info)
131 		show_startup_string();
132 
133 	/*
134 	 *	initialize the scores
135 	 */
136 	init_scores();
137 
138 
139 	/*
140 	 *	initialize the application
141 	 */
142 	DEBUG("xtrojka.c", "main/1")
143 	main_screen = XtVaAppInitialize(
144 		&app_context,	/* application context */
145 		"XTrojka",	/* application class */
146 		NULL,0,		/* commandlineoptionlist */
147 		&argc, argv,	/* command line arguments */
148 		fallback_resources,	/* fallback resources*/
149 		NULL);		/* terminate varargslist */
150 
151 	/*
152 	 *	check if we have color
153 	 */
154 	DEBUG("xtrojka.c", "main/2")
155 
156 	the_depth = DefaultDepthOfScreen(XtScreen(main_screen));
157 	is_color = the_depth > 1;
158 
159 	the_colormap = DefaultColormap(XtDisplay(main_screen),
160 		DefaultScreen(XtDisplay(main_screen)));
161 
162 	/*
163 	 *	now set the properties for the toplevel widget
164 	 */
165 	XtVaSetValues(main_screen,
166 		XtNcolormap, the_colormap,
167 		XtNdepth, the_depth,
168 		NULL
169 	);
170 	XSetWindowColormap(XtDisplay(main_screen), DefaultRootWindow(XtDisplay(main_screen)), the_colormap);
171 
172 	/*
173 	 *	get the application resource from hardcoded data
174 	 */
175 	DEBUG("xtrojka.c", "main/3")
176 	XtGetApplicationResources(main_screen, &app_data, resources,
177 		XtNumber(resources), NULL, 0);
178 
179 	/*
180 	 *	read the preferences
181 	 */
182 	init_preferences();
183 
184 	/*
185 	 *	intialize menu's, windows, shells
186 	 */
187 	init_uif();
188 
189 	/*
190 	 *	this is the last manual initialization before
191 	 *	the application is realized
192 	 */
193 	init_xtrojka();
194 
195 
196 	/*
197 	 *	Now install the routine that will catch the
198 	 *	'MapNotify' event
199 	 */
200 	init_map_catcher();
201 
202 	/*
203 	 *	Realize the application. As soon as the application
204 	 *	is realized, a 'MapNotify' event will be generated
205 	 *	which will be caught by 'i_just_got_mapped_hlr()'.
206 	 */
207 
208 	DEBUG("xtrojka.c", "main/4");
209 	XtRealizeWidget(main_screen);
210 
211 	/*
212 	 *	Finally, start the game loop
213 	 */
214 	show_slist();
215 	show_stat();
216 	mainloop();
217 }
218 
219 
220 
quit_appl_action(w,unused,event,continue_to_dispatch)221 void quit_appl_action(w, unused, event, continue_to_dispatch)
222 Widget w;
223 XtPointer unused;
224 XEvent *event;
225 Boolean *continue_to_dispatch;
226 /*
227  *	this callback is called on 'Alt-Q'
228  */
229 {
230 	quit_appl();
231 }
232 
233 
quit_appl(void)234 void quit_appl(void)
235 {
236 	DEBUG("xtrojka.c", "quit_appl")
237 
238 	write_prefs();
239 	exit(0);
240 }
241 
242 
init_xtrojka(void)243 void init_xtrojka(void)
244 {
245 	DEBUG("xtrojka.c", "initialize")
246 
247 	check_wizard_item(is_wizard);
248 	check_slick_item(is_slick);
249 	set_speed_item(speed);
250 }
251 
252 
init_uif()253 void init_uif()
254 {
255 	/*
256 	 *	first initialize the basic widget system
257 	 */
258 	init_windows();
259 
260 	/*
261 	 *	now initialize the subsystem
262 	 */
263 	init_menu_sys();
264 
265 	init_bitmaps();
266 
267 
268 	set_icons();
269 }
270 
271 
init_map_catcher(void)272 void init_map_catcher(void)
273 /*
274  *	install a map handler. Call just before 'XtAppRealize'
275  */
276 {
277 	XtAddEventHandler(main_screen,
278 		StructureNotifyMask,
279 		False,
280 		(XtEventHandler)i_just_got_mapped_hlr,
281 		(Opaque)NULL
282 	);
283 }
284 
285 
i_just_got_mapped_hlr(w,p,me,continue_to_dispatch)286 void i_just_got_mapped_hlr(w, p, me, continue_to_dispatch)
287 Widget w;
288 XtPointer p;
289 XMapEvent *me;
290 Boolean *continue_to_dispatch;
291 {
292 	*continue_to_dispatch = TRUE;
293 
294 
295 	if(me->type != MapNotify)
296 		return;
297 
298 	fix_dimensions(main_screen);
299 	fix_dimensions(slist_shell);
300 
301 	init_slist_mgr();
302 	init_actions();
303 	init_event_handlers();
304 }
305 
306 
show_no_debug(void)307 void show_no_debug(void)
308 {
309 	fprintf(stderr,"%s\n", STR_NO_DEBUG);
310 }
311 
312 
show_help(void)313 void show_help(void)
314 {
315 	fprintf(stderr,"%s: xtrojka [-scores] [-help] [-debug] ", STR_USAGE);
316 	fprintf(stderr,"[%s] \n\n", STR_X11_OPTIONS);
317 	fprintf(stderr,"-scores\t%s\n", STR_DISP_SCORE);
318 	fprintf(stderr,"-help\t%s\n", 	STR_THIS_HELP);
319 	fprintf(stderr,"-debug\t%s\n",	STR_SHOW_DEBUG);
320 	fprintf(stderr,"%s.\n\n", 	STR_FOR_X11);
321 	fflush(stderr);
322 }
323 
324 
show_startup_string(void)325 void show_startup_string(void)
326 {
327 	fprintf(stderr,"Xtrojka %s\n", VERSION);
328 
329 	fprintf(stderr,"%s:\n",STR_COM_OPT);
330 #ifdef LOCKING
331 	fprintf(stderr,"\tLOCKING\n");
332 #endif
333 #ifdef DEBUG_INFO
334 	fprintf(stderr,"\tDEBUG_INFO\n");
335 #endif
336 #ifdef XPM
337 	fprintf(stderr,"\tXPM\n");
338 #endif
339 	fputc('\n', stderr);
340 
341 	fprintf(stderr,"%s: %s\n", STR_SCOREFILE, SCOREFILE);
342 
343 	fflush(stderr);
344 }
345 
346 
347 
348 
349