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