1. a1ex
  2. Magic Lantern

Source

Magic Lantern / menu.c

hud...@kremvax 3a9ca37 


hud...@kremvax 963f6af 



















hud...@kremvax 3a9ca37 


hud...@kremvax 1d34190 
hud...@kremvax efc4bbb 
hud...@kremvax 71a6cd6 
hud...@kremvax 67c9d64 
hud...@kremvax 4d27742 
hud...@kremvax 25409ac 
hud...@kremvax 9b13198 
a1ex c6a7800 






















hud...@kremvax 09167d3 

hud...@andromeda… 7cd771a 
hud...@kremvax 5d92319 
hud...@kremvax ea9cdb5 
hud...@kremvax 38fb71e 
hud...@kremvax 09167d3 
hud...@kremvax 164b600 
hud...@kremvax b184dcf 
hud...@kremvax 164b600 
hud...@kremvax 3a9ca37 


hud...@kremvax ffce76b 


hud...@kremvax 3a9ca37 






hud...@kremvax efc4bbb 
hud...@kremvax ffce76b 
hud...@kremvax efc4bbb 

hud...@kremvax ffce76b 

hud...@kremvax efc4bbb 


hud...@kremvax ffce76b 
hud...@kremvax efc4bbb 
hud...@kremvax ffce76b 
hud...@kremvax efc4bbb 
hud...@kremvax ffce76b 
hud...@kremvax 3a9ca37 


hud...@kremvax 38fb71e 
hud...@kremvax.i… de442ec 
hud...@kremvax 1d34190 

hud...@kremvax 98bcb6e 
hud...@kremvax fd10b5d 



hud...@kremvax 5d92319 

hud...@kremvax fd10b5d 



hud...@kremvax 98bcb6e 



hud...@kremvax.i… 8b6047f 
hud...@kremvax 98bcb6e 

hud...@kremvax.i… 8b6047f 



hud...@kremvax 98bcb6e 




hud...@kremvax.i… de442ec 




hud...@kremvax 09167d3 

hud...@kremvax.i… de442ec 




hud...@kremvax 09167d3 

hud...@kremvax.i… de442ec 
hud...@kremvax 09167d3 
hud...@kremvax.i… de442ec 








hud...@kremvax 09167d3 

hud...@kremvax.i… de442ec 
hud...@kremvax 09167d3 
hud...@kremvax.i… de442ec 

















hud...@kremvax 09167d3 
hud...@kremvax.i… de442ec 



hud...@kremvax 25409ac 

hud...@kremvax.i… de442ec 
hud...@kremvax 5528cc5 

hud...@kremvax 25409ac 
hud...@kremvax 1d34190 
hud...@kremvax 25409ac 
hud...@kremvax.i… de442ec 




hud...@kremvax 09167d3 

hud...@kremvax.i… de442ec 











hud...@kremvax 5528cc5 













hud...@kremvax 09167d3 
hud...@kremvax 25409ac 























hud...@kremvax 1d34190 

hud...@kremvax 430fb5e 
hud...@kremvax 1d34190 








hud...@kremvax 25409ac 
hud...@kremvax 1d34190 






hud...@kremvax 25409ac 


hud...@kremvax 1d34190 




hud...@kremvax.i… de442ec 



hud...@kremvax 1d34190 

hud...@kremvax.i… de442ec 

hud...@kremvax b7d1d96 

hud...@kremvax 25409ac 
hud...@kremvax 1d34190 
hud...@kremvax.i… de442ec 




hud...@kremvax 09167d3 

hud...@kremvax 1d34190 
hud...@kremvax.i… de442ec 







hud...@kremvax b7d1d96 

hud...@kremvax.i… de442ec 










hud...@kremvax b7d1d96 
hud...@kremvax.i… de442ec 






hud...@kremvax b7d1d96 
hud...@kremvax.i… de442ec 










hud...@kremvax 367e606 
hud...@kremvax.i… de442ec 

hud...@kremvax 5d92319 

hud...@kremvax.i… de442ec 


hud...@kremvax b7d1d96 



hud...@kremvax 367e606 


hud...@kremvax 687f197 
hud...@kremvax.i… de442ec 
hud...@kremvax 367e606 















hud...@kremvax.i… de442ec 
hud...@kremvax 367e606 

hud...@kremvax b7d1d96 
hud...@kremvax 1d34190 


hud...@kremvax.i… de442ec 
hud...@kremvax 1d34190 
hud...@kremvax.i… de442ec 

hud...@kremvax 367e606 
hud...@kremvax 1d34190 

hud...@kremvax.i… de442ec 

hud...@kremvax 1d34190 
hud...@kremvax b7d1d96 



hud...@kremvax.i… de442ec 


hud...@kremvax 1d34190 
hud...@kremvax.i… de442ec 


hud...@kremvax 1d34190 
hud...@kremvax.i… de442ec 

hud...@kremvax b7d1d96 

hud...@kremvax.i… de442ec 
hud...@kremvax b7d1d96 
hud...@kremvax.i… de442ec 
hud...@kremvax 367e606 



hud...@kremvax.i… de442ec 

hud...@kremvax 367e606 
















hud...@kremvax 1d34190 
hud...@kremvax 367e606 

hud...@kremvax b7d1d96 
hud...@kremvax 1d34190 







hud...@kremvax.w… 3baf6dc 

hud...@kremvax 1d34190 

hud...@kremvax 38fb71e 





hud...@kremvax.w… 3baf6dc 
hud...@kremvax b7d1d96 
hud...@kremvax.w… 3baf6dc 






hud...@kremvax 1d34190 
hud...@kremvax.w… 3baf6dc 

















hud...@kremvax.i… de442ec 
hud...@kremvax b7d1d96 
hud...@kremvax.i… de442ec 



hud...@kremvax 1d34190 



hud...@kremvax c510292 
hud...@kremvax.i… 73a6c51 
hud...@kremvax.i… 553f18b 


hud...@kremvax 38fb71e 
hud...@kremvax b7d1d96 
hud...@kremvax 1d34190 
hud...@kremvax 38fb71e 
























hud...@kremvax 1d34190 
hud...@kremvax 367e606 

hud...@kremvax 38fb71e 
hud...@kremvax.i… de442ec 

hud...@kremvax 367e606 


hud...@kremvax 38fb71e 
hud...@kremvax 367e606 



hud...@kremvax 687f197 
hud...@kremvax b7d1d96 
hud...@kremvax 367e606 
hud...@kremvax.i… de442ec 
hud...@kremvax 367e606 
hud...@kremvax 687f197 
hud...@kremvax b7d1d96 
hud...@kremvax 1d34190 

hud...@kremvax.i… de442ec 
hud...@kremvax 1d34190 

hud...@kremvax.w… d017afc 
hud...@kremvax 906cb88 
hud...@kremvax ea9cdb5 
hud...@kremvax 3d89ed1 
hud...@kremvax 906cb88 

hud...@kremvax.w… d017afc 
hud...@kremvax 38fb71e 
hud...@kremvax 3d89ed1 

hud...@kremvax ea9cdb5 
hud...@kremvax 0adcc89 

hud...@kremvax 38fb71e 
hud...@kremvax 0adcc89 

hud...@kremvax 38fb71e 

hud...@kremvax 0adcc89 
hud...@kremvax 906cb88 
hud...@kremvax.w… d017afc 
hud...@kremvax 906cb88 
hud...@kremvax 3d89ed1 



hud...@kremvax 408ac07 


hud...@kremvax 1d34190 
hud...@kremvax 408ac07 






hud...@kremvax 1801406 
hud...@kremvax 1d34190 
hud...@kremvax.i… 73a6c51 
hud...@kremvax 38fb71e 

hud...@kremvax b184dcf 
hud...@kremvax 38fb71e 
hud...@kremvax.w… c184fa4 

hud...@kremvax 38fb71e 





hud...@kremvax b7d1d96 
hud...@kremvax 1d34190 



hud...@kremvax 6efd822 

hud...@kremvax 71a6cd6 
hud...@kremvax 67c9d64 
hud...@kremvax 09167d3 
hud...@kremvax 67c9d64 
hud...@kremvax 09167d3 
hud...@kremvax 38fb71e 
hud...@kremvax 09167d3 
hud...@kremvax b7d1d96 
hud...@kremvax 67c9d64 
hud...@kremvax 09167d3 



hud...@kremvax 38fb71e 
hud...@kremvax 09167d3 
hud...@kremvax 67c9d64 
hud...@kremvax 09167d3 













hud...@kremvax 67c9d64 

hud...@kremvax 297aaeb 



hud...@kremvax 38fb71e 







hud...@kremvax d70d7a8 
hud...@kremvax 297aaeb 


hud...@kremvax ea9cdb5 






hud...@kremvax d70d7a8 
hud...@kremvax 38fb71e 






hud...@kremvax ea9cdb5 


hud...@kremvax 3a9ca37 
hud...@kremvax 906cb88 














hud...@kremvax 3a9ca37 

hud...@kremvax b7d1d96 
hud...@kremvax.w… d017afc 
hud...@kremvax 367e606 
hud...@kremvax 906cb88 


hud...@kremvax 3a9ca37 

hud...@kremvax 3d89ed1 
hud...@kremvax 38fb71e 
hud...@kremvax 3a9ca37 
hud...@kremvax 38fb71e 











hud...@kremvax 3d89ed1 



















hud...@kremvax 38fb71e 
hud...@kremvax 3a9ca37 


hud...@kremvax 38fb71e 
hud...@kremvax 1d34190 
hud...@kremvax 38fb71e 
hud...@kremvax ea9cdb5 
hud...@kremvax 1d34190 

hud...@kremvax 38fb71e 



hud...@kremvax 3a9ca37 


hud...@kremvax 38fb71e 
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
/** \file
 * Magic Lantern GUI
 */
/*
 * Copyright (C) 2009 Trammell Hudson <hudson+ml@osresearch.net>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include "dryos.h"
#include "version.h"
#include "bmp.h"
#include "gui.h"
#include "config.h"
#include "property.h"
#include "lens.h"
#include "font.h"
#include "menu.h"

/*
#define NEW_MENU(name, entries) \
static struct menu_entry name##_menu[] = { \
  entries \
} \
\
static void test##_menu_init( void )\
{\
	menu_add( #name, name##_menu, COUNT(name##_menu) );\
}\
\
INIT_FUNC( __FILE__, name##_menu_init );


#define MENU_ENTRY(a) a,


NEW_MENU(test, MENU_ENTRY({
		.priv		= "Run Stack focus",
		.display	= menu_print,
		.select		= focus_stack_unlock,
	}));
*/

static struct semaphore * menu_sem;
extern struct semaphore * gui_sem;
static int menu_damage;
static int menu_hidden;
static int menu_timeout;

CONFIG_INT( "debug.draw-event", draw_event, 0 );
CONFIG_INT( "debug.menu-timeout", menu_timeout_time, 15 );

static void
draw_version( void )
{
	bmp_printf(
		FONT( FONT_SMALL, COLOR_WHITE, COLOR_BLUE ),
		0, 32,
		"Magic Lantern Firmware version %s (%s)\nBuilt on%s by %s\n%s",
		build_version,
		build_id,
		build_date,
		build_user,
		"http://magiclantern.wikia.com/"
	);

/*
	int y = 200;
	struct config * config = global_config;
	bmp_printf( FONT_SMALL, 0, y, "Config: %x", (unsigned) global_config );
	y += font_small.height;

	while( config )
	{
		bmp_printf( FONT_SMALL, 0, y, "'%s' => '%s'", config->name, config->value );
		config = config->next;
		y += font_small.height;
	}
*/
}


struct gui_task * gui_menu_task;
static struct menu * menus;


void
menu_binary_toggle(
	void *			priv
)
{
	unsigned * val = priv;
	*val = !*val;
}


void
menu_print(
	void *			priv,
	int			x,
	int			y,
	int			selected
)
{
	bmp_printf(
		selected ? MENU_FONT_SEL : MENU_FONT,
		x, y,
		"%s",
		(const char*) priv
	);
}


static struct menu *
menu_find_by_name(
	const char *		name
)
{
	take_semaphore( menu_sem, 0 );

	struct menu *		menu = menus;

	for( ; menu ; menu = menu->next )
	{
		if( streq( menu->name, name ) )
		{
			give_semaphore( menu_sem );
			return menu;
		}

		// Stop just before we get to the end
		if( !menu->next )
			break;
	}

	// Not found; create it
	struct menu * new_menu = malloc( sizeof(*new_menu) );
	if( !new_menu )
	{
		give_semaphore( menu_sem );
		return NULL;
	}

	new_menu->name		= name;
	new_menu->prev		= menu;
	new_menu->next		= NULL; // Inserting at end
	new_menu->children	= NULL;

	// menu points to the last entry or NULL if there are none
	if( menu )
	{
		// We are adding to the end
		menu->next		= new_menu;
		new_menu->selected	= 0;
	} else {
		// This is the first one
		menus			= new_menu;
		new_menu->selected	= 1;
	}

	give_semaphore( menu_sem );
	return new_menu;
}


void
menu_add(
	const char *		name,
	struct menu_entry *	new_entry,
	int			count
)
{
#if 1
	// Walk the menu list to find a menu
	struct menu *		menu = menu_find_by_name( name );
	if( !menu )
		return;

	take_semaphore( menu_sem, 0 );

	struct menu_entry *	head = menu->children;
	if( !head )
	{
		// First one -- insert it as the selected item
		head = menu->children	= new_entry;
		new_entry->next		= NULL;
		new_entry->prev		= NULL;
		new_entry->selected	= 1;
		new_entry++;
		count--;
	}

	// Find the end of the entries on the menu already
	while( head->next )
		head = head->next;

	while( count-- )
	{
		new_entry->selected	= 0;
		new_entry->next		= head->next;
		new_entry->prev		= head;
		head->next		= new_entry;

		head			= new_entry;
		new_entry++;
	}
	give_semaphore( menu_sem );
#else
	// Maybe later...
	struct menu_entry * child = head->child;
	if( !child )
	{
		// No other child entries; add this one
		// and select it
		new_entry->highlighted	= 1;
		new_entry->prev		= NULL;
		new_entry->next		= NULL;
		head->child		= new_entry;
		return;
	}

	// Walk the child list to find the end
	while( child->next )
		child = child->next;

	// Push the new entry onto the end of the list
	new_entry->selected	= 0;
	new_entry->prev		= child;
	new_entry->next		= NULL;
	child->next		= new_entry;
#endif
}



void
menu_display(
	struct menu_entry *	menu,
	int			x,
	int			y,
	int			selected
)
{
	while( menu )
	{
		menu->display(
			menu->priv,
			x,
			y,
			menu->selected
		);

		y += font_large.height;
		menu = menu->next;
	}
}


void
menus_display(
	struct menu *		menu,
	int			orig_x,
	int			y
)
{
	int			x = orig_x;

	take_semaphore( menu_sem, 0 );

	for( ; menu ; menu = menu->next )
	{
		unsigned fontspec = FONT(
			FONT_MED,
			COLOR_YELLOW,
			menu->selected ? 0x7F : COLOR_BG
		);
		bmp_printf( fontspec, x, y, "%6s", menu->name );
		x += fontspec_font( fontspec )->width * 6;

		if( menu->selected )
			menu_display(
				menu->children,
				orig_x,
				y + fontspec_font( fontspec )->height + 4,
				1
			);
	}

	give_semaphore( menu_sem );
}


void
menu_entry_select(
	struct menu *	menu
)
{
	if( !menu )
		return;

	take_semaphore( menu_sem, 0 );
	struct menu_entry * entry = menu->children;

	for( ; entry ; entry = entry->next )
	{
		if( entry->selected )
			break;
	}
	give_semaphore( menu_sem );

	if( !entry || !entry->select )
		return;

	entry->select( entry->priv );
}

/** Scroll side to side in the list of menus */
void
menu_move(
	struct menu *		menu,
	int			direction
)
{
	menu_damage = 1;

	if( !menu )
		return;

	int rc = take_semaphore( menu_sem, 100 );
	if( rc != 0 )
		return;

	// Deselect the current one
	menu->selected		= 0;

	if( direction < 0 )
	{
		if( menu->prev )
			menu = menu->prev;
		else {
			// Go to the last one
			while( menu->next )
				menu = menu->next;
		}
	} else {
		if( menu->next )
			menu = menu->next;
		else {
			// Go to the first one
			while( menu->prev )
				menu = menu->prev;
		}
	}

	// Select the new one (which might be the same)
	menu->selected		= 1;
	give_semaphore( menu_sem );
}


/** Scroll up or down in the currently displayed menu */
void
menu_entry_move(
	struct menu *		menu,
	int			direction
)
{
	if( !menu )
		return;

	int rc = take_semaphore( menu_sem, 100 );
	if( rc != 0 )
		return;

	struct menu_entry *	entry = menu->children;

	for( ; entry ; entry = entry->next )
	{
		if( entry->selected )
			break;
	}

	// Nothing selected?
	if( !entry )
	{
		give_semaphore( menu_sem );
		return;
	}

	// Deslect the current one
	entry->selected = 0;

	if( direction < 0 )
	{
		// First and moving up?
		if( entry->prev )
			entry = entry->prev;
		else {
			// Go to the last one
			while( entry->next )
				entry = entry->next;
		}
	} else {
		// Last and moving down?
		if( entry->next )
			entry = entry->next;
		else {
			// Go to the first one
			while( entry->prev )
				entry = entry->prev;
		}
	}

	// Select the new one, which might be the same as the old one
	entry->selected = 1;
	give_semaphore( menu_sem );
}


static int
menu_handler(
	void *			priv,
	gui_event_t		event,
	int			arg2,
	int			arg3,
	unsigned		arg4
)
{
	// Ignore periodic events (pass them on)
	if( 0
	||  event == GUI_TIMER2
	||  event == GUI_TIMER3
	||  event == GUI_TIMER4
	)
		return 0;

	if( event == GUI_PROP_EVENT )
	{
		if(0) bmp_printf( FONT_SMALL, 400, 40,
			"prop %08x => %08x",
			arg4,
			*(unsigned*) arg4
		);

		// Mine!  No one else gets it
		return 0;
	}

	if( event != 1 )
	{
		if( draw_event )
			bmp_printf( FONT_SMALL, 400, 40,
				"event %08x args %08x %08x %08x",
				event,
				arg2,
				arg3,
				arg4
			);

		DebugMsg( DM_MAGIC, 3, "%s: event %x", __func__, event );
	}


	// Find the selected menu (should be cached?)
	struct menu * menu = menus;
	for( ; menu ; menu = menu->next )
		if( menu->selected )
			break;

	switch( event )
	{
	case INITIALIZE_CONTROLLER:
		DebugMsg( DM_MAGIC, 3, "Menu task INITIALIZE_CONTROLLER" );
		return 0;

	case GOT_TOP_OF_CONTROL:
		DebugMsg( DM_MAGIC, 3, "Menu task GOT_TOP_OF_CONTROL" );
		menu_damage = 1;
		break;

	case TERMINATE_WINSYS:
		// Must propagate to all gui elements
		DebugMsg( DM_MAGIC, 3, "%s: TERMINATE_WINSYS", __func__ );
		gui_stop_menu();
		return 1;

	case DELETE_DIALOG_REQUEST:
		// Must not propagate
		DebugMsg( DM_MAGIC, 3, "%s: DELETE_DIALOG", __func__ );
		gui_stop_menu();
		return 0;

	case PRESS_MENU_BUTTON:
	case EVENTID_METERING_START: // If they press the shutter halfway
		gui_stop_menu();
		return 1;

	case JOY_CENTER:
		// We don't process it, but dont' let anyone else, either
		return 0;

	case EVENTID_94:
		// Generated when buttons are pressed?  Forward it on
		return 1;

	case PRESS_JOY_UP:
	case ELECTRONIC_SUB_DIAL_LEFT:
		menu_entry_move( menu, -1 );
		menu_damage = 1;
		break;

	case PRESS_JOY_DOWN:
	case ELECTRONIC_SUB_DIAL_RIGHT:
		menu_entry_move( menu, 1 );
		menu_damage = 1;
		break;

	case PRESS_JOY_RIGHT:
	case DIAL_RIGHT:
		menu_move( menu, 1 );
		break;

	case PRESS_JOY_LEFT:
	case DIAL_LEFT:
		menu_move( menu, -1 );
		break;

	case PRESS_SET_BUTTON:
		menu_entry_select( menu );
		break;

#if 0
	case PRESS_ZOOM_IN_BUTTON:
		gui_hide_menu( 100 );
		lens_focus_start( 0 );
		break;


#if 0
	// This breaks playback if enabled; figure out why!
	case PRESS_ZOOM_OUT_BUTTON:
		gui_hide_menu( 100 );
		lens_focus_start( -1 );
		break;
#endif

	case UNPRESS_ZOOM_IN_BUTTON:
	//case UNPRESS_ZOOM_OUT_BUTTON:
		gui_hide_menu( 2 );
		lens_focus_stop();
		break;
#endif

	case 1:
		// Synthetic redraw event
		break;

	case 0x10000086:
		// Who knows?  Fall through

	default:
		DebugMsg( DM_MAGIC, 3, "%s: unknown event %08x? %08x %08x %x08",
			__func__,
			event,
			arg2,
			arg3,
			arg4
		);
		return 0;
	}

	// If we end up here, something has been changed.
	// Reset the timeout
	menu_timeout = menu_timeout_time;

	// If we are hidden or no longer exit, do not redraw
	if( menu_hidden || !gui_menu_task )
		return 0;

	if( menu_damage )
		bmp_fill( COLOR_BG, 90, 90, 720-160, 480-180 );
	menu_damage = 0;
	menus_display( menus, 100, 100 );

	return 0;
}





void
menu_init( void )
{
	menus = NULL;
	gui_menu_task = NULL;
	menu_sem = create_named_semaphore( "menus", 1 );
	gui_sem = create_named_semaphore( "gui", 0 );

	menu_find_by_name( "Audio" );
	menu_find_by_name( "Video" );
	menu_find_by_name( "Brack" );
	menu_find_by_name( "Focus" );
	//menu_find_by_name( "Games" );
	menu_find_by_name( "Debug" );

/*
	bmp_printf( FONT_LARGE, 0, 40, "Yes, use this battery" );
	gui_control( ELECTRONIC_SUB_DIAL_RIGHT, 1, 0 );
	msleep( 2000 );
	gui_control( PRESS_SET_BUTTON, 1, 0 );
	msleep( 2000 );

	// Try to defeat the battery message
	//GUI_SetErrBattery( 1 );
	//msleep( 100 );
	//StopErrBatteryApp();

	msleep( 1000 );
*/
}


void
gui_stop_menu( void )
{
	menu_hidden = 0;
	menu_damage = 0;

	if( !gui_menu_task )
		return;

	gui_task_destroy( gui_menu_task );
	gui_menu_task = NULL;
	bmp_fill( 0, 90, 90, 720-160, 480-180 );
}


void
gui_hide_menu(
	int			redisplay_time
)
{
	menu_hidden = redisplay_time;
	menu_damage = 1;
	bmp_fill( 0, 90, 90, 720-160, 480-180 );
}


int
gui_menu_shown( void )
{
	return (int) gui_menu_task;
}


static void
toggle_draw_event( void * priv )
{
	draw_event = !draw_event;
}

static struct menu_entry draw_prop_menus[] = {
	{
		.priv		= "Toggle draw-event",
		.display	= menu_print,
		.select		= toggle_draw_event,
	},
};


static void
menu_task( void )
{
	int x, y;
	DebugMsg( DM_MAGIC, 3, "%s: Starting up\n", __func__ );

	// Add the draw_prop menu
	menu_add( "Debug", draw_prop_menus, COUNT(draw_prop_menus) );

	while(1)
	{
		int rc = take_semaphore( gui_sem, 500 );
		if( rc != 0 )
		{
			// We woke up after 1 second
			if( !gui_menu_task )
				continue;

			// Count down the menu timeout
			if( --menu_timeout == 0 )
			{
				gui_stop_menu();
				continue;
			}

			// Count down the menu_hidden timer
			if( menu_hidden )
			{
				if( --menu_hidden != 0 )
					continue;
				// Force an update on timer expiration
				ctrlman_dispatch_event(
					gui_menu_task,
					GOT_TOP_OF_CONTROL,
					0,
					0
				);
			} else {
				// Inject a synthetic timing event
				ctrlman_dispatch_event(
					gui_menu_task,
					1,
					0,
					0
				);
			}

			continue;
		}

		if( gui_menu_task )
		{
			gui_stop_menu();
			continue;
		}

		DebugMsg( DM_MAGIC, 3, "Creating menu task" );
		menu_damage = 1;
		menu_hidden = 0;
		gui_menu_task = gui_task_create( menu_handler, 0 );
	}
}

TASK_CREATE( "menu_task", menu_task, 0, 0x1e, 0x1000 );