soc/intel/xeon_sp/util: Enhance lock_pam0123
[coreboot2.git] / payloads / libpayload / curses / PDCurses / sdl1 / pdcscrn.c
blobe422b25cee84228cdd3c373d67d6e0734f0541a5
1 /* Public Domain Curses */
3 #include "pdcsdl.h"
5 RCSID("$Id: pdcscrn.c,v 1.34 2008/07/14 04:24:52 wmcbrine Exp $")
7 #include <stdlib.h>
8 #include "deffont.h"
9 #include "deficon.h"
11 SDL_Surface *pdc_screen = NULL, *pdc_font = NULL, *pdc_icon = NULL,
12 *pdc_back = NULL, *pdc_tileback = NULL;
13 int pdc_sheight = 0, pdc_swidth = 0, pdc_yoffset = 0, pdc_xoffset = 0;
15 SDL_Color pdc_color[16];
16 Uint32 pdc_mapped[16];
17 int pdc_fheight, pdc_fwidth, pdc_flastc;
18 bool pdc_own_screen;
20 /* COLOR_PAIR to attribute encoding table. */
22 static struct {short f, b;} atrtab[PDC_COLOR_PAIRS];
24 void PDC_retile(void)
26 if (pdc_tileback)
27 SDL_FreeSurface(pdc_tileback);
29 pdc_tileback = SDL_DisplayFormat(pdc_screen);
31 if (pdc_back)
33 SDL_Rect dest;
35 dest.y = 0;
37 while (dest.y < pdc_tileback->h)
39 dest.x = 0;
41 while (dest.x < pdc_tileback->w)
43 SDL_BlitSurface(pdc_back, 0, pdc_tileback, &dest);
44 dest.x += pdc_back->w;
47 dest.y += pdc_back->h;
50 SDL_BlitSurface(pdc_tileback, 0, pdc_screen, 0);
54 void PDC_scr_close(void)
56 PDC_LOG(("PDC_scr_close() - called\n"));
59 void PDC_scr_free(void)
61 if (SP)
62 free(SP);
65 /* open the physical screen -- allocate SP, miscellaneous intialization */
67 int PDC_scr_open(int argc, char **argv)
69 int i;
71 PDC_LOG(("PDC_scr_open() - called\n"));
73 SP = calloc(1, sizeof(SCREEN));
75 if (!SP)
76 return ERR;
78 pdc_own_screen = !pdc_screen;
80 if (pdc_own_screen)
82 if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) < 0)
84 fprintf(stderr, "Could not start SDL: %s\n", SDL_GetError());
85 return ERR;
88 atexit(SDL_Quit);
91 if (!pdc_font)
93 const char *fname = getenv("PDC_FONT");
94 pdc_font = SDL_LoadBMP(fname ? fname : "pdcfont.bmp");
97 if (!pdc_font)
98 pdc_font = SDL_LoadBMP_RW(SDL_RWFromMem(deffont, sizeof(deffont)), 0);
100 if (!pdc_font)
102 fprintf(stderr, "Could not load font\n");
103 return ERR;
106 SP->mono = !pdc_font->format->palette;
108 if (!SP->mono && !pdc_back)
110 const char *bname = getenv("PDC_BACKGROUND");
111 pdc_back = SDL_LoadBMP(bname ? bname : "pdcback.bmp");
114 if (!SP->mono && (pdc_back || !pdc_own_screen))
116 SP->orig_attr = TRUE;
117 SP->orig_fore = COLOR_WHITE;
118 SP->orig_back = -1;
120 else
121 SP->orig_attr = FALSE;
123 pdc_fheight = pdc_font->h / 8;
124 pdc_fwidth = pdc_font->w / 32;
126 if (!SP->mono)
127 pdc_flastc = pdc_font->format->palette->ncolors - 1;
129 if (pdc_own_screen && !pdc_icon)
131 const char *iname = getenv("PDC_ICON");
132 pdc_icon = SDL_LoadBMP(iname ? iname : "pdcicon.bmp");
134 if (!pdc_icon)
135 pdc_icon = SDL_LoadBMP_RW(SDL_RWFromMem(deficon,
136 sizeof(deficon)), 0);
138 if (pdc_icon)
139 SDL_WM_SetIcon(pdc_icon, NULL);
142 if (pdc_own_screen)
144 const char *env = getenv("PDC_LINES");
145 pdc_sheight = (env ? atoi(env) : 25) * pdc_fheight;
147 env = getenv("PDC_COLS");
148 pdc_swidth = (env ? atoi(env) : 80) * pdc_fwidth;
150 pdc_screen = SDL_SetVideoMode(pdc_swidth, pdc_sheight, 0,
151 SDL_SWSURFACE|SDL_ANYFORMAT|SDL_RESIZABLE);
153 else
155 if (!pdc_sheight)
156 pdc_sheight = pdc_screen->h - pdc_yoffset;
158 if (!pdc_swidth)
159 pdc_swidth = pdc_screen->w - pdc_xoffset;
162 if (!pdc_screen)
164 fprintf(stderr, "Couldn't create a surface: %s\n", SDL_GetError());
165 return ERR;
168 if (SP->orig_attr)
169 PDC_retile();
171 for (i = 0; i < 8; i++)
173 pdc_color[i].r = (i & COLOR_RED) ? 0xc0 : 0;
174 pdc_color[i].g = (i & COLOR_GREEN) ? 0xc0 : 0;
175 pdc_color[i].b = (i & COLOR_BLUE) ? 0xc0 : 0;
177 pdc_color[i + 8].r = (i & COLOR_RED) ? 0xff : 0x40;
178 pdc_color[i + 8].g = (i & COLOR_GREEN) ? 0xff : 0x40;
179 pdc_color[i + 8].b = (i & COLOR_BLUE) ? 0xff : 0x40;
182 for (i = 0; i < 16; i++)
183 pdc_mapped[i] = SDL_MapRGB(pdc_screen->format, pdc_color[i].r,
184 pdc_color[i].g, pdc_color[i].b);
186 SDL_EnableUNICODE(1);
188 PDC_mouse_set();
190 if (pdc_own_screen)
191 PDC_set_title(argc ? argv[0] : "PDCurses");
193 SP->lines = PDC_get_rows();
194 SP->cols = PDC_get_columns();
196 SP->mouse_wait = PDC_CLICK_PERIOD;
197 SP->audible = FALSE;
199 PDC_reset_prog_mode();
201 return OK;
204 /* the core of resize_term() */
206 int PDC_resize_screen(int nlines, int ncols)
208 if (!pdc_own_screen)
209 return ERR;
211 if (nlines && ncols)
213 pdc_sheight = nlines * pdc_fheight;
214 pdc_swidth = ncols * pdc_fwidth;
217 SDL_FreeSurface(pdc_screen);
219 pdc_screen = SDL_SetVideoMode(pdc_swidth, pdc_sheight, 0,
220 SDL_SWSURFACE|SDL_ANYFORMAT|SDL_RESIZABLE);
222 if (pdc_tileback)
223 PDC_retile();
225 SP->resized = FALSE;
226 SP->cursrow = SP->curscol = 0;
228 return OK;
231 void PDC_reset_prog_mode(void)
233 PDC_LOG(("PDC_reset_prog_mode() - called.\n"));
235 PDC_flushinp();
236 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
239 void PDC_reset_shell_mode(void)
241 PDC_LOG(("PDC_reset_shell_mode() - called.\n"));
243 SDL_EnableKeyRepeat(0, 0);
244 PDC_flushinp();
247 void PDC_restore_screen_mode(int i)
251 void PDC_save_screen_mode(int i)
255 void PDC_init_pair(short pair, short fg, short bg)
257 atrtab[pair].f = fg;
258 atrtab[pair].b = bg;
261 int PDC_pair_content(short pair, short *fg, short *bg)
263 *fg = atrtab[pair].f;
264 *bg = atrtab[pair].b;
266 return OK;
269 bool PDC_can_change_color(void)
271 return TRUE;
274 int PDC_color_content(short color, short *red, short *green, short *blue)
276 *red = DIVROUND(pdc_color[color].r * 1000, 255);
277 *green = DIVROUND(pdc_color[color].g * 1000, 255);
278 *blue = DIVROUND(pdc_color[color].b * 1000, 255);
280 return OK;
283 int PDC_init_color(short color, short red, short green, short blue)
285 pdc_color[color].r = DIVROUND(red * 255, 1000);
286 pdc_color[color].g = DIVROUND(green * 255, 1000);
287 pdc_color[color].b = DIVROUND(blue * 255, 1000);
289 pdc_mapped[color] = SDL_MapRGB(pdc_screen->format, pdc_color[color].r,
290 pdc_color[color].g, pdc_color[color].b);
292 wrefresh(curscr);
294 return OK;