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