--- joy2key-1.6.1.orig/joy2key.c
+++ joy2key-1.6.1/joy2key.c
@@ -96,6 +96,7 @@
     button_actions[256],
     axis_actions[256][2],
     axis_threshold[256][2], /* 0 = low (down); 1 = hi (up) */
+    axis_threshold_defined[256], /* 1 == defined */
     axis_act_counter=0,
     button_act_counter=0,
     thresh_counter=0,
@@ -134,6 +135,7 @@
 void cleanup(int s);
 void repeat_handler(int s);
 void calibrate(int num);
+int argtokey(char *arg);
 
 int main(int argc, char **argv)
 {
@@ -160,6 +162,7 @@
 		   JOY2KEY_VERSION, __DATE__, __TIME__);
 
     memset(axis_threshold, 0, sizeof(axis_threshold));
+    memset(axis_threshold_defined, 0, sizeof(axis_threshold_defined));
     memset(axis_actions, 0, sizeof(axis_actions));
     memset(button_actions, 0, sizeof(button_actions));
     memset(button_repeat_flags, 0, sizeof(button_repeat_flags));
@@ -250,7 +253,7 @@
 
     for(i=0; i<numaxes; i++)
     {
-		if(!(axis_threshold[i][0] | axis_threshold[i][1]))
+		if(! axis_threshold_defined[i])
 			calibrate(i);
     }
 
@@ -383,9 +386,12 @@
 						else rcargv[rcargc]=strdup(line);
 						fscanf(file, " %s ", line);
 					}
-					if(feof(file)) rcargv[rcargc]=strdup(line);
+					if(feof(file)) {
+						rcargv[rcargc]=strdup(line);
+						rcargc++;
+					}
 					process_args(rcargc, rcargv);
-					for(rcargc--;rcargc-1;rcargc--) free(rcargv[rcargc]);
+					for(rcargc--;rcargc>0;rcargc--) free(rcargv[rcargc]);
 				}		
 				fscanf(file, " %s ", line);
 			}
@@ -432,10 +438,13 @@
 							else rcargv[rcargc]=strdup(line);
 							fscanf(file, " %s ", line);
 						}
-						if(feof(file)) rcargv[rcargc]=strdup(line);
+						if(feof(file)) {
+							rcargv[rcargc]=strdup(line);
+							rcargc++;
+						}
 /* XXX			process_args(rcargc+1, rcargv); */
 						process_args(rcargc, rcargv);
-						for(;rcargc-1;rcargc--) free(rcargv[rcargc]);
+						for(rcargc--;rcargc>0;rcargc--) free(rcargv[rcargc]);
 					}		
 					fscanf(file, " %s ", line);
 				}
@@ -463,10 +472,13 @@
 							else rcargv[rcargc]=strdup(line);
 							fscanf(file, " %s ", line);
 						}
-						if(feof(file)) rcargv[rcargc]=strdup(line);
+						if(feof(file)) {
+							rcargv[rcargc]=strdup(line);
+							rcargc++;
+						}
 /* XXX			process_args(rcargc+1, rcargv); */
 						process_args(rcargc, rcargv);
-						for(rcargc--;rcargc-1;rcargc--) free(rcargv[rcargc]);
+						for(rcargc--;rcargc>0;rcargc--) free(rcargv[rcargc]);
 					}
 				}	
 				fscanf(file, " %s ", line);
@@ -498,7 +510,7 @@
 				puts("Must specify a target first!");
 				exit(1);
 			}
-			if(i+1>argc) 
+			if(i+1==argc) 
 			{
 				puts("Not enough arguments to -axis");
 				exit(1);
@@ -506,7 +518,7 @@
 			axis_act_counter=0;
 			while((i+1)<argc && argv[i+1][0]!='-')
 			{		
-				if(i+2>argc) 
+				if(i+2==argc) 
 				{
 					puts("Not enough arguments to -axis");
 					exit(1);
@@ -526,7 +538,7 @@
 				exit(1);
 			}
 
-			if(i+1>argc) 
+			if(i+1==argc) 
 			{
 				puts("Not enough arguments to -buttons");
 				exit(1);
@@ -542,7 +554,7 @@
 		}
 		if(!strcmp(argv[i], "-thresh"))
 		{
-			if(i+1>argc) 
+			if(i+1==argc) 
 			{
 				puts("Not enough arguments to -thresh");
 				exit(1);
@@ -552,11 +564,13 @@
 				   (argv[i+1][0]=='-' && isdigit(argv[i+1][1]))))
 			{
 				i++;
-				if(i+2>argc) 
+				if(i+1==argc) 
 				{
 					puts("Not enough arguments to -thresh");
 					exit(1);
 				}
+                if(argv[i][0] != 'x'  && argv[i+1][1] != 'x') 
+                    axis_threshold_defined[thresh_counter] = 1;
 				axis_threshold[thresh_counter][0]=atoi(argv[i]);
 				axis_threshold[thresh_counter++][1]=atoi(argv[++i]);
 			}
@@ -564,7 +578,7 @@
 		}
 		if(!strcmp(argv[i], "-autorepeat"))
 		{
-			if(i+2>argc || (argv[i+1][0]=='-')) 
+			if(i+1==argc || (argv[i+1][0]=='-')) 
 			{
 				repeat_time.it_interval.tv_usec=1000000/DEFAULT_AUTOREPEAT;
 				continue;
@@ -585,7 +599,7 @@
 		}
 		if(!strcmp(argv[i], "-deadzone"))
 		{
-			if(i+2>argc || (argv[i+1][0]=='-')) 
+			if(i+1==argc || (argv[i+1][0]=='-')) 
 			{
 				puts("Not enough arguments to -deadzone");
 				exit(1);		
@@ -611,7 +625,7 @@
 
 		if(!strcmp(argv[i], "-dev"))
 		{
-			if(i+2>argc) 
+			if(i+1==argc) 
 			{
 				puts("Not enough arguments to -dev");
 				exit(1);
@@ -718,6 +732,7 @@
     printf("\nUsing deadzone of %i%%\n", deadzone);
     axis_threshold[num][0]=joymid+((axis_threshold[num][0] - joymid) * (deadzone/100.0));
     axis_threshold[num][1]=joymid+((axis_threshold[num][1] - joymid) * (deadzone/100.0));
+    axis_threshold_defined[num] = 1;
     puts("Calibrations set at:");
     printf("Axis %i low threshold set at %i\n", num, axis_threshold[num][0]);
     printf("Axis %i high threshold set at %i\n", num, axis_threshold[num][1]);    
--- joy2key-1.6.1.orig/configure
+++ joy2key-1.6.1/configure
@@ -3622,15 +3622,31 @@
 # Check whether --enable-X or --disable-X was given.
 if test "${enable_X+set}" = set; then
   enableval="$enable_X"
-  if test $enableval != no; then cat >>confdefs.h <<\_ACEOF
+   if test $enableval != no; then
+                  if test x$x_libraries != x; then
+                      cat >>confdefs.h <<\_ACEOF
 #define ENABLE_X 1
 _ACEOF
- export xstuff="-L$x_libraries -lX11"; fi
+ export xstuff="-L$x_libraries -lX11"
+                  else
+                      cat >>confdefs.h <<\_ACEOF
+#define ENABLE_X 1
+_ACEOF
+ export xstuff="-lX11"
+                  fi
+              fi
 else
-  cat >>confdefs.h <<\_ACEOF
+   if test x$x_libraries != x; then
+                  cat >>confdefs.h <<\_ACEOF
 #define ENABLE_X 1
 _ACEOF
  export X_Stuff="-L$x_libraries -lX11"
+              else
+                  cat >>confdefs.h <<\_ACEOF
+#define ENABLE_X 1
+_ACEOF
+ export X_Stuff="-lX11"
+              fi
 fi;
 
 
--- joy2key-1.6.1.orig/configure.ac
+++ joy2key-1.6.1/configure.ac
@@ -20,8 +20,18 @@
               AC_DEFINE(ENABLE_CONSOLE))
 
 AC_ARG_ENABLE(X, [  --disable-X             do not compile in X features],
-              if test $enableval != no; then AC_DEFINE(ENABLE_X) export xstuff="-L$x_libraries -lX11"; fi,
-              AC_DEFINE(ENABLE_X) export X_Stuff="-L$x_libraries -lX11")
+            [ if test $enableval != no; then
+                  if test x$x_libraries != x; then
+                      AC_DEFINE(ENABLE_X) export xstuff="-L$x_libraries -lX11"
+                  else
+                      AC_DEFINE(ENABLE_X) export xstuff="-lX11"
+                  fi
+              fi ],
+            [ if test x$x_libraries != x; then
+                  AC_DEFINE(ENABLE_X) export X_Stuff="-L$x_libraries -lX11"
+              else
+                  AC_DEFINE(ENABLE_X) export X_Stuff="-lX11"
+              fi ])
 
 AC_SUBST(X_Stuff)
 AC_SUBST(x_headers)
--- joy2key-1.6.1.orig/debian/changelog
+++ joy2key-1.6.1/debian/changelog
@@ -0,0 +1,45 @@
+joy2key (1.6.1-5) unstable; urgency=medium
+
+  * Apply Matthias Bläsing's correct_string_freeing.diff to fix a segmentation
+    fault when parsing a joy2keyrc file.  (Closes: #404550)
+  * Apply Matthias Bläsing's accept_0_as_threshold.diff to allow specifying 0
+    as a threshold value on the command-line or config file.  (Previously, this
+    would cause the built-in calibration function to come up, since a value of
+    0, evaluating as false, was taken as unspecified.)  (Closes: #404543)
+  * Fix an implicit declaration in joy2key.c
+  * Fix some config parsing issues where sometimes some arguments given in a
+    config file would be ignored
+  * debian/rules:
+    - Remove config.{sub,guess} in the clean target, and copy the autotools-dev
+      versions from /usr/share/misc in the configure target.
+
+ -- John Wright <john@movingsucks.org>  Fri,  5 Jan 2007 18:07:47 -0700
+
+joy2key (1.6.1-4) unstable; urgency=low
+
+  * Bump Standards-Version to 3.7.2
+  * Don't install empty NEWS or TODO files
+
+ -- John Wright <john@movingsucks.org>  Mon, 20 Nov 2006 14:08:11 -0700
+
+joy2key (1.6.1-3) unstable; urgency=low
+
+  * Fix configure.ac so the configure script will correctly handle the case of
+    X libraries living in the standard library search path, and regenerate
+    configure with autoconf (Closes: #365146)
+
+ -- John Wright <john@movingsucks.org>  Sat,  6 May 2006 02:56:08 -0600
+
+joy2key (1.6.1-2) unstable; urgency=low
+
+  * Remove dh_make generated joy2key.doc-base.EX I accidentally left behind
+    (Closes: #349611)
+
+ -- John Wright <john@movingsucks.org>  Mon, 23 Jan 2006 23:38:39 -0700
+
+joy2key (1.6.1-1) unstable; urgency=low
+
+  * Initial release (Closes: #345591)
+
+ -- John Wright <john@movingsucks.org>  Sat, 31 Dec 2005 18:52:04 -0700
+
--- joy2key-1.6.1.orig/debian/control
+++ joy2key-1.6.1/debian/control
@@ -0,0 +1,14 @@
+Source: joy2key
+Section: x11
+Priority: optional
+Maintainer: John Wright <john@movingsucks.org>
+Build-Depends: debhelper (>= 4.0.0), autotools-dev, libx11-dev, libxt-dev
+Standards-Version: 3.7.2
+
+Package: joy2key
+Architecture: any
+Depends: ${shlibs:Depends}, ${misc:Depends}
+Description: Translate joystick movements into equivalent keystrokes
+ joy2key allows one to choose keyboard events for joystick axes and buttons,
+ so that a joystick or gamepad can be used with an application that doesn't
+ have native joystick support.
--- joy2key-1.6.1.orig/debian/dirs
+++ joy2key-1.6.1/debian/dirs
@@ -0,0 +1 @@
+usr/bin
--- joy2key-1.6.1.orig/debian/compat
+++ joy2key-1.6.1/debian/compat
@@ -0,0 +1 @@
+4
--- joy2key-1.6.1.orig/debian/copyright
+++ joy2key-1.6.1/debian/copyright
@@ -0,0 +1,25 @@
+This package was debianized by John Wright <john@movingsucks.org> on
+Sat, 31 Dec 2005 18:52:04 -0700.
+
+It was downloaded from http://interreality.org/~tetron/technology/joy2key/
+
+Copyright Holder: Peter Amstutz <tetron@interreality.org>
+
+License:
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+
+On Debian systems, the complete text of the GNU General
+Public License can be found in `/usr/share/common-licenses/GPL'.
--- joy2key-1.6.1.orig/debian/docs
+++ joy2key-1.6.1/debian/docs
@@ -0,0 +1,2 @@
+README
+rawscancodes
--- joy2key-1.6.1.orig/debian/rules
+++ joy2key-1.6.1/debian/rules
@@ -0,0 +1,106 @@
+#!/usr/bin/make -f
+# -*- makefile -*-
+# Sample debian/rules that uses debhelper.
+# This file was originally written by Joey Hess and Craig Small.
+# As a special exception, when this file is copied by dh-make into a
+# dh-make output file, you may use that output file without restriction.
+# This special exception was added by Craig Small in version 0.37 of dh-make.
+
+# Uncomment this to turn on verbose mode.
+#export DH_VERBOSE=1
+
+
+# These are used for cross-compiling and for saving the configure script
+# from having to guess our platform (since we know it already)
+DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
+DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
+
+
+CFLAGS = -Wall -g
+
+ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
+	CFLAGS += -O0
+else
+	CFLAGS += -O2
+endif
+
+config.status: configure
+	dh_testdir
+	# Add here commands to configure the package.
+ifneq "$(wildcard /usr/share/misc/config.sub)" ""
+	cp -f /usr/share/misc/config.sub config.sub
+endif
+ifneq "$(wildcard /usr/share/misc/config.guess)" ""
+	cp -f /usr/share/misc/config.guess config.guess
+endif
+	CFLAGS="$(CFLAGS)" ./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info
+
+
+build: build-stamp
+
+build-stamp:  config.status
+	dh_testdir
+
+	# Add here commands to compile the package.
+	$(MAKE)
+
+	touch build-stamp
+
+clean:
+	dh_testdir
+	dh_testroot
+	rm -f build-stamp 
+
+	# Add here commands to clean up after the build process.
+	-$(MAKE) distclean
+	rm -f config.sub config.guess
+
+	dh_clean 
+
+install: build
+	dh_testdir
+	dh_testroot
+	dh_clean -k 
+	dh_installdirs
+
+	# Add here commands to install the package into debian/joy2key.
+	$(MAKE) install DESTDIR=$(CURDIR)/debian/joy2key
+
+
+# Build architecture-independent files here.
+binary-indep: build install
+# We have nothing to do by default.
+
+# Build architecture-dependent files here.
+binary-arch: build install
+	dh_testdir
+	dh_testroot
+	dh_installchangelogs ChangeLog
+	dh_installdocs
+	dh_installexamples
+#	dh_install
+#	dh_installmenu
+#	dh_installdebconf	
+#	dh_installlogrotate
+#	dh_installemacsen
+#	dh_installpam
+#	dh_installmime
+#	dh_installinit
+#	dh_installcron
+#	dh_installinfo
+	dh_installman
+	dh_link
+	dh_strip
+	dh_compress
+	dh_fixperms
+#	dh_perl
+#	dh_python
+#	dh_makeshlibs
+	dh_installdeb
+	dh_shlibdeps
+	dh_gencontrol
+	dh_md5sums
+	dh_builddeb
+
+binary: binary-indep binary-arch
+.PHONY: build clean binary-indep binary-arch binary install 
--- joy2key-1.6.1.orig/debian/examples
+++ joy2key-1.6.1/debian/examples
@@ -0,0 +1 @@
+joy2keyrc.sample
