si4700 tuner: lower stereo switchover range, so it switches to stereo for weaker...
[kugel-rb.git] / apps / gui / usb_screen.c
blob78f61f5cc746d1512fe6952900af019727334206
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Björn Stenberg
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 ****************************************************************************/
22 #include <stdio.h>
23 #include <stdbool.h>
24 #include "action.h"
25 #include "font.h"
26 #ifdef HAVE_REMOTE_LCD
27 #include "lcd-remote.h"
28 #endif
29 #include "lang.h"
30 #include "usb.h"
31 #if defined(HAVE_USBSTACK)
32 #include "usb_core.h"
33 #ifdef USB_ENABLE_HID
34 #include "usb_keymaps.h"
35 #endif
36 #endif
37 #include "settings.h"
38 #include "led.h"
39 #include "appevents.h"
40 #include "usb_screen.h"
42 #ifdef HAVE_LCD_BITMAP
43 #include "bitmaps/usblogo.h"
44 #include "skin_engine/skin_fonts.h"
45 #endif
47 #ifdef HAVE_REMOTE_LCD
48 #include "bitmaps/remote_usblogo.h"
49 #endif
51 #if (CONFIG_STORAGE & STORAGE_MMC)
52 #include "ata_mmc.h"
53 #endif
55 #ifdef USB_ENABLE_HID
56 int usb_keypad_mode;
57 static bool usb_hid;
58 #endif
60 #ifndef SIMULATOR
62 static int handle_usb_events(void)
64 #if (CONFIG_STORAGE & STORAGE_MMC)
65 int next_update=0;
66 #endif /* STORAGE_MMC */
68 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
69 while(1)
71 int button;
72 #ifdef USB_ENABLE_HID
73 if (usb_hid)
75 button = get_hid_usb_action();
77 /* On mode change, we need to refresh the screen */
78 if (button == ACTION_USB_HID_MODE_SWITCH_NEXT ||
79 button == ACTION_USB_HID_MODE_SWITCH_PREV)
81 break;
84 else
85 #endif
87 button = button_get_w_tmo(HZ/2);
88 /* hid emits the event in get_action */
89 send_event(GUI_EVENT_ACTIONUPDATE, NULL);
92 switch(button)
94 case SYS_USB_DISCONNECTED:
95 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
96 return 1;
97 case SYS_CHARGER_DISCONNECTED:
98 /*reset rockbox battery runtime*/
99 global_status.runtime = 0;
100 break;
101 case SYS_TIMEOUT:
102 break;
105 #if (CONFIG_STORAGE & STORAGE_MMC) /* USB-MMC bridge can report activity */
106 if(TIME_AFTER(current_tick,next_update))
108 if(usb_inserted()) {
109 led(mmc_usb_active(HZ));
111 next_update=current_tick+HZ/2;
113 #endif /* STORAGE_MMC */
116 return 0;
118 #endif /* SIMULATOR */
120 #define MODE_NAME_LEN 32
122 struct usb_screen_vps_t
124 struct viewport parent;
125 #ifdef HAVE_LCD_BITMAP
126 struct viewport logo;
127 #ifdef USB_ENABLE_HID
128 struct viewport title;
129 #endif
130 #endif
133 #ifdef HAVE_LCD_BITMAP
134 static void usb_screen_fix_viewports(struct screen *screen,
135 struct usb_screen_vps_t *usb_screen_vps)
137 bool disable = true;
138 int logo_width, logo_height;
139 struct viewport *parent = &usb_screen_vps->parent;
140 struct viewport *logo = &usb_screen_vps->logo;
142 #ifdef HAVE_REMOTE_LCD
143 if (screen->screen_type == SCREEN_REMOTE)
145 logo_width = BMPWIDTH_remote_usblogo;
146 logo_height = BMPHEIGHT_remote_usblogo;
148 else
149 #endif
151 logo_width = BMPWIDTH_usblogo;
152 logo_height = BMPHEIGHT_usblogo;
155 viewport_set_defaults(parent, screen->screen_type);
156 disable = (parent->width < logo_width || parent->height < logo_height);
157 viewportmanager_theme_enable(screen->screen_type, !disable, parent);
158 screen->clear_display();
159 screen->stop_scroll();
161 *logo = *parent;
162 logo->x = parent->x + parent->width - logo_width;
163 logo->y = parent->y + (parent->height - logo_height) / 2;
164 logo->width = logo_width;
165 logo->height = logo_height;
167 #ifdef USB_ENABLE_HID
168 if (usb_hid)
170 struct viewport *title = &usb_screen_vps->title;
171 int char_height = font_get(parent->font)->height;
172 *title = *parent;
173 title->y = logo->y + logo->height + char_height;
174 title->height = char_height;
175 /* try to fit logo and title to parent */
176 if (parent->y + parent->height < title->y + title->height)
178 logo->y = parent->y;
179 title->y = parent->y + logo->height;
182 #endif
184 #endif
186 static void usb_screens_draw(struct usb_screen_vps_t *usb_screen_vps_ar)
188 int i;
189 FOR_NB_SCREENS(i)
191 struct screen *screen = &screens[i];
193 struct usb_screen_vps_t *usb_screen_vps = &usb_screen_vps_ar[i];
194 struct viewport *parent = &usb_screen_vps->parent;
195 #ifdef HAVE_LCD_BITMAP
196 struct viewport *logo = &usb_screen_vps->logo;
197 #endif
199 screen->set_viewport(parent);
200 screen->clear_viewport();
201 screen->backlight_on();
203 #ifdef HAVE_LCD_BITMAP
204 screen->set_viewport(logo);
205 #ifdef HAVE_REMOTE_LCD
206 if (i == SCREEN_REMOTE)
208 screen->bitmap(remote_usblogo, 0, 0, logo->width,
209 logo->height);
211 else
212 #endif
214 screen->transparent_bitmap(usblogo, 0, 0, logo->width,
215 logo->height);
216 #ifdef USB_ENABLE_HID
217 if (usb_hid)
219 char modestring[100];
220 screen->set_viewport(&usb_screen_vps->title);
221 usb_screen_vps->title.flags |= VP_FLAG_ALIGN_CENTER;
222 snprintf(modestring, sizeof(modestring), "%s: %s",
223 str(LANG_USB_KEYPAD_MODE),
224 str(keypad_mode_name_get()));
225 screen->puts_scroll(0, 0, modestring);
227 #endif /* USB_ENABLE_HID */
229 screen->set_viewport(parent);
231 #else /* HAVE_LCD_BITMAP */
232 screen->double_height(false);
233 screen->puts_scroll(0, 0, "[USB Mode]");
234 status_set_param(false);
235 status_set_audio(false);
236 status_set_usb(true);
237 #endif /* HAVE_LCD_BITMAP */
239 screen->set_viewport(NULL);
240 screen->update_viewport();
244 void gui_usb_screen_run(bool early_usb)
246 int i;
247 struct usb_screen_vps_t usb_screen_vps_ar[NB_SCREENS];
248 #if defined HAVE_TOUCHSCREEN
249 enum touchscreen_mode old_mode = touchscreen_get_mode();
251 /* TODO: Paint buttons on screens OR switch to point mode and use
252 * touchscreen as a touchpad to move the host's mouse cursor */
253 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
254 #endif
256 #ifndef SIMULATOR
257 usb_acknowledge(SYS_USB_CONNECTED_ACK);
258 #endif
260 #ifdef USB_ENABLE_HID
261 usb_hid = global_settings.usb_hid;
262 usb_keypad_mode = global_settings.usb_keypad_mode;
263 #endif
265 if(!early_usb)
267 /* The font system leaves the .fnt fd's open, so we need for force close them all */
268 #ifdef HAVE_LCD_BITMAP
269 font_reset(NULL);
270 #ifdef HAVE_REMOTE_LCD
271 font_load_remoteui(NULL);
272 #endif
273 skin_font_init(); /* unload all the skin fonts */
274 #endif
277 FOR_NB_SCREENS(i)
279 struct screen *screen = &screens[i];
281 screen->set_viewport(NULL);
282 #ifdef HAVE_LCD_CHARCELLS
283 /* Quick fix. Viewports should really be enabled proper for charcell */
284 viewport_set_defaults(&usb_screen_vps_ar[i].parent, i);
285 #else
286 usb_screen_fix_viewports(screen, &usb_screen_vps_ar[i]);
287 #endif
290 while (1)
292 usb_screens_draw(usb_screen_vps_ar);
293 #ifdef SIMULATOR
294 if (button_get_w_tmo(HZ/2))
295 break;
296 send_event(GUI_EVENT_ACTIONUPDATE, NULL);
297 #else
298 if (handle_usb_events())
299 break;
300 #endif /* SIMULATOR */
303 FOR_NB_SCREENS(i)
305 const struct viewport* vp = NULL;
307 #if defined(HAVE_LCD_BITMAP) && defined(USB_ENABLE_HID)
308 vp = usb_hid ? &usb_screen_vps_ar[i].title : NULL;
309 #elif !defined(HAVE_LCD_BITMAP)
310 vp = &usb_screen_vps_ar[i].parent;
311 #endif
312 if (vp)
313 screens[i].scroll_stop(vp);
315 #ifdef USB_ENABLE_HID
316 if (global_settings.usb_keypad_mode != usb_keypad_mode)
318 global_settings.usb_keypad_mode = usb_keypad_mode;
319 settings_save();
321 #endif
323 #ifdef HAVE_TOUCHSCREEN
324 touchscreen_set_mode(old_mode);
325 #endif
327 #ifdef HAVE_LCD_CHARCELLS
328 status_set_usb(false);
329 #endif /* HAVE_LCD_CHARCELLS */
330 #ifdef HAVE_LCD_BITMAP
331 if(!early_usb)
333 /* Not pretty, reload all settings so fonts are loaded again correctly */
334 settings_apply(true);
335 settings_apply_skins();
337 #endif
339 FOR_NB_SCREENS(i)
341 screens[i].backlight_on();
342 viewportmanager_theme_undo(i, false);