Sort bulgarian.lang into english.lang order
[maemo-rb.git] / uisimulator / common / sim_tasks.c
blob46e893b437204bebcf7ca810a44ff1eb6cc2d8f2
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2009 by Jens Arnold
11 * Copyright (C) 2011 by Thomas Martitz
13 * Rockbox simulator specific tasks
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
25 #include "config.h"
26 #include "kernel.h"
27 #include "screendump.h"
28 #include "thread.h"
29 #include "debug.h"
30 #include "usb.h"
32 static void sim_thread(void);
33 static long sim_thread_stack[DEFAULT_STACK_SIZE/sizeof(long)];
34 /* stack isn't actually used in the sim */
35 static const char sim_thread_name[] = "sim";
36 static struct event_queue sim_queue;
38 /* possible events for the sim thread */
39 enum {
40 SIM_SCREENDUMP,
41 SIM_USB_INSERTED,
42 SIM_USB_EXTRACTED,
45 void sim_thread(void)
47 struct queue_event ev;
48 long last_broadcast_tick = current_tick;
49 int num_acks_to_expect = 0;
51 while (1)
53 queue_wait(&sim_queue, &ev);
54 switch(ev.id)
56 case SIM_SCREENDUMP:
57 screen_dump();
58 #ifdef HAVE_REMOTE_LCD
59 remote_screen_dump();
60 #endif
61 break;
62 case SIM_USB_INSERTED:
63 /* from firmware/usb.c: */
64 /* Tell all threads that they have to back off the storage.
65 We subtract one for our own thread. Expect an ACK for every
66 listener for each broadcast they received. If it has been too
67 long, the user might have entered a screen that didn't ACK
68 when inserting the cable, such as a debugging screen. In that
69 case, reset the count or else USB would be locked out until
70 rebooting because it most likely won't ever come. Simply
71 resetting to the most recent broadcast count is racy. */
72 if(TIME_AFTER(current_tick, last_broadcast_tick + HZ*5))
74 num_acks_to_expect = 0;
75 last_broadcast_tick = current_tick;
78 num_acks_to_expect += queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
79 DEBUGF("USB inserted. Waiting for %d acks...\n",
80 num_acks_to_expect);
81 break;
82 case SYS_USB_CONNECTED_ACK:
83 if(num_acks_to_expect > 0 && --num_acks_to_expect == 0)
85 DEBUGF("All threads have acknowledged the connect.\n");
87 else
89 DEBUGF("usb: got ack, %d to go...\n",
90 num_acks_to_expect);
92 break;
93 case SIM_USB_EXTRACTED:
94 /* in usb.c, this is only done for exclusive storage
95 * do it here anyway but don't depend on the acks */
96 queue_broadcast(SYS_USB_DISCONNECTED, 0);
97 break;
98 default:
99 DEBUGF("sim_tasks: unhandled event: %ld\n", ev.id);
100 break;
105 void sim_tasks_init(void)
107 queue_init(&sim_queue, false);
109 create_thread(sim_thread, sim_thread_stack, sizeof(sim_thread_stack), 0,
110 sim_thread_name IF_PRIO(,PRIORITY_BACKGROUND) IF_COP(,CPU));
113 void sim_trigger_screendump(void)
115 queue_post(&sim_queue, SIM_SCREENDUMP, 0);
118 static bool is_usb_inserted;
119 void sim_trigger_usb(bool inserted)
121 if (inserted)
122 queue_post(&sim_queue, SIM_USB_INSERTED, 0);
123 else
124 queue_post(&sim_queue, SIM_USB_EXTRACTED, 0);
125 is_usb_inserted = inserted;
128 int usb_detect(void)
130 return is_usb_inserted ? USB_INSERTED : USB_EXTRACTED;
133 void usb_init(void)
137 void usb_start_monitoring(void)
141 void usb_acknowledge(long id)
143 queue_post(&sim_queue, id, 0);
146 void usb_wait_for_disconnect(struct event_queue *q)
148 struct queue_event ev;
150 /* Don't return until we get SYS_USB_DISCONNECTED */
151 while(1)
153 queue_wait(q, &ev);
154 if(ev.id == SYS_USB_DISCONNECTED)
155 return;