Initial checkin of a native Mac OS X port, sharing most of its code
[u/mdw/putty] / macosx / osxwin.m
1 /*
2 * osxwin.m: code to manage a session window in Mac OS X PuTTY.
3 */
4
5 #import <Cocoa/Cocoa.h>
6 #include "putty.h"
7 #include "terminal.h"
8 #include "osxclass.h"
9
10 /* Colours come in two flavours: configurable, and xterm-extended. */
11 #define NCFGCOLOURS (lenof(((Config *)0)->colours))
12 #define NEXTCOLOURS 240 /* 216 colour-cube plus 24 shades of grey */
13 #define NALLCOLOURS (NCFGCOLOURS + NEXTCOLOURS)
14
15 /*
16 * The key component of the per-session data is the SessionWindow
17 * class. A pointer to this is used as the frontend handle, to be
18 * passed to all the platform-independent subsystems that require
19 * one.
20 */
21
22 @interface TerminalView : NSImageView
23 {
24 NSFont *font;
25 NSImage *image;
26 Terminal *term;
27 Config cfg;
28 NSColor *colours[NALLCOLOURS];
29 float fw, fasc, fdesc, fh;
30 }
31 - (void)drawStartFinish:(BOOL)start;
32 - (void)setColour:(int)n r:(float)r g:(float)g b:(float)b;
33 - (void)doText:(wchar_t *)text len:(int)len x:(int)x y:(int)y
34 attr:(unsigned long)attr lattr:(int)lattr;
35 @end
36
37 @implementation TerminalView
38 - (BOOL)isFlipped
39 {
40 return YES;
41 }
42 - (id)initWithTerminal:(Terminal *)aTerm config:(Config)aCfg
43 {
44 float w, h;
45
46 self = [self initWithFrame:NSMakeRect(0,0,100,100)];
47
48 term = aTerm;
49 cfg = aCfg;
50
51 /*
52 * Initialise the fonts we're going to use.
53 *
54 * FIXME: for the moment I'm sticking with exactly one default font.
55 */
56 font = [NSFont userFixedPitchFontOfSize:0];
57
58 /*
59 * Now determine the size of the primary font.
60 *
61 * FIXME: If we have multiple fonts, we may need to set fasc
62 * and fdesc to the _maximum_ asc and desc out of all the
63 * fonts, _before_ adding them together to get fh.
64 */
65 fw = [font widthOfString:@"A"];
66 fasc = [font ascender];
67 fdesc = -[font descender];
68 fh = fasc + fdesc;
69 fh = (int)fh + (fh > (int)fh); /* round up, ickily */
70
71 /*
72 * Use this to figure out the size of the terminal view.
73 */
74 w = fw * term->cols;
75 h = fh * term->rows;
76
77 /*
78 * And set our size and subimage.
79 */
80 image = [[NSImage alloc] initWithSize:NSMakeSize(w,h)];
81 [image setFlipped:YES];
82 [self setImage:image];
83 [self setFrame:NSMakeRect(0,0,w,h)];
84
85 term_invalidate(term);
86
87 return self;
88 }
89 - (void)drawStartFinish:(BOOL)start
90 {
91 if (start)
92 [image lockFocus];
93 else
94 [image unlockFocus];
95 }
96 - (void)doText:(wchar_t *)text len:(int)len x:(int)x y:(int)y
97 attr:(unsigned long)attr lattr:(int)lattr
98 {
99 int nfg, nbg, rlen, widefactor;
100 float ox, oy, tw, th;
101 NSDictionary *attrdict;
102
103 /* FIXME: TATTR_COMBINING */
104
105 nfg = ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
106 nbg = ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
107 if (attr & ATTR_REVERSE) {
108 int t = nfg;
109 nfg = nbg;
110 nbg = t;
111 }
112 if (cfg.bold_colour && (attr & ATTR_BOLD)) {
113 if (nfg < 16) nfg |= 8;
114 else if (nfg >= 256) nfg |= 1;
115 }
116 if (cfg.bold_colour && (attr & ATTR_BLINK)) {
117 if (nbg < 16) nbg |= 8;
118 else if (nbg >= 256) nbg |= 1;
119 }
120 if (attr & TATTR_ACTCURS) {
121 nfg = 260;
122 nbg = 261;
123 }
124
125 if (attr & ATTR_WIDE) {
126 widefactor = 2;
127 /* FIXME: what do we actually have to do about wide characters? */
128 } else {
129 widefactor = 1;
130 }
131
132 /* FIXME: ATTR_BOLD without cfg.bold_colour */
133
134 if ((lattr & LATTR_MODE) != LATTR_NORM) {
135 x *= 2;
136 if (x >= term->cols)
137 return;
138 if (x + len*2*widefactor > term->cols)
139 len = (term->cols-x)/2/widefactor;/* trim to LH half */
140 rlen = len * 2;
141 } else
142 rlen = len;
143
144 /* FIXME: how do we actually implement double-{width,height} lattrs? */
145
146 ox = x * fw;
147 oy = y * fh;
148 tw = rlen * widefactor * fw;
149 th = fh;
150
151 /*
152 * Set the clipping rectangle.
153 */
154 [[NSGraphicsContext currentContext] saveGraphicsState];
155 [NSBezierPath clipRect:NSMakeRect(ox, oy, tw, th)];
156
157 attrdict = [NSDictionary dictionaryWithObjectsAndKeys:
158 colours[nfg], NSForegroundColorAttributeName,
159 colours[nbg], NSBackgroundColorAttributeName,
160 font, NSFontAttributeName, nil];
161
162 /*
163 * Create an NSString and draw it.
164 *
165 * Annoyingly, although our input is wchar_t which is four
166 * bytes wide on OS X and terminal.c supports 32-bit Unicode,
167 * we must convert into the two-byte type `unichar' to store in
168 * NSString, so we lose display capability for extra-BMP stuff
169 * at this point.
170 */
171 {
172 NSString *string;
173 unichar *utext;
174 int i;
175
176 utext = snewn(len, unichar);
177 for (i = 0; i < len; i++)
178 utext[i] = (text[i] >= 0x10000 ? 0xFFFD : text[i]);
179
180 string = [NSString stringWithCharacters:utext length:len];
181 [string drawAtPoint:NSMakePoint(ox, oy) withAttributes:attrdict];
182
183 sfree(utext);
184 }
185
186 /*
187 * Restore the graphics state from before the clipRect: call.
188 */
189 [[NSGraphicsContext currentContext] restoreGraphicsState];
190
191 /*
192 * And flag this area as needing display.
193 */
194 [self setNeedsDisplayInRect:NSMakeRect(ox, oy, tw, th)];
195 }
196
197 - (void)setColour:(int)n r:(float)r g:(float)g b:(float)b
198 {
199 assert(n >= 0 && n < lenof(colours));
200 colours[n] = [[NSColor colorWithDeviceRed:r green:g blue:b alpha:1.0]
201 retain];
202 }
203 @end
204
205 @implementation SessionWindow
206 - (id)initWithConfig:(Config)aCfg
207 {
208 NSRect rect = { {0,0}, {0,0} };
209
210 cfg = aCfg; /* structure copy */
211
212 init_ucs(&ucsdata, cfg.line_codepage, cfg.utf8_override,
213 CS_UTF8, cfg.vtmode);
214 term = term_init(&cfg, &ucsdata, self);
215 logctx = log_init(self, &cfg);
216 term_provide_logctx(term, logctx);
217 term_size(term, cfg.height, cfg.width, cfg.savelines);
218
219 termview = [[[TerminalView alloc] initWithTerminal:term config:cfg]
220 autorelease];
221
222 /*
223 * Now work out the size of the window.
224 */
225 rect = [termview frame];
226 rect.origin = NSMakePoint(0,0);
227 rect.size.width += 2 * cfg.window_border;
228 rect.size.height += 2 * cfg.window_border;
229
230 /*
231 * Set up a backend.
232 */
233 {
234 int i;
235 back = &pty_backend;
236 for (i = 0; backends[i].backend != NULL; i++)
237 if (backends[i].protocol == cfg.protocol) {
238 back = backends[i].backend;
239 break;
240 }
241 }
242
243 {
244 const char *error;
245 char *realhost = NULL;
246 error = back->init(self, &backhandle, &cfg, cfg.host, cfg.port,
247 &realhost, cfg.tcp_nodelay, cfg.tcp_keepalives);
248 if (error) {
249 fatalbox("%s\n", error); /* FIXME: connection_fatal at worst */
250 }
251
252 if (realhost)
253 sfree(realhost); /* FIXME: do something with this */
254 }
255
256 /*
257 * Create a line discipline. (This must be done after creating
258 * the terminal _and_ the backend, since it needs to be passed
259 * pointers to both.)
260 */
261 ldisc = ldisc_create(&cfg, term, back, backhandle, self);
262
263 /*
264 * FIXME: Set up a scrollbar.
265 */
266
267 self = [super initWithContentRect:rect
268 styleMask:(NSTitledWindowMask | NSMiniaturizableWindowMask |
269 NSClosableWindowMask)
270 backing:NSBackingStoreBuffered
271 defer:YES];
272 [self setTitle:@"PuTTY"];
273
274 [self setIgnoresMouseEvents:NO];
275
276 /*
277 * Put the terminal view in the window.
278 */
279 rect = [termview frame];
280 rect.origin = NSMakePoint(cfg.window_border, cfg.window_border);
281 [termview setFrame:rect];
282 [[self contentView] addSubview:termview];
283
284 /*
285 * Set up the colour palette.
286 */
287 palette_reset(self);
288
289 /*
290 * FIXME: Only the _first_ document window should be centred.
291 * The subsequent ones should appear down and to the right of
292 * it, probably using the cascade function provided by Cocoa.
293 * Also we're apparently required by the HIG to remember and
294 * reuse previous positions of windows, although I'm not sure
295 * how that works if the user opens more than one of the same
296 * session type.
297 */
298 [self center]; /* :-) */
299
300 return self;
301 }
302
303 - (void)dealloc
304 {
305 /*
306 * FIXME: Here we must deallocate all sorts of stuff: the
307 * terminal, the backend, the ldisc, the logctx, you name it.
308 * Do so.
309 */
310 [super dealloc];
311 }
312
313 - (void)drawStartFinish:(BOOL)start
314 {
315 [termview drawStartFinish:start];
316 }
317
318 - (void)setColour:(int)n r:(float)r g:(float)g b:(float)b
319 {
320 [termview setColour:n r:r g:g b:b];
321 }
322
323 - (void)doText:(wchar_t *)text len:(int)len x:(int)x y:(int)y
324 attr:(unsigned long)attr lattr:(int)lattr
325 {
326 /* Pass this straight on to the TerminalView. */
327 [termview doText:text len:len x:x y:y attr:attr lattr:lattr];
328 }
329
330 - (Config *)cfg
331 {
332 return &cfg;
333 }
334
335 - (void)keyDown:(NSEvent *)ev
336 {
337 NSString *s = [ev characters];
338 int i;
339 int n = [s length], c = [s characterAtIndex:0], m = [ev modifierFlags];
340 int cm = [[ev charactersIgnoringModifiers] characterAtIndex:0];
341 wchar_t output[32];
342 char coutput[32];
343 int use_coutput = FALSE, special = FALSE, start, end;
344
345 printf("n=%d c=U+%04x cm=U+%04x m=%08x\n", n, c, cm, m);
346
347 /*
348 * FIXME: Alt+numberpad codes.
349 */
350
351 /*
352 * Shift and Ctrl with PageUp/PageDown for scrollback.
353 */
354 if (n == 1 && c == NSPageUpFunctionKey && (m & NSShiftKeyMask)) {
355 term_scroll(term, 0, -term->rows/2);
356 return;
357 }
358 if (n == 1 && c == NSPageUpFunctionKey && (m & NSControlKeyMask)) {
359 term_scroll(term, 0, -1);
360 return;
361 }
362 if (n == 1 && c == NSPageDownFunctionKey && (m & NSShiftKeyMask)) {
363 term_scroll(term, 0, +term->rows/2);
364 return;
365 }
366 if (n == 1 && c == NSPageDownFunctionKey && (m & NSControlKeyMask)) {
367 term_scroll(term, 0, +1);
368 return;
369 }
370
371 /*
372 * FIXME: Shift-Ins for paste? Or is that not Maccy enough?
373 */
374
375 /*
376 * FIXME: Alt (Option? Command?) prefix in general.
377 *
378 * (Note that Alt-Shift-thing will work just by looking at
379 * charactersIgnoringModifiers; but Alt-Ctrl-thing will need
380 * processing properly, and Alt-as-in-Option won't happen at
381 * all. Hmmm.)
382 *
383 * (Note also that we need to be able to override menu key
384 * equivalents before this is particularly useful.)
385 */
386 start = 1;
387 end = start;
388
389 /*
390 * Ctrl-` is the same as Ctrl-\, unless we already have a
391 * better idea.
392 */
393 if ((m & NSControlKeyMask) && n == 1 && cm == '`' && c == '`') {
394 output[1] = '\x1c';
395 end = 2;
396 }
397
398 /* We handle Return ourselves, because it needs to be flagged as
399 * special to ldisc. */
400 if (n == 1 && c == '\015') {
401 coutput[1] = '\015';
402 use_coutput = TRUE;
403 end = 2;
404 special = TRUE;
405 }
406
407 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
408 if (n == 1 && (m & NSControlKeyMask) && (m & NSShiftKeyMask) &&
409 cm == ' ') {
410 output[1] = '\240';
411 end = 2;
412 }
413
414 /* Control-2, Control-Space and Control-@ are all NUL. */
415 if ((m & NSControlKeyMask) && n == 1 &&
416 (cm == '2' || cm == '@' || cm == ' ') && c == cm) {
417 output[1] = '\0';
418 end = 2;
419 }
420
421 /* We don't let MacOS tell us what Backspace is! We know better. */
422 if (cm == 0x7F && !(m & NSShiftKeyMask)) {
423 coutput[1] = cfg.bksp_is_delete ? '\x7F' : '\x08';
424 end = 2;
425 use_coutput = special = TRUE;
426 }
427 /* For Shift Backspace, do opposite of what is configured. */
428 if (cm == 0x7F && (m & NSShiftKeyMask)) {
429 coutput[1] = cfg.bksp_is_delete ? '\x08' : '\x7F';
430 end = 2;
431 use_coutput = special = TRUE;
432 }
433
434 /* Shift-Tab is ESC [ Z. Oddly, this combination generates ^Y by
435 * default on MacOS! */
436 if (cm == 0x19 && (m & NSShiftKeyMask) && !(m & NSControlKeyMask)) {
437 end = 1;
438 output[end++] = '\033';
439 output[end++] = '[';
440 output[end++] = 'Z';
441 }
442
443 /*
444 * NetHack keypad mode.
445 */
446 if (cfg.nethack_keypad && (m & NSNumericPadKeyMask)) {
447 wchar_t *keys = NULL;
448 switch (cm) {
449 case '1': keys = L"bB"; break;
450 case '2': keys = L"jJ"; break;
451 case '3': keys = L"nN"; break;
452 case '4': keys = L"hH"; break;
453 case '5': keys = L".."; break;
454 case '6': keys = L"lL"; break;
455 case '7': keys = L"yY"; break;
456 case '8': keys = L"kK"; break;
457 case '9': keys = L"uU"; break;
458 }
459 if (keys) {
460 end = 2;
461 if (m & NSShiftKeyMask)
462 output[1] = keys[1];
463 else
464 output[1] = keys[0];
465 goto done;
466 }
467 }
468
469 /*
470 * Application keypad mode.
471 */
472 if (term->app_keypad_keys && !cfg.no_applic_k &&
473 (m & NSNumericPadKeyMask)) {
474 int xkey = 0;
475 switch (cm) {
476 case NSClearLineFunctionKey: xkey = 'P'; break;
477 case '=': xkey = 'Q'; break;
478 case '/': xkey = 'R'; break;
479 case '*': xkey = 'S'; break;
480 /*
481 * FIXME: keypad - and + need to be mapped to ESC O l
482 * and ESC O k, or ESC O l and ESC O m, depending on
483 * xterm function key mode, and I can't remember which
484 * goes where.
485 */
486 case '\003': xkey = 'M'; break;
487 case '0': xkey = 'p'; break;
488 case '1': xkey = 'q'; break;
489 case '2': xkey = 'r'; break;
490 case '3': xkey = 's'; break;
491 case '4': xkey = 't'; break;
492 case '5': xkey = 'u'; break;
493 case '6': xkey = 'v'; break;
494 case '7': xkey = 'w'; break;
495 case '8': xkey = 'x'; break;
496 case '9': xkey = 'y'; break;
497 case '.': xkey = 'n'; break;
498 }
499 if (xkey) {
500 if (term->vt52_mode) {
501 if (xkey >= 'P' && xkey <= 'S') {
502 output[end++] = '\033';
503 output[end++] = xkey;
504 } else {
505 output[end++] = '\033';
506 output[end++] = '?';
507 output[end++] = xkey;
508 }
509 } else {
510 output[end++] = '\033';
511 output[end++] = 'O';
512 output[end++] = xkey;
513 }
514 goto done;
515 }
516 }
517
518 /*
519 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
520 * for integer decimal nn.)
521 *
522 * We also deal with the weird ones here. Linux VCs replace F1
523 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
524 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
525 * respectively.
526 */
527 {
528 int code = 0;
529 switch (cm) {
530 case NSF1FunctionKey:
531 code = (m & NSShiftKeyMask ? 23 : 11);
532 break;
533 case NSF2FunctionKey:
534 code = (m & NSShiftKeyMask ? 24 : 12);
535 break;
536 case NSF3FunctionKey:
537 code = (m & NSShiftKeyMask ? 25 : 13);
538 break;
539 case NSF4FunctionKey:
540 code = (m & NSShiftKeyMask ? 26 : 14);
541 break;
542 case NSF5FunctionKey:
543 code = (m & NSShiftKeyMask ? 28 : 15);
544 break;
545 case NSF6FunctionKey:
546 code = (m & NSShiftKeyMask ? 29 : 17);
547 break;
548 case NSF7FunctionKey:
549 code = (m & NSShiftKeyMask ? 31 : 18);
550 break;
551 case NSF8FunctionKey:
552 code = (m & NSShiftKeyMask ? 32 : 19);
553 break;
554 case NSF9FunctionKey:
555 code = (m & NSShiftKeyMask ? 33 : 20);
556 break;
557 case NSF10FunctionKey:
558 code = (m & NSShiftKeyMask ? 34 : 21);
559 break;
560 case NSF11FunctionKey:
561 code = 23;
562 break;
563 case NSF12FunctionKey:
564 code = 24;
565 break;
566 case NSF13FunctionKey:
567 code = 25;
568 break;
569 case NSF14FunctionKey:
570 code = 26;
571 break;
572 case NSF15FunctionKey:
573 code = 28;
574 break;
575 case NSF16FunctionKey:
576 code = 29;
577 break;
578 case NSF17FunctionKey:
579 code = 31;
580 break;
581 case NSF18FunctionKey:
582 code = 32;
583 break;
584 case NSF19FunctionKey:
585 code = 33;
586 break;
587 case NSF20FunctionKey:
588 code = 34;
589 break;
590 }
591 if (!(m & NSControlKeyMask)) switch (cm) {
592 case NSHomeFunctionKey:
593 code = 1;
594 break;
595 #ifdef FIXME
596 case GDK_Insert: case GDK_KP_Insert:
597 code = 2;
598 break;
599 #endif
600 case NSDeleteFunctionKey:
601 code = 3;
602 break;
603 case NSEndFunctionKey:
604 code = 4;
605 break;
606 case NSPageUpFunctionKey:
607 code = 5;
608 break;
609 case NSPageDownFunctionKey:
610 code = 6;
611 break;
612 }
613 /* Reorder edit keys to physical order */
614 if (cfg.funky_type == FUNKY_VT400 && code <= 6)
615 code = "\0\2\1\4\5\3\6"[code];
616
617 if (term->vt52_mode && code > 0 && code <= 6) {
618 output[end++] = '\033';
619 output[end++] = " HLMEIG"[code];
620 goto done;
621 }
622
623 if (cfg.funky_type == FUNKY_SCO && /* SCO function keys */
624 code >= 11 && code <= 34) {
625 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
626 int index = 0;
627 switch (cm) {
628 case NSF1FunctionKey: index = 0; break;
629 case NSF2FunctionKey: index = 1; break;
630 case NSF3FunctionKey: index = 2; break;
631 case NSF4FunctionKey: index = 3; break;
632 case NSF5FunctionKey: index = 4; break;
633 case NSF6FunctionKey: index = 5; break;
634 case NSF7FunctionKey: index = 6; break;
635 case NSF8FunctionKey: index = 7; break;
636 case NSF9FunctionKey: index = 8; break;
637 case NSF10FunctionKey: index = 9; break;
638 case NSF11FunctionKey: index = 10; break;
639 case NSF12FunctionKey: index = 11; break;
640 }
641 if (m & NSShiftKeyMask) index += 12;
642 if (m & NSControlKeyMask) index += 24;
643 output[end++] = '\033';
644 output[end++] = '[';
645 output[end++] = codes[index];
646 goto done;
647 }
648 if (cfg.funky_type == FUNKY_SCO && /* SCO small keypad */
649 code >= 1 && code <= 6) {
650 char codes[] = "HL.FIG";
651 if (code == 3) {
652 output[1] = '\x7F';
653 end = 2;
654 } else {
655 output[end++] = '\033';
656 output[end++] = '[';
657 output[end++] = codes[code-1];
658 }
659 goto done;
660 }
661 if ((term->vt52_mode || cfg.funky_type == FUNKY_VT100P) &&
662 code >= 11 && code <= 24) {
663 int offt = 0;
664 if (code > 15)
665 offt++;
666 if (code > 21)
667 offt++;
668 if (term->vt52_mode) {
669 output[end++] = '\033';
670 output[end++] = code + 'P' - 11 - offt;
671 } else {
672 output[end++] = '\033';
673 output[end++] = 'O';
674 output[end++] = code + 'P' - 11 - offt;
675 }
676 goto done;
677 }
678 if (cfg.funky_type == FUNKY_LINUX && code >= 11 && code <= 15) {
679 output[end++] = '\033';
680 output[end++] = '[';
681 output[end++] = '[';
682 output[end++] = code + 'A' - 11;
683 goto done;
684 }
685 if (cfg.funky_type == FUNKY_XTERM && code >= 11 && code <= 14) {
686 if (term->vt52_mode) {
687 output[end++] = '\033';
688 output[end++] = code + 'P' - 11;
689 } else {
690 output[end++] = '\033';
691 output[end++] = 'O';
692 output[end++] = code + 'P' - 11;
693 }
694 goto done;
695 }
696 if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
697 if (code == 1) {
698 output[end++] = '\033';
699 output[end++] = '[';
700 output[end++] = 'H';
701 } else {
702 output[end++] = '\033';
703 output[end++] = 'O';
704 output[end++] = 'w';
705 }
706 goto done;
707 }
708 if (code) {
709 char buf[20];
710 sprintf(buf, "\x1B[%d~", code);
711 for (i = 0; buf[i]; i++)
712 output[end++] = buf[i];
713 goto done;
714 }
715 }
716
717 /*
718 * Cursor keys. (This includes the numberpad cursor keys,
719 * if we haven't already done them due to app keypad mode.)
720 */
721 {
722 int xkey = 0;
723 switch (cm) {
724 case NSUpArrowFunctionKey: xkey = 'A'; break;
725 case NSDownArrowFunctionKey: xkey = 'B'; break;
726 case NSRightArrowFunctionKey: xkey = 'C'; break;
727 case NSLeftArrowFunctionKey: xkey = 'D'; break;
728 }
729 if (xkey) {
730 /*
731 * The arrow keys normally do ESC [ A and so on. In
732 * app cursor keys mode they do ESC O A instead.
733 * Ctrl toggles the two modes.
734 */
735 if (term->vt52_mode) {
736 output[end++] = '\033';
737 output[end++] = xkey;
738 } else if (!term->app_cursor_keys ^ !(m & NSControlKeyMask)) {
739 output[end++] = '\033';
740 output[end++] = 'O';
741 output[end++] = xkey;
742 } else {
743 output[end++] = '\033';
744 output[end++] = '[';
745 output[end++] = xkey;
746 }
747 goto done;
748 }
749 }
750
751 done:
752
753 /*
754 * Failing everything else, send the exact Unicode we got from
755 * OS X.
756 */
757 if (end == start) {
758 if (n > lenof(output)-start)
759 n = lenof(output)-start; /* _shouldn't_ happen! */
760 for (i = 0; i < n; i++) {
761 output[i+start] = [s characterAtIndex:i];
762 }
763 end = n+start;
764 }
765
766 if (use_coutput) {
767 assert(special);
768 assert(end < lenof(coutput));
769 coutput[end] = '\0';
770 ldisc_send(ldisc, coutput+start, -2, TRUE);
771 } else {
772 luni_send(ldisc, output+start, end-start, TRUE);
773 }
774 }
775
776 - (int)fromBackend:(const char *)data len:(int)len isStderr:(int)is_stderr
777 {
778 return term_data(term, is_stderr, data, len);
779 }
780
781 @end
782
783 int from_backend(void *frontend, int is_stderr, const char *data, int len)
784 {
785 SessionWindow *win = (SessionWindow *)frontend;
786 return [win fromBackend:data len:len isStderr:is_stderr];
787 }
788
789 void frontend_keypress(void *handle)
790 {
791 /* FIXME */
792 }
793
794 void connection_fatal(void *frontend, char *p, ...)
795 {
796 //SessionWindow *win = (SessionWindow *)frontend;
797 /* FIXME: proper OS X GUI stuff */
798 va_list ap;
799 fprintf(stderr, "FATAL ERROR: ");
800 va_start(ap, p);
801 vfprintf(stderr, p, ap);
802 va_end(ap);
803 fputc('\n', stderr);
804 exit(1);
805 }
806
807 void notify_remote_exit(void *frontend)
808 {
809 //SessionWindow *win = (SessionWindow *)frontend;
810 /* FIXME */
811 }
812
813 void ldisc_update(void *frontend, int echo, int edit)
814 {
815 //SessionWindow *win = (SessionWindow *)frontend;
816 /*
817 * In a GUI front end, this need do nothing.
818 */
819 }
820
821 void update_specials_menu(void *frontend)
822 {
823 //SessionWindow *win = (SessionWindow *)frontend;
824 /* FIXME */
825 }
826
827 /*
828 * This is still called when mode==BELL_VISUAL, even though the
829 * visual bell is handled entirely within terminal.c, because we
830 * may want to perform additional actions on any kind of bell (for
831 * example, taskbar flashing in Windows).
832 */
833 void beep(void *frontend, int mode)
834 {
835 //SessionWindow *win = (SessionWindow *)frontend;
836 if (mode != BELL_VISUAL)
837 NSBeep();
838 }
839
840 int char_width(Context ctx, int uc)
841 {
842 /*
843 * Under X, any fixed-width font really _is_ fixed-width.
844 * Double-width characters will be dealt with using a separate
845 * font. For the moment we can simply return 1.
846 */
847 return 1;
848 }
849
850 void palette_set(void *frontend, int n, int r, int g, int b)
851 {
852 SessionWindow *win = (SessionWindow *)frontend;
853
854 if (n >= 16)
855 n += 256 - 16;
856 if (n > NALLCOLOURS)
857 return;
858 [win setColour:n r:r/255.0 g:g/255.0 b:b/255.0];
859
860 /*
861 * FIXME: do we need an OS X equivalent of set_window_background?
862 */
863 }
864
865 void palette_reset(void *frontend)
866 {
867 SessionWindow *win = (SessionWindow *)frontend;
868 Config *cfg = [win cfg];
869
870 /* This maps colour indices in cfg to those used in colours[]. */
871 static const int ww[] = {
872 256, 257, 258, 259, 260, 261,
873 0, 8, 1, 9, 2, 10, 3, 11,
874 4, 12, 5, 13, 6, 14, 7, 15
875 };
876
877 int i;
878
879 for (i = 0; i < NCFGCOLOURS; i++) {
880 [win setColour:ww[i] r:cfg->colours[i][0]/255.0
881 g:cfg->colours[i][1]/255.0 b:cfg->colours[i][2]/255.0];
882 }
883
884 for (i = 0; i < NEXTCOLOURS; i++) {
885 if (i < 216) {
886 int r = i / 36, g = (i / 6) % 6, b = i % 6;
887 [win setColour:i+16 r:r/5.0 g:g/5.0 b:b/5.0];
888 } else {
889 int shade = i - 216;
890 float fshade = (shade + 1) / (float)(NEXTCOLOURS - 216 + 1);
891 [win setColour:i+16 r:fshade g:fshade b:fshade];
892 }
893 }
894
895 /*
896 * FIXME: do we need an OS X equivalent of set_window_background?
897 */
898 }
899
900 Context get_ctx(void *frontend)
901 {
902 SessionWindow *win = (SessionWindow *)frontend;
903
904 /*
905 * Lock the drawing focus on the image inside the TerminalView.
906 */
907 [win drawStartFinish:YES];
908
909 [[NSGraphicsContext currentContext] setShouldAntialias:YES];
910
911 /*
912 * Cocoa drawing functions don't take a graphics context: that
913 * parameter is implicit. Therefore, we'll use the frontend
914 * handle itself as the context, on the grounds that it's as
915 * good a thing to use as any.
916 */
917 return frontend;
918 }
919
920 void free_ctx(Context ctx)
921 {
922 SessionWindow *win = (SessionWindow *)ctx;
923
924 [win drawStartFinish:NO];
925 }
926
927 void do_text(Context ctx, int x, int y, wchar_t *text, int len,
928 unsigned long attr, int lattr)
929 {
930 SessionWindow *win = (SessionWindow *)ctx;
931
932 [win doText:text len:len x:x y:y attr:attr lattr:lattr];
933 }
934
935 void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
936 unsigned long attr, int lattr)
937 {
938 SessionWindow *win = (SessionWindow *)ctx;
939 Config *cfg = [win cfg];
940 int active, passive;
941
942 if (attr & TATTR_PASCURS) {
943 attr &= ~TATTR_PASCURS;
944 passive = 1;
945 } else
946 passive = 0;
947 if ((attr & TATTR_ACTCURS) && cfg->cursor_type != 0) {
948 attr &= ~TATTR_ACTCURS;
949 active = 1;
950 } else
951 active = 0;
952
953 [win doText:text len:len x:x y:y attr:attr lattr:lattr];
954
955 /*
956 * FIXME: now draw the various cursor types (both passive and
957 * active underlines and vertical lines, plus passive blocks).
958 */
959 }
960
961 /*
962 * Minimise or restore the window in response to a server-side
963 * request.
964 */
965 void set_iconic(void *frontend, int iconic)
966 {
967 //SessionWindow *win = (SessionWindow *)frontend;
968 /* FIXME */
969 }
970
971 /*
972 * Move the window in response to a server-side request.
973 */
974 void move_window(void *frontend, int x, int y)
975 {
976 //SessionWindow *win = (SessionWindow *)frontend;
977 /* FIXME */
978 }
979
980 /*
981 * Move the window to the top or bottom of the z-order in response
982 * to a server-side request.
983 */
984 void set_zorder(void *frontend, int top)
985 {
986 //SessionWindow *win = (SessionWindow *)frontend;
987 /* FIXME */
988 }
989
990 /*
991 * Refresh the window in response to a server-side request.
992 */
993 void refresh_window(void *frontend)
994 {
995 //SessionWindow *win = (SessionWindow *)frontend;
996 /* FIXME */
997 }
998
999 /*
1000 * Maximise or restore the window in response to a server-side
1001 * request.
1002 */
1003 void set_zoomed(void *frontend, int zoomed)
1004 {
1005 //SessionWindow *win = (SessionWindow *)frontend;
1006 /* FIXME */
1007 }
1008
1009 /*
1010 * Report whether the window is iconic, for terminal reports.
1011 */
1012 int is_iconic(void *frontend)
1013 {
1014 //SessionWindow *win = (SessionWindow *)frontend;
1015 return NO; /* FIXME */
1016 }
1017
1018 /*
1019 * Report the window's position, for terminal reports.
1020 */
1021 void get_window_pos(void *frontend, int *x, int *y)
1022 {
1023 //SessionWindow *win = (SessionWindow *)frontend;
1024 /* FIXME */
1025 }
1026
1027 /*
1028 * Report the window's pixel size, for terminal reports.
1029 */
1030 void get_window_pixels(void *frontend, int *x, int *y)
1031 {
1032 //SessionWindow *win = (SessionWindow *)frontend;
1033 /* FIXME */
1034 }
1035
1036 /*
1037 * Return the window or icon title.
1038 */
1039 char *get_window_title(void *frontend, int icon)
1040 {
1041 //SessionWindow *win = (SessionWindow *)frontend;
1042 return NULL; /* FIXME */
1043 }
1044
1045 void set_title(void *frontend, char *title)
1046 {
1047 //SessionWindow *win = (SessionWindow *)frontend;
1048 /* FIXME */
1049 }
1050
1051 void set_icon(void *frontend, char *title)
1052 {
1053 //SessionWindow *win = (SessionWindow *)frontend;
1054 /* FIXME */
1055 }
1056
1057 void set_sbar(void *frontend, int total, int start, int page)
1058 {
1059 //SessionWindow *win = (SessionWindow *)frontend;
1060 /* FIXME */
1061 }
1062
1063 void get_clip(void *frontend, wchar_t ** p, int *len)
1064 {
1065 //SessionWindow *win = (SessionWindow *)frontend;
1066 /* FIXME */
1067 }
1068
1069 void write_clip(void *frontend, wchar_t * data, int len, int must_deselect)
1070 {
1071 //SessionWindow *win = (SessionWindow *)frontend;
1072 /* FIXME */
1073 }
1074
1075 void request_paste(void *frontend)
1076 {
1077 //SessionWindow *win = (SessionWindow *)frontend;
1078 /* FIXME */
1079 }
1080
1081 void set_raw_mouse_mode(void *frontend, int activate)
1082 {
1083 //SessionWindow *win = (SessionWindow *)frontend;
1084 /* FIXME */
1085 }
1086
1087 void request_resize(void *frontend, int w, int h)
1088 {
1089 //SessionWindow *win = (SessionWindow *)frontend;
1090 /* FIXME */
1091 }
1092
1093 void sys_cursor(void *frontend, int x, int y)
1094 {
1095 //SessionWindow *win = (SessionWindow *)frontend;
1096 /*
1097 * This is probably meaningless under OS X. FIXME: find out for
1098 * sure.
1099 */
1100 }
1101
1102 void logevent(void *frontend, const char *string)
1103 {
1104 //SessionWindow *win = (SessionWindow *)frontend;
1105 /* FIXME */
1106 printf("logevent: %s\n", string);
1107 }
1108
1109 int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
1110 {
1111 //SessionWindow *win = (SessionWindow *)frontend;
1112 return 1; /* FIXME */
1113 }
1114
1115 void set_busy_status(void *frontend, int status)
1116 {
1117 /*
1118 * We need do nothing here: the OS X `application is busy'
1119 * beachball pointer appears _automatically_ when the
1120 * application isn't responding to GUI messages.
1121 */
1122 }