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
diff --git a/android/src/org/rockbox/RockboxFramebuffer.java b/android/src/org/rockbox/RockboxFramebuffer.java
index 84974d6..b6523f6 100644
--- a/android/src/org/rockbox/RockboxFramebuffer.java
+++ b/android/src/org/rockbox/RockboxFramebuffer.java
@@ -28,16 +28,20 @@ import org.rockbox.Helper.MediaButtonReceiver;
 import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
+import android.util.DisplayMetrics;
 import android.util.Log;
 import android.view.KeyEvent;
 import android.view.MotionEvent;
 import android.view.View;
+import android.view.ViewConfiguration;

 public class RockboxFramebuffer extends View
 {
     private Bitmap btm;
     private ByteBuffer native_buf;
     private MediaButtonReceiver media_monitor;
+    private final DisplayMetrics metrics;
+    private final ViewConfiguration view_config;

     public RockboxFramebuffer(Context c, int lcd_width, 
                               int lcd_height, ByteBuffer native_fb)
@@ -54,6 +58,9 @@ public class RockboxFramebuffer extends View
         media_monitor.register();
         /* the service needs to know the about us */
         ((RockboxService)c).set_fb(this);
+        
+        metrics = c.getResources().getDisplayMetrics();
+        view_config = ViewConfiguration.get(c);
     }

     public void onDraw(Canvas c) 
@@ -133,6 +140,18 @@ public class RockboxFramebuffer extends View
         media_monitor.unregister();
     }

+    @SuppressWarnings("unused")
+    private int getDpi()
+    {
+        return metrics.densityDpi;
+    }
+    
+    @SuppressWarnings("unused")
+    private int getScrollThreshold()
+    {
+        return view_config.getScaledTouchSlop();
+    }
+
     public native void set_lcd_active(int active);
     public native void touchHandler(boolean down, int x, int y);
     public native boolean buttonHandler(int keycode, boolean state);
diff --git a/apps/gui/bitmap/list.c b/apps/gui/bitmap/list.c
index d78f005..aad3b60 100644
--- a/apps/gui/bitmap/list.c
+++ b/apps/gui/bitmap/list.c
@@ -486,16 +486,26 @@ void _gui_synclist_stop_kinetic_scrolling(void)
  * otherwise it returns true even if it didn't actually scroll,
  * but scrolling mode shouldn't be changed
  **/
+
+ 
+static int scroll_begin_threshold;
+static int threshold_accumulation ;
 static bool swipe_scroll(struct gui_synclist * gui_list, int line_height, int difference)
 {
     /* fixme */
     const enum screen_type screen = screens[SCREEN_MAIN].screen_type;
     const int nb_lines = viewport_get_nb_lines(&list_text[screen]);

+    if (UNLIKELY(scroll_begin_threshold == 0))
+        scroll_begin_threshold = touchscreen_get_scroll_threshold();
+
     /* make selecting items easier */
-    if (abs(difference) < SCROLL_BEGIN_THRESHOLD && scroll_mode == SCROLL_NONE)
+    threshold_accumulation += abs(difference);
+    if (threshold_accumulation < scroll_begin_threshold && scroll_mode == SCROLL_NONE)
         return false;

+    threshold_accumulation = 0;
+
     /* does the list even scroll? if no, return but still show
      * the caller that we would scroll */
     if (nb_lines >= gui_list->nb_items)
@@ -630,8 +640,11 @@ unsigned gui_synclist_do_touchscreen(struct gui_synclist * gui_list)
                         && !is_kinetic_over());
     int icon_width = 0;
     int line, list_width = list_text_vp->width;
+    static bool wait_for_release = false;

     released = (button&BUTTON_REL) != 0;
+    if (released)
+        wait_for_release = false;

     if (button == ACTION_NONE || button == ACTION_UNKNOWN)
     {
@@ -738,21 +751,23 @@ unsigned gui_synclist_do_touchscreen(struct gui_synclist * gui_list)
                 return ACTION_NONE;
             }

+            if (button & BUTTON_REPEAT && scroll_mode == SCROLL_NONE
+                && !wait_for_release)
+            {
+                /* held a single line for a while, bring up the context menu */
+                gui_synclist_select_item(gui_list, list_start_item + line);
+                /* don't sent context repeatedly */
+                wait_for_release = true;
+                return ACTION_STD_CONTEXT;
+            }
             if (released && !cancelled_kinetic)
             {
                 /* Pen was released anywhere on the screen */
                 last_position = 0;
                 if (scroll_mode == SCROLL_NONE)
                 {
+                    /* select current line */
                     gui_synclist_select_item(gui_list, list_start_item + line);
-                    /* If BUTTON_REPEAT is set, then the pen was hold on
-                     * the same line for some time
-                     *  -> context menu
-                     * otherwise,
-                     *  -> select
-                     **/
-                    if (button & BUTTON_REPEAT)
-                        return ACTION_STD_CONTEXT;
                     return ACTION_STD_OK;
                 }
                 else
diff --git a/firmware/drivers/touchscreen.c b/firmware/drivers/touchscreen.c
index 9660e0c..f82409d 100644
--- a/firmware/drivers/touchscreen.c
+++ b/firmware/drivers/touchscreen.c
@@ -167,3 +167,31 @@ enum touchscreen_mode touchscreen_get_mode(void)
 {
     return current_mode;
 }
+
+
+#if ((CONFIG_PLATFORM & PLATFORM_ANDROID) == 0)
+/* android has an API for this */
+
+#define TOUCH_SLOP      16u
+#define REFERENCE_DPI   160
+
+int touchscreen_get_scroll_threshold(void)
+{
+    static int dpi;
+    if (UNLIKELY(!dpi))
+        dpi = lcd_get_dpi();
+
+    /* Inspired by Android calculation
+     *
+     * float density = real dpi / reference dpi (=160)
+     * int threshold = (int) (density * TOUCH_SLOP + 0.5f);(original calculation)
+     *
+     * + 0.5f is for rounding, we use fixed point math to achieve that
+     */
+
+    int result = dpi * (TOUCH_SLOP<<1) / REFERENCE_DPI;
+    result += result & 1; /* round up if needed */
+    return result>>1;
+}
+
+#endif
diff --git a/firmware/export/lcd.h b/firmware/export/lcd.h
index 79231d1..e6e19b1 100644
--- a/firmware/export/lcd.h
+++ b/firmware/export/lcd.h
@@ -549,4 +549,14 @@ extern void lcd_bitmap_transparent(const fb_data *src, int x, int y,

 #endif /* HAVE_LCD_BITMAP */

+
+#ifdef HAVE_TOUCHSCREEN
+/* only needed for touchscreen for now, feel free to implement it for others
+ * once needed
+ */
+
+/* returns the pixel density of the display */
+extern int lcd_get_dpi(void);
+#endif
+
 #endif /* __LCD_H__ */
diff --git a/firmware/export/touchscreen.h b/firmware/export/touchscreen.h
index 7d1eb4a..a27e60c 100644
--- a/firmware/export/touchscreen.h
+++ b/firmware/export/touchscreen.h
@@ -50,5 +50,6 @@ void touchscreen_set_mode(enum touchscreen_mode mode);
 enum touchscreen_mode touchscreen_get_mode(void);
 void touchscreen_disable_mapping(void);
 void touchscreen_reset_mapping(void);
+int touchscreen_get_scroll_threshold(void);

 #endif /* __TOUCHSCREEN_INCLUDE_H_ */
diff --git a/firmware/target/hosted/android/lcd-android.c b/firmware/target/hosted/android/lcd-android.c
index 78b1f12..f4ef7b5 100644
--- a/firmware/target/hosted/android/lcd-android.c
+++ b/firmware/target/hosted/android/lcd-android.c
@@ -35,6 +35,8 @@ static jmethodID java_lcd_update;
 static jmethodID java_lcd_update_rect;

 static bool display_on;
+static int dpi;
+static int scroll_threshold;

 void lcd_init_device(void)
 {
@@ -77,6 +79,20 @@ void lcd_init_device(void)
                                                    RockboxFramebuffer_class,
                                                    "java_lcd_update_rect",
                                                    "(IIII)V");
+
+    jmethodID get_dpi    = e->GetMethodID(env_ptr,
+                                          RockboxFramebuffer_class,
+                                          "getDpi", "()I");
+
+    jmethodID get_scroll_threshold
+                         = e->GetMethodID(env_ptr,
+                                          RockboxFramebuffer_class,
+                                          "getScrollThreshold", "()I");
+
+    dpi              = e->CallIntMethod(env_ptr, RockboxFramebuffer_instance,
+                                        get_dpi);
+    scroll_threshold = e->CallIntMethod(env_ptr, RockboxFramebuffer_instance,
+                                        get_scroll_threshold);
     display_on = true;
 }

@@ -101,6 +117,16 @@ bool lcd_active(void)
     return display_on;
 }

+int lcd_get_dpi(void)
+{
+    return dpi;
+}
+
+int touchscreen_get_scroll_threshold(void)
+{
+    return scroll_threshold;
+}
+
 /*
  * (un)block lcd updates.
  *