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