Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /* PDCurses */
  2.  
  3. #include "pdcsdl.h"
  4.  
  5. #include <stdlib.h>
  6. #ifndef PDC_WIDE
  7. #include "../common/font437.h"
  8. #endif
  9. #include "../common/iconbmp.h"
  10.  
  11. #ifdef PDC_WIDE
  12. # ifndef PDC_FONT_PATH
  13. #  define PDC_FONT_PATH "/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf"
  14. # endif
  15. TTF_Font *pdc_ttffont = NULL;
  16. int pdc_font_size = 17;
  17. #endif
  18.  
  19. SDL_Surface *pdc_screen = NULL, *pdc_font = NULL, *pdc_icon = NULL,
  20.             *pdc_back = NULL, *pdc_tileback = NULL;
  21. int pdc_sheight = 0, pdc_swidth = 0, pdc_yoffset = 0, pdc_xoffset = 0;
  22.  
  23. SDL_Color pdc_color[PDC_MAXCOL];
  24. Uint32 pdc_mapped[PDC_MAXCOL];
  25. int pdc_fheight, pdc_fwidth, pdc_fthick, pdc_flastc;
  26. bool pdc_own_screen;
  27.  
  28. static int max_height, max_width;
  29.  
  30. static void _clean(void)
  31. {
  32. #ifdef PDC_WIDE
  33.     if (pdc_ttffont)
  34.     {
  35.         TTF_CloseFont(pdc_ttffont);
  36.         TTF_Quit();
  37.     }
  38. #endif
  39.     SDL_FreeSurface(pdc_tileback);
  40.     SDL_FreeSurface(pdc_back);
  41.     SDL_FreeSurface(pdc_icon);
  42.     SDL_FreeSurface(pdc_font);
  43.  
  44.     SDL_Quit();
  45. }
  46.  
  47. void PDC_retile(void)
  48. {
  49.     if (pdc_tileback)
  50.         SDL_FreeSurface(pdc_tileback);
  51.  
  52.     pdc_tileback = SDL_DisplayFormat(pdc_screen);
  53.     if (pdc_tileback == NULL)
  54.         return;
  55.  
  56.     if (pdc_back)
  57.     {
  58.         SDL_Rect dest;
  59.  
  60.         dest.y = 0;
  61.  
  62.         while (dest.y < pdc_tileback->h)
  63.         {
  64.             dest.x = 0;
  65.  
  66.             while (dest.x < pdc_tileback->w)
  67.             {
  68.                 SDL_BlitSurface(pdc_back, 0, pdc_tileback, &dest);
  69.                 dest.x += pdc_back->w;
  70.             }
  71.  
  72.             dest.y += pdc_back->h;
  73.         }
  74.  
  75.         SDL_BlitSurface(pdc_tileback, 0, pdc_screen, 0);
  76.     }
  77. }
  78.  
  79. void PDC_scr_close(void)
  80. {
  81.     PDC_LOG(("PDC_scr_close() - called\n"));
  82. }
  83.  
  84. void PDC_scr_free(void)
  85. {
  86. }
  87.  
  88. static void _initialize_colors(void)
  89. {
  90.     int i, r, g, b;
  91.  
  92.     for (i = 0; i < 8; i++)
  93.     {
  94.         pdc_color[i].r = (i & COLOR_RED) ? 0xc0 : 0;
  95.         pdc_color[i].g = (i & COLOR_GREEN) ? 0xc0 : 0;
  96.         pdc_color[i].b = (i & COLOR_BLUE) ? 0xc0 : 0;
  97.  
  98.         pdc_color[i + 8].r = (i & COLOR_RED) ? 0xff : 0x40;
  99.         pdc_color[i + 8].g = (i & COLOR_GREEN) ? 0xff : 0x40;
  100.         pdc_color[i + 8].b = (i & COLOR_BLUE) ? 0xff : 0x40;
  101.     }
  102.  
  103.     /* 256-color xterm extended palette: 216 colors in a 6x6x6 color
  104.        cube, plus 24 shades of gray */
  105.  
  106.     for (i = 16, r = 0; r < 6; r++)
  107.         for (g = 0; g < 6; g++)
  108.             for (b = 0; b < 6; b++, i++)
  109.             {
  110.                 pdc_color[i].r = (r ? r * 40 + 55 : 0);
  111.                 pdc_color[i].g = (g ? g * 40 + 55 : 0);
  112.                 pdc_color[i].b = (b ? b * 40 + 55 : 0);
  113.             }
  114.  
  115.     for (i = 232; i < 256; i++)
  116.         pdc_color[i].r = pdc_color[i].g = pdc_color[i].b = (i - 232) * 10 + 8;
  117.  
  118.     for (i = 0; i < 256; i++)
  119.         pdc_mapped[i] = SDL_MapRGB(pdc_screen->format, pdc_color[i].r,
  120.                                    pdc_color[i].g, pdc_color[i].b);
  121. }
  122.  
  123. /* open the physical screen -- miscellaneous initialization */
  124.  
  125. int PDC_scr_open(void)
  126. {
  127.     PDC_LOG(("PDC_scr_open() - called\n"));
  128.  
  129.     pdc_own_screen = !pdc_screen;
  130.  
  131.     if (pdc_own_screen)
  132.     {
  133.         if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) < 0)
  134.         {
  135.             fprintf(stderr, "Could not start SDL: %s\n", SDL_GetError());
  136.             return ERR;
  137.         }
  138.  
  139.         atexit(_clean);
  140.     }
  141.  
  142. #ifdef PDC_WIDE
  143.     if (!pdc_ttffont)
  144.     {
  145.         const char *ptsz, *fname;
  146.  
  147.         if (TTF_Init() == -1)
  148.         {
  149.             fprintf(stderr, "Could not start SDL_TTF: %s\n", SDL_GetError());
  150.             return ERR;
  151.         }
  152.  
  153.         ptsz = getenv("PDC_FONT_SIZE");
  154.         if (ptsz != NULL)
  155.            pdc_font_size = atoi(ptsz);
  156.         if (pdc_font_size <= 0)
  157.            pdc_font_size = 18;
  158.  
  159.         fname = getenv("PDC_FONT");
  160.         pdc_ttffont = TTF_OpenFont(fname ? fname : PDC_FONT_PATH,
  161.                                    pdc_font_size);
  162.     }
  163.  
  164.     if (!pdc_ttffont)
  165.     {
  166.         fprintf(stderr, "Could not load font\n");
  167.         return ERR;
  168.     }
  169.  
  170.     TTF_SetFontKerning(pdc_ttffont, 0);
  171.     TTF_SetFontHinting(pdc_ttffont, TTF_HINTING_MONO);
  172.  
  173.     SP->mono = FALSE;
  174. #else
  175.     if (!pdc_font)
  176.     {
  177.         const char *fname = getenv("PDC_FONT");
  178.         pdc_font = SDL_LoadBMP(fname ? fname : "pdcfont.bmp");
  179.     }
  180.  
  181.     if (!pdc_font)
  182.         pdc_font = SDL_LoadBMP_RW(SDL_RWFromMem(font437, sizeof(font437)), 0);
  183.  
  184.     if (!pdc_font)
  185.     {
  186.         fprintf(stderr, "Could not load font\n");
  187.         return ERR;
  188.     }
  189.  
  190.     SP->mono = !pdc_font->format->palette;
  191. #endif
  192.  
  193.     if (!SP->mono && !pdc_back)
  194.     {
  195.         const char *bname = getenv("PDC_BACKGROUND");
  196.         pdc_back = SDL_LoadBMP(bname ? bname : "pdcback.bmp");
  197.     }
  198.  
  199.     if (!SP->mono && (pdc_back || !pdc_own_screen))
  200.     {
  201.         SP->orig_attr = TRUE;
  202.         SP->orig_fore = COLOR_WHITE;
  203.         SP->orig_back = -1;
  204.     }
  205.     else
  206.         SP->orig_attr = FALSE;
  207.  
  208. #ifdef PDC_WIDE
  209.     TTF_SizeText(pdc_ttffont, "W", &pdc_fwidth, &pdc_fheight);
  210.     pdc_fthick = pdc_font_size / 20 + 1;
  211. #else
  212.     pdc_fheight = pdc_font->h / 8;
  213.     pdc_fwidth = pdc_font->w / 32;
  214.     pdc_fthick = 1;
  215.  
  216.     if (!SP->mono)
  217.         pdc_flastc = pdc_font->format->palette->ncolors - 1;
  218. #endif
  219.  
  220.     if (pdc_own_screen && !pdc_icon)
  221.     {
  222.         const char *iname = getenv("PDC_ICON");
  223.         pdc_icon = SDL_LoadBMP(iname ? iname : "pdcicon.bmp");
  224.  
  225.         if (!pdc_icon)
  226.             pdc_icon = SDL_LoadBMP_RW(SDL_RWFromMem(iconbmp,
  227.                                                     sizeof(iconbmp)), 0);
  228.  
  229.         if (pdc_icon)
  230.             SDL_WM_SetIcon(pdc_icon, NULL);
  231.     }
  232.  
  233.     if (pdc_own_screen)
  234.     {
  235.         const SDL_VideoInfo *info = SDL_GetVideoInfo();
  236.        // max_height = info->current_h;
  237.        // max_width = info->current_w;
  238.  
  239.         const char *env = getenv("PDC_LINES");
  240.         pdc_sheight = (env ? atoi(env) : 25) * pdc_fheight;
  241.  
  242.         env = getenv("PDC_COLS");
  243.         pdc_swidth = (env ? atoi(env) : 80) * pdc_fwidth;
  244.  
  245.         pdc_screen = SDL_SetVideoMode(pdc_swidth, pdc_sheight, 0,
  246.             SDL_SWSURFACE|SDL_ANYFORMAT|SDL_RESIZABLE);
  247.     }
  248.     else
  249.     {
  250.         if (!pdc_sheight)
  251.             pdc_sheight = pdc_screen->h - pdc_yoffset;
  252.  
  253.         if (!pdc_swidth)
  254.             pdc_swidth = pdc_screen->w - pdc_xoffset;
  255.     }
  256.  
  257.     if (!pdc_screen)
  258.     {
  259.         fprintf(stderr, "Couldn't create a surface: %s\n", SDL_GetError());
  260.         return ERR;
  261.     }
  262.  
  263.     if (SP->orig_attr)
  264.         PDC_retile();
  265.  
  266.     _initialize_colors();
  267.  
  268.     SDL_EnableUNICODE(1);
  269.  
  270.     PDC_mouse_set();
  271.  
  272.     if (pdc_own_screen)
  273.         PDC_set_title("PDCurses");
  274.  
  275.     SP->mouse_wait = PDC_CLICK_PERIOD;
  276.     SP->audible = FALSE;
  277.  
  278.     SP->termattrs = A_COLOR | A_UNDERLINE | A_LEFT | A_RIGHT | A_REVERSE;
  279. #ifdef PDC_WIDE
  280.     SP->termattrs |= A_ITALIC;
  281. #endif
  282.  
  283.     PDC_reset_prog_mode();
  284.  
  285.     return OK;
  286. }
  287.  
  288. /* the core of resize_term() */
  289.  
  290. int PDC_resize_screen(int nlines, int ncols)
  291. {
  292.     if (!pdc_own_screen)
  293.         return ERR;
  294.  
  295.     if (nlines && ncols)
  296.     {
  297.         while (nlines * pdc_fheight > max_height)
  298.             nlines--;
  299.         pdc_sheight = nlines * pdc_fheight;
  300.         while (ncols * pdc_fwidth > max_width)
  301.             ncols--;
  302.         pdc_swidth = ncols * pdc_fwidth;
  303.     }
  304.  
  305.     SDL_FreeSurface(pdc_screen);
  306.  
  307.     pdc_screen = SDL_SetVideoMode(pdc_swidth, pdc_sheight, 0,
  308.         SDL_SWSURFACE|SDL_ANYFORMAT|SDL_RESIZABLE);
  309.  
  310.     if (pdc_tileback)
  311.         PDC_retile();
  312.  
  313.     return OK;
  314. }
  315.  
  316. void PDC_reset_prog_mode(void)
  317. {
  318.     PDC_LOG(("PDC_reset_prog_mode() - called.\n"));
  319.  
  320.     PDC_flushinp();
  321.     SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
  322. }
  323.  
  324. void PDC_reset_shell_mode(void)
  325. {
  326.     PDC_LOG(("PDC_reset_shell_mode() - called.\n"));
  327.  
  328.     SDL_EnableKeyRepeat(0, 0);
  329.     PDC_flushinp();
  330. }
  331.  
  332. void PDC_restore_screen_mode(int i)
  333. {
  334. }
  335.  
  336. void PDC_save_screen_mode(int i)
  337. {
  338. }
  339.  
  340. bool PDC_can_change_color(void)
  341. {
  342.     return TRUE;
  343. }
  344.  
  345. int PDC_color_content(short color, short *red, short *green, short *blue)
  346. {
  347.     *red = DIVROUND(pdc_color[color].r * 1000, 255);
  348.     *green = DIVROUND(pdc_color[color].g * 1000, 255);
  349.     *blue = DIVROUND(pdc_color[color].b * 1000, 255);
  350.  
  351.     return OK;
  352. }
  353.  
  354. int PDC_init_color(short color, short red, short green, short blue)
  355. {
  356.     pdc_color[color].r = DIVROUND(red * 255, 1000);
  357.     pdc_color[color].g = DIVROUND(green * 255, 1000);
  358.     pdc_color[color].b = DIVROUND(blue * 255, 1000);
  359.  
  360.     pdc_mapped[color] = SDL_MapRGB(pdc_screen->format, pdc_color[color].r,
  361.                                    pdc_color[color].g, pdc_color[color].b);
  362.  
  363.     return OK;
  364. }
  365.