$OpenBSD: patch-xmine_c,v 1.1 2007/10/26 21:19:16 ajacoutot Exp $
--- xmine.c.orig	Mon Dec 26 12:31:20 1994
+++ xmine.c	Fri Oct 26 23:07:54 2007
@@ -78,6 +78,13 @@
 #define GSPACEX 16
 #define GSPACEY 16
 
+/* some systems might be better off with using "random()" instead of "rand()"*/
+#if defined(__FreeBSD__) || defined(__OpenBSD__)
+# define USE_RANDOM 1
+#else
+# define USE_RANDOM 0
+#endif
+
 #define SCORE_FILE "~/.xmine_scores"
 #define TOPMARGIN 60
 #define BOTMARGIN 12
@@ -119,6 +126,7 @@ Widget drawarea;
 Display *disp;
 Window win;
 int colordisp;
+Atom delw;
 
 #define COL_BLUE 0
 #define COL_LIMEGREEN 1
@@ -239,6 +247,7 @@ void	get_score_file_name P ((char *));
 int	cant_write_score_file P ((void));
 void	fix_size P ((void));
 void	relax_size P ((void));
+void	GlobalProtoHandler P ((Widget w, XEvent *xev, String *params, Cardinal *n));
 
 Pixmap fillface;
 XtAppContext app;
@@ -271,7 +280,7 @@ main(argc, argv)
 	XColor unused;
 	int i;
 
-	XtActionsRec actions[3];
+	XtActionsRec actions[4];
 	String translations =
 		"<Btn1Down>:   search(down)\n\
 		 <Btn1Up>:     search(up)\n\
@@ -284,6 +293,8 @@ main(argc, argv)
 	toplevel = XtVaAppInitialize(&app, "Xmine", NULL, 0,
 				     &argc, argv, fallbacks, NULL);
 
+	delw = XInternAtom(XtDisplay(toplevel), "WM_DELETE_WINDOW", False);
+
 	main_w = XtVaCreateManagedWidget("main_w", boxWidgetClass, toplevel,
 					 XtNorientation, XtorientVertical,
 					 NULL);
@@ -383,8 +394,14 @@ main(argc, argv)
 	actions[1].proc = search_action;
 	actions[2].string = "mark";
 	actions[2].proc = mark_action;
-	XtAppAddActions(app, actions, 3);
+	actions[3].string = "GlobalProtoHandler";
+	actions[3].proc = GlobalProtoHandler;
 
+	XtAppAddActions(app, actions, 4);
+	XtOverrideTranslations(toplevel, XtParseTranslationTable(
+		"<Message>WM_PROTOCOLS : GlobalProtoHandler()")
+	);
+
 	drawing_a = XtVaCreateManagedWidget
 		("drawing_a",
 		 canvasWidgetClass, main_w,
@@ -404,6 +421,7 @@ main(argc, argv)
 	XtAddEventHandler(toplevel,
 			  StructureNotifyMask, False, resize_handler, 0);
 	XtRealizeWidget(toplevel);
+	XSetWMProtocols(XtDisplay(toplevel), XtWindow(toplevel), &delw, 1);
 	fillface = XCreateBitmapFromData(XtDisplay(main_w),
 					 XtWindow(main_w), fillface_bits,
 					 fillface_width, fillface_height);
@@ -461,13 +479,15 @@ search_action(widget, ev, args, num_args)
 		if (xg == armed_x && yg == armed_y) {
 			if (!armed) {
 				set_face(FACE_OHNO);
-				draw_blank_square(armed_x, armed_y, True);
+				if (gridview[xg][yg] == COVERED)
+					draw_blank_square(armed_x, armed_y, True);
 				armed = True;
 			}
 		} else {
 			if (armed) {
 				set_face(FACE_HAPPY);
-				draw_button(armed_x, armed_y);
+				if (gridview[armed_x][armed_y] == COVERED)
+					draw_button(armed_x, armed_y);
 				armed = False;
 			}
 		}
@@ -628,11 +648,12 @@ arm_clear(x, y, armit)
 	set_face(armit ? FACE_OHNO : FACE_HAPPY);
 	for (dx = -1; dx <= 1; dx++)
 		for (dy = -1; dy <= 1; dy++)
-			if (is_state(x+dx, y+dy, COVERED))
+			if (is_state(x+dx, y+dy, COVERED)) {
 				if (armit)
 					draw_blank_square(x+dx, y+dy, True);
 				else
 					draw_button(x+dx, y+dy);
+			}
 }
 
 #if NeedFunctionPrototypes
@@ -911,12 +932,21 @@ layout_board(fx, fy)
 {
 	int i, x, y, xd, yd, tries;
 
+#if USE_RANDOM
+	srandom((unsigned) time(0));
+#else
 	srand((unsigned int) time(0));
+#endif
 	for (i = 0; i != mine_count; i++) {
 		tries = 1000;
 		do {
+#if USE_RANDOM
+			x = (random()>>1) % gsizex;
+			y = (random()>>1) % gsizey;
+#else
 			x = (rand()>>1) % gsizex;
 			y = (rand()>>1) % gsizey;
+#endif
 			tries--;
 		} while (tries && (grid[x][y] ||
 				   !(x < fx-1 || x > fx+1 || y < fy-1
@@ -1069,6 +1099,7 @@ winner()
 		for (y = 0; y != gsizey; y++)
 			if (gridview[x][y] == COVERED) {
 				gridview[x][y] = MARKED;
+				marked_count++;
 				redrawsquare(x, y);
 			}
 	draw_digits(0, 0);
@@ -1346,6 +1377,10 @@ set_custom(dummy, closure, call_data)
 	XtAddCallback(w, XtNcallback, dialog_ok, NULL);
 	XtManageChild(pane);
 	XtPopup(custom, XtGrabExclusive);
+	XtOverrideTranslations(custom, XtParseTranslationTable(
+		"<Message>WM_PROTOCOLS : GlobalProtoHandler()")
+	);
+	XSetWMProtocols(XtDisplay(custom), XtWindow(custom), &delw, 1);
 	dialog_up = True;
 	while (dialog_up) {
 		XtAppProcessEvent(app, XtIMAll);
@@ -1391,7 +1426,6 @@ get_text_int(w, val)
 	str = XawDialogGetValueString(w);
 	if (!str) return;
 	if (atoi(str)) *val = atoi(str);
-	XawAsciiSourceFreeString(w);
 }
 
 /* ARGSUSED */
@@ -1510,6 +1544,10 @@ best_times(dummy, closure, call_data)
 	if (cant_write_score_file()) XtSetSensitive(w, False);
 	XtManageChild(pane);
 	XtPopup(best, XtGrabExclusive);
+	XtOverrideTranslations(best, XtParseTranslationTable(
+		"<Message>WM_PROTOCOLS : GlobalProtoHandler()")
+	);
+	XSetWMProtocols(XtDisplay(best), XtWindow(best), &delw, 1);
 	dialog_up = True;
 	while (dialog_up) {
 		XtAppProcessEvent(app, XtIMAll);
@@ -1554,6 +1592,10 @@ Xaw version by J%crg Wunsch", PATCHLEVEL, 0xf6 /* o-um
 	XtAddCallback(w, XtNcallback, dialog_ok, NULL);
 	XtManageChild(pane);
 	XtPopup(about, XtGrabExclusive);
+	XtOverrideTranslations(about, XtParseTranslationTable(
+		"<Message>WM_PROTOCOLS : GlobalProtoHandler()")
+	);
+	XSetWMProtocols(XtDisplay(about), XtWindow(about), &delw, 1);
 	dialog_up = True;
 	while (dialog_up) {
 		XtAppProcessEvent(app, XtIMAll);
@@ -1659,6 +1701,10 @@ new_best(closure, id)
 	
 	XtManageChild(pane);
 	XtPopup(custom, XtGrabExclusive);
+	XtOverrideTranslations(custom, XtParseTranslationTable(
+		"<Message>WM_PROTOCOLS : GlobalProtoHandler()")
+	);
+	XSetWMProtocols(XtDisplay(custom), XtWindow(custom), &delw, 1);
 
 	dialog_up = True;
 	while (dialog_up) {
@@ -1672,7 +1718,6 @@ new_best(closure, id)
 		sc->times[level] = timer;
 		write_scores(sc);
 	}
-	if (str) XawAsciiSourceFreeString(pane);
 	best_times(NULL, NULL, NULL);
 }
 
@@ -1814,3 +1859,24 @@ exit_game(dummy, closure, call_data)
 {
 	exit(0);
 }
+
+#if NeedFunctionPrototypes
+void
+GlobalProtoHandler(Widget w, XEvent *xev, String *params, Cardinal *n)
+#else
+void
+GlobalProtoHandler(w, xev, params, n)
+	Widget w;
+	XEvent *xev;
+	String *params;
+	Cardinal *n;
+#endif
+{
+	if(xev->xclient.data.l[0] == delw) {
+		if(w == toplevel)
+			exit(0);
+		else
+			XtPopdown(w);
+	}
+}
+
