summaryrefslogtreecommitdiff
path: root/navit/graphics/cocoa/graphics_cocoa.m
blob: 16c879bd7806a801078f94d686276b172376301e (plain)
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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
#include <glib.h>
#include "config.h"
#include "config_.h"
#include "debug.h"
#include "plugin.h"
#include "point.h"
#include "window.h"
#include "graphics.h"
#include "event.h"
#include "item.h"
#include "callback.h"
#include "color.h"
#include <iconv.h>

#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
#define USE_UIKIT 1
#else
#define USE_UIKIT 0
#endif

#if USE_UIKIT
#import <UIKit/UIKit.h>
#define NSRect CGRect
#define NSMakeRect CGRectMake
#define REVERSE_Y 0

CGContextRef
current_context(void)
{
	return UIGraphicsGetCurrentContext();
}

#else
#import <Cocoa/Cocoa.h>
#define UIView NSView
#define UIViewController NSViewController
#define UIApplicationDelegate NSApplicationDelegate
#define UIWindow NSWindow
#define UIApplication NSApplication
#define UIApplicationMain(a,b,c,d) NSApplicationMain(a,b)
#define UIScreen NSScreen
#define UIEvent NSEvent
#define applicationFrame frame
#define REVERSE_Y 1

CGContextRef
current_context(void)
{
	return [[NSGraphicsContext currentContext] graphicsPort];
}

#endif


@interface NavitView : UIView {
@public
	struct graphics_priv *graphics;
}

@end

static struct graphics_priv {
	struct window win;
	NavitView *view;
	CGLayerRef layer;
	CGContextRef layer_context;
	struct callback_list *cbl;
	struct point p, pclean;
	int w, h, wraparound, overlay_disabled, cleanup;
	struct graphics_priv *parent, *next, *overlays;
} *global_graphics_cocoa;

iconv_t utf8_macosroman;

struct graphics_gc_priv {
	CGFloat rgba[4];
	int w;
};

struct graphics_font_priv {
	int size;
	char *name;
};



@implementation NavitView

- (void)drawRect:(NSRect)rect
{
	struct graphics_priv *gr=NULL;
#if 0
	NSLog(@"NavitView:drawRect...");
#endif

	CGContextRef X = current_context();

	CGContextDrawLayerAtPoint(X, CGPointZero, graphics->layer);
	if (!graphics->overlay_disabled)
		gr=graphics->overlays;
	while (gr) {
		if (!gr->overlay_disabled) {
			struct CGPoint pc;
			pc.x=gr->p.x;
			pc.y=gr->p.y;
			if (gr->wraparound) {
				if (pc.x < 0)
					pc.x+=graphics->w;
				if (pc.y < 0)
					pc.y+=graphics->h;
			}
	#if REVERSE_Y
			pc.y=graphics->h-pc.y-gr->h;
	#endif
			dbg(1,"draw %dx%d at %f,%f\n",gr->w,gr->h,pc.x,pc.y);
			CGContextDrawLayerAtPoint(X, pc, gr->layer);
		}
		gr=gr->next;
	}
}

#if USE_UIKIT
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	NSArray *arr=touches.allObjects;
	UITouch *touch=[arr objectAtIndex: 0];
	struct CGPoint pc=[touch locationInView: self];
	struct point p;
	p.x=pc.x;
	p.y=pc.y;
	dbg(1,"Enter count=%d %d %d\n",touches.count,p.x,p.y);
        callback_list_call_attr_3(graphics->cbl, attr_button, GINT_TO_POINTER(1), GINT_TO_POINTER(1), (void *)&p);
}


- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
	NSArray *arr=touches.allObjects;
	UITouch *touch=[arr objectAtIndex: 0];
	struct CGPoint pc=[touch locationInView: self];
	struct point p;
	p.x=pc.x;
	p.y=pc.y;
	dbg(1,"Enter count=%d %d %d\n",touches.count,p.x,p.y);
        callback_list_call_attr_3(graphics->cbl, attr_button, GINT_TO_POINTER(0), GINT_TO_POINTER(1), (void *)&p);
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	NSArray *arr=touches.allObjects;
	UITouch *touch=[arr objectAtIndex: 0];
	struct CGPoint pc=[touch locationInView: self];
	struct point p;
	p.x=pc.x;
	p.y=pc.y;
	dbg(1,"Enter count=%d %d %d\n",touches.count,p.x,p.y);
        callback_list_call_attr_3(graphics->cbl, attr_button, GINT_TO_POINTER(0), GINT_TO_POINTER(1), (void *)&p);
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
	NSArray *arr=touches.allObjects;
	UITouch *touch=[arr objectAtIndex: 0];
	struct CGPoint pc=[touch locationInView: self];
	struct point p;
	p.x=pc.x;
	p.y=pc.y;
	dbg(1,"Enter count=%d %d %d\n",touches.count,p.x,p.y);
	callback_list_call_attr_1(graphics->cbl, attr_motion, (void *)&p);
}

#else
- (void)mouseDown:(UIEvent *)theEvent
{
	struct point p;
	p.x=theEvent.locationInWindow.x;
	p.y=graphics->h-theEvent.locationInWindow.y;
	
	dbg(1,"Enter %d %d\n",p.x,p.y);
        callback_list_call_attr_3(graphics->cbl, attr_button, GINT_TO_POINTER(1), GINT_TO_POINTER(1), (void *)&p);
}

- (void)mouseUp:(UIEvent *)theEvent
{
	struct point p;
	p.x=theEvent.locationInWindow.x;
	p.y=graphics->h-theEvent.locationInWindow.y;
	
	dbg(1,"Enter %d %d\n",p.x,p.y);
        callback_list_call_attr_3(graphics->cbl, attr_button, GINT_TO_POINTER(0), GINT_TO_POINTER(1), (void *)&p);
}

- (void)mouseDragged:(UIEvent *)theEvent
{
	struct point p;
	p.x=theEvent.locationInWindow.x;
	p.y=graphics->h-theEvent.locationInWindow.y;
	
	dbg(1,"Enter %d %d\n",p.x,p.y);
	callback_list_call_attr_1(graphics->cbl, attr_motion, (void *)&p);
}
#endif

- (void)dealloc {
	[super dealloc];
}


@end

@interface NavitViewController : UIViewController
{
	NSRect frame;
	CGLayerRef layer;
}

@property (nonatomic) NSRect frame;

- (id) init_withFrame : (NSRect) _frame;

@end



@implementation NavitViewController

@synthesize frame;

- (id) init_withFrame : (NSRect) _frame
{
	NSLog(@"init with frame\n");
	frame = _frame;
	return [self init];
}

static 
void free_graphics(struct graphics_priv *gr)
{
	if (gr->layer) {
		CGLayerRelease(gr->layer);
		gr->layer=NULL;
	}
}

static void
setup_graphics(struct graphics_priv *gr)
{
	CGRect lr=CGRectMake(0, 0, gr->w, gr->h);
	gr->layer=CGLayerCreateWithContext(current_context(), lr.size, NULL);
	gr->layer_context=CGLayerGetContext(gr->layer);
#if REVERSE_Y
	CGContextScaleCTM(gr->layer_context, 1, -1);
	CGContextTranslateCTM(gr->layer_context, 0, -gr->h);
#endif
	CGContextSetRGBFillColor(gr->layer_context, 0, 0, 0, 0);
  	CGContextSetRGBStrokeColor(gr->layer_context, 0, 0, 0, 0);
	CGContextClearRect(gr->layer_context, lr);
}



- (void)loadView
{
	NSLog(@"loadView");
	NavitView* myV = [NavitView alloc];

	if (global_graphics_cocoa) {
		global_graphics_cocoa->view=myV;
		myV->graphics=global_graphics_cocoa;
		global_graphics_cocoa->w=frame.size.width;
		global_graphics_cocoa->h=frame.size.height;
		setup_graphics(global_graphics_cocoa);
	}

	[myV initWithFrame: frame];

	[self setView: myV];

	[myV release];
}

- (void)didReceiveMemoryWarning {
	dbg(0,"enter\n");
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[super dealloc];
}

@end

@class NavitViewController;

@interface NavitAppDelegate : NSObject <UIApplicationDelegate> {
	UIWindow *window;
	NavitViewController *viewController;
}

@property (nonatomic, retain) /*IBOutlet*/ UIWindow *window;
@property (nonatomic, retain) /*IBOutlet*/ NavitViewController *viewController;

@end


@implementation NavitAppDelegate

@synthesize window;
@synthesize viewController;


#if USE_UIKIT
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
#else
- (void)
applicationDidFinishLaunching:(NSNotification *)aNotification
#endif
{
	NSLog(@"DidFinishLaunching\n");
#if USE_UIKIT
	[[UIApplication sharedApplication] setStatusBarHidden:NO];
#endif

	NSRect appFrame = [UIScreen mainScreen].applicationFrame;

	self.viewController = [[[NavitViewController alloc] init_withFrame : appFrame] autorelease];

	NSRect windowRect = NSMakeRect(0, 0, appFrame.size.width, appFrame.size.height);

#if USE_UIKIT
	self.window = [[[UIWindow alloc] initWithFrame:windowRect] autorelease];
#else
	self.window = [[[UIWindow alloc] initWithContentRect:windowRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO] autorelease];
#endif
	utf8_macosroman=iconv_open("MACROMAN","UTF-8");

#if USE_UIKIT
	[window addSubview:viewController.view];
	[window makeKeyAndVisible];
#else
	NSWindowController * controller = [[NSWindowController alloc] initWithWindow : window];
	NSLog(@"Setting view\n");
	[viewController loadView];
	[window setContentView : viewController.view];
	[controller setWindow : window];
	[controller showWindow : nil];

#endif

	if (global_graphics_cocoa) {
		callback_list_call_attr_2(global_graphics_cocoa->cbl, attr_resize, (int)appFrame.size.width, (int)appFrame.size.height);
		
	}

#if USE_UIKIT
	return YES;
#endif
}


- (void)dealloc {
	[viewController release];
	[window release];
	[super dealloc];
}


@end


static void
draw_mode(struct graphics_priv *gr, enum draw_mode_num mode)
{
	if (mode == draw_mode_end) {
		dbg(1,"end %p\n",gr);
		if (!gr->parent) {
#if USE_UIKIT
			[gr->view setNeedsDisplay];
#else
			[gr->view display];
#endif
		}
	}
}

static void
draw_lines(struct graphics_priv *gr, struct graphics_gc_priv *gc, struct point *p, int count)
{
	CGPoint points[count];
	int i;
	for (i = 0 ; i < count ; i++) {
		points[i].x=p[i].x;
		points[i].y=p[i].y;
	}
	CGContextSetStrokeColor(gr->layer_context, gc->rgba);
	CGContextSetLineWidth(gr->layer_context, gc->w);
	CGContextSetLineCap(gr->layer_context, kCGLineCapRound);
    	CGContextBeginPath(gr->layer_context);
	CGContextAddLines(gr->layer_context, points, count);
	CGContextStrokePath(gr->layer_context);
	
}

static void
draw_polygon(struct graphics_priv *gr, struct graphics_gc_priv *gc, struct point *p, int count)
{
	CGPoint points[count];
	int i;
	for (i = 0 ; i < count ; i++) {
		points[i].x=p[i].x;
		points[i].y=p[i].y;
	}
	CGContextSetFillColor(gr->layer_context, gc->rgba);
    	CGContextBeginPath(gr->layer_context);
	CGContextAddLines(gr->layer_context, points, count);
	CGContextFillPath(gr->layer_context);
}

static void
draw_rectangle(struct graphics_priv *gr, struct graphics_gc_priv *gc, struct point *p, int w, int h)
{
	CGRect lr=CGRectMake(p->x, p->y, w, h);
	if (p->x <= 0 && p->y <= 0 && p->x+w+1 >= gr->w && p->y+h+1 >= gr->h) {
		dbg(1,"clear %p %dx%d\n",gr,w,h);
		free_graphics(gr);
		setup_graphics(gr);
	}
	CGContextSetFillColor(gr->layer_context, gc->rgba);
	CGContextFillRect(gr->layer_context, lr);
}

static void
draw_text(struct graphics_priv *gr, struct graphics_gc_priv *fg, struct graphics_gc_priv *bg, struct graphics_font_priv *font, char *text, struct point *p, int dx, int dy)
{
	size_t len,inlen=strlen(text)+1,outlen=strlen(text)+1;
	char outb[outlen];
	char *inp=text,*outp=outb;

	len=iconv (utf8_macosroman, &inp, &inlen, &outp, &outlen);

	CGContextSetFillColor(gr->layer_context, fg->rgba);

	CGContextSelectFont(gr->layer_context, font->name, font->size/16.0, kCGEncodingMacRoman);
	CGContextSetTextDrawingMode(gr->layer_context, kCGTextFill);
	CGAffineTransform xform = CGAffineTransformMake(dx/65536.0, dy/65536.0, dy/65536.0, -dx/65536.0, 0.0f, 0.0f);
	CGContextSetTextMatrix(gr->layer_context, xform);
	CGContextShowTextAtPoint(gr->layer_context, p->x, p->y, outb, strlen(outb));
}

static void
draw_image(struct graphics_priv *gr, struct graphics_gc_priv *fg, struct point *p, struct graphics_image_priv *img)
{
	CGImageRef imgc=(CGImageRef) img;
	int w=CGImageGetWidth(imgc);
	int h=CGImageGetHeight(imgc);
	CGRect rect=CGRectMake(0, 0, w, h);
	CGContextSaveGState(gr->layer_context);
	CGContextTranslateCTM(gr->layer_context, p->x, p->y+h);
	CGContextScaleCTM(gr->layer_context, 1.0, -1.0);
	CGContextDrawImage(gr->layer_context, rect, imgc);
	CGContextRestoreGState(gr->layer_context);
}

static void font_destroy(struct graphics_font_priv *font)
{
	g_free(font);
}

static struct graphics_font_methods font_methods = {
	font_destroy
};

static void
draw_drag(struct graphics_priv *gr, struct point *p)
{
	if (!gr->cleanup) {
		gr->pclean=gr->p;
		gr->cleanup=1;
	}
	if (p)
		gr->p=*p;
	else {
		gr->p.x=0;
		gr->p.y=0;
	}
}

static struct graphics_font_priv *font_new(struct graphics_priv *gr, struct graphics_font_methods *meth, char *font,  int size, int flags)
{
	struct graphics_font_priv *ret=g_new0(struct graphics_font_priv, 1);
	*meth=font_methods;

	ret->size=size;
	ret->name="Helvetica";
	return ret;
}

static void
gc_destroy(struct graphics_gc_priv *gc)
{
	g_free(gc);
}

static void
gc_set_linewidth(struct graphics_gc_priv *gc, int w)
{
	gc->w=w;
}

static void
gc_set_dashes(struct graphics_gc_priv *gc, int w, int offset, unsigned char *dash_list, int n)
{
}

static void
gc_set_foreground(struct graphics_gc_priv *gc, struct color *c)
{
	gc->rgba[0]=c->r/65535.0;
	gc->rgba[1]=c->g/65535.0;
	gc->rgba[2]=c->b/65535.0;
	gc->rgba[3]=c->a/65535.0;
}

static void
gc_set_background(struct graphics_gc_priv *gc, struct color *c)
{
}

static struct graphics_gc_methods gc_methods = {
	gc_destroy, 
	gc_set_linewidth, 
	gc_set_dashes,
	gc_set_foreground,
	gc_set_background,
};

static struct graphics_gc_priv *gc_new(struct graphics_priv *gr, struct graphics_gc_methods *meth)
{
        struct graphics_gc_priv *gc=g_new(struct graphics_gc_priv, 1);
	gc->w=1;

        *meth=gc_methods;
        return gc;
}


static void
background_gc(struct graphics_priv *gr, struct graphics_gc_priv *gc)
{
}

static struct graphics_priv *
overlay_new(struct graphics_priv *gr, struct graphics_methods *meth, struct point *p, int w, int h, int wraparound);


static struct graphics_image_priv *
image_new(struct graphics_priv *gra, struct graphics_image_methods *meth, char *path, int *w, int *h, struct point *hot, int rotation)
{
	NSString *s=[[NSString alloc]initWithCString:path encoding:NSMacOSRomanStringEncoding];
	CGDataProviderRef imgDataProvider = CGDataProviderCreateWithCFData((CFDataRef)[NSData dataWithContentsOfFile:s]);
	[s release];

	if (!imgDataProvider)
		return NULL;

	CGImageRef image = CGImageCreateWithPNGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault);
	CGDataProviderRelease(imgDataProvider);
	dbg(1,"size %dx%d\n",CGImageGetWidth(image),CGImageGetHeight(image));
	if (w)
		*w=CGImageGetWidth(image);
	if (h)
		*h=CGImageGetHeight(image);
	if (hot) {
		hot->x=CGImageGetWidth(image)/2;
		hot->y=CGImageGetHeight(image)/2;
	}
	return (struct graphics_image_priv *)image;
}

static void *
get_data(struct graphics_priv *this, const char *type)
{
	dbg(0,"enter\n");
	if (strcmp(type,"window"))
		return NULL;
	return &this->win;
}

static void
image_free(struct graphics_priv *gr, struct graphics_image_priv *priv)
{
	CGImageRelease((CGImageRef)priv);
}

static void
get_text_bbox(struct graphics_priv *gr, struct graphics_font_priv *font, char *text, int dx, int dy, struct point *ret, int estimate)
{
	int len = g_utf8_strlen(text, -1);
	int xMin = 0;
	int yMin = 0;
	int yMax = 13*font->size/256;
	int xMax = 9*font->size*len/256;

	ret[0].x = xMin;
	ret[0].y = -yMin;
	ret[1].x = xMin;
	ret[1].y = -yMax;
	ret[2].x = xMax;
	ret[2].y = -yMax;
	ret[3].x = xMax;
	ret[3].y = -yMin;
}

static void
overlay_disable(struct graphics_priv *gr, int disabled)
{
	gr->overlay_disabled=disabled;
}

static struct graphics_methods graphics_methods = {
	NULL, /* graphics_destroy, */
	draw_mode,
	draw_lines,
	draw_polygon,
	draw_rectangle,
	NULL, /* draw_circle, */
	draw_text, 
	draw_image,
	NULL, /* draw_image_warp, */
	draw_drag,
	font_new,
	gc_new,
	background_gc,
	overlay_new, 
	image_new,
	get_data,
	image_free,
	get_text_bbox,
	overlay_disable,
	NULL, /* overlay_resize, */
	NULL, /* set_attr, */
};


static struct graphics_priv *
overlay_new(struct graphics_priv *gr, struct graphics_methods *meth, struct point *p, int w, int h, int wraparound)
{
	struct graphics_priv *ret=g_new0(struct graphics_priv, 1);
	*meth=graphics_methods;
	ret->p=*p;
	ret->w=w;
	ret->h=h;
	ret->parent=gr;
	ret->next=gr->overlays;
	ret->wraparound=wraparound;
	gr->overlays=ret;
	setup_graphics(gr);
	return ret;
}


struct graphics_priv *
graphics_cocoa_new(struct navit *nav, struct graphics_methods *meth, struct attr **attrs, struct callback_list *cbl)
{
	struct graphics_priv *ret;
	*meth=graphics_methods;
	dbg(0,"enter\n");
	if(!event_request_system("cocoa","graphics_cocoa"))
		return NULL;
	ret=g_new0(struct graphics_priv, 1);
	ret->cbl=cbl;
	global_graphics_cocoa=ret;
	return ret;
}

static void
event_cocoa_main_loop_run(void)
{

	dbg(0,"enter\n");
#if 0
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"console.log"];
	freopen("/tmp/log.txt","a+",stderr);
	NSLog(@"Test\n");
#endif
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
#if USE_UIKIT
	dbg(0,"calling main\n");
	int retval = UIApplicationMain(main_argc, main_argv, nil, @"NavitAppDelegate");
	dbg(0,"retval=%d\n",retval);
#else
	NavitAppDelegate * delegate = [[NavitAppDelegate alloc] init];
	NSApplication * application = [NSApplication sharedApplication];
	[application setDelegate:delegate];
	NSLog(@"Test\n");

	[NSApp run];

	[delegate release];
#endif
	[pool release];
}

@interface NavitTimer : NSObject{
@public
	struct callback *cb;
	NSTimer *timer;
}
- (void)onTimer:(NSTimer*)theTimer;

@end

@implementation NavitTimer

- (void)onTimer:(NSTimer*)theTimer
{
	callback_call_0(cb);
}


@end

struct event_idle {
	struct callback *cb;
	NSTimer *timer;
};

static struct event_timeout *
event_cocoa_add_timeout(int timeout, int multi, struct callback *cb)
{
	NavitTimer *ret=[[NavitTimer alloc]init];
	ret->cb=cb;
	ret->timer=[NSTimer scheduledTimerWithTimeInterval:(timeout/1000.0) target:ret selector:@selector(onTimer:) userInfo:nil repeats:multi?YES:NO];
	dbg(1,"timer=%p\n",ret->timer);
	return (struct event_timeout *)ret;
}


static void
event_cocoa_remove_timeout(struct event_timeout *ev)
{
	NavitTimer *t=(NavitTimer *)ev;
	
	[t->timer invalidate];
	[t release];
}


static struct event_idle *
event_cocoa_add_idle(int priority, struct callback *cb)
{
	NavitTimer *ret=[[NavitTimer alloc]init];
	ret->cb=cb;
	ret->timer=[NSTimer scheduledTimerWithTimeInterval:(0.0) target:ret selector:@selector(onTimer:) userInfo:nil repeats:YES];

	dbg(1,"timer=%p\n",ret->timer);
	return (struct event_idle *)ret;
}

static void
event_cocoa_remove_idle(struct event_idle *ev)
{
	NavitTimer *t=(NavitTimer *)ev;
	
	[t->timer invalidate];
	[t release];
}

static struct event_methods event_cocoa_methods = {
	event_cocoa_main_loop_run,
	NULL, /* event_cocoa_main_loop_quit, */
	NULL, /* event_cocoa_add_watch, */
	NULL, /* event_cocoa_remove_watch, */
	event_cocoa_add_timeout,
	event_cocoa_remove_timeout,
	event_cocoa_add_idle,
	event_cocoa_remove_idle, 
	NULL, /* event_cocoa_call_callback, */
};


static struct event_priv *
event_cocoa_new(struct event_methods *meth)
{
	dbg(0,"enter\n");
	*meth=event_cocoa_methods;
	return NULL;
}


void
plugin_init(void)
{
	dbg(0,"enter\n");
	plugin_register_category_graphics("cocoa", graphics_cocoa_new);
	plugin_register_category_event("cocoa", event_cocoa_new);
}