]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - macterm.c
Most attributes work now.
[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
165     SetPort(s->window);
166     TextFont(s->fontnum);
167     if (cfg.fontisbold || (attr & ATTR_BOLD) && !cfg.bold_colour)
168         style |= bold;
169     if (attr & ATTR_UNDER)
170         style |= underline;
171     TextFace(style);
172     TextSize(cfg.fontheight);
173     TextMode(srcCopy);
174     if (attr & ATTR_REVERSE) {
175         bgcolour = ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT) * 2;
176         fgcolour = ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT) * 2;
177     } else {
178         fgcolour = ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT) * 2;
179         bgcolour = ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT) * 2;
180     }
181     if ((attr & ATTR_BOLD) && cfg.bold_colour)
182         fgcolour++;
183     RGBForeColor(&s->palette[fgcolour]);
184     RGBBackColor(&s->palette[bgcolour]);
185     SetFractEnable(FALSE); /* We want characters on pixel boundaries */
186     MoveTo(x * font_width, y * font_height + s->font_ascent);
187     DrawText(text, 0, len);
188 }
189
190 /*
191  * Call from the terminal emulator to get its graphics context.
192  * I feel this should disappear entirely (and do_text should take
193  * a Session as an argument).  Simon may disagree.
194  */
195 struct mac_session *get_ctx(void) {
196
197     return onlysession;
198 }
199
200 /*
201  * Presumably this does something in Windows
202  */
203 void free_ctx(struct mac_session *ctx) {
204
205 }
206
207 /*
208  * Set the scroll bar position
209  */
210 void set_sbar(int total, int start, int page) {
211
212     /* Do something once we actually have a scroll bar */
213 }
214
215 /*
216  * Beep
217  */
218 void beep(void) {
219
220     SysBeep(30);
221 }
222
223 /*
224  * Set icon string -- a no-op here (WIndowshade?)
225  */
226 void set_icon(char *icon) {
227
228 }
229
230 /*
231  * Set the window title
232  */
233 void set_title(char *title) {
234     Str255 mactitle;
235
236     mactitle[0] = sprintf((char *)&mactitle[1], "%s", title);
237     SetWTitle(onlysession->window, mactitle);
238 }
239
240 /*
241  * Resize the window at the emulator's request
242  */
243 void request_resize(int w, int h) {
244
245     /* XXX: Do something */
246 }
247
248 /*
249  * Set the logical palette
250  */
251 void palette_set(int n, int r, int g, int b) {
252
253     /* XXX: Do something */
254 }
255
256 /*
257  * Reset to the default palette
258  */
259 void palette_reset(void) {
260     int i;
261     struct mac_session *s = onlysession;
262
263     for (i = 0; i < 24; i++) {
264         s->palette[i].red   = s->defpal[i].red;
265         s->palette[i].green = s->defpal[i].green;
266         s->palette[i].blue  = s->defpal[i].blue;
267     }
268     term_invalidate();
269 }