Android: Fix crash on start up.
[maemo-rb.git] / firmware / target / hosted / android / button-android.c
blob406d37d25b98e016ed0f5a95937badb20ed02996
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (c) 2010 Thomas Martitz
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 #include <jni.h>
24 #include <stdbool.h>
25 #include "button.h"
26 #include "buttonmap.h"
27 #include "config.h"
28 #include "kernel.h"
29 #include "system.h"
30 #include "touchscreen.h"
31 #include "powermgmt.h"
33 extern JNIEnv *env_ptr;
34 extern jclass RockboxService_class;
35 extern jobject RockboxService_instance;
37 static int last_y, last_x;
38 static int last_btns;
40 static enum {
41 STATE_UNKNOWN,
42 STATE_UP,
43 STATE_DOWN,
44 } last_touch_state = STATE_UNKNOWN;
47 * this notifies us in an interrupt-like fashion whether the user just
48 * began or stopped the touch action + where (pixel coordinates) */
49 JNIEXPORT void JNICALL
50 Java_org_rockbox_RockboxFramebuffer_touchHandler(JNIEnv*env, jobject this,
51 jboolean down, jint x, jint y)
53 (void)env;
54 (void)this;
56 if (down)
57 last_touch_state = STATE_DOWN;
58 else
59 last_touch_state = STATE_UP;
61 last_x = x;
62 last_y = y;
66 * this writes in an interrupt-like fashion the button events that the user
67 * generated by pressing/releasing them to a variable */
68 JNIEXPORT bool JNICALL
69 Java_org_rockbox_RockboxFramebuffer_buttonHandler(JNIEnv*env, jclass class,
70 jint keycode, jboolean state)
72 (void)env;
73 (void)class;
75 unsigned button = 0;
77 if (!state)
79 button = multimedia_to_button((int)keycode);
80 if (!button)
81 button = dpad_to_button((int)keycode);
82 if (button)
84 reset_poweroff_timer();
85 queue_post(&button_queue, button, 0);
86 return true;
90 if (!button)
92 button = key_to_button(keycode);
95 if (button == BUTTON_NONE)
97 last_btns = button;
98 return false;
101 if (state)
103 last_btns |= button;
105 else
107 last_btns &= (~button);
108 return false;
111 return true;
114 void button_init_device(void)
116 JNIEnv e = *env_ptr;
117 jclass class = e->FindClass(env_ptr, "org/rockbox/monitors/HeadphoneMonitor");
118 jmethodID constructor = e->GetMethodID(env_ptr, class,
119 "<init>",
120 "(Landroid/content/Context;)V");
121 e->NewObject(env_ptr, class,
122 constructor,
123 RockboxService_instance);
126 int button_read_device(int *data)
128 int btn = last_btns;
129 /* Get grid button/coordinates based on the current touchscreen mode
131 * Caveat: the caller seemingly depends on *data always being filled with
132 * the last known touchscreen position, so always call
133 * touchscreen_to_pixels() */
134 int touch = touchscreen_to_pixels(last_x, last_y, data);
136 if (last_touch_state == STATE_DOWN)
137 btn |= touch;
139 return btn;
142 static int hp_state;
143 JNIEXPORT void JNICALL
144 Java_org_rockbox_monitors_HeadphoneMonitor_postHpStateChanged(JNIEnv *env,
145 jobject this,
146 jint state)
148 (void)env; (void)this;
149 hp_state = state;
151 /* Tell if anything is in the jack.
153 * since this is called from the tick task, which isn't attached to
154 * the dalvik VM, it's not permitted to make JNI calls (therefore
155 * we need the above callback) */
156 bool headphones_inserted(void)
158 /* 0 is disconnected, 1 and 2 are connected */
159 return (hp_state == 0) ? false : true;