Index: apps/playback.c
===================================================================
RCS file: /cvsroot/rockbox/apps/playback.c,v
retrieving revision 1.226
diff -u -r1.226 playback.c
--- apps/playback.c	2 Apr 2006 20:19:00 -0000	1.226
+++ apps/playback.c	4 Apr 2006 22:57:57 -0000
@@ -1797,6 +1797,9 @@
     struct event ev;
     int last_tick = 0;
     bool play_pending = false;
+#ifdef HAVE_HEADPHONE_DETECTION
+    bool phones_present = headphones_inserted();
+#endif
 
     /* At first initialize audio system in background. */
     playback_init();
@@ -1819,6 +1822,41 @@
             ev.id = Q_AUDIO_PLAY;
             ev.data = (bool *)1;
         }
+        
+#ifdef HAVE_HEADPHONE_DETECTION
+        if (global_settings.pause_phones_unplugged > 0)
+        {
+            if ( headphones_inserted() )
+            {
+                if ( phones_present )
+                {
+                    ev.id = Q_AUDIO_PAUSE;
+                    ev.data = (void *)1;
+                    if (global_settings.pause_phones_rw > 0)
+                    {
+                        if ( cur_ti-> id3.elapsed > 
+                        (unsigned int)(global_settings.pause_phones_rw*1000))
+                            audio_ff_rewind(cur_ti->id3.elapsed - 
+                            (global_settings.pause_phones_rw*1000));
+                        else
+                            audio_ff_rewind(0);
+                    }
+                    phones_present = false;
+                }
+            } else {
+                if (! phones_present )
+                {
+                    if ( global_settings.pause_phones_unplugged > 1 )
+                    {
+                        ev.id = Q_AUDIO_RESUME;
+                        ev.data = (void *)1;
+                    }
+                    backlight_on();
+                    phones_present = true;
+                }
+            }
+        }
+#endif
 
         switch (ev.id) {
             case Q_AUDIO_PLAY:
Index: apps/settings.c
===================================================================
RCS file: /cvsroot/rockbox/apps/settings.c,v
retrieving revision 1.378
diff -u -r1.378 settings.c
--- apps/settings.c	2 Apr 2006 12:23:08 -0000	1.378
+++ apps/settings.c	4 Apr 2006 22:58:00 -0000
@@ -583,6 +583,12 @@
     {8, S_O(eq_precut), 0, "eq precut", NULL },
 #endif
 
+#ifdef HAVE_HEADPHONE_DETECTION
+    {2, S_O(pause_phones_unplugged), 0, "pause on headphone unplug", NULL},
+    {4, S_O(pause_phones_rw), 0, "rewind duration on pause", NULL},
+    {1, S_O(pause_phones_autoresume), 0, "disable autoresume if phones not present", off_on },
+#endif
+
     /* If values are just added to the end, no need to bump the version. */
     /* new stuff to be added at the end */
 
Index: apps/settings.h
===================================================================
RCS file: /cvsroot/rockbox/apps/settings.h,v
retrieving revision 1.215
diff -u -r1.215 settings.h
--- apps/settings.h	2 Apr 2006 12:23:08 -0000	1.215
+++ apps/settings.h	4 Apr 2006 22:58:00 -0000
@@ -488,6 +488,12 @@
 #ifdef HAVE_LCD_BITMAP
     unsigned char kbd_file[MAX_FILENAME+1]; /* last keyboard */
 #endif
+
+#ifdef HAVE_HEADPHONE_DETECTION
+    int pause_phones_unplugged; /* pause ipods on headphone unplug */
+    int pause_phones_rw; /* time in s to rewind when pausing */
+    bool pause_phones_autoresume; /* disable auto-resume if no phones? */
+#endif
 };
 
 enum optiontype { INT, BOOL };
Index: apps/settings_menu.c
===================================================================
RCS file: /cvsroot/rockbox/apps/settings_menu.c,v
retrieving revision 1.248
diff -u -r1.248 settings_menu.c
--- apps/settings_menu.c	30 Mar 2006 05:56:19 -0000	1.248
+++ apps/settings_menu.c	4 Apr 2006 22:58:01 -0000
@@ -1517,6 +1517,57 @@
 }
 #endif /* HAVE_DIRCACHE */
 
+#ifdef HAVE_HEADPHONE_DETECTION
+static bool pause_phones_unplugged(void)
+{
+    static const struct opt_items names[] = {
+        { STR(LANG_OFF) },
+        { STR(LANG_PAUSE) },
+        { STR(LANG_PAUSE_PHONES_RESUME) },
+    };
+    bool ret;
+    ret=set_option( str(LANG_PAUSE_PHONES),
+                    &global_settings.pause_phones_unplugged, INT, names, 3, NULL);
+        
+    return ret;
+}
+
+static bool pause_phones_rw(void)
+{
+    bool ret;
+
+    ret = set_int(str(LANG_PAUSE_PHONES_RW), "s", UNIT_SEC,
+                   &global_settings.pause_phones_rw,
+                   NULL, 1, 0, 15, NULL );
+    audio_set_crossfade(global_settings.pause_phones_rw);
+    return ret;
+}
+
+static bool pause_phones_autoresume(void)
+{
+    return set_bool( str(LANG_PAUSE_PHONES_DISABLE_AUTORESUME), 
+                    &global_settings.pause_phones_autoresume );
+}
+
+static bool pause_phones_menu(void)
+{
+    int m;
+    bool result;
+
+    static const struct menu_item items[] = {
+        { ID2P(LANG_PAUSE_PHONES), pause_phones_unplugged },
+        { ID2P(LANG_PAUSE_PHONES_RW), pause_phones_rw },
+        { ID2P(LANG_PAUSE_PHONES_DISABLE_AUTORESUME), pause_phones_autoresume },
+    };
+
+    m=menu_init( items, sizeof(items) / sizeof(*items), NULL,
+                 NULL, NULL, NULL);
+    result = menu_run(m);
+    menu_exit(m);
+    return result;
+}
+#endif
+
 static bool playback_settings_menu(void)
 {
     int m;
@@ -1546,6 +1597,9 @@
 #endif
         { ID2P(LANG_TAGCACHE_FORCE_UPDATE), tagcache_force_update },
         { ID2P(LANG_RUNTIMEDB_ACTIVE), runtimedb },
+#ifdef HAVE_HEADPHONE_DETECTION
+        { ID2P(LANG_PAUSE_PHONES), pause_phones_menu },
+#endif
     };
 
     bool old_shuffle = global_settings.playlist_shuffle;
Index: apps/tree.c
===================================================================
RCS file: /cvsroot/rockbox/apps/tree.c,v
retrieving revision 1.396
diff -u -r1.396 tree.c
--- apps/tree.c	4 Apr 2006 19:28:13 -0000	1.396
+++ apps/tree.c	4 Apr 2006 22:58:02 -0000
@@ -414,7 +414,20 @@
 
         /* always resume? */
         if ( global_settings.resume || ! just_powered_on)
+#ifdef HAVE_HEADPHONE_DETECTION
+        {
+            if ( just_powered_on )
+            {
+                if ( !global_settings.pause_phones_autoresume
+                    || !headphones_inserted() )
+                    do_resume = true;
+            }
+            else
+                do_resume = true;
+        }
+#else
             do_resume = true;
+#endif
 
         if (! do_resume) return;
 
Index: apps/lang/english.lang
===================================================================
RCS file: /cvsroot/rockbox/apps/lang/english.lang,v
retrieving revision 1.242
diff -u -r1.242 english.lang
--- apps/lang/english.lang	3 Apr 2006 21:11:10 -0000	1.242
+++ apps/lang/english.lang	4 Apr 2006 22:58:05 -0000
@@ -8394,3 +8394,59 @@
     *: ""
   </voice>
 </phrase>
+<phrase>
+  id: LANG_PAUSE_PHONES
+  desc: in settings_menu.
+  user:
+  <source>
+    *: "Pause on headphone unplug"
+  </source>
+  <dest>
+    *: "Pause on headphone unplug"
+  </dest>
+  <voice>
+    *: "Pause on headphone unplug"
+  </voice>
+</phrase>
+<phrase>
+  id: LANG_PAUSE_PHONES_RESUME
+  desc: in pause_phones_menu.
+  user:
+  <source>
+    *: "Pause and Resume"
+  </source>
+  <dest>
+    *: "Pause and Resume"
+  </dest>
+  <voice>
+    *: "Pause and Resume"
+  </voice>
+</phrase>
+<phrase>
+  id: LANG_PAUSE_PHONES_RW
+  desc: in pause_phones_menu.
+  user:
+  <source>
+    *: "Duration to rewind"
+  </source>
+  <dest>
+    *: "Duration to rewind"
+  </dest>
+  <voice>
+    *: "Duration to rewind"
+  </voice>
+</phrase>
+<phrase>
+  id: LANG_PAUSE_PHONES_DISABLE_AUTORESUME
+  desc: in pause_phones_menu.
+  user:
+  <source>
+    *: "Disable auto-resume if phones not present"
+  </source>
+  <dest>
+    *: "Disable auto-resume if phones not present"
+  </dest>
+  <voice>
+    *: "Disable auto-resume if phones not present"
+  </voice>
+</phrase>
Index: firmware/drivers/button.c
===================================================================
RCS file: /cvsroot/rockbox/firmware/drivers/button.c,v
retrieving revision 1.136
diff -u -r1.136 button.c
--- firmware/drivers/button.c	1 Apr 2006 23:48:03 -0000	1.136
+++ firmware/drivers/button.c	4 Apr 2006 22:58:07 -0000
@@ -1338,3 +1338,9 @@
     queue_clear(&button_queue);
 }
 
+#ifdef HAVE_HEADPHONE_DETECTION
+bool headphones_inserted(void)
+{
+    return (GPIOA_INPUT_VAL & 0x80)?false:true;
+}
+#endif
Index: firmware/export/button.h
===================================================================
RCS file: /cvsroot/rockbox/firmware/export/button.h,v
retrieving revision 1.50
diff -u -r1.50 button.h
--- firmware/export/button.h	26 Mar 2006 22:20:27 -0000	1.50
+++ firmware/export/button.h	4 Apr 2006 22:58:07 -0000
@@ -55,7 +55,9 @@
 #ifdef HAS_REMOTE_BUTTON_HOLD
 bool remote_button_hold(void);
 #endif
-
+#ifdef HAVE_HEADPHONE_DETECTION
+bool headphones_inserted(void);
+#endif
 
 #define  BUTTON_NONE        0x00000000
 
Index: firmware/export/config-ipodnano.h
===================================================================
RCS file: /cvsroot/rockbox/firmware/export/config-ipodnano.h,v
retrieving revision 1.17
diff -u -r1.17 config-ipodnano.h
--- firmware/export/config-ipodnano.h	4 Apr 2006 10:03:09 -0000	1.17
+++ firmware/export/config-ipodnano.h	4 Apr 2006 22:58:07 -0000
@@ -100,6 +100,9 @@
 /* Define this if you have adjustable CPU frequency */
 #define HAVE_ADJUSTABLE_CPU_FREQ
 
+/* Define this if you can detect headphones */
+#define HAVE_HEADPHONE_DETECTION
+
 #define BOOTFILE_EXT "ipod"
 #define BOOTFILE "rockbox." BOOTFILE_EXT
 
Index: firmware/export/config-ipodvideo.h
===================================================================
RCS file: /cvsroot/rockbox/firmware/export/config-ipodvideo.h,v
retrieving revision 1.13
diff -u -r1.13 config-ipodvideo.h
--- firmware/export/config-ipodvideo.h	19 Mar 2006 17:42:58 -0000	1.13
+++ firmware/export/config-ipodvideo.h	4 Apr 2006 22:58:07 -0000
@@ -102,6 +102,9 @@
 /* Define this if you have adjustable CPU frequency */
 #define HAVE_ADJUSTABLE_CPU_FREQ
 
+/* Define this if you can detect headphones */
+#define HAVE_HEADPHONE_DETECTION
+
 #define BOOTFILE_EXT "ipod"
 #define BOOTFILE "rockbox." BOOTFILE_EXT
 
