Move c/h files implementing/defining standard library stuff into a new libc directory...
[kugel-rb.git] / apps / gui / usb_screen.c
blob9895defc2ba7fb8c40b660b2278c0558a40fb5b1
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 #endif
34 #endif
35 #include "settings.h"
36 #include "led.h"
37 #include "appevents.h"
38 #include "usb_screen.h"
40 #ifdef HAVE_LCD_BITMAP
41 #include "bitmaps/usblogo.h"
42 #endif
44 #ifdef HAVE_REMOTE_LCD
45 #include "bitmaps/remote_usblogo.h"
46 #endif
48 #if (CONFIG_STORAGE & STORAGE_MMC)
49 #include "ata_mmc.h"
50 #endif
52 #ifdef USB_ENABLE_HID
53 int usb_keypad_mode;
54 static bool usb_hid;
55 #endif
57 #ifndef SIMULATOR
59 static int handle_usb_events(void)
61 #if (CONFIG_STORAGE & STORAGE_MMC)
62 int next_update=0;
63 #endif /* STORAGE_MMC */
65 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
66 while(1)
68 int button;
69 #ifdef USB_ENABLE_HID
70 if (usb_hid)
72 button = get_hid_usb_action();
74 /* On mode change, we need to refresh the screen */
75 if (button == ACTION_USB_HID_MODE_SWITCH_NEXT ||
76 button == ACTION_USB_HID_MODE_SWITCH_PREV)
78 break;
81 else
82 #endif
84 button = button_get_w_tmo(HZ/2);
85 /* hid emits the event in get_action */
86 send_event(GUI_EVENT_ACTIONUPDATE, NULL);
89 switch(button)
91 case SYS_USB_DISCONNECTED:
92 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
93 return 1;
94 case SYS_CHARGER_DISCONNECTED:
95 /*reset rockbox battery runtime*/
96 global_status.runtime = 0;
97 break;
98 case SYS_TIMEOUT:
99 break;
102 #if (CONFIG_STORAGE & STORAGE_MMC) /* USB-MMC bridge can report activity */
103 if(TIME_AFTER(current_tick,next_update))
105 if(usb_inserted()) {
106 led(mmc_usb_active(HZ));
108 next_update=current_tick+HZ/2;
110 #endif /* STORAGE_MMC */
113 return 0;
115 #endif /* SIMULATOR */
117 #define MODE_NAME_LEN 32
119 struct usb_screen_vps_t
121 struct viewport parent;
122 #ifdef HAVE_LCD_BITMAP
123 struct viewport logo;
124 #ifdef USB_ENABLE_HID
125 struct viewport title;
126 #endif
127 #endif
130 #ifdef HAVE_LCD_BITMAP
131 static void usb_screen_fix_viewports(struct screen *screen,
132 struct usb_screen_vps_t *usb_screen_vps)
134 bool disable = true;
135 int logo_width, logo_height;
136 struct viewport *parent = &usb_screen_vps->parent;
137 struct viewport *logo = &usb_screen_vps->logo;
139 #ifdef HAVE_REMOTE_LCD
140 if (screen->screen_type == SCREEN_REMOTE)
142 logo_width = BMPWIDTH_remote_usblogo;
143 logo_height = BMPHEIGHT_remote_usblogo;
145 else
146 #endif
148 logo_width = BMPWIDTH_usblogo;
149 logo_height = BMPHEIGHT_usblogo;
152 viewport_set_defaults(parent, screen->screen_type);
153 disable = (parent->width < logo_width || parent->height < logo_height);
154 viewportmanager_theme_enable(screen->screen_type, !disable, parent);
156 *logo = *parent;
157 logo->x = parent->x + parent->width - logo_width;
158 logo->y = parent->y + (parent->height - logo_height) / 2;
159 logo->width = logo_width;
160 logo->height = logo_height;
162 #ifdef USB_ENABLE_HID
163 if (usb_hid)
165 struct viewport *title = &usb_screen_vps->title;
166 int char_height = font_get(parent->font)->height;
167 *title = *parent;
168 title->y = logo->y + logo->height + char_height;
169 title->height = char_height;
170 /* try to fit logo and title to parent */
171 if (parent->y + parent->height < title->y + title->height)
173 logo->y = parent->y;
174 title->y = parent->y + logo->height;
177 #endif
179 #endif
181 static void usb_screens_draw(struct usb_screen_vps_t *usb_screen_vps_ar)
183 int i;
185 /* Clear main and remote screens to remove scrolling line artifacts */
186 lcd_clear_display();
187 #ifdef HAVE_LCD_REMOTE
188 lcd_remote_clear_display();
189 #endif
191 FOR_NB_SCREENS(i)
193 struct screen *screen = &screens[i];
195 struct usb_screen_vps_t *usb_screen_vps = &usb_screen_vps_ar[i];
196 struct viewport *parent = &usb_screen_vps->parent;
197 #ifdef HAVE_LCD_BITMAP
198 struct viewport *logo = &usb_screen_vps->logo;
199 #endif
201 screen->set_viewport(parent);
202 screen->clear_viewport();
203 screen->backlight_on();
205 #ifdef HAVE_LCD_BITMAP
206 screen->set_viewport(logo);
207 #ifdef HAVE_REMOTE_LCD
208 if (i == SCREEN_REMOTE)
210 screen->bitmap(remote_usblogo, 0, 0, logo->width,
211 logo->height);
213 else
214 #endif
216 screen->transparent_bitmap(usblogo, 0, 0, logo->width,
217 logo->height);
218 #ifdef USB_ENABLE_HID
219 if (usb_hid)
221 char modestring[100];
222 screen->set_viewport(&usb_screen_vps->title);
223 usb_screen_vps->title.flags |= VP_FLAG_ALIGN_CENTER;
224 snprintf(modestring, sizeof(modestring), "%s: %s",
225 str(LANG_USB_KEYPAD_MODE),
226 str(keypad_mode_name_get()));
227 screen->puts_scroll(0, 0, modestring);
229 #endif /* USB_ENABLE_HID */
231 screen->set_viewport(parent);
233 #else /* HAVE_LCD_BITMAP */
234 screen->double_height(false);
235 screen->puts_scroll(0, 0, "[USB Mode]");
236 status_set_param(false);
237 status_set_audio(false);
238 status_set_usb(true);
239 #endif /* HAVE_LCD_BITMAP */
241 screen->set_viewport(NULL);
242 screen->update_viewport();
246 void gui_usb_screen_run(void)
248 int i;
249 struct usb_screen_vps_t usb_screen_vps_ar[NB_SCREENS];
250 #if defined HAVE_TOUCHSCREEN
251 enum touchscreen_mode old_mode = touchscreen_get_mode();
253 /* TODO: Paint buttons on screens OR switch to point mode and use
254 * touchscreen as a touchpad to move the host's mouse cursor */
255 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
256 #endif
258 #ifndef SIMULATOR
259 usb_acknowledge(SYS_USB_CONNECTED_ACK);
260 #endif
262 #ifdef USB_ENABLE_HID
263 usb_hid = global_settings.usb_hid;
264 usb_keypad_mode = global_settings.usb_keypad_mode;
265 #endif
267 FOR_NB_SCREENS(i)
269 struct screen *screen = &screens[i];
271 screen->set_viewport(NULL);
272 #ifdef HAVE_LCD_CHARCELLS
273 /* Quick fix. Viewports should really be enabled proper for charcell */
274 viewport_set_defaults(&usb_screen_vps_ar[i].parent, i);
275 #else
276 usb_screen_fix_viewports(screen, &usb_screen_vps_ar[i]);
277 #endif
280 while (1)
282 usb_screens_draw(usb_screen_vps_ar);
283 #ifdef SIMULATOR
284 if (button_get_w_tmo(HZ/2))
285 break;
286 send_event(GUI_EVENT_ACTIONUPDATE, NULL);
287 #else
288 if (handle_usb_events())
289 break;
290 #endif /* SIMULATOR */
293 FOR_NB_SCREENS(i)
295 const struct viewport* vp = NULL;
297 #if defined(HAVE_LCD_BITMAP) && defined(USB_ENABLE_HID)
298 vp = usb_hid ? &usb_screen_vps_ar[i].title : NULL;
299 #elif !defined(HAVE_LCD_BITMAP)
300 vp = &usb_screen_vps_ar[i].parent;
301 #endif
302 if (vp)
303 screens[i].scroll_stop(vp);
305 #ifdef USB_ENABLE_HID
306 if (global_settings.usb_keypad_mode != usb_keypad_mode)
308 global_settings.usb_keypad_mode = usb_keypad_mode;
309 settings_save();
311 #endif
313 #ifdef HAVE_TOUCHSCREEN
314 touchscreen_set_mode(old_mode);
315 #endif
317 #ifdef HAVE_LCD_CHARCELLS
318 status_set_usb(false);
319 #endif /* HAVE_LCD_CHARCELLS */
320 FOR_NB_SCREENS(i)
322 screens[i].backlight_on();
323 viewportmanager_theme_undo(i, false);