]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - macterm.c
Slight optimisation of do_text so we don't do anything if the entire string's
[PuTTY.git] / macterm.c
1 /*
2  * macterm.c -- Macintosh terminal front-end
3  */
4
5 #include <MacTypes.h>
6 #include <Fonts.h>
7 #include <Gestalt.h>
8 #include <MacWindows.h>
9 #include <Quickdraw.h>
10 #include <QuickdrawText.h>
11 #include <Sound.h>
12
13 #include <stdlib.h>
14
15 #include "macresid.h"
16 #include "putty.h"
17 #include "mac.h"
18
19 struct mac_session {
20     short fontnum;
21     int font_ascent;
22     WindowPtr(window);
23     RGBColor defpal[24];
24     RGBColor palette[24];
25 };
26
27 static void mac_initfont(struct mac_session *);
28 static void mac_initpalette(struct mac_session *);
29
30 /* Temporary hack till I get the terminal emulator supporting multiple sessions */
31
32 static struct mac_session *onlysession;
33
34 void mac_newsession(void) {
35     struct mac_session *s;
36     int i;
37
38     /* This should obviously be initialised by other means */
39     s = smalloc(sizeof(*s));
40     cfg.bksp_is_delete = TRUE;
41     cfg.rxvt_homeend = FALSE;
42     cfg.linux_funkeys = FALSE;
43     cfg.app_cursor = FALSE;
44     cfg.app_keypad = FALSE;
45     cfg.savelines = 100;
46     cfg.dec_om = FALSE;
47     cfg.wrap_mode = 
48     cfg.lfhascr = FALSE;
49     cfg.win_name_always = FALSE;
50     cfg.width = 80;
51     cfg.height = 24;
52     strcpy(cfg.font, "Monaco");
53     cfg.fontisbold = 0;
54     cfg.fontheight = 9;
55     cfg.vtmode = VT_POORMAN;
56     cfg.try_palette = FALSE;
57     cfg.bold_colour = TRUE;
58     for (i = 0; i < 22; i++) {
59         static char defaults[22][3] = {
60             {187, 187, 187}, {255, 255, 255},
61             {0, 0, 0}, {85, 85, 85},
62             {0, 0, 0}, {0, 255, 0},
63             {0, 0, 0}, {85, 85, 85},
64             {187, 0, 0}, {255, 85, 85},
65             {0, 187, 0}, {85, 255, 85},
66             {187, 187, 0}, {255, 255, 85},
67             {0, 0, 187}, {85, 85, 255},
68             {187, 0, 187}, {255, 85, 255},
69             {0, 187, 187}, {85, 255, 255},
70             {187, 187, 187}, {255, 255, 255}
71          };
72          cfg.colours[i][0] = defaults[i][0];
73          cfg.colours[i][1] = defaults[i][1];
74          cfg.colours[i][2] = defaults[i][2];
75     }
76     onlysession = s;
77         
78     /* XXX: non-Color-QuickDraw?  Own storage management? */
79     if (mac_qdversion == gestaltOriginalQD)
80         s->window = GetNewWindow(wTerminal, NULL, (WindowPtr)-1);
81     else
82         s->window = GetNewCWindow(wTerminal, NULL, (WindowPtr)-1);
83     SetWRefCon(s->window, (long)s);
84     term_init();
85     term_size(24, 80, 100);
86     mac_initfont(s);
87     mac_initpalette(s);
88     ShowWindow(s->window);
89 }
90
91 static void inbuf_putc(int c) {
92     inbuf[inbuf_head] = c;
93     inbuf_head = (inbuf_head+1) & INBUF_MASK;
94 }
95
96 static void inbuf_putstr(const char *c) {
97     while (*c)
98         inbuf_putc(*c++);
99 }
100
101 static void mac_initfont(struct mac_session *s) {
102     Str255 macfont;
103     FontInfo fi;
104  
105     SetPort(s->window);
106     macfont[0] = sprintf((char *)&macfont[1], "%s", cfg.font);
107     GetFNum(macfont, &s->fontnum);
108     TextFont(s->fontnum);
109     TextFace(cfg.fontisbold ? bold : 0);
110     TextSize(cfg.fontheight);
111     GetFontInfo(&fi);
112     font_width = fi.widMax;
113     font_height = fi.ascent + fi.descent + fi.leading;
114     s->font_ascent = fi.ascent;
115     SizeWindow(s->window, cols * font_width, rows * font_height, true);
116     inbuf_putstr("\033[1mBold\033[m    \033[2mfaint\033[m   \033[3mitalic\033[m  \033[4mu_line\033[m  "
117                  "\033[5mslow bl\033[m \033[6mfast bl\033[m \033[7minverse\033[m \033[8mconceal\033[m "
118                  "\033[9mstruck\033[m  \033[21mdbl ul\033[m\015\012");
119     term_out();
120     inbuf_putstr("\033[30mblack   \033[31mred     \033[32mgreen   \033[33myellow  "
121                  "\033[34mblue    \033[35mmagenta \033[36mcyan    \033[37mwhite\015\012");
122     term_out();
123     inbuf_putstr("\033[1m\033[30mblack   \033[31mred     \033[32mgreen   \033[33myellow  "
124                  "\033[1m\033[34mblue    \033[35mmagenta \033[36mcyan    \033[37mwhite\015\012");
125     term_out();
126     inbuf_putstr("\033[37;44mwhite on blue     \033[32;41mgreen on red\015\012");
127     term_out();
128 }
129
130
131 /*
132  * Set up the default palette, then call palette_reset to transfer
133  * it to the working palette (should the emulator do this at
134  * startup?
135  */
136 static void mac_initpalette(struct mac_session *s) {
137     int i;
138     static const int ww[] = {
139         6, 7, 8, 9, 10, 11, 12, 13,
140         14, 15, 16, 17, 18, 19, 20, 21,
141         0, 1, 2, 3, 4, 4, 5, 5
142     };
143
144     for (i=0; i<24; i++) {
145         int w = ww[i];
146         s->defpal[i].red   = cfg.colours[w][0] * 0x0101;
147         s->defpal[i].green = cfg.colours[w][1] * 0x0101;
148         s->defpal[i].blue  = cfg.colours[w][2] * 0x0101;
149     }
150     palette_reset();
151 }
152
153
154 /*
155  * Call from the terminal emulator to draw a bit of text
156  *
157  * x and y are text row and column (zero-based)
158  */
159 void do_text(struct mac_session *s, int x, int y, char *text, int len,
160              unsigned long attr) {
161     int style = 0;
162     int bgcolour, fgcolour;
163     RGBColor rgbfore, rgbback;
164     RgnHandle textregion, intersection;
165     Rect textrect;
166
167     SetPort(s->window);
168 #if 0
169     /* First check this text is relevant */
170     textregion = NewRgn();
171     SetRectRgn(textregion, x * font_width, (x + len) * font_width,
172                y * font_height, (y + 1) * font_height);
173     SectRgn(textregion, s->window->visRgn, textregion);
174     if (EmptyRgn(textregion)) {
175         DisposeRgn(textregion);
176         return;
177     }
178 #else
179     /* alternatively */
180     textrect.top = y * font_height;
181     textrect.bottom = (y + 1) * font_height;
182     textrect.left = x * font_width;
183     textrect.right = (x + len) * font_width;
184     if (!RectInRgn(&textrect, s->window->visRgn))
185         return;
186 #endif
187
188     TextFont(s->fontnum);
189     if (cfg.fontisbold || (attr & ATTR_BOLD) && !cfg.bold_colour)
190         style |= bold;
191     if (attr & ATTR_UNDER)
192         style |= underline;
193     TextFace(style);
194     TextSize(cfg.fontheight);
195     TextMode(srcCopy);
196     if (attr & ATTR_REVERSE) {
197         bgcolour = ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT) * 2;
198         fgcolour = ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT) * 2;
199     } else {
200         fgcolour = ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT) * 2;
201         bgcolour = ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT) * 2;
202     }
203     if ((attr & ATTR_BOLD) && cfg.bold_colour)
204         fgcolour++;
205     RGBForeColor(&s->palette[fgcolour]);
206     RGBBackColor(&s->palette[bgcolour]);
207     SetFractEnable(FALSE); /* We want characters on pixel boundaries */
208     MoveTo(x * font_width, y * font_height + s->font_ascent);
209     DrawText(text, 0, len);
210 }
211
212 /*
213  * Call from the terminal emulator to get its graphics context.
214  * I feel this should disappear entirely (and do_text should take
215  * a Session as an argument).  Simon may disagree.
216  */
217 struct mac_session *get_ctx(void) {
218
219     return onlysession;
220 }
221
222 /*
223  * Presumably this does something in Windows
224  */
225 void free_ctx(struct mac_session *ctx) {
226
227 }
228
229 /*
230  * Set the scroll bar position
231  */
232 void set_sbar(int total, int start, int page) {
233
234     /* Do something once we actually have a scroll bar */
235 }
236
237 /*
238  * Beep
239  */
240 void beep(void) {
241
242     SysBeep(30);
243 }
244
245 /*
246  * Set icon string -- a no-op here (WIndowshade?)
247  */
248 void set_icon(char *icon) {
249
250 }
251
252 /*
253  * Set the window title
254  */
255 void set_title(char *title) {
256     Str255 mactitle;
257
258     mactitle[0] = sprintf((char *)&mactitle[1], "%s", title);
259     SetWTitle(onlysession->window, mactitle);
260 }
261
262 /*
263  * Resize the window at the emulator's request
264  */
265 void request_resize(int w, int h) {
266
267     /* XXX: Do something */
268 }
269
270 /*
271  * Set the logical palette
272  */
273 void palette_set(int n, int r, int g, int b) {
274
275     /* XXX: Do something */
276 }
277
278 /*
279  * Reset to the default palette
280  */
281 void palette_reset(void) {
282     int i;
283     struct mac_session *s = onlysession;
284
285     for (i = 0; i < 24; i++) {
286         s->palette[i].red   = s->defpal[i].red;
287         s->palette[i].green = s->defpal[i].green;
288         s->palette[i].blue  = s->defpal[i].blue;
289     }
290     term_invalidate();
291 }