Android:
[kugel-rb.git] / firmware / target / hosted / android / button-android.c
blobed1e125223b80fec8a9f318fbcfac19b432816cd
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"
32 extern JNIEnv *env_ptr;
33 static int last_y, last_x;
34 static int last_btns;
36 static enum {
37 STATE_UNKNOWN,
38 STATE_UP,
39 STATE_DOWN,
40 } last_touch_state = STATE_UNKNOWN;
43 * this notifies us in an interrupt-like fashion whether the user just
44 * began or stopped the touch action + where (pixel coordinates) */
45 JNIEXPORT void JNICALL
46 Java_org_rockbox_RockboxFramebuffer_touchHandler(JNIEnv*env, jobject this,
47 jboolean down, jint x, jint y)
49 (void)env;
50 (void)this;
52 if (down)
53 last_touch_state = STATE_DOWN;
54 else
55 last_touch_state = STATE_UP;
57 last_x = x;
58 last_y = y;
62 * this writes in an interrupt-like fashion the button events that the user
63 * generated by pressing/releasing them to a variable */
64 JNIEXPORT bool JNICALL
65 Java_org_rockbox_RockboxFramebuffer_buttonHandler(JNIEnv*env, jclass class,
66 jint keycode, jboolean state)
68 (void)env;
69 (void)class;
71 unsigned button = 0;
73 if (!state)
75 button = multimedia_to_button((int)keycode);
76 if (!button)
77 button = dpad_to_button((int)keycode);
78 if (button)
80 queue_post(&button_queue, button, 0);
81 return true;
85 if (!button)
87 button = key_to_button(keycode);
90 if (button == BUTTON_NONE)
92 last_btns = button;
93 return false;
96 if (state)
98 last_btns |= button;
100 else
102 last_btns &= (~button);
103 return false;
106 return true;
109 void button_init_device(void)
113 int button_read_device(int *data)
115 int btn = last_btns;
116 /* Get grid button/coordinates based on the current touchscreen mode
118 * Caveat: the caller seemingly depends on *data always being filled with
119 * the last known touchscreen position, so always call
120 * touchscreen_to_pixels() */
121 int touch = touchscreen_to_pixels(last_x, last_y, data);
123 if (last_touch_state == STATE_DOWN)
124 btn |= touch;
126 return btn;