Remove debugging printfs from mac_workoutfontscale().
[sgt/putty] / mac / macterm.c
1 /* $Id: macterm.c,v 1.48 2003/01/14 18:44:34 ben Exp $ */
2 /*
3 * Copyright (c) 1999 Simon Tatham
4 * Copyright (c) 1999, 2002 Ben Harris
5 * All rights reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person
8 * obtaining a copy of this software and associated documentation
9 * files (the "Software"), to deal in the Software without
10 * restriction, including without limitation the rights to use,
11 * copy, modify, merge, publish, distribute, sublicense, and/or
12 * sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following
14 * conditions:
15 *
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
24 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 /*
30 * macterm.c -- Macintosh terminal front-end
31 */
32
33 #include <MacTypes.h>
34 #include <Controls.h>
35 #include <ControlDefinitions.h>
36 #include <Fonts.h>
37 #include <Gestalt.h>
38 #include <LowMem.h>
39 #include <MacMemory.h>
40 #include <MacWindows.h>
41 #include <MixedMode.h>
42 #include <Palettes.h>
43 #include <Quickdraw.h>
44 #include <QuickdrawText.h>
45 #include <Resources.h>
46 #include <Scrap.h>
47 #include <Script.h>
48 #include <Sound.h>
49 #include <StandardFile.h>
50 #include <TextCommon.h>
51 #include <Threads.h>
52 #include <ToolUtils.h>
53 #include <UnicodeConverter.h>
54
55 #include <assert.h>
56 #include <limits.h>
57 #include <stdlib.h>
58 #include <stdio.h>
59 #include <string.h>
60
61 #include "macresid.h"
62 #include "putty.h"
63 #include "charset.h"
64 #include "mac.h"
65 #include "storage.h"
66 #include "terminal.h"
67
68 #define NCOLOURS (lenof(((Config *)0)->colours))
69
70 #define DEFAULT_FG 16
71 #define DEFAULT_FG_BOLD 17
72 #define DEFAULT_BG 18
73 #define DEFAULT_BG_BOLD 19
74 #define CURSOR_FG 20
75 #define CURSOR_BG 21
76
77 #define PTOCC(x) ((x) < 0 ? -(-(x - s->font_width - 1) / s->font_width) : \
78 (x) / s->font_width)
79 #define PTOCR(y) ((y) < 0 ? -(-(y - s->font_height - 1) / s->font_height) : \
80 (y) / s->font_height)
81
82 static void mac_initfont(Session *);
83 static pascal OSStatus uni_to_font_fallback(UniChar *, ByteCount, ByteCount *,
84 TextPtr, ByteCount, ByteCount *,
85 LogicalAddress *,
86 ConstUnicodeMappingPtr);
87 static void mac_initpalette(Session *);
88 static void mac_adjustwinbg(Session *);
89 static void mac_adjustsize(Session *, int, int);
90 static void mac_drawgrowicon(Session *s);
91 static pascal void mac_growtermdraghook(void);
92 static pascal void mac_scrolltracker(ControlHandle, short);
93 static pascal void do_text_for_device(short, short, GDHandle, long);
94 static int mac_keytrans(Session *, EventRecord *, unsigned char *);
95 static void text_click(Session *, EventRecord *);
96
97 void pre_paint(Session *s);
98 void post_paint(Session *s);
99
100 #if TARGET_RT_MAC_CFM
101 static RoutineDescriptor mac_scrolltracker_upp =
102 BUILD_ROUTINE_DESCRIPTOR(uppControlActionProcInfo,
103 (ProcPtr)mac_scrolltracker);
104 static RoutineDescriptor do_text_for_device_upp =
105 BUILD_ROUTINE_DESCRIPTOR(uppDeviceLoopDrawingProcInfo,
106 (ProcPtr)do_text_for_device);
107 #else /* not TARGET_RT_MAC_CFM */
108 #define mac_scrolltracker_upp mac_scrolltracker
109 #define do_text_for_device_upp do_text_for_device
110 #endif /* not TARGET_RT_MAC_CFM */
111
112 void mac_opensession(void) {
113 Session *s;
114 StandardFileReply sfr;
115 static const OSType sftypes[] = { 'Sess', 0, 0, 0 };
116 void *sesshandle;
117 int i;
118
119 s = smalloc(sizeof(*s));
120 memset(s, 0, sizeof(*s));
121
122 StandardGetFile(NULL, 1, sftypes, &sfr);
123 if (!sfr.sfGood) goto fail;
124
125 sesshandle = open_settings_r_fsp(&sfr.sfFile);
126 if (sesshandle == NULL) goto fail;
127 load_open_settings(sesshandle, TRUE, &s->cfg);
128 close_settings_r(sesshandle);
129
130 /*
131 * Select protocol. This is farmed out into a table in a
132 * separate file to enable an ssh-free variant.
133 */
134 s->back = NULL;
135 for (i = 0; backends[i].backend != NULL; i++)
136 if (backends[i].protocol == s->cfg.protocol) {
137 s->back = backends[i].backend;
138 break;
139 }
140 if (s->back == NULL) {
141 fatalbox("Unsupported protocol number found");
142 }
143 mac_startsession(s);
144 return;
145
146 fail:
147 sfree(s);
148 return;
149 }
150
151 void mac_startsession(Session *s)
152 {
153 char *errmsg;
154
155 /* XXX: Own storage management? */
156 if (HAVE_COLOR_QD())
157 s->window = GetNewCWindow(wTerminal, NULL, (WindowPtr)-1);
158 else
159 s->window = GetNewWindow(wTerminal, NULL, (WindowPtr)-1);
160 SetWRefCon(s->window, (long)s);
161 s->scrollbar = GetNewControl(cVScroll, s->window);
162 s->term = term_init(&s->cfg, s);
163
164 mac_initfont(s);
165 mac_initpalette(s);
166 if (HAVE_COLOR_QD()) {
167 /* Set to FALSE to not get palette updates in the background. */
168 SetPalette(s->window, s->palette, TRUE);
169 ActivatePalette(s->window);
170 }
171
172 s->logctx = log_init(s, &s->cfg);
173 term_provide_logctx(s->term, s->logctx);
174
175 errmsg = s->back->init(s->term, &s->backhandle, &s->cfg, s->cfg.host,
176 s->cfg.port, &s->realhost, s->cfg.tcp_nodelay);
177 if (errmsg != NULL)
178 fatalbox("%s", errmsg);
179 s->back->provide_logctx(s->backhandle, s->logctx);
180 set_title(s, s->realhost);
181
182 term_provide_resize_fn(s->term, s->back->size, s->backhandle);
183
184 mac_adjustsize(s, s->cfg.height, s->cfg.width);
185 term_size(s->term, s->cfg.height, s->cfg.width, s->cfg.savelines);
186
187 s->ldisc = ldisc_create(&s->cfg, s->term, s->back, s->backhandle, s);
188 ldisc_send(s->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
189
190 ShowWindow(s->window);
191 s->next = sesslist;
192 s->prev = s->next->prev;
193 if (s->next != NULL)
194 s->next->prev = &s->next;
195 sesslist = s;
196 }
197
198 /*
199 * Try to work out a horizontal scaling factor for the current font
200 * that will give a chracter width of wantwidth. Return it in numer
201 * and denom (suitable for passing to StdText()).
202 */
203 static void mac_workoutfontscale(Session *s, int wantwidth,
204 Point *numerp, Point *denomp)
205 {
206 Point numer, denom, tmpnumer, tmpdenom;
207 int gotwidth, i;
208 const char text = 'W';
209 FontInfo fi;
210
211 numer.v = denom.v = 1; /* always */
212 numer.h = denom.h = 1;
213 for (i = 0; i < 3; i++) {
214 tmpnumer = numer;
215 tmpdenom = denom;
216 if (s->window->grafProcs != NULL)
217 gotwidth = InvokeQDTxMeasUPP(1, &text, &tmpnumer, &tmpdenom, &fi,
218 s->window->grafProcs->txMeasProc);
219 else
220 gotwidth = StdTxMeas(1, &text, &tmpnumer, &tmpdenom, &fi);
221 /* The result of StdTxMeas must be scaled by the factors it returns. */
222 gotwidth = FixRound(FixMul(gotwidth << 16,
223 FixRatio(tmpnumer.h, tmpdenom.h)));
224 if (gotwidth == wantwidth)
225 break;
226 numer.h *= wantwidth;
227 denom.h *= gotwidth;
228 }
229 *numerp = numer;
230 *denomp = denom;
231 }
232
233 static UnicodeToTextFallbackUPP uni_to_font_fallback_upp;
234
235 static void mac_initfont(Session *s) {
236 Str255 macfont;
237 FontInfo fi;
238 TextEncoding enc;
239 OptionBits fbflags;
240
241 SetPort(s->window);
242 c2pstrcpy(macfont, s->cfg.font);
243 GetFNum(macfont, &s->fontnum);
244 TextFont(s->fontnum);
245 TextFace(s->cfg.fontisbold ? bold : 0);
246 TextSize(s->cfg.fontheight);
247 GetFontInfo(&fi);
248 s->font_width = CharWidth('W'); /* Well, it's what NCSA uses. */
249 s->font_ascent = fi.ascent;
250 s->font_leading = fi.leading;
251 s->font_height = s->font_ascent + fi.descent + s->font_leading;
252 mac_workoutfontscale(s, s->font_width,
253 &s->font_stdnumer, &s->font_stddenom);
254 mac_workoutfontscale(s, s->font_width * 2,
255 &s->font_widenumer, &s->font_widedenom);
256 TextSize(s->cfg.fontheight * 2);
257 mac_workoutfontscale(s, s->font_width * 2,
258 &s->font_bignumer, &s->font_bigdenom);
259 TextSize(s->cfg.fontheight);
260 if (!s->cfg.bold_colour) {
261 TextFace(bold);
262 s->font_boldadjust = s->font_width - CharWidth('W');
263 } else
264 s->font_boldadjust = 0;
265
266 if (s->uni_to_font != NULL)
267 DisposeUnicodeToTextInfo(&s->uni_to_font);
268 if (mac_gestalts.encvvers != 0 &&
269 UpgradeScriptInfoToTextEncoding(kTextScriptDontCare,
270 kTextLanguageDontCare,
271 kTextRegionDontCare, macfont,
272 &enc) == noErr &&
273 CreateUnicodeToTextInfoByEncoding(enc, &s->uni_to_font) == noErr) {
274 if (uni_to_font_fallback_upp == NULL)
275 uni_to_font_fallback_upp =
276 NewUnicodeToTextFallbackProc(&uni_to_font_fallback);
277 fbflags = kUnicodeFallbackCustomOnly;
278 if (mac_gestalts.uncvattr & kTECAddFallbackInterruptMask)
279 fbflags |= kUnicodeFallbackInterruptSafeMask;
280 if (SetFallbackUnicodeToText(s->uni_to_font,
281 uni_to_font_fallback_upp, fbflags, NULL) != noErr) {
282 DisposeUnicodeToTextInfo(&s->uni_to_font);
283 goto no_encv;
284 }
285 } else {
286 no_encv:
287 s->uni_to_font = NULL;
288 s->font_charset =
289 charset_from_macenc(FontToScript(s->fontnum),
290 GetScriptManagerVariable(smRegionCode),
291 mac_gestalts.sysvers, s->cfg.font);
292 }
293
294 mac_adjustsize(s, s->term->rows, s->term->cols);
295 }
296
297 static pascal OSStatus uni_to_font_fallback(UniChar *ucp,
298 ByteCount ilen, ByteCount *iusedp, TextPtr obuf, ByteCount olen,
299 ByteCount *ousedp, LogicalAddress *cookie, ConstUnicodeMappingPtr mapping)
300 {
301
302 if (olen < 1)
303 return kTECOutputBufferFullStatus;
304 /*
305 * What I'd _like_ to do here is to somehow generate the
306 * missing-character glyph that every font is required to have.
307 * Unfortunately (and somewhat surprisingly), I can't find any way
308 * to actually ask for it explicitly. Bah.
309 */
310 *obuf = '.';
311 *iusedp = ilen;
312 *ousedp = 1;
313 return noErr;
314 }
315
316 /*
317 * Called every time round the event loop.
318 */
319 void mac_pollterm(void)
320 {
321 Session *s;
322
323 for (s = sesslist; s != NULL; s = s->next) {
324 term_out(s->term);
325 term_update(s->term);
326 }
327 }
328
329 /*
330 * To be called whenever the window size changes.
331 * rows and cols should be desired values.
332 * It's assumed the terminal emulator will be informed, and will set rows
333 * and cols for us.
334 */
335 static void mac_adjustsize(Session *s, int newrows, int newcols) {
336 int winwidth, winheight;
337
338 winwidth = newcols * s->font_width + 15;
339 winheight = newrows * s->font_height;
340 SizeWindow(s->window, winwidth, winheight, true);
341 HideControl(s->scrollbar);
342 MoveControl(s->scrollbar, winwidth - 15, -1);
343 SizeControl(s->scrollbar, 16, winheight - 13);
344 ShowControl(s->scrollbar);
345 mac_drawgrowicon(s);
346 }
347
348 static void mac_initpalette(Session *s) {
349
350 if (!HAVE_COLOR_QD())
351 return;
352 /*
353 * Most colours should be inhibited on 2bpp displays.
354 * Palette manager documentation suggests inhibiting all tolerant colours
355 * on greyscale displays.
356 */
357 #define PM_NORMAL ( pmTolerant | pmInhibitC2 | \
358 pmInhibitG2 | pmInhibitG4 | pmInhibitG8 )
359 #define PM_TOLERANCE 0x2000
360 s->palette = NewPalette(22, NULL, PM_NORMAL, PM_TOLERANCE);
361 if (s->palette == NULL)
362 fatalbox("Unable to create palette");
363 /* In 2bpp, these are the colours we want most. */
364 SetEntryUsage(s->palette, DEFAULT_BG,
365 PM_NORMAL &~ pmInhibitC2, PM_TOLERANCE);
366 SetEntryUsage(s->palette, DEFAULT_FG,
367 PM_NORMAL &~ pmInhibitC2, PM_TOLERANCE);
368 SetEntryUsage(s->palette, DEFAULT_FG_BOLD,
369 PM_NORMAL &~ pmInhibitC2, PM_TOLERANCE);
370 SetEntryUsage(s->palette, CURSOR_BG,
371 PM_NORMAL &~ pmInhibitC2, PM_TOLERANCE);
372 palette_reset(s);
373 }
374
375 /*
376 * Set the background colour of the window correctly. Should be
377 * called whenever the default background changes.
378 */
379 static void mac_adjustwinbg(Session *s) {
380
381 if (!HAVE_COLOR_QD())
382 return;
383 #if !TARGET_CPU_68K
384 if (mac_gestalts.windattr & gestaltWindowMgrPresent)
385 SetWindowContentColor(s->window,
386 &(*s->palette)->pmInfo[DEFAULT_BG].ciRGB);
387 else
388 #endif
389 {
390 if (s->wctab == NULL)
391 s->wctab = (WCTabHandle)NewHandle(sizeof(**s->wctab));
392 if (s->wctab == NULL)
393 return; /* do without */
394 (*s->wctab)->wCSeed = 0;
395 (*s->wctab)->wCReserved = 0;
396 (*s->wctab)->ctSize = 0;
397 (*s->wctab)->ctTable[0].value = wContentColor;
398 (*s->wctab)->ctTable[0].rgb = (*s->palette)->pmInfo[DEFAULT_BG].ciRGB;
399 SetWinColor(s->window, s->wctab);
400 }
401 }
402
403 /*
404 * Set the cursor shape correctly
405 */
406 void mac_adjusttermcursor(WindowPtr window, Point mouse, RgnHandle cursrgn) {
407 Session *s;
408 ControlHandle control;
409 short part;
410 int x, y;
411
412 SetPort(window);
413 s = (Session *)GetWRefCon(window);
414 GlobalToLocal(&mouse);
415 part = FindControl(mouse, window, &control);
416 if (control == s->scrollbar) {
417 SetCursor(&qd.arrow);
418 RectRgn(cursrgn, &(*s->scrollbar)->contrlRect);
419 SectRgn(cursrgn, window->visRgn, cursrgn);
420 } else {
421 x = mouse.h / s->font_width;
422 y = mouse.v / s->font_height;
423 if (s->raw_mouse)
424 SetCursor(&qd.arrow);
425 else
426 SetCursor(*GetCursor(iBeamCursor));
427 /* Ask for shape changes if we leave this character cell. */
428 SetRectRgn(cursrgn, x * s->font_width, y * s->font_height,
429 (x + 1) * s->font_width, (y + 1) * s->font_height);
430 SectRgn(cursrgn, window->visRgn, cursrgn);
431 }
432 }
433
434 /*
435 * Enable/disable menu items based on the active terminal window.
436 */
437 void mac_adjusttermmenus(WindowPtr window) {
438 Session *s;
439 MenuHandle menu;
440 long offset;
441
442 s = (Session *)GetWRefCon(window);
443 menu = GetMenuHandle(mEdit);
444 EnableItem(menu, 0);
445 DisableItem(menu, iUndo);
446 DisableItem(menu, iCut);
447 if (1/*s->term->selstate == SELECTED*/)
448 EnableItem(menu, iCopy);
449 else
450 DisableItem(menu, iCopy);
451 if (GetScrap(NULL, 'TEXT', &offset) == noTypeErr)
452 DisableItem(menu, iPaste);
453 else
454 EnableItem(menu, iPaste);
455 DisableItem(menu, iClear);
456 EnableItem(menu, iSelectAll);
457 }
458
459 void mac_menuterm(WindowPtr window, short menu, short item) {
460 Session *s;
461
462 s = (Session *)GetWRefCon(window);
463 switch (menu) {
464 case mEdit:
465 switch (item) {
466 case iCopy:
467 /* term_copy(s); */
468 break;
469 case iPaste:
470 term_do_paste(s->term);
471 break;
472 }
473 }
474 }
475
476 void mac_clickterm(WindowPtr window, EventRecord *event) {
477 Session *s;
478 Point mouse;
479 ControlHandle control;
480 int part;
481
482 s = (Session *)GetWRefCon(window);
483 SetPort(window);
484 mouse = event->where;
485 GlobalToLocal(&mouse);
486 part = FindControl(mouse, window, &control);
487 if (control == s->scrollbar) {
488 switch (part) {
489 case kControlIndicatorPart:
490 if (TrackControl(control, mouse, NULL) == kControlIndicatorPart)
491 term_scroll(s->term, +1, GetControlValue(control));
492 break;
493 case kControlUpButtonPart:
494 case kControlDownButtonPart:
495 case kControlPageUpPart:
496 case kControlPageDownPart:
497 TrackControl(control, mouse, &mac_scrolltracker_upp);
498 break;
499 }
500 } else {
501 text_click(s, event);
502 }
503 }
504
505 static void text_click(Session *s, EventRecord *event) {
506 Point localwhere;
507 int row, col;
508 static UInt32 lastwhen = 0;
509 static Session *lastsess = NULL;
510 static int lastrow = -1, lastcol = -1;
511 static Mouse_Action lastact = MA_NOTHING;
512
513 SetPort(s->window);
514 localwhere = event->where;
515 GlobalToLocal(&localwhere);
516
517 col = PTOCC(localwhere.h);
518 row = PTOCR(localwhere.v);
519 if (event->when - lastwhen < GetDblTime() &&
520 row == lastrow && col == lastcol && s == lastsess)
521 lastact = (lastact == MA_CLICK ? MA_2CLK :
522 lastact == MA_2CLK ? MA_3CLK :
523 lastact == MA_3CLK ? MA_CLICK : MA_NOTHING);
524 else
525 lastact = MA_CLICK;
526 /* Fake right button with shift key */
527 term_mouse(s->term, event->modifiers & shiftKey ? MBT_RIGHT : MBT_LEFT,
528 lastact, col, row, event->modifiers & shiftKey,
529 event->modifiers & controlKey, event->modifiers & optionKey);
530 lastsess = s;
531 lastrow = row;
532 lastcol = col;
533 while (StillDown()) {
534 GetMouse(&localwhere);
535 col = PTOCC(localwhere.h);
536 row = PTOCR(localwhere.v);
537 term_mouse(s->term,
538 event->modifiers & shiftKey ? MBT_RIGHT : MBT_LEFT,
539 MA_DRAG, col, row, event->modifiers & shiftKey,
540 event->modifiers & controlKey,
541 event->modifiers & optionKey);
542 if (row > s->term->rows - 1)
543 term_scroll(s->term, 0, row - (s->term->rows - 1));
544 else if (row < 0)
545 term_scroll(s->term, 0, row);
546 }
547 term_mouse(s->term, event->modifiers & shiftKey ? MBT_RIGHT : MBT_LEFT,
548 MA_RELEASE, col, row, event->modifiers & shiftKey,
549 event->modifiers & controlKey, event->modifiers & optionKey);
550 lastwhen = TickCount();
551 }
552
553 Mouse_Button translate_button(void *frontend, Mouse_Button button)
554 {
555
556 switch (button) {
557 case MBT_LEFT:
558 return MBT_SELECT;
559 case MBT_RIGHT:
560 return MBT_EXTEND;
561 default:
562 return 0;
563 }
564 }
565
566 void write_clip(void *cookie, wchar_t *data, int len, int must_deselect) {
567
568 /*
569 * See "Programming with the Text Encoding Conversion Manager"
570 * Appendix E for Unicode scrap conventions.
571 *
572 * XXX Need to support TEXT/styl scrap as well.
573 * See STScrpRec in TextEdit (Inside Macintosh: Text) for styl details.
574 * XXX Maybe PICT scrap too.
575 */
576 if (ZeroScrap() != noErr)
577 return;
578 PutScrap(len * sizeof(*data), 'utxt', data);
579 }
580
581 void get_clip(void *frontend, wchar_t **p, int *lenp) {
582 Session *s = frontend;
583 static Handle h = NULL;
584 long offset;
585
586 if (p == NULL) {
587 /* release memory */
588 if (h != NULL)
589 DisposeHandle(h);
590 h = NULL;
591 } else
592 /* XXX Support TEXT-format scrap as well. */
593 if (GetScrap(NULL, 'utxt', &offset) > 0) {
594 h = NewHandle(0);
595 *lenp = GetScrap(h, 'utxt', &offset) / sizeof(**p);
596 HLock(h);
597 *p = (wchar_t *)*h;
598 if (*p == NULL || *lenp <= 0)
599 fatalbox("Empty scrap");
600 } else {
601 *p = NULL;
602 *lenp = 0;
603 }
604 }
605
606 static pascal void mac_scrolltracker(ControlHandle control, short part) {
607 Session *s;
608
609 s = (Session *)GetWRefCon((*control)->contrlOwner);
610 switch (part) {
611 case kControlUpButtonPart:
612 term_scroll(s->term, 0, -1);
613 break;
614 case kControlDownButtonPart:
615 term_scroll(s->term, 0, +1);
616 break;
617 case kControlPageUpPart:
618 term_scroll(s->term, 0, -(s->term->rows - 1));
619 break;
620 case kControlPageDownPart:
621 term_scroll(s->term, 0, +(s->term->rows - 1));
622 break;
623 }
624 }
625
626 #define K_BS 0x3300
627 #define K_F1 0x7a00
628 #define K_F2 0x7800
629 #define K_F3 0x6300
630 #define K_F4 0x7600
631 #define K_F5 0x6000
632 #define K_F6 0x6100
633 #define K_F7 0x6200
634 #define K_F8 0x6400
635 #define K_F9 0x6500
636 #define K_F10 0x6d00
637 #define K_F11 0x6700
638 #define K_F12 0x6f00
639 #define K_F13 0x6900
640 #define K_F14 0x6b00
641 #define K_F15 0x7100
642 #define K_INSERT 0x7200
643 #define K_HOME 0x7300
644 #define K_PRIOR 0x7400
645 #define K_DELETE 0x7500
646 #define K_END 0x7700
647 #define K_NEXT 0x7900
648 #define K_LEFT 0x7b00
649 #define K_RIGHT 0x7c00
650 #define K_DOWN 0x7d00
651 #define K_UP 0x7e00
652 #define KP_0 0x5200
653 #define KP_1 0x5300
654 #define KP_2 0x5400
655 #define KP_3 0x5500
656 #define KP_4 0x5600
657 #define KP_5 0x5700
658 #define KP_6 0x5800
659 #define KP_7 0x5900
660 #define KP_8 0x5b00
661 #define KP_9 0x5c00
662 #define KP_CLEAR 0x4700
663 #define KP_EQUAL 0x5100
664 #define KP_SLASH 0x4b00
665 #define KP_STAR 0x4300
666 #define KP_PLUS 0x4500
667 #define KP_MINUS 0x4e00
668 #define KP_DOT 0x4100
669 #define KP_ENTER 0x4c00
670
671 void mac_keyterm(WindowPtr window, EventRecord *event) {
672 unsigned char buf[20];
673 int len;
674 Session *s;
675
676 s = (Session *)GetWRefCon(window);
677 len = mac_keytrans(s, event, buf);
678 ldisc_send(s->ldisc, (char *)buf, len, 1);
679 ObscureCursor();
680 term_seen_key_event(s->term);
681 term_out(s->term);
682 term_update(s->term);
683 }
684
685 static int mac_keytrans(Session *s, EventRecord *event,
686 unsigned char *output) {
687 unsigned char *p = output;
688 int code;
689
690 /* No meta key yet -- that'll be rather fun. */
691
692 /* Keys that we handle locally */
693 if (event->modifiers & shiftKey) {
694 switch (event->message & keyCodeMask) {
695 case K_PRIOR: /* shift-pageup */
696 term_scroll(s->term, 0, -(s->term->rows - 1));
697 return 0;
698 case K_NEXT: /* shift-pagedown */
699 term_scroll(s->term, 0, +(s->term->rows - 1));
700 return 0;
701 }
702 }
703
704 /*
705 * Control-2 should return ^@ (0x00), Control-6 should return
706 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
707 * the DOS keyboard handling did it, and we have nothing better
708 * to do with the key combo in question, we'll also map
709 * Control-Backquote to ^\ (0x1C).
710 */
711
712 if (event->modifiers & controlKey) {
713 switch (event->message & charCodeMask) {
714 case ' ': case '2':
715 *p++ = 0x00;
716 return p - output;
717 case '`':
718 *p++ = 0x1c;
719 return p - output;
720 case '6':
721 *p++ = 0x1e;
722 return p - output;
723 case '/':
724 *p++ = 0x1f;
725 return p - output;
726 }
727 }
728
729 /*
730 * First, all the keys that do tilde codes. (ESC '[' nn '~',
731 * for integer decimal nn.)
732 *
733 * We also deal with the weird ones here. Linux VCs replace F1
734 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
735 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
736 * respectively.
737 */
738 code = 0;
739 switch (event->message & keyCodeMask) {
740 case K_F1: code = (event->modifiers & shiftKey ? 23 : 11); break;
741 case K_F2: code = (event->modifiers & shiftKey ? 24 : 12); break;
742 case K_F3: code = (event->modifiers & shiftKey ? 25 : 13); break;
743 case K_F4: code = (event->modifiers & shiftKey ? 26 : 14); break;
744 case K_F5: code = (event->modifiers & shiftKey ? 28 : 15); break;
745 case K_F6: code = (event->modifiers & shiftKey ? 29 : 17); break;
746 case K_F7: code = (event->modifiers & shiftKey ? 31 : 18); break;
747 case K_F8: code = (event->modifiers & shiftKey ? 32 : 19); break;
748 case K_F9: code = (event->modifiers & shiftKey ? 33 : 20); break;
749 case K_F10: code = (event->modifiers & shiftKey ? 34 : 21); break;
750 case K_F11: code = 23; break;
751 case K_F12: code = 24; break;
752 case K_HOME: code = 1; break;
753 case K_INSERT: code = 2; break;
754 case K_DELETE: code = 3; break;
755 case K_END: code = 4; break;
756 case K_PRIOR: code = 5; break;
757 case K_NEXT: code = 6; break;
758 }
759 if (s->cfg.funky_type == 1 && code >= 11 && code <= 15) {
760 p += sprintf((char *)p, "\x1B[[%c", code + 'A' - 11);
761 return p - output;
762 }
763 if (s->cfg.rxvt_homeend && (code == 1 || code == 4)) {
764 p += sprintf((char *)p, code == 1 ? "\x1B[H" : "\x1BOw");
765 return p - output;
766 }
767 if (code) {
768 p += sprintf((char *)p, "\x1B[%d~", code);
769 return p - output;
770 }
771
772 if (s->term->app_keypad_keys) {
773 switch (event->message & keyCodeMask) {
774 case KP_ENTER: p += sprintf((char *)p, "\x1BOM"); return p - output;
775 case KP_CLEAR: p += sprintf((char *)p, "\x1BOP"); return p - output;
776 case KP_EQUAL: p += sprintf((char *)p, "\x1BOQ"); return p - output;
777 case KP_SLASH: p += sprintf((char *)p, "\x1BOR"); return p - output;
778 case KP_STAR: p += sprintf((char *)p, "\x1BOS"); return p - output;
779 case KP_PLUS: p += sprintf((char *)p, "\x1BOl"); return p - output;
780 case KP_MINUS: p += sprintf((char *)p, "\x1BOm"); return p - output;
781 case KP_DOT: p += sprintf((char *)p, "\x1BOn"); return p - output;
782 case KP_0: p += sprintf((char *)p, "\x1BOp"); return p - output;
783 case KP_1: p += sprintf((char *)p, "\x1BOq"); return p - output;
784 case KP_2: p += sprintf((char *)p, "\x1BOr"); return p - output;
785 case KP_3: p += sprintf((char *)p, "\x1BOs"); return p - output;
786 case KP_4: p += sprintf((char *)p, "\x1BOt"); return p - output;
787 case KP_5: p += sprintf((char *)p, "\x1BOu"); return p - output;
788 case KP_6: p += sprintf((char *)p, "\x1BOv"); return p - output;
789 case KP_7: p += sprintf((char *)p, "\x1BOw"); return p - output;
790 case KP_8: p += sprintf((char *)p, "\x1BOx"); return p - output;
791 case KP_9: p += sprintf((char *)p, "\x1BOy"); return p - output;
792 }
793 }
794
795 switch (event->message & keyCodeMask) {
796 case K_UP:
797 p += sprintf((char *)p,
798 s->term->app_cursor_keys ? "\x1BOA" : "\x1B[A");
799 return p - output;
800 case K_DOWN:
801 p += sprintf((char *)p,
802 s->term->app_cursor_keys ? "\x1BOB" : "\x1B[B");
803 return p - output;
804 case K_RIGHT:
805 p += sprintf((char *)p,
806 s->term->app_cursor_keys ? "\x1BOC" : "\x1B[C");
807 return p - output;
808 case K_LEFT:
809 p += sprintf((char *)p,
810 s->term->app_cursor_keys ? "\x1BOD" : "\x1B[D");
811 return p - output;
812 case KP_ENTER:
813 *p++ = 0x0d;
814 return p - output;
815 case K_BS:
816 *p++ = (s->cfg.bksp_is_delete ? 0x7f : 0x08);
817 return p - output;
818 default:
819 *p++ = event->message & charCodeMask;
820 return p - output;
821 }
822 }
823
824 void request_paste(void *frontend)
825 {
826 Session *s = frontend;
827
828 /*
829 * In the Mac OS, pasting is synchronous: we can read the
830 * clipboard with no difficulty, so request_paste() can just go
831 * ahead and paste.
832 */
833 term_do_paste(s->term);
834 }
835
836 static struct {
837 Rect msgrect;
838 Point msgorigin;
839 Point zeromouse;
840 Session *s;
841 char oldmsg[20];
842 } growterm_state;
843
844 void mac_growterm(WindowPtr window, EventRecord *event) {
845 Rect limits;
846 long grow_result;
847 int newrows, newcols;
848 Session *s;
849 DragGrayRgnUPP draghooksave;
850 GrafPtr portsave;
851 FontInfo fi;
852
853 s = (Session *)GetWRefCon(window);
854
855 draghooksave = LMGetDragHook();
856 growterm_state.oldmsg[0] = '\0';
857 growterm_state.zeromouse = event->where;
858 growterm_state.zeromouse.h -= s->term->cols * s->font_width;
859 growterm_state.zeromouse.v -= s->term->rows * s->font_height;
860 growterm_state.s = s;
861 GetPort(&portsave);
862 SetPort(s->window);
863 BackColor(whiteColor);
864 ForeColor(blackColor);
865 TextFont(systemFont);
866 TextFace(0);
867 TextSize(12);
868 GetFontInfo(&fi);
869 SetRect(&growterm_state.msgrect, 0, 0,
870 StringWidth("\p99999x99999") + 4, fi.ascent + fi.descent + 4);
871 SetPt(&growterm_state.msgorigin, 2, fi.ascent + 2);
872 LMSetDragHook(NewDragGrayRgnUPP(mac_growtermdraghook));
873
874 SetRect(&limits, s->font_width + 15, s->font_height, SHRT_MAX, SHRT_MAX);
875 grow_result = GrowWindow(window, event->where, &limits);
876
877 DisposeDragGrayRgnUPP(LMGetDragHook());
878 LMSetDragHook(draghooksave);
879 InvalRect(&growterm_state.msgrect);
880
881 SetPort(portsave);
882
883 if (grow_result != 0) {
884 newrows = HiWord(grow_result) / s->font_height;
885 newcols = (LoWord(grow_result) - 15) / s->font_width;
886 mac_adjustsize(s, newrows, newcols);
887 term_size(s->term, newrows, newcols, s->cfg.savelines);
888 }
889 }
890
891 static pascal void mac_growtermdraghook(void)
892 {
893 Session *s = growterm_state.s;
894 GrafPtr portsave;
895 Point mouse;
896 char buf[20];
897 unsigned char pbuf[20];
898 int newrows, newcols;
899
900 GetMouse(&mouse);
901 newrows = (mouse.v - growterm_state.zeromouse.v) / s->font_height;
902 if (newrows < 1) newrows = 1;
903 newcols = (mouse.h - growterm_state.zeromouse.h) / s->font_width;
904 if (newcols < 1) newcols = 1;
905 sprintf(buf, "%dx%d", newcols, newrows);
906 if (strcmp(buf, growterm_state.oldmsg) == 0)
907 return;
908 strcpy(growterm_state.oldmsg, buf);
909 c2pstrcpy(pbuf, buf);
910
911 GetPort(&portsave);
912 SetPort(growterm_state.s->window);
913 EraseRect(&growterm_state.msgrect);
914 MoveTo(growterm_state.msgorigin.h, growterm_state.msgorigin.v);
915 DrawString(pbuf);
916 SetPort(portsave);
917 }
918
919 void mac_activateterm(WindowPtr window, Boolean active) {
920 Session *s;
921
922 s = (Session *)GetWRefCon(window);
923 s->term->has_focus = active;
924 term_update(s->term);
925 if (active)
926 ShowControl(s->scrollbar);
927 else {
928 if (HAVE_COLOR_QD())
929 PmBackColor(DEFAULT_BG);/* HideControl clears behind the control */
930 else
931 BackColor(blackColor);
932 HideControl(s->scrollbar);
933 }
934 mac_drawgrowicon(s);
935 }
936
937 void mac_updateterm(WindowPtr window) {
938 Session *s;
939
940 s = (Session *)GetWRefCon(window);
941 SetPort(window);
942 BeginUpdate(window);
943 pre_paint(s);
944 term_paint(s->term, s,
945 PTOCC((*window->visRgn)->rgnBBox.left),
946 PTOCR((*window->visRgn)->rgnBBox.top),
947 PTOCC((*window->visRgn)->rgnBBox.right),
948 PTOCR((*window->visRgn)->rgnBBox.bottom), 1);
949 /* Restore default colours in case the Window Manager uses them */
950 if (HAVE_COLOR_QD()) {
951 PmForeColor(DEFAULT_FG);
952 PmBackColor(DEFAULT_BG);
953 } else {
954 ForeColor(whiteColor);
955 BackColor(blackColor);
956 }
957 if (FrontWindow() != window)
958 EraseRect(&(*s->scrollbar)->contrlRect);
959 UpdateControls(window, window->visRgn);
960 mac_drawgrowicon(s);
961 post_paint(s);
962 EndUpdate(window);
963 }
964
965 static void mac_drawgrowicon(Session *s) {
966 Rect clip;
967 RgnHandle savergn;
968
969 SetPort(s->window);
970 /*
971 * Stop DrawGrowIcon giving us space for a horizontal scrollbar
972 * See Tech Note TB575 for details.
973 */
974 clip = s->window->portRect;
975 clip.left = clip.right - 15;
976 savergn = NewRgn();
977 GetClip(savergn);
978 ClipRect(&clip);
979 DrawGrowIcon(s->window);
980 SetClip(savergn);
981 DisposeRgn(savergn);
982 }
983
984 struct do_text_args {
985 Session *s;
986 Rect textrect;
987 char *text;
988 int len;
989 unsigned long attr;
990 int lattr;
991 Point numer, denom;
992 };
993
994 /*
995 * Call from the terminal emulator to draw a bit of text
996 *
997 * x and y are text row and column (zero-based)
998 */
999 void do_text(Context ctx, int x, int y, char *text, int len,
1000 unsigned long attr, int lattr) {
1001 Session *s = ctx;
1002 int style = 0;
1003 struct do_text_args a;
1004 RgnHandle textrgn, saveclip;
1005 char mactextbuf[1024];
1006 UniChar unitextbuf[1024];
1007 wchar_t *unitextptr;
1008 int i, fontwidth;
1009 ByteCount iread, olen;
1010 OSStatus err;
1011
1012 assert(len <= 1024);
1013
1014 SetPort(s->window);
1015
1016 fontwidth = s->font_width;
1017 if ((lattr & LATTR_MODE) != LATTR_NORM)
1018 fontwidth *= 2;
1019
1020 /* First check this text is relevant */
1021 a.textrect.top = y * s->font_height;
1022 a.textrect.bottom = (y + 1) * s->font_height;
1023 a.textrect.left = x * fontwidth;
1024 a.textrect.right = (x + len) * fontwidth;
1025 if (a.textrect.right > s->term->cols * s->font_width)
1026 a.textrect.right = s->term->cols * s->font_width;
1027 if (!RectInRgn(&a.textrect, s->window->visRgn))
1028 return;
1029
1030 /* Unpack Unicode from the mad format we get passed */
1031 for (i = 0; i < len; i++)
1032 unitextbuf[i] = (unsigned char)text[i] | (attr & CSET_MASK);
1033
1034 if (s->uni_to_font != NULL) {
1035 err = ConvertFromUnicodeToText(s->uni_to_font, len * sizeof(UniChar),
1036 unitextbuf, kUnicodeUseFallbacksMask,
1037 0, NULL, NULL, NULL,
1038 1024, &iread, &olen, mactextbuf);
1039 if (err != noErr && err != kTECUsedFallbacksStatus)
1040 olen = 0;
1041 } else if (s->font_charset != CS_NONE) {
1042 /* XXX this is bogus if wchar_t and UniChar are different sizes. */
1043 unitextptr = (wchar_t *)unitextbuf;
1044 olen = charset_from_unicode(&unitextptr, &len, mactextbuf, 1024,
1045 s->font_charset, NULL, ".", 1);
1046 } else
1047 olen = 0;
1048
1049 a.s = s;
1050 a.text = mactextbuf;
1051 a.len = olen;
1052 a.attr = attr;
1053 a.lattr = lattr;
1054 switch (lattr & LATTR_MODE) {
1055 case LATTR_NORM:
1056 TextSize(s->cfg.fontheight);
1057 a.numer = s->font_stdnumer;
1058 a.denom = s->font_stddenom;
1059 break;
1060 case LATTR_WIDE:
1061 TextSize(s->cfg.fontheight);
1062 a.numer = s->font_widenumer;
1063 a.denom = s->font_widedenom;
1064 break;
1065 case LATTR_TOP:
1066 case LATTR_BOT:
1067 TextSize(s->cfg.fontheight * 2);
1068 a.numer = s->font_bignumer;
1069 a.denom = s->font_bigdenom;
1070 break;
1071 }
1072 SetPort(s->window);
1073 TextFont(s->fontnum);
1074 if (s->cfg.fontisbold || (attr & ATTR_BOLD) && !s->cfg.bold_colour)
1075 style |= bold;
1076 if (attr & ATTR_UNDER)
1077 style |= underline;
1078 TextFace(style);
1079 TextMode(srcOr);
1080 if (HAVE_COLOR_QD())
1081 if (style & bold) {
1082 SpaceExtra(s->font_boldadjust << 16);
1083 CharExtra(s->font_boldadjust << 16);
1084 } else {
1085 SpaceExtra(0);
1086 CharExtra(0);
1087 }
1088 saveclip = NewRgn();
1089 GetClip(saveclip);
1090 ClipRect(&a.textrect);
1091 textrgn = NewRgn();
1092 RectRgn(textrgn, &a.textrect);
1093 if (HAVE_COLOR_QD())
1094 DeviceLoop(textrgn, &do_text_for_device_upp, (long)&a, 0);
1095 else
1096 do_text_for_device(1, 0, NULL, (long)&a);
1097 SetClip(saveclip);
1098 DisposeRgn(saveclip);
1099 DisposeRgn(textrgn);
1100 /* Tell the window manager about it in case this isn't an update */
1101 ValidRect(&a.textrect);
1102 }
1103
1104 static pascal void do_text_for_device(short depth, short devflags,
1105 GDHandle device, long cookie) {
1106 struct do_text_args *a;
1107 int bgcolour, fgcolour, bright, reverse, tmp;
1108
1109 a = (struct do_text_args *)cookie;
1110
1111 bright = (a->attr & ATTR_BOLD) && a->s->cfg.bold_colour;
1112 reverse = a->attr & ATTR_REVERSE;
1113
1114 if (depth == 1 && (a->attr & TATTR_ACTCURS))
1115 reverse = !reverse;
1116
1117 if (HAVE_COLOR_QD()) {
1118 if (depth > 2) {
1119 fgcolour = ((a->attr & ATTR_FGMASK) >> ATTR_FGSHIFT) * 2;
1120 bgcolour = ((a->attr & ATTR_BGMASK) >> ATTR_BGSHIFT) * 2;
1121 } else {
1122 /*
1123 * NB: bold reverse in 2bpp breaks with the usual PuTTY model and
1124 * boldens the background, because that's all we can do.
1125 */
1126 fgcolour = bright ? DEFAULT_FG_BOLD : DEFAULT_FG;
1127 bgcolour = DEFAULT_BG;
1128 }
1129 if (reverse) {
1130 tmp = fgcolour;
1131 fgcolour = bgcolour;
1132 bgcolour = tmp;
1133 }
1134 if (bright && depth > 2)
1135 fgcolour++;
1136 if ((a->attr & TATTR_ACTCURS) && depth > 1) {
1137 fgcolour = CURSOR_FG;
1138 bgcolour = CURSOR_BG;
1139 }
1140 PmForeColor(fgcolour);
1141 PmBackColor(bgcolour);
1142 } else { /* No Color Quickdraw */
1143 /* XXX This should be done with a _little_ more configurability */
1144 if (reverse) {
1145 ForeColor(blackColor);
1146 BackColor(whiteColor);
1147 } else {
1148 ForeColor(whiteColor);
1149 BackColor(blackColor);
1150 }
1151 }
1152
1153 EraseRect(&a->textrect);
1154 switch (a->lattr & LATTR_MODE) {
1155 case LATTR_NORM:
1156 case LATTR_WIDE:
1157 MoveTo(a->textrect.left, a->textrect.top + a->s->font_ascent);
1158 break;
1159 case LATTR_TOP:
1160 MoveTo(a->textrect.left, a->textrect.top + a->s->font_ascent * 2);
1161 break;
1162 case LATTR_BOT:
1163 MoveTo(a->textrect.left,
1164 a->textrect.top - a->s->font_height + a->s->font_ascent * 2);
1165 break;
1166 }
1167 /* FIXME: Sort out bold width adjustments on Original QuickDraw. */
1168 if (a->s->window->grafProcs != NULL)
1169 InvokeQDTextUPP(a->len, a->text, a->numer, a->denom,
1170 a->s->window->grafProcs->textProc);
1171 else
1172 StdText(a->len, a->text, a->numer, a->denom);
1173
1174 if (a->attr & TATTR_PASCURS) {
1175 PenNormal();
1176 switch (depth) {
1177 case 1:
1178 PenMode(patXor);
1179 break;
1180 default:
1181 PmForeColor(CURSOR_BG);
1182 break;
1183 }
1184 FrameRect(&a->textrect);
1185 }
1186 }
1187
1188 void do_cursor(Context ctx, int x, int y, char *text, int len,
1189 unsigned long attr, int lattr)
1190 {
1191
1192 do_text(ctx, x, y, text, len, attr, lattr);
1193 }
1194
1195 /*
1196 * Call from the terminal emulator to get its graphics context.
1197 * Should probably be called start_redraw or something.
1198 */
1199 void pre_paint(Session *s) {
1200 GDHandle gdh;
1201 Rect myrect, tmprect;
1202
1203 if (HAVE_COLOR_QD()) {
1204 s->term->attr_mask = 0;
1205 SetPort(s->window);
1206 myrect = (*s->window->visRgn)->rgnBBox;
1207 LocalToGlobal((Point *)&myrect.top);
1208 LocalToGlobal((Point *)&myrect.bottom);
1209 for (gdh = GetDeviceList();
1210 gdh != NULL;
1211 gdh = GetNextDevice(gdh)) {
1212 if (TestDeviceAttribute(gdh, screenDevice) &&
1213 TestDeviceAttribute(gdh, screenActive) &&
1214 SectRect(&(*gdh)->gdRect, &myrect, &tmprect)) {
1215 switch ((*(*gdh)->gdPMap)->pixelSize) {
1216 case 1:
1217 if (s->cfg.bold_colour)
1218 s->term->attr_mask |= ~(ATTR_COLOURS |
1219 (s->cfg.bold_colour ? ATTR_BOLD : 0));
1220 break;
1221 case 2:
1222 s->term->attr_mask |= ~ATTR_COLOURS;
1223 break;
1224 default:
1225 s->term->attr_mask = ~0;
1226 return; /* No point checking more screens. */
1227 }
1228 }
1229 }
1230 } else
1231 s->term->attr_mask = ~(ATTR_COLOURS |
1232 (s->cfg.bold_colour ? ATTR_BOLD : 0));
1233 }
1234
1235 Context get_ctx(void *frontend) {
1236 Session *s = frontend;
1237
1238 pre_paint(s);
1239 return s;
1240 }
1241
1242 void free_ctx(Context ctx) {
1243
1244 }
1245
1246 /*
1247 * Presumably this does something in Windows
1248 */
1249 void post_paint(Session *s) {
1250
1251 }
1252
1253 /*
1254 * Set the scroll bar position
1255 *
1256 * total is the line number of the bottom of the working screen
1257 * start is the line number of the top of the display
1258 * page is the length of the displayed page
1259 */
1260 void set_sbar(void *frontend, int total, int start, int page) {
1261 Session *s = frontend;
1262
1263 /* We don't redraw until we've set everything up, to avoid glitches */
1264 (*s->scrollbar)->contrlMin = 0;
1265 (*s->scrollbar)->contrlMax = total - page;
1266 SetControlValue(s->scrollbar, start);
1267 #if !TARGET_CPU_68K
1268 if (mac_gestalts.cntlattr & gestaltControlMgrPresent)
1269 SetControlViewSize(s->scrollbar, page);
1270 #endif
1271 }
1272
1273 void sys_cursor(void *frontend, int x, int y)
1274 {
1275 /*
1276 * I think his is meaningless under Mac OS.
1277 */
1278 }
1279
1280 /*
1281 * This is still called when mode==BELL_VISUAL, even though the
1282 * visual bell is handled entirely within terminal.c, because we
1283 * may want to perform additional actions on any kind of bell (for
1284 * example, taskbar flashing in Windows).
1285 */
1286 void beep(void *frontend, int mode)
1287 {
1288 if (mode != BELL_VISUAL)
1289 SysBeep(30);
1290 /*
1291 * XXX We should indicate the relevant window and/or use the
1292 * Notification Manager
1293 */
1294 }
1295
1296 int char_width(Context ctx, int uc)
1297 {
1298 /*
1299 * Until we support exciting character-set stuff, assume all chars are
1300 * single-width.
1301 */
1302 return 1;
1303 }
1304
1305 /*
1306 * Set icon string -- a no-op here (Windowshade?)
1307 */
1308 void set_icon(void *frontend, char *icon) {
1309 Session *s = frontend;
1310
1311 }
1312
1313 /*
1314 * Set the window title
1315 */
1316 void set_title(void *frontend, char *title) {
1317 Session *s = frontend;
1318 Str255 mactitle;
1319
1320 c2pstrcpy(mactitle, title);
1321 SetWTitle(s->window, mactitle);
1322 }
1323
1324 /*
1325 * set or clear the "raw mouse message" mode
1326 */
1327 void set_raw_mouse_mode(void *frontend, int activate)
1328 {
1329 Session *s = frontend;
1330
1331 s->raw_mouse = activate;
1332 /* FIXME: Should call mac_updatetermcursor as appropriate. */
1333 }
1334
1335 /*
1336 * Resize the window at the emulator's request
1337 */
1338 void request_resize(void *frontend, int w, int h) {
1339 Session *s = frontend;
1340
1341 term_size(s->term, h, w, s->cfg.savelines);
1342 mac_initfont(s);
1343 }
1344
1345 /*
1346 * Iconify (actually collapse) the window at the emulator's request.
1347 */
1348 void set_iconic(void *frontend, int iconic)
1349 {
1350 Session *s = frontend;
1351 UInt32 features;
1352
1353 if (mac_gestalts.apprvers >= 0x0100 &&
1354 GetWindowFeatures(s->window, &features) == noErr &&
1355 (features & kWindowCanCollapse))
1356 CollapseWindow(s->window, iconic);
1357 }
1358
1359 /*
1360 * Move the window in response to a server-side request.
1361 */
1362 void move_window(void *frontend, int x, int y)
1363 {
1364 Session *s = frontend;
1365
1366 MoveWindow(s->window, x, y, FALSE);
1367 }
1368
1369 /*
1370 * Move the window to the top or bottom of the z-order in response
1371 * to a server-side request.
1372 */
1373 void set_zorder(void *frontend, int top)
1374 {
1375 Session *s = frontend;
1376
1377 /*
1378 * We also change the input focus to point to the topmost window,
1379 * since that's probably what the Human Interface Guidelines would
1380 * like us to do.
1381 */
1382 if (top)
1383 SelectWindow(s->window);
1384 else
1385 SendBehind(s->window, NULL);
1386 }
1387
1388 /*
1389 * Refresh the window in response to a server-side request.
1390 */
1391 void refresh_window(void *frontend)
1392 {
1393 Session *s = frontend;
1394
1395 term_invalidate(s->term);
1396 }
1397
1398 /*
1399 * Maximise or restore the window in response to a server-side
1400 * request.
1401 */
1402 void set_zoomed(void *frontend, int zoomed)
1403 {
1404 Session *s = frontend;
1405
1406 ZoomWindow(s->window, zoomed ? inZoomOut : inZoomIn, FALSE);
1407 }
1408
1409 /*
1410 * Report whether the window is iconic, for terminal reports.
1411 */
1412 int is_iconic(void *frontend)
1413 {
1414 Session *s = frontend;
1415 UInt32 features;
1416
1417 if (mac_gestalts.apprvers >= 0x0100 &&
1418 GetWindowFeatures(s->window, &features) == noErr &&
1419 (features & kWindowCanCollapse))
1420 return IsWindowCollapsed(s->window);
1421 return FALSE;
1422 }
1423
1424 /*
1425 * Report the window's position, for terminal reports.
1426 */
1427 void get_window_pos(void *frontend, int *x, int *y)
1428 {
1429 Session *s = frontend;
1430
1431 *x = s->window->portRect.left;
1432 *y = s->window->portRect.top;
1433 }
1434
1435 /*
1436 * Report the window's pixel size, for terminal reports.
1437 */
1438 void get_window_pixels(void *frontend, int *x, int *y)
1439 {
1440 Session *s = frontend;
1441
1442 *x = s->window->portRect.right - s->window->portRect.left;
1443 *y = s->window->portRect.bottom - s->window->portRect.top;
1444 }
1445
1446 /*
1447 * Return the window or icon title.
1448 */
1449 char *get_window_title(void *frontend, int icon)
1450 {
1451 Session *s = frontend;
1452 Str255 ptitle;
1453 static char title[256];
1454
1455 GetWTitle(s->window, ptitle);
1456 p2cstrcpy(title, ptitle);
1457 return title;
1458 }
1459
1460 /*
1461 * real_palette_set(): This does the actual palette-changing work on behalf
1462 * of palette_set(). Does _not_ call ActivatePalette() in case the caller
1463 * is doing a batch of updates.
1464 */
1465 static void real_palette_set(Session *s, int n, int r, int g, int b)
1466 {
1467 RGBColor col;
1468
1469 if (!HAVE_COLOR_QD())
1470 return;
1471 col.red = r * 0x0101;
1472 col.green = g * 0x0101;
1473 col.blue = b * 0x0101;
1474 SetEntryColor(s->palette, n, &col);
1475 }
1476
1477 /*
1478 * Set the logical palette. Called by the terminal emulator.
1479 */
1480 void palette_set(void *frontend, int n, int r, int g, int b) {
1481 Session *s = frontend;
1482 static const int first[21] = {
1483 0, 2, 4, 6, 8, 10, 12, 14,
1484 1, 3, 5, 7, 9, 11, 13, 15,
1485 16, 17, 18, 20, 21
1486 };
1487
1488 if (!HAVE_COLOR_QD())
1489 return;
1490 real_palette_set(s, first[n], r, g, b);
1491 if (first[n] == 18)
1492 real_palette_set(s, first[n]+1, r, g, b);
1493 if (first[n] == DEFAULT_BG)
1494 mac_adjustwinbg(s);
1495 ActivatePalette(s->window);
1496 }
1497
1498 /*
1499 * Reset to the default palette
1500 */
1501 void palette_reset(void *frontend) {
1502 Session *s = frontend;
1503 /* This maps colour indices in cfg to those used in our palette. */
1504 static const int ww[] = {
1505 6, 7, 8, 9, 10, 11, 12, 13,
1506 14, 15, 16, 17, 18, 19, 20, 21,
1507 0, 1, 2, 3, 4, 5
1508 };
1509 int i;
1510
1511 if (!HAVE_COLOR_QD())
1512 return;
1513
1514 assert(lenof(ww) == NCOLOURS);
1515
1516 for (i = 0; i < NCOLOURS; i++) {
1517 real_palette_set(s, i,
1518 s->cfg.colours[ww[i]][0],
1519 s->cfg.colours[ww[i]][1],
1520 s->cfg.colours[ww[i]][2]);
1521 }
1522 mac_adjustwinbg(s);
1523 ActivatePalette(s->window);
1524 /* Palette Manager will generate update events as required. */
1525 }
1526
1527 /*
1528 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1529 * for backward.)
1530 */
1531 void do_scroll(void *frontend, int topline, int botline, int lines) {
1532 Session *s = frontend;
1533 Rect r;
1534 RgnHandle scrollrgn = NewRgn();
1535 RgnHandle movedupdate = NewRgn();
1536 RgnHandle update = NewRgn();
1537 Point g2l = { 0, 0 };
1538
1539 SetPort(s->window);
1540
1541 /*
1542 * Work out the part of the update region that will scrolled by
1543 * this operation.
1544 */
1545 if (lines > 0)
1546 SetRectRgn(scrollrgn, 0, (topline + lines) * s->font_height,
1547 s->term->cols * s->font_width,
1548 (botline + 1) * s->font_height);
1549 else
1550 SetRectRgn(scrollrgn, 0, topline * s->font_height,
1551 s->term->cols * s->font_width,
1552 (botline - lines + 1) * s->font_height);
1553 CopyRgn(((WindowPeek)s->window)->updateRgn, movedupdate);
1554 GlobalToLocal(&g2l);
1555 OffsetRgn(movedupdate, g2l.h, g2l.v); /* Convert to local co-ords. */
1556 SectRgn(scrollrgn, movedupdate, movedupdate); /* Clip scrolled section. */
1557 ValidRgn(movedupdate);
1558 OffsetRgn(movedupdate, 0, -lines * s->font_height); /* Scroll it. */
1559
1560 PenNormal();
1561 if (HAVE_COLOR_QD())
1562 PmBackColor(DEFAULT_BG);
1563 else
1564 BackColor(blackColor); /* XXX make configurable */
1565 SetRect(&r, 0, topline * s->font_height,
1566 s->term->cols * s->font_width, (botline + 1) * s->font_height);
1567 ScrollRect(&r, 0, - lines * s->font_height, update);
1568
1569 InvalRgn(update);
1570 InvalRgn(movedupdate);
1571
1572 DisposeRgn(scrollrgn);
1573 DisposeRgn(movedupdate);
1574 DisposeRgn(update);
1575 }
1576
1577 void logevent(void *frontend, char *str) {
1578
1579 fprintf(stderr, "%s\n", str);
1580 }
1581
1582 /* Dummy routine, only required in plink. */
1583 void ldisc_update(void *frontend, int echo, int edit)
1584 {
1585 }
1586
1587 /*
1588 * Mac PuTTY doesn't support printing yet.
1589 */
1590 printer_job *printer_start_job(char *printer)
1591 {
1592
1593 return NULL;
1594 }
1595
1596 void printer_job_data(printer_job *pj, void *data, int len)
1597 {
1598 }
1599
1600 void printer_finish_job(printer_job *pj)
1601 {
1602 }
1603
1604 void frontend_keypress(void *handle)
1605 {
1606 /*
1607 * Keypress termination in non-Close-On-Exit mode is not
1608 * currently supported in PuTTY proper, because the window
1609 * always has a perfectly good Close button anyway. So we do
1610 * nothing here.
1611 */
1612 return;
1613 }
1614
1615 /*
1616 * Ask whether to wipe a session log file before writing to it.
1617 * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
1618 */
1619 int askappend(void *frontend, char *filename)
1620 {
1621
1622 /* FIXME: not implemented yet. */
1623 return 2;
1624 }
1625
1626 /*
1627 * Emacs magic:
1628 * Local Variables:
1629 * c-file-style: "simon"
1630 * End:
1631 */
1632