Index: apps/playback.c
===================================================================
RCS file: /cvsroot/rockbox/apps/playback.c,v
retrieving revision 1.315
diff -u -r1.315 playback.c
--- apps/playback.c	15 May 2006 02:37:06 -0000	1.315
+++ apps/playback.c	23 May 2006 21:09:50 -0000
@@ -2149,6 +2149,9 @@
 void audio_thread(void)
 {
     struct event ev;
+#ifdef HAVE_HEADPHONE_DETECTION
+    bool phones_present = false;
+#endif
 
     /* At first initialize audio system in background. */
     playback_init();
@@ -2163,6 +2166,39 @@
         else
             queue_wait_w_tmo(&audio_queue, &ev, HZ);
 
+#ifdef HAVE_HEADPHONE_DETECTION
+        if (global_settings.pause_phones_unplugged > 0)
+        {
+            if ( headphones_inserted() )
+            {
+                if ( phones_present )
+                {
+                    audio_pause();
+                    //queue_post(&audio_queue, Q_AUDIO_PAUSE, (bool *)true);
+                    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 )
+                        audio_resume();
+                        //queue_post(&audio_queue, Q_AUDIO_PAUSE, (bool *)false);
+                    backlight_on();
+                    phones_present = true;
+                }
+            }
+        }
+#endif
+
         switch (ev.id) {
             case Q_AUDIO_FILL_BUFFER:
                 if (!filling)
Index: apps/settings.c
===================================================================
RCS file: /cvsroot/rockbox/apps/settings.c,v
retrieving revision 1.387
diff -u -r1.387 settings.c
--- apps/settings.c	22 May 2006 16:40:40 -0000	1.387
+++ apps/settings.c	23 May 2006 21:09:51 -0000
@@ -563,6 +563,12 @@
     {1, S_O(warnon_erase_dynplaylist), false,
         "warn when erasing dynamic playlist", off_on },
 
+#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.218
diff -u -r1.218 settings.h
--- apps/settings.h	22 May 2006 16:40:40 -0000	1.218
+++ apps/settings.h	23 May 2006 21:09:52 -0000
@@ -494,6 +494,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.255
diff -u -r1.255 settings_menu.c
--- apps/settings_menu.c	22 May 2006 16:40:40 -0000	1.255
+++ apps/settings_menu.c	23 May 2006 21:09:54 -0000
@@ -1548,6 +1548,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;
@@ -1577,6 +1628,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.404
diff -u -r1.404 tree.c
--- apps/tree.c	21 May 2006 11:00:01 -0000	1.404
+++ apps/tree.c	23 May 2006 21:09:54 -0000
@@ -419,7 +419,20 @@
 
         /* always resume? */
         if ( global_settings.resume || ! just_powered_on)
-            do_resume = true;
+#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.251
diff -u -r1.251 english.lang
--- apps/lang/english.lang	22 May 2006 16:40:41 -0000	1.251
+++ apps/lang/english.lang	23 May 2006 21:09:58 -0000
@@ -8529,3 +8529,59 @@
     *: "Remote Scrolling Options"
   </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>
\ No newline at end of file
Index: firmware/drivers/button.c
===================================================================
RCS file: /cvsroot/rockbox/firmware/drivers/button.c,v
retrieving revision 1.142
diff -u -r1.142 button.c
--- firmware/drivers/button.c	24 Apr 2006 07:14:16 -0000	1.142
+++ firmware/drivers/button.c	23 May 2006 21:10:00 -0000
@@ -1397,3 +1397,10 @@
     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	23 May 2006 21:10:00 -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.22
diff -u -r1.22 config-ipodnano.h
--- firmware/export/config-ipodnano.h	15 Apr 2006 13:07:21 -0000	1.22
+++ firmware/export/config-ipodnano.h	23 May 2006 21:10:00 -0000
@@ -108,6 +108,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.18
diff -u -r1.18 config-ipodvideo.h
--- firmware/export/config-ipodvideo.h	15 Apr 2006 13:07:21 -0000	1.18
+++ firmware/export/config-ipodvideo.h	23 May 2006 21:10:00 -0000
@@ -110,6 +110,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
 
