Merge tag 'v3.13-final' into maemo-port
[maemo-rb.git] / apps / gui / usb_screen.c
bloba02516fefb667875708ece9e9904f538a80c220b
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"
41 #include "skin_engine/skin_engine.h"
42 #include "playlist.h"
44 #ifdef HAVE_LCD_BITMAP
45 #include "bitmaps/usblogo.h"
46 #endif
48 #ifdef HAVE_REMOTE_LCD
49 #include "bitmaps/remote_usblogo.h"
50 #endif
52 #if (CONFIG_STORAGE & STORAGE_MMC)
53 #include "ata_mmc.h"
54 #endif
56 #ifdef USB_ENABLE_HID
57 int usb_keypad_mode;
58 static bool usb_hid;
59 #endif
61 #ifndef SIMULATOR
63 static int handle_usb_events(void)
65 #if (CONFIG_STORAGE & STORAGE_MMC)
66 int next_update=0;
67 #endif /* STORAGE_MMC */
69 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
70 while(1)
72 int button;
73 #ifdef USB_ENABLE_HID
74 if (usb_hid)
76 button = get_hid_usb_action();
78 /* On mode change, we need to refresh the screen */
79 if (button == ACTION_USB_HID_MODE_SWITCH_NEXT ||
80 button == ACTION_USB_HID_MODE_SWITCH_PREV)
82 break;
85 else
86 #endif
88 button = button_get_w_tmo(HZ/2);
89 /* hid emits the event in get_action */
90 send_event(GUI_EVENT_ACTIONUPDATE, NULL);
93 switch(button)
95 case SYS_USB_DISCONNECTED:
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 #ifdef HAVE_LCD_BITMAP
189 static const struct bitmap* logos[NB_SCREENS] = {
190 &bm_usblogo,
191 #ifdef HAVE_REMOTE_LCD
192 &bm_remote_usblogo,
193 #endif
195 #endif
197 FOR_NB_SCREENS(i)
199 struct screen *screen = &screens[i];
201 struct usb_screen_vps_t *usb_screen_vps = &usb_screen_vps_ar[i];
202 struct viewport *parent = &usb_screen_vps->parent;
203 #ifdef HAVE_LCD_BITMAP
204 struct viewport *logo = &usb_screen_vps->logo;
205 #endif
207 screen->set_viewport(parent);
208 screen->clear_viewport();
209 screen->backlight_on();
211 #ifdef HAVE_LCD_BITMAP
212 screen->set_viewport(logo);
213 screen->bmp(logos[i], 0, 0);
214 if (i == SCREEN_MAIN)
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 struct usb_screen_vps_t usb_screen_vps_ar[NB_SCREENS];
247 #if defined HAVE_TOUCHSCREEN
248 enum touchscreen_mode old_mode = touchscreen_get_mode();
250 /* TODO: Paint buttons on screens OR switch to point mode and use
251 * touchscreen as a touchpad to move the host's mouse cursor */
252 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
253 #endif
255 #ifdef USB_ENABLE_HID
256 usb_hid = global_settings.usb_hid;
257 usb_keypad_mode = global_settings.usb_keypad_mode;
258 #endif
260 if(!early_usb)
262 /* The font system leaves the .fnt fd's open, so we need for force close them all */
263 #ifdef HAVE_LCD_BITMAP
264 FOR_NB_SCREENS(i)
266 font_unload(screens[i].getuifont());
267 screens[i].setuifont(FONT_SYSFIXED);
269 skin_unload_all();
270 #endif
273 FOR_NB_SCREENS(i)
275 struct screen *screen = &screens[i];
277 screen->set_viewport(NULL);
278 #ifdef HAVE_LCD_CHARCELLS
279 /* Quick fix. Viewports should really be enabled proper for charcell */
280 viewport_set_defaults(&usb_screen_vps_ar[i].parent, i);
281 #else
282 usb_screen_fix_viewports(screen, &usb_screen_vps_ar[i]);
283 #endif
286 usb_acknowledge(SYS_USB_CONNECTED_ACK);
288 while (1)
290 usb_screens_draw(usb_screen_vps_ar);
291 #ifdef SIMULATOR
292 if (button_get_w_tmo(HZ/2))
293 break;
294 send_event(GUI_EVENT_ACTIONUPDATE, NULL);
295 #else
296 if (handle_usb_events())
297 break;
298 #endif /* SIMULATOR */
301 FOR_NB_SCREENS(i)
303 const struct viewport* vp = NULL;
305 #if defined(HAVE_LCD_BITMAP) && defined(USB_ENABLE_HID)
306 vp = usb_hid ? &usb_screen_vps_ar[i].title : NULL;
307 #elif !defined(HAVE_LCD_BITMAP)
308 vp = &usb_screen_vps_ar[i].parent;
309 #endif
310 if (vp)
311 screens[i].scroll_stop(vp);
313 #ifdef USB_ENABLE_HID
314 if (global_settings.usb_keypad_mode != usb_keypad_mode)
316 global_settings.usb_keypad_mode = usb_keypad_mode;
317 settings_save();
319 #endif
321 #ifdef HAVE_TOUCHSCREEN
322 touchscreen_set_mode(old_mode);
323 #endif
325 #ifdef HAVE_LCD_CHARCELLS
326 status_set_usb(false);
327 #endif /* HAVE_LCD_CHARCELLS */
328 #ifdef HAVE_LCD_BITMAP
329 if(!early_usb)
331 /* Not pretty, reload all settings so fonts are loaded again correctly */
332 settings_apply(true);
333 settings_apply_skins();
334 /* Reload playlist */
335 playlist_resume();
337 #endif
339 FOR_NB_SCREENS(i)
341 screens[i].backlight_on();
342 viewportmanager_theme_undo(i, false);