Index: apps/playback.c
===================================================================
RCS file: /cvsroot/rockbox/apps/playback.c,v
retrieving revision 1.225
diff -u -b -r1.225 playback.c
--- apps/playback.c	30 Mar 2006 12:07:32 -0000	1.225
+++ apps/playback.c	30 Mar 2006 20:20:42 -0000
@@ -1797,6 +1797,9 @@
     struct event ev;
     int last_tick = 0;
     bool play_pending = false;
+#ifdef HAVE_HEADPHONE_DETECTION
+    bool phones_present = false;
+#endif
 
     /* At first initialize audio system in background. */
     playback_init();
@@ -1820,6 +1823,41 @@
             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:
                 /* Don't start playing immediately if user is skipping tracks
Index: apps/settings.c
===================================================================
RCS file: /cvsroot/rockbox/apps/settings.c,v
retrieving revision 1.375
diff -u -b -r1.375 settings.c
--- apps/settings.c	30 Mar 2006 05:56:19 -0000	1.375
+++ apps/settings.c	30 Mar 2006 20:20:43 -0000
@@ -577,6 +577,11 @@
     {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},
+#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.214
diff -u -b -r1.214 settings.h
--- apps/settings.h	29 Mar 2006 16:21:42 -0000	1.214
+++ apps/settings.h	30 Mar 2006 20:20:43 -0000
@@ -485,6 +485,11 @@
 #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 */
+#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 -b -r1.248 settings_menu.c
--- apps/settings_menu.c	30 Mar 2006 05:56:19 -0000	1.248
+++ apps/settings_menu.c	30 Mar 2006 20:20:46 -0000
@@ -1517,6 +1517,51 @@
 }
 #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_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 },
+    };
+
+    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 +1591,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/lang/english.lang
===================================================================
RCS file: /cvsroot/rockbox/apps/lang/english.lang,v
retrieving revision 1.236
diff -u -b -r1.236 english.lang
--- apps/lang/english.lang	30 Mar 2006 05:56:19 -0000	1.236
+++ apps/lang/english.lang	30 Mar 2006 20:20:47 -0000
@@ -3904,3 +3904,21 @@
 eng: "Track Skip Only"
 voice: "Track Skip Only"
 new:
+
+id: LANG_PAUSE_PHONES
+desc: Pause when headphones are unplugged
+eng: "Pause on headphone unplug"
+voice: "Pause on headphone unplug"
+new:
+
+id: LANG_PAUSE_PHONES_RESUME
+desc: Pause and Resume when headphones are unplugged
+eng: "Pause and Resume"
+voice: "Pause and Resume"
+new:
+
+id: LANG_PAUSE_PHONES_RW
+desc: Time in sec to rewind when pausing
+eng: "Duration to rewind"
+voice: "Duration to rewind"
+new:
Index: firmware/drivers/button.c
===================================================================
RCS file: /cvsroot/rockbox/firmware/drivers/button.c,v
retrieving revision 1.135
diff -u -b -r1.135 button.c
--- firmware/drivers/button.c	30 Mar 2006 18:14:08 -0000	1.135
+++ firmware/drivers/button.c	30 Mar 2006 20:20:49 -0000
@@ -1339,3 +1339,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 -b -r1.50 button.h
--- firmware/export/button.h	26 Mar 2006 22:20:27 -0000	1.50
+++ firmware/export/button.h	30 Mar 2006 20:20:49 -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.16
diff -u -b -r1.16 config-ipodnano.h
--- firmware/export/config-ipodnano.h	19 Mar 2006 17:42:58 -0000	1.16
+++ firmware/export/config-ipodnano.h	30 Mar 2006 20:20:49 -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 -b -r1.13 config-ipodvideo.h
--- firmware/export/config-ipodvideo.h	19 Mar 2006 17:42:58 -0000	1.13
+++ firmware/export/config-ipodvideo.h	30 Mar 2006 20:20:49 -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
 
