#! /bin/sh /usr/share/dpatch/dpatch-run
## 0001-Code-changes.patch.dpatch by  <jari.aalto@cante.net>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: Original code changes

@DPATCH@

From c456c424872593382a29597d1e9e6668acda14af Mon Sep 17 00:00:00 2001
From: Jari Aalto <jari.aalto@cante.net>
Date: Wed, 5 Aug 2009 00:38:31 +0300
Subject: [PATCH] Code changes

Signed-off-by: Jari Aalto <jari.aalto@cante.net>
---
 games/freecell.c   |    1 +
 games/golf.c       |    2 +-
 games/mastermind.c |    2 +-
 games/solitaire.c  |   15 +++++++++++++++
 games/thornq.c     |    3 ++-
 lib/stack.c        |    1 +
 lib/table.c        |    3 ++-
 lib/xwin.c         |    8 +++++++-
 lib/xwin.h         |    2 +-
 9 files changed, 31 insertions(+), 6 deletions(-)

diff --git a/games/freecell.c b/games/freecell.c
index ac3bf51..3ea53d9 100644
--- a/games/freecell.c
+++ b/games/freecell.c
@@ -18,6 +18,7 @@
 #include <stdlib.h>
 #include <time.h>
 #include "cards.h"
+#include "imagelib.h"
 
 #define W CARD_WIDTH
 #define H CARD_HEIGHT
diff --git a/games/golf.c b/games/golf.c
index 88e100d..632c746 100644
--- a/games/golf.c
+++ b/games/golf.c
@@ -31,7 +31,7 @@ Picture *arrow, *no_arrow;
 
 Stack *deck, *discard, *stacks[7];
 
-static int table_width, table_height;
+int table_width, table_height;
 
 int supress_arrows = 0;
 
diff --git a/games/mastermind.c b/games/mastermind.c
index 2521b44..152e215 100644
--- a/games/mastermind.c
+++ b/games/mastermind.c
@@ -257,7 +257,7 @@ key(int k, int x, int y)
   }
   if (k == 3 || k == 27 || k == 'q')
     exit(0);
-  if (k == KEY_F(2) || k == 'r')
+  if (k == KEY_F(2))
   {
     start_again();
     invalidate(0, 0, table_width, table_height);
diff --git a/games/solitaire.c b/games/solitaire.c
index a2bf83f..772a29c 100644
--- a/games/solitaire.c
+++ b/games/solitaire.c
@@ -19,6 +19,7 @@
 #include <time.h>
 #include <math.h>
 #include "cards.h"
+#include "imagelib.h"
 
 #define W CARD_WIDTH
 #define H CARD_HEIGHT
@@ -31,6 +32,8 @@ Stack *deck, *hole;
 Stack *outcells[4];
 Stack *maincells[7];
 
+static int drag_active = 0;
+
 static int auto_move();
 
 int
@@ -153,6 +156,8 @@ redraw()
   stack_redraw();
 }
 
+static void check_for_end_of_game();
+
 extern char solitaire_help[];
 
 void
@@ -357,6 +362,13 @@ click(int x, int y, int b)
   Picture *cp = get_centered_pic();
   src_stack = 0;
 
+  // we are in a drag and annother mouse-button is pressed: abort drag
+  if (drag_active) {
+    stack_drop(dest_stack, last_n);
+    drag_active = 0;
+    return;
+  };
+  
   if ((cp == youlose || cp == youwin)
       && (x > table_width/2-cp->w/2
 	  && x < table_width/2+cp->w/2
@@ -504,6 +516,7 @@ double_click(int x, int y, int b)
 void
 drag(int x, int y, int b)
 {
+  drag_active = 1;
   if (b > 1) return;
   last_n = n_droppable(x, y);
   stack_continue_drag(last_n, x, y);
@@ -521,6 +534,8 @@ drop(int x, int y, int b)
     return;
   }
 
+  drag_active = 0;
+  
   stack_drop(dest_stack, last_n);
 
   check_for_end_of_game();
diff --git a/games/thornq.c b/games/thornq.c
index 5a4f485..c5216de 100644
--- a/games/thornq.c
+++ b/games/thornq.c
@@ -19,6 +19,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include "cards.h"
+#include "imagelib.h"
 
 #define W CARD_WIDTH
 #define H CARD_HEIGHT
@@ -571,7 +572,7 @@ try_moving_to(int i)
 	int f = stack_get_card(maincells[i], j);
 	if (SUIT(f) == SUIT(c)
 	    && VALUE(c) == VALUE(f) - 1
-	    && ! FACEDOWNP(f)) {
+	    && ! FACEDOWNP(c)) {
 	  clear_arrows();
 	  stack_move_cards(src_stack, src_n, maincells[i]);
 	  while (auto_move()) ;
diff --git a/lib/stack.c b/lib/stack.c
index 3a54465..577d942 100644
--- a/lib/stack.c
+++ b/lib/stack.c
@@ -16,6 +16,7 @@
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <sys/time.h>
 #include <sys/types.h>
 #include <unistd.h>
diff --git a/lib/table.c b/lib/table.c
index 0ae77c3..3f2f33e 100644
--- a/lib/table.c
+++ b/lib/table.c
@@ -118,7 +118,8 @@ init_ace(int argc, char **argv)
     argv[i++] = argv[a++];
   argv[i] = 0;
 
-  xwin_init(argc, argv);
+  if (xwin_init(argc, argv))
+    exit(1);
 }
 
 void
diff --git a/lib/xwin.c b/lib/xwin.c
index ebeebcf..8e040c2 100644
--- a/lib/xwin.c
+++ b/lib/xwin.c
@@ -125,7 +125,7 @@ void break_here(){}
 
 static char *name;
 
-void
+int
 xwin_init(int argc, char **argv)
 {
   char *sl;
@@ -140,6 +140,10 @@ xwin_init(int argc, char **argv)
   if (sl) name = sl+1;
 
   display = XOpenDisplay(0);
+  if (display == NULL) {
+    fprintf(stderr, "Error: Can't open display!\n");
+    return 1;
+  };
   screen = XDefaultScreen(display);
   rootwin = XDefaultRootWindow(display);
 
@@ -215,6 +219,8 @@ xwin_init(int argc, char **argv)
   if (!font) font = XLoadQueryFont(display, "fixed");
   font_width = font->max_bounds.width;
   font_height = font->ascent + font->descent;
+
+  return 0;
 }
 
 void
diff --git a/lib/xwin.h b/lib/xwin.h
index e29ba70..9b60e40 100644
--- a/lib/xwin.h
+++ b/lib/xwin.h
@@ -27,7 +27,7 @@ typedef struct {
 } XWin_Event;
 
 /* sets display_width, display_height, table_width/height to preferred */
-void xwin_init (int argc, char **argv);
+int xwin_init (int argc, char **argv);
 /* sets table_width, table_height to actual */
 void xwin_create (int width, int height);
 int xwin_nextevent (XWin_Event *event);
-- 
1.6.3.3

