1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
diff --git a/android/AndroidManifest.xml b/android/AndroidManifest.xml
index 4518241..be7045d 100644
--- a/android/AndroidManifest.xml
+++ b/android/AndroidManifest.xml
@@ -16,8 +16,19 @@
             </intent-filter>
       	</activity>
         <service android:name=".RockboxService"/>
+        <receiver android:name="RockboxMediaButtonReceiver$MediaReceiver"
+				  android:enabled="true"
+				  android:exported="true">
+    		<intent-filter>
+        		<action android:name="android.intent.action.MEDIA_BUTTON" />
+    		</intent-filter>
+		</receiver>

     </application>
 <uses-sdk android:minSdkVersion="4" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
+
+
+
+
 </manifest> 
diff --git a/android/src/org/rockbox/RockboxMediaButtonReceiver.java b/android/src/org/rockbox/RockboxMediaButtonReceiver.java
new file mode 100644
index 0000000..5aa2188
--- /dev/null
+++ b/android/src/org/rockbox/RockboxMediaButtonReceiver.java
@@ -0,0 +1,144 @@
+package org.rockbox;
+
+import java.lang.reflect.Method;
+import android.content.BroadcastReceiver;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.media.AudioManager;
+import android.view.KeyEvent;
+
+public class RockboxMediaButtonReceiver
+{
+    /* A note on the API being used. 2.2 introduces a new and sane API
+     * for handling multimedia button presses
+     * http://android-developers.blogspot.com/2010/06/allowing-applications-to-play-nicer.html
+     * 
+     * the old API is flawed. It doesn't have management for
+     * concurrent media apps
+     * 
+     * if multiple media apps are running 
+     * probably all of them want to respond to media keys
+     * 
+     * it's not clear which app wins, it depends on the
+     * priority set for the IntentFilter (see below)
+     * 
+     * so this all might or might not work on < 2.2 */
+    
+    private interface IMultiMediaReceiver {
+        void register();
+        void unregister();
+    }
+    IMultiMediaReceiver api;
+    
+    private static class NewApi implements IMultiMediaReceiver
+    {
+        private Method register_method;
+        private Method unregister_method;
+        private AudioManager audio_manager;
+        private ComponentName receiver_name;
+        /* the constructor gets the methods through reflection so that
+         * this compiles on pre-2.2 devices */
+        NewApi(Context c) throws SecurityException, NoSuchMethodException
+        {
+            register_method = AudioManager.class.getMethod(
+                                    "registerMediaButtonEventReceiver",
+                                    new Class[] { ComponentName.class } );
+            unregister_method = AudioManager.class.getMethod(
+                                    "unregisterMediaButtonEventReceiver",
+                                    new Class[] { ComponentName.class } );
+
+            audio_manager = (AudioManager)c.getSystemService(Context.AUDIO_SERVICE);
+            receiver_name = new ComponentName(c, MediaReceiver.class);
+        }
+        @Override
+        public void register()
+        {
+            try {
+                register_method.invoke(audio_manager, receiver_name);
+            } catch (Exception e) {
+                // Nothing
+                e.printStackTrace();
+            }            
+        }
+
+        @Override
+        public void unregister()
+        {
+            try
+            {
+                unregister_method.invoke(audio_manager, receiver_name);
+            } catch (Exception e) {
+                // Nothing
+                e.printStackTrace();
+            }
+        }
+        
+    }
+
+    private static class OldApi implements IMultiMediaReceiver
+    {
+        private static final IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
+        private MediaReceiver receiver;
+        private Context context;
+        OldApi(Context c)
+        {
+            filter.setPriority(1); /* 1 higher than the built-in media player */
+            receiver = new MediaReceiver();
+            context = c;
+        }
+        
+        @Override
+        public void register()
+        {
+            context.registerReceiver(receiver, filter);            
+        }
+
+        @Override
+        public void unregister()
+        {
+            context.unregisterReceiver(receiver);
+        }
+        
+    }
+
+    public RockboxMediaButtonReceiver(Context c)
+    {
+        try {
+            api = new NewApi(c);
+        } catch (Exception e) {
+            api = new OldApi(c);
+        }
+    }
+
+    public void register()
+    {
+        api.register();
+    }
+    
+    public void unregister()
+    {
+        api.register();
+    }
+
+    /* helper class for the manifest */
+    public static class MediaReceiver extends BroadcastReceiver
+    {
+        private native void postMediaButton(int key_code);
+        
+        @Override
+        public void onReceive(Context context, Intent intent)
+        {
+            if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction()))
+            {
+                KeyEvent key = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
+                if (key.getAction() == KeyEvent.ACTION_UP)
+                {   /* pass the pressed key to Rockbox */
+                    postMediaButton(key.getKeyCode());
+                    abortBroadcast();
+                }
+            }
+        }
+    }
+}
diff --git a/android/src/org/rockbox/RockboxService.java b/android/src/org/rockbox/RockboxService.java
index 1416886..ba2327f 100644
--- a/android/src/org/rockbox/RockboxService.java
+++ b/android/src/org/rockbox/RockboxService.java
@@ -64,6 +64,9 @@ public class RockboxService extends Service
     private Object[] mStopForegroundArgs = new Object[1];
     private IntentFilter itf;
     private BroadcastReceiver batt_monitor;
+    private RockboxMediaButtonReceiver media_monitor;
+
+    @SuppressWarnings("unused")
     private int battery_level;

     @Override
@@ -184,11 +187,17 @@ public class RockboxService extends Service
         {
             public void run()
             {
+                LOG("main");
                 main();
             }
         },"Rockbox thread");
         rb.setDaemon(false);
         rb.start();
+        
+        IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
+        filter.setPriority(1);
+        media_monitor = new RockboxMediaButtonReceiver(this);
+        media_monitor.register();
     }

     private native void main();
@@ -349,5 +358,6 @@ public class RockboxService extends Service
         super.onDestroy();
         /* Make sure our notification is gone. */
         stopForegroundCompat(R.string.notification);
+        media_monitor.unregister();
     }
 }
diff --git a/apps/action.c b/apps/action.c
index 8f427c8..d61930a 100644
--- a/apps/action.c
+++ b/apps/action.c
@@ -183,8 +183,9 @@ static int get_action_worker(int context, int timeout,
     else
         button = button_get_w_tmo(timeout);

-    /* Data from sys events can be pulled with button_get_data */
-    if (button == BUTTON_NONE || button & SYS_EVENT)
+    /* Data from sys events can be pulled with button_get_data
+     * multimedia button presses don't go through the action system */
+    if (button == BUTTON_NONE || button & (SYS_EVENT|BUTTON_MULTIMEDIA))
         return button;
     /* Don't send any buttons through untill we see the release event */
     if (wait_for_release)
diff --git a/apps/gui/wps.c b/apps/gui/wps.c
index a5fe304..7d633ad 100644
--- a/apps/gui/wps.c
+++ b/apps/gui/wps.c
@@ -1045,18 +1045,18 @@ long gui_wps_show(void)
                 exit = true;
                 break;
 #endif
-            case SYS_POWEROFF:
-                default_event_handler(SYS_POWEROFF);
-                break;
             case ACTION_WPS_VIEW_PLAYLIST:
                 gwps_leave_wps();
                 return GO_TO_PLAYLIST_VIEWER;
                 break;
             default:
-                if(default_event_handler(button) == SYS_USB_CONNECTED)
-                {
-                    gwps_leave_wps();
-                    return GO_TO_ROOT;
+                switch(default_event_handler(button))
+                {   /* music has been stopped by the default handler */
+                    case SYS_USB_CONNECTED:
+                    case SYS_CALL_INCOMING:
+                    case BUTTON_MULTIMEDIA_STOP:
+                        gwps_leave_wps();
+                        return GO_TO_ROOT;
                 }
                 update = true;
                 break;
diff --git a/apps/menu.c b/apps/menu.c
index 8142f99..01cc9bf 100644
--- a/apps/menu.c
+++ b/apps/menu.c
@@ -645,10 +645,20 @@ int do_menu(const struct menu_item_ex *start_menu, int *start_selected,
             }
 #endif
         }
-        else if(default_event_handler(action) == SYS_USB_CONNECTED)
+        else
         {
-            ret = MENU_ATTACHED_USB;
-            done = true;
+            switch(default_event_handler(action))
+            {
+                case SYS_USB_CONNECTED:
+                    ret = MENU_ATTACHED_USB;
+                    done = true;
+                    break;
+                case SYS_CALL_HUNG_UP:
+                case BUTTON_MULTIMEDIA_PLAYPAUSE:
+                /* remove splash from playlist_resume() */
+                    redraw_lists = true;
+                    break;
+            }
         }

         if (redraw_lists && !done)
diff --git a/apps/misc.c b/apps/misc.c
index 5f6df46..efa7b97 100644
--- a/apps/misc.c
+++ b/apps/misc.c
@@ -521,6 +521,7 @@ long default_event_handler_ex(long event, void (*callback)(void *), void *parame
 #if CONFIG_PLATFORM & PLATFORM_ANDROID
     static bool resume = false;
 #endif
+
     switch(event)
     {
         case SYS_BATTERY_UPDATE:
@@ -621,11 +622,45 @@ long default_event_handler_ex(long event, void (*callback)(void *), void *parame
             if (resume && playlist_resume() != -1)
             {
                 playlist_start(global_status.resume_index,
-                    global_status.resume_offset);
+                               global_status.resume_offset);
             }
             resume = false;
             return SYS_CALL_HUNG_UP;
 #endif
+#ifdef HAVE_MULTIMEDIA_KEYS
+        /* multimedia keys on keyboards, headsets */
+        case BUTTON_MULTIMEDIA_PLAYPAUSE:
+        {
+            int status = audio_status();
+            if (status & AUDIO_STATUS_PLAY)
+            {
+                if (status & AUDIO_STATUS_PAUSE)
+                    audio_resume();
+                else
+                    audio_pause();
+            }
+            else
+                if (playlist_resume() != -1)
+                {
+                    playlist_start(global_status.resume_index,
+                                   global_status.resume_offset);
+                }
+            return event;
+        }
+        case BUTTON_MULTIMEDIA_NEXT:
+            audio_next();
+            return event;
+        case BUTTON_MULTIMEDIA_PREV:
+            audio_prev();
+            return event;
+        case BUTTON_MULTIMEDIA_STOP:
+            list_stop_handler();
+            return event;
+        case BUTTON_MULTIMEDIA_REW:
+        case BUTTON_MULTIMEDIA_FFWD:
+            /* not supported yet, needs to be done in the WPS */
+            return 0;
+#endif
     }
     return 0;
 }
diff --git a/firmware/export/button.h b/firmware/export/button.h
index 097f50a..3847d2a 100644
--- a/firmware/export/button.h
+++ b/firmware/export/button.h
@@ -69,6 +69,21 @@ int button_apply_acceleration(const unsigned int data);
 #define BUTTON_REL         0x02000000
 #define BUTTON_REPEAT      0x04000000
 #define BUTTON_TOUCHSCREEN 0x08000000
+#define BUTTON_MULTIMEDIA  0x10000000
+
+#define BUTTON_MULTIMEDIA_PLAYPAUSE (BUTTON_MULTIMEDIA|0x01)
+#define BUTTON_MULTIMEDIA_STOP      (BUTTON_MULTIMEDIA|0x02)
+#define BUTTON_MULTIMEDIA_PREV      (BUTTON_MULTIMEDIA|0x04)
+#define BUTTON_MULTIMEDIA_NEXT      (BUTTON_MULTIMEDIA|0x08)
+#define BUTTON_MULTIMEDIA_REW       (BUTTON_MULTIMEDIA|0x10)
+#define BUTTON_MULTIMEDIA_FFWD      (BUTTON_MULTIMEDIA|0x20)
+
+#define BUTTON_MULTIMEDIA_ALL       (BUTTON_MULTIMEDIA_PLAYPAUSE| \
+                                     BUTTON_MULTIMEDIA_STOP| \
+                                     BUTTON_MULTIMEDIA_PREV| \
+                                     BUTTON_MULTIMEDIA_NEXT| \
+                                     BUTTON_MULTIMEDIA_REW | \
+                                     BUTTON_MULTIMEDIA_FFWD)

 #ifdef HAVE_TOUCHSCREEN
 int touchscreen_last_touch(void);
diff --git a/firmware/export/config/application.h b/firmware/export/config/application.h
index 4dc34c1..b731f0c 100644
--- a/firmware/export/config/application.h
+++ b/firmware/export/config/application.h
@@ -77,6 +77,7 @@

 #if (CONFIG_PLATFORM & PLATFORM_ANDROID)
 #define CONFIG_KEYPAD ANDROID_PAD
+#define HAVE_MULTIMEDIA_KEYS
 #elif (CONFIG_PLATFORM & PLATFORM_SDL)
 #define HAVE_SCROLLWHEEL
 #define CONFIG_KEYPAD SDL_PAD
diff --git a/firmware/target/hosted/android/app/button-application.c b/firmware/target/hosted/android/app/button-application.c
index 47798a6..a7d75ef 100644
--- a/firmware/target/hosted/android/app/button-application.c
+++ b/firmware/target/hosted/android/app/button-application.c
@@ -45,3 +45,24 @@ int key_to_button(int keyboard_key)
             return BUTTON_MENU;
     }
 }
+
+unsigned multimedia_to_button(int keyboard_key)
+{
+    switch (keyboard_key)
+    {
+        case KEYCODE_MEDIA_PLAY_PAUSE:
+            return BUTTON_MULTIMEDIA_PLAYPAUSE;
+        case KEYCODE_MEDIA_STOP:
+            return BUTTON_MULTIMEDIA_STOP;
+        case KEYCODE_MEDIA_NEXT:
+            return BUTTON_MULTIMEDIA_NEXT;
+        case KEYCODE_MEDIA_PREVIOUS:
+            return BUTTON_MULTIMEDIA_PREV;
+        case KEYCODE_MEDIA_REWIND:
+            return BUTTON_MULTIMEDIA_REW;
+        case KEYCODE_MEDIA_FAST_FORWARD:
+            return BUTTON_MULTIMEDIA_FFWD;
+        default:
+            return 0;
+    }
+}
diff --git a/firmware/target/hosted/android/app/button-target.h b/firmware/target/hosted/android/app/button-target.h
index 6c7bd27..ca306d4 100644
--- a/firmware/target/hosted/android/app/button-target.h
+++ b/firmware/target/hosted/android/app/button-target.h
@@ -28,6 +28,7 @@
 #undef button_init_device
 void button_init_device(void);
 int button_read_device(int *data);
+unsigned multimedia_to_button(int keyboard_key);

 /* Main unit's buttons */
 #define BUTTON_MENU        0x00000001
diff --git a/firmware/target/hosted/android/button-android.c b/firmware/target/hosted/android/button-android.c
index a7ac9ba..f6e55d7 100644
--- a/firmware/target/hosted/android/button-android.c
+++ b/firmware/target/hosted/android/button-android.c
@@ -28,7 +28,7 @@
 #include "kernel.h"
 #include "system.h"
 #include "touchscreen.h"
-
+#include "debug.h"
 static int last_y, last_x;
 static int last_btns;

@@ -43,7 +43,7 @@ static enum {
  * began or stopped the touch action + where (pixel coordinates) */
 JNIEXPORT void JNICALL
 Java_org_rockbox_RockboxFramebuffer_touchHandler(JNIEnv*env, jobject this,
-                                                 bool down, int x, int y)
+                                                 jboolean down, jint x, jint y)
 {
     (void)env;
     (void)this;
@@ -62,7 +62,7 @@ Java_org_rockbox_RockboxFramebuffer_touchHandler(JNIEnv*env, jobject this,
  * generated by pressing/releasing them to a variable */
 JNIEXPORT bool JNICALL
 Java_org_rockbox_RockboxFramebuffer_buttonHandler(JNIEnv*env, jobject this,
-                                                  int keycode, bool state)
+                                                  jint keycode, jboolean state)
 {
     (void)env;
     (void)this;
@@ -99,3 +99,16 @@ int button_read_device(int *data)

     return btn;
 }
+
+
+JNIEXPORT void JNICALL
+Java_org_rockbox_RockboxMediaButtonReceiver_00024MediaReceiver_postMediaButton
+                                        (JNIEnv*env, jobject this, jint keycode)
+{
+    (void)env;
+    (void)this;
+    unsigned btn = multimedia_to_button((int)keycode);
+
+    if (btn)
+        queue_post(&button_queue, btn, 0);
+}