add Packard Bell Vibe 500 to RBUtil.
[kugel-rb.git] / apps / gui / usb_screen.c
blob52f9f994f163074503a28c5661daae12c923ade2
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 "action.h"
23 #include "font.h"
24 #ifdef HAVE_REMOTE_LCD
25 #include "lcd-remote.h"
26 #endif
27 #include "lang.h"
28 #include "usb.h"
29 #if defined(HAVE_USBSTACK)
30 #include "usb_core.h"
31 #ifdef USB_ENABLE_HID
32 #include "usb_keymaps.h"
33 #include "sprintf.h"
34 #endif
35 #endif
36 #include "settings.h"
37 #include "led.h"
38 #include "appevents.h"
39 #include "usb_screen.h"
41 #ifdef HAVE_LCD_BITMAP
42 #include "bitmaps/usblogo.h"
43 #endif
45 #ifdef HAVE_REMOTE_LCD
46 #include "bitmaps/remote_usblogo.h"
47 #endif
49 #if (CONFIG_STORAGE & STORAGE_MMC)
50 #include "ata_mmc.h"
51 #endif
53 #ifdef USB_ENABLE_HID
54 int usb_keypad_mode;
55 static bool usb_hid;
56 #endif
58 #ifndef SIMULATOR
60 static int handle_usb_events(void)
62 #if (CONFIG_STORAGE & STORAGE_MMC)
63 int next_update=0;
64 #endif /* STORAGE_MMC */
66 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
67 while(1)
69 int button;
70 #ifdef USB_ENABLE_HID
71 if (usb_hid)
73 button = get_hid_usb_action();
75 /* On mode change, we need to refresh the screen */
76 if (button == ACTION_USB_HID_MODE_SWITCH_NEXT ||
77 button == ACTION_USB_HID_MODE_SWITCH_PREV)
79 break;
82 else
83 #endif
85 button = button_get_w_tmo(HZ/2);
86 /* hid emits the event in get_action */
87 send_event(GUI_EVENT_ACTIONUPDATE, NULL);
90 switch(button)
92 case SYS_USB_DISCONNECTED:
93 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
94 return 1;
95 case SYS_CHARGER_DISCONNECTED:
96 /*reset rockbox battery runtime*/
97 global_status.runtime = 0;
98 break;
99 case SYS_TIMEOUT:
100 break;
103 #if (CONFIG_STORAGE & STORAGE_MMC) /* USB-MMC bridge can report activity */
104 if(TIME_AFTER(current_tick,next_update))
106 if(usb_inserted()) {
107 led(mmc_usb_active(HZ));
109 next_update=current_tick+HZ/2;
111 #endif /* STORAGE_MMC */
114 return 0;
116 #endif /* SIMULATOR */
118 #define MODE_NAME_LEN 32
120 struct usb_screen_vps_t
122 struct viewport parent;
123 #ifdef HAVE_LCD_BITMAP
124 struct viewport logo;
125 #ifdef USB_ENABLE_HID
126 struct viewport title;
127 #endif
128 #endif
131 #ifdef HAVE_LCD_BITMAP
132 static void usb_screen_fix_viewports(struct screen *screen,
133 struct usb_screen_vps_t *usb_screen_vps)
135 bool disable = true;
136 int logo_width, logo_height;
137 struct viewport *parent = &usb_screen_vps->parent;
138 struct viewport *logo = &usb_screen_vps->logo;
140 #ifdef HAVE_REMOTE_LCD
141 if (screen->screen_type == SCREEN_REMOTE)
143 logo_width = BMPWIDTH_remote_usblogo;
144 logo_height = BMPHEIGHT_remote_usblogo;
146 else
147 #endif
149 logo_width = BMPWIDTH_usblogo;
150 logo_height = BMPHEIGHT_usblogo;
153 viewport_set_defaults(parent, screen->screen_type);
154 disable = (parent->width < logo_width || parent->height < logo_height);
155 viewportmanager_theme_enable(screen->screen_type, !disable, parent);
157 *logo = *parent;
158 logo->x = parent->x + parent->width - logo_width;
159 logo->y = parent->y + (parent->height - logo_height) / 2;
160 logo->width = logo_width;
161 logo->height = logo_height;
163 #ifdef USB_ENABLE_HID
164 if (usb_hid)
166 struct viewport *title = &usb_screen_vps->title;
167 int char_height = font_get(parent->font)->height;
168 *title = *parent;
169 title->y = logo->y + logo->height + char_height;
170 title->height = char_height;
171 /* try to fit logo and title to parent */
172 if (parent->y + parent->height < title->y + title->height)
174 logo->y = parent->y;
175 title->y = parent->y + logo->height;
178 #endif
180 #endif
182 static void usb_screens_draw(struct usb_screen_vps_t *usb_screen_vps_ar)
184 int i;
185 FOR_NB_SCREENS(i)
187 struct screen *screen = &screens[i];
189 struct usb_screen_vps_t *usb_screen_vps = &usb_screen_vps_ar[i];
190 struct viewport *parent = &usb_screen_vps->parent;
191 #ifdef HAVE_LCD_BITMAP
192 struct viewport *logo = &usb_screen_vps->logo;
193 #endif
195 screen->set_viewport(parent);
196 screen->clear_viewport();
197 screen->backlight_on();
199 #ifdef HAVE_LCD_BITMAP
200 screen->set_viewport(logo);
201 #ifdef HAVE_REMOTE_LCD
202 if (i == SCREEN_REMOTE)
204 screen->bitmap(remote_usblogo, 0, 0, logo->width,
205 logo->height);
207 else
208 #endif
210 screen->transparent_bitmap(usblogo, 0, 0, logo->width,
211 logo->height);
212 #ifdef USB_ENABLE_HID
213 if (usb_hid)
215 char modestring[100];
216 screen->set_viewport(&usb_screen_vps->title);
217 usb_screen_vps->title.flags |= VP_FLAG_ALIGN_CENTER;
218 snprintf(modestring, sizeof(modestring), "%s: %s",
219 str(LANG_USB_KEYPAD_MODE),
220 str(keypad_mode_name_get()));
221 screen->puts_scroll(0, 0, modestring);
223 #endif /* USB_ENABLE_HID */
225 screen->set_viewport(parent);
227 #else /* HAVE_LCD_BITMAP */
228 screen->double_height(false);
229 screen->puts_scroll(0, 0, "[USB Mode]");
230 status_set_param(false);
231 status_set_audio(false);
232 status_set_usb(true);
233 #endif /* HAVE_LCD_BITMAP */
235 screen->set_viewport(NULL);
236 screen->update_viewport();
240 void gui_usb_screen_run(void)
242 int i;
243 struct usb_screen_vps_t usb_screen_vps_ar[NB_SCREENS];
244 #if defined HAVE_TOUCHSCREEN
245 enum touchscreen_mode old_mode = touchscreen_get_mode();
247 /* TODO: Paint buttons on screens OR switch to point mode and use
248 * touchscreen as a touchpad to move the host's mouse cursor */
249 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
250 #endif
252 #ifndef SIMULATOR
253 usb_acknowledge(SYS_USB_CONNECTED_ACK);
254 #endif
256 #ifdef USB_ENABLE_HID
257 usb_hid = global_settings.usb_hid;
258 usb_keypad_mode = global_settings.usb_keypad_mode;
259 #endif
261 FOR_NB_SCREENS(i)
263 struct screen *screen = &screens[i];
265 screen->set_viewport(NULL);
266 #ifdef HAVE_LCD_BITMAP
267 usb_screen_fix_viewports(screen, &usb_screen_vps_ar[i]);
268 #endif
271 while (1)
273 usb_screens_draw(usb_screen_vps_ar);
274 #ifdef SIMULATOR
275 if (button_get_w_tmo(HZ/2))
276 break;
277 send_event(GUI_EVENT_ACTIONUPDATE, NULL);
278 #else
279 if (handle_usb_events())
280 break;
281 #endif /* SIMULATOR */
284 FOR_NB_SCREENS(i)
286 const struct viewport* vp = NULL;
288 #if defined(HAVE_LCD_BITMAP) && defined(USB_ENABLE_HID)
289 vp = usb_hid ? &usb_screen_vps_ar[i].title : NULL;
290 #elif !defined(HAVE_LCD_BITMAP)
291 vp = &usb_screen_vps_ar[i].parent;
292 #endif
293 if (vp)
294 screens[i].scroll_stop(vp);
296 #ifdef USB_ENABLE_HID
297 if (global_settings.usb_keypad_mode != usb_keypad_mode)
299 global_settings.usb_keypad_mode = usb_keypad_mode;
300 settings_save();
302 #endif
304 #ifdef HAVE_TOUCHSCREEN
305 touchscreen_set_mode(old_mode);
306 #endif
308 #ifdef HAVE_LCD_CHARCELLS
309 status_set_usb(false);
310 #endif /* HAVE_LCD_CHARCELLS */
311 FOR_NB_SCREENS(i)
313 screens[i].backlight_on();
314 viewportmanager_theme_undo(i, false);