2 * macterm.c -- Macintosh terminal front-end
8 #include <MacWindows.h>
10 #include <Quickdraw.h>
11 #include <QuickdrawText.h>
24 PaletteHandle palette;
27 static void mac_initfont(struct mac_session *);
28 static void mac_initpalette(struct mac_session *);
30 /* Temporary hack till I get the terminal emulator supporting multiple sessions */
32 static struct mac_session *onlysession;
34 static void inbuf_putc(int c) {
35 inbuf[inbuf_head] = c;
36 inbuf_head = (inbuf_head+1) & INBUF_MASK;
39 static void inbuf_putstr(const char *c) {
44 void mac_newsession(void) {
45 struct mac_session *s;
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;
59 cfg.win_name_always = FALSE;
62 strcpy(cfg.font, "Monaco");
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");
73 /* XXX: Own storage management? */
74 if (mac_qdversion == gestaltOriginalQD)
75 s->window = GetNewWindow(wTerminal, NULL, (WindowPtr)-1);
77 s->window = GetNewCWindow(wTerminal, NULL, (WindowPtr)-1);
78 SetWRefCon(s->window, (long)s);
80 term_size(24, 80, 100);
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");
91 inbuf_putstr("\033[30mblack \033[31mred \033[32mgreen \033[33myellow "
92 "\033[34mblue \033[35mmagenta \033[36mcyan \033[37mwhite\015\012");
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");
97 inbuf_putstr("\033[37;44mwhite on blue \033[32;41mgreen on red\015\012");
102 static void mac_initfont(struct mac_session *s) {
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);
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);
119 static void mac_initpalette(struct mac_session *s) {
121 if (mac_qdversion == gestaltOriginalQD)
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);
130 * Call from the terminal emulator to draw a bit of text
132 * x and y are text row and column (zero-based)
134 void do_text(struct mac_session *s, int x, int y, char *text, int len,
135 unsigned long attr) {
137 int bgcolour, fgcolour;
138 RGBColor rgbfore, rgbback;
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))
151 TextFont(s->fontnum);
152 if (cfg.fontisbold || (attr & ATTR_BOLD) && !cfg.bold_colour)
154 if (attr & ATTR_UNDER)
157 TextSize(cfg.fontheight);
159 if (attr & ATTR_REVERSE) {
160 bgcolour = ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT) * 2;
161 fgcolour = ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT) * 2;
163 fgcolour = ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT) * 2;
164 bgcolour = ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT) * 2;
166 if ((attr & ATTR_BOLD) && cfg.bold_colour)
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);
176 /* Tell the window manager about it in case this isn't an update */
177 ValidRect(&textrect);
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.
185 struct mac_session *get_ctx(void) {
191 * Presumably this does something in Windows
193 void free_ctx(struct mac_session *ctx) {
198 * Set the scroll bar position
200 void set_sbar(int total, int start, int page) {
202 /* Do something once we actually have a scroll bar */
212 * XXX We should indicate the relevant window and/or use the
213 * Notification Manager
218 * Set icon string -- a no-op here (Windowshade?)
220 void set_icon(char *icon) {
225 * Set the window title
227 void set_title(char *title) {
229 struct mac_session *s = onlysession;
231 mactitle[0] = sprintf((char *)&mactitle[1], "%s", title);
232 SetWTitle(s->window, mactitle);
236 * Resize the window at the emulator's request
238 void request_resize(int w, int h) {
242 mac_initfont(onlysession);
246 * Set the logical palette
248 void palette_set(int n, int r, int g, int b) {
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,
257 col.red = r * 0x0101;
258 col.green = g * 0x0101;
259 col.blue = b * 0x0101;
260 SetEntryColor(s->palette, first[n], &col);
262 SetEntryColor(s->palette, first[n]+1, &col);
263 ActivatePalette(s->window);
267 * Reset to the default palette
269 void palette_reset(void) {
270 struct mac_session *s = onlysession;
272 if (mac_qdversion == gestaltOriginalQD)
274 CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
275 ActivatePalette(s->window);
276 /* Palette Manager will generate update events as required. */