slsmg.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  1. /* SLang Screen management routines */
  2. /* Copyright (c) 1992, 1999, 2001, 2002 John E. Davis
  3. * This file is part of the S-Lang library.
  4. *
  5. * You may distribute under the terms of either the GNU General Public
  6. * License or the Perl Artistic License.
  7. */
  8. #include "slinclud.h"
  9. #include "slang.h"
  10. #include "_slang.h"
  11. typedef struct Screen_Type
  12. {
  13. int n; /* number of chars written last time */
  14. int flags; /* line untouched, etc... */
  15. SLsmg_Char_Type *old, *neew;
  16. #ifndef IBMPC_SYSTEM
  17. unsigned long old_hash, new_hash;
  18. #endif
  19. }
  20. Screen_Type;
  21. #define TOUCHED 0x1
  22. #define TRASHED 0x2
  23. static int Screen_Trashed;
  24. #if !defined(__MSDOS_16BIT__)
  25. # define MAX_SCREEN_SIZE 256
  26. #else
  27. # define MAX_SCREEN_SIZE 75
  28. #endif
  29. Screen_Type SL_Screen[MAX_SCREEN_SIZE];
  30. static int Start_Col, Start_Row;
  31. static int Screen_Cols, Screen_Rows;
  32. static int This_Row, This_Col;
  33. static int This_Color; /* only the first 8 bits of this
  34. * are used. The highest bit is used
  35. * to indicate an alternate character
  36. * set. This leaves 127 userdefineable
  37. * color combination.
  38. */
  39. #ifndef IBMPC_SYSTEM
  40. #define ALT_CHAR_FLAG 0x80
  41. #else
  42. #define ALT_CHAR_FLAG 0x00
  43. #endif
  44. #if SLTT_HAS_NON_BCE_SUPPORT && !defined(IBMPC_SYSTEM)
  45. #define REQUIRES_NON_BCE_SUPPORT 1
  46. static int Bce_Color_Offset;
  47. #endif
  48. int SLsmg_Newline_Behavior = 0;
  49. int SLsmg_Backspace_Moves = 0;
  50. /* Backward compatibility. Not used. */
  51. /* int SLsmg_Newline_Moves; */
  52. static void (*tt_normal_video)(void) = SLtt_normal_video;
  53. static void (*tt_goto_rc)(int, int) = SLtt_goto_rc;
  54. static void (*tt_cls) (void) = SLtt_cls;
  55. static void (*tt_del_eol) (void) = SLtt_del_eol;
  56. static void (*tt_smart_puts) (SLsmg_Char_Type *, SLsmg_Char_Type *, int, int) = SLtt_smart_puts;
  57. static int (*tt_flush_output) (void) = SLtt_flush_output;
  58. static int (*tt_reset_video) (void) = SLtt_reset_video;
  59. static int (*tt_init_video) (void) = SLtt_init_video;
  60. static int *tt_Screen_Rows = &SLtt_Screen_Rows;
  61. static int *tt_Screen_Cols = &SLtt_Screen_Cols;
  62. #ifndef IBMPC_SYSTEM
  63. static void (*tt_set_scroll_region)(int, int) = SLtt_set_scroll_region;
  64. static void (*tt_reverse_index)(int) = SLtt_reverse_index;
  65. static void (*tt_reset_scroll_region)(void) = SLtt_reset_scroll_region;
  66. static void (*tt_delete_nlines)(int) = SLtt_delete_nlines;
  67. #endif
  68. #ifndef IBMPC_SYSTEM
  69. static int *tt_Term_Cannot_Scroll = &SLtt_Term_Cannot_Scroll;
  70. static int *tt_Has_Alt_Charset = &SLtt_Has_Alt_Charset;
  71. static char **tt_Graphics_Char_Pairs = &SLtt_Graphics_Char_Pairs;
  72. static int *tt_Use_Blink_For_ACS = &SLtt_Use_Blink_For_ACS;
  73. #endif
  74. static int Smg_Inited;
  75. static void blank_line (SLsmg_Char_Type *p, int n, unsigned char ch)
  76. {
  77. register SLsmg_Char_Type *pmax = p + n;
  78. register SLsmg_Char_Type color_ch;
  79. color_ch = SLSMG_BUILD_CHAR(ch,This_Color);
  80. while (p < pmax)
  81. {
  82. *p++ = color_ch;
  83. }
  84. }
  85. static void clear_region (int row, int n)
  86. {
  87. int i;
  88. int imax = row + n;
  89. if (imax > Screen_Rows) imax = Screen_Rows;
  90. for (i = row; i < imax; i++)
  91. {
  92. if (i >= 0)
  93. {
  94. blank_line (SL_Screen[i].neew, Screen_Cols, ' ');
  95. SL_Screen[i].flags |= TOUCHED;
  96. }
  97. }
  98. }
  99. void SLsmg_erase_eol (void)
  100. {
  101. int r, c;
  102. if (Smg_Inited == 0) return;
  103. c = This_Col - Start_Col;
  104. r = This_Row - Start_Row;
  105. if ((r < 0) || (r >= Screen_Rows)) return;
  106. if (c < 0) c = 0; else if (c >= Screen_Cols) return;
  107. blank_line (SL_Screen[This_Row].neew + c , Screen_Cols - c, ' ');
  108. SL_Screen[This_Row].flags |= TOUCHED;
  109. }
  110. static void scroll_up (void)
  111. {
  112. unsigned int i, imax;
  113. SLsmg_Char_Type *neew;
  114. neew = SL_Screen[0].neew;
  115. imax = Screen_Rows - 1;
  116. for (i = 0; i < imax; i++)
  117. {
  118. SL_Screen[i].neew = SL_Screen[i + 1].neew;
  119. SL_Screen[i].flags |= TOUCHED;
  120. }
  121. SL_Screen[i].neew = neew;
  122. SL_Screen[i].flags |= TOUCHED;
  123. blank_line (neew, Screen_Cols, ' ');
  124. This_Row--;
  125. }
  126. void SLsmg_gotorc (int r, int c)
  127. {
  128. This_Row = r;
  129. This_Col = c;
  130. }
  131. int SLsmg_get_row (void)
  132. {
  133. return This_Row;
  134. }
  135. int SLsmg_get_column (void)
  136. {
  137. return This_Col;
  138. }
  139. void SLsmg_erase_eos (void)
  140. {
  141. if (Smg_Inited == 0) return;
  142. SLsmg_erase_eol ();
  143. clear_region (This_Row + 1, Screen_Rows);
  144. }
  145. static int This_Alt_Char;
  146. void SLsmg_set_char_set (int i)
  147. {
  148. #ifdef IBMPC_SYSTEM
  149. (void) i;
  150. #else
  151. if ((tt_Use_Blink_For_ACS != NULL)
  152. && (*tt_Use_Blink_For_ACS != 0))
  153. return;/* alt chars not used and the alt bit
  154. * is used to indicate a blink.
  155. */
  156. if (i) This_Alt_Char = ALT_CHAR_FLAG;
  157. else This_Alt_Char = 0;
  158. This_Color &= 0x7F;
  159. This_Color |= This_Alt_Char;
  160. #endif
  161. }
  162. void SLsmg_set_color (int color)
  163. {
  164. if (color < 0) return;
  165. #ifdef REQUIRES_NON_BCE_SUPPORT
  166. color += Bce_Color_Offset;
  167. #endif
  168. This_Color = color | This_Alt_Char;
  169. }
  170. void SLsmg_reverse_video (void)
  171. {
  172. SLsmg_set_color (1);
  173. }
  174. void SLsmg_normal_video (void)
  175. {
  176. SLsmg_set_color (0);
  177. }
  178. static int point_visible (int col_too)
  179. {
  180. return ((This_Row >= Start_Row) && (This_Row < Start_Row + Screen_Rows)
  181. && ((col_too == 0)
  182. || ((This_Col >= Start_Col)
  183. && (This_Col < Start_Col + Screen_Cols))));
  184. }
  185. void SLsmg_write_string (char *str)
  186. {
  187. SLsmg_write_nchars (str, strlen (str));
  188. }
  189. void SLsmg_write_nstring (char *str, unsigned int n)
  190. {
  191. unsigned int width;
  192. char blank = ' ';
  193. /* Avoid a problem if a user accidently passes a negative value */
  194. if ((int) n < 0)
  195. return;
  196. if (str == NULL) width = 0;
  197. else
  198. {
  199. width = strlen (str);
  200. if (width > n) width = n;
  201. SLsmg_write_nchars (str, width);
  202. }
  203. while (width++ < n) SLsmg_write_nchars (&blank, 1);
  204. }
  205. void SLsmg_write_wrapped_string (char *s, int r, int c,
  206. unsigned int dr, unsigned int dc,
  207. int fill)
  208. {
  209. register char ch, *p;
  210. int maxc = (int) dc;
  211. if ((dr == 0) || (dc == 0)) return;
  212. p = s;
  213. dc = 0;
  214. while (1)
  215. {
  216. ch = *p++;
  217. if ((ch == 0) || (ch == '\n'))
  218. {
  219. int diff;
  220. diff = maxc - (int) dc;
  221. SLsmg_gotorc (r, c);
  222. SLsmg_write_nchars (s, dc);
  223. if (fill && (diff > 0))
  224. {
  225. while (diff--) SLsmg_write_char (' ');
  226. }
  227. if ((ch == 0) || (dr == 1)) break;
  228. r++;
  229. dc = 0;
  230. dr--;
  231. s = p;
  232. }
  233. else if ((int) dc == maxc)
  234. {
  235. SLsmg_gotorc (r, c);
  236. SLsmg_write_nchars (s, dc + 1);
  237. if (dr == 1) break;
  238. r++;
  239. dc = 0;
  240. dr--;
  241. s = p;
  242. }
  243. else dc++;
  244. }
  245. }
  246. int SLsmg_Tab_Width = 8;
  247. /* Minimum value for which eight bit char is displayed as is. */
  248. #ifndef IBMPC_SYSTEM
  249. int SLsmg_Display_Eight_Bit = 160;
  250. static unsigned char Alt_Char_Set[129];/* 129th is used as a flag */
  251. #else
  252. int SLsmg_Display_Eight_Bit = 128;
  253. #endif
  254. void SLsmg_write_nchars (char *str, unsigned int n)
  255. {
  256. register SLsmg_Char_Type *p, old, neew, color;
  257. unsigned char ch;
  258. unsigned int flags;
  259. int len, start_len, max_len;
  260. char *str_max;
  261. int newline_flag;
  262. #ifndef IBMPC_SYSTEM
  263. int alt_char_set_flag;
  264. alt_char_set_flag = ((This_Color & ALT_CHAR_FLAG)
  265. && ((tt_Use_Blink_For_ACS == NULL)
  266. || (*tt_Use_Blink_For_ACS == 0)));
  267. #endif
  268. if (Smg_Inited == 0) return;
  269. str_max = str + n;
  270. color = This_Color;
  271. top: /* get here only on newline */
  272. newline_flag = 0;
  273. start_len = Start_Col;
  274. if (point_visible (0) == 0) return;
  275. len = This_Col;
  276. max_len = start_len + Screen_Cols;
  277. p = SL_Screen[This_Row - Start_Row].neew;
  278. if (len > start_len) p += (len - start_len);
  279. flags = SL_Screen[This_Row - Start_Row].flags;
  280. while ((len < max_len) && (str < str_max))
  281. {
  282. ch = (unsigned char) *str++;
  283. #ifndef IBMPC_SYSTEM
  284. if (alt_char_set_flag)
  285. ch = Alt_Char_Set [ch & 0x7F];
  286. #endif
  287. if (((ch >= ' ') && (ch < 127))
  288. || (ch >= (unsigned char) SLsmg_Display_Eight_Bit)
  289. #ifndef IBMPC_SYSTEM
  290. || alt_char_set_flag
  291. #endif
  292. )
  293. {
  294. len += 1;
  295. if (len > start_len)
  296. {
  297. old = *p;
  298. neew = SLSMG_BUILD_CHAR(ch,color);
  299. if (old != neew)
  300. {
  301. flags |= TOUCHED;
  302. *p = neew;
  303. }
  304. p++;
  305. }
  306. }
  307. else if ((ch == '\t') && (SLsmg_Tab_Width > 0))
  308. {
  309. n = len;
  310. n += SLsmg_Tab_Width;
  311. n = SLsmg_Tab_Width - (n % SLsmg_Tab_Width);
  312. if ((unsigned int) len + n > (unsigned int) max_len)
  313. n = (unsigned int) (max_len - len);
  314. neew = SLSMG_BUILD_CHAR(' ',color);
  315. while (n--)
  316. {
  317. len += 1;
  318. if (len > start_len)
  319. {
  320. if (*p != neew)
  321. {
  322. flags |= TOUCHED;
  323. *p = neew;
  324. }
  325. p++;
  326. }
  327. }
  328. }
  329. else if ((ch == '\n')
  330. && (SLsmg_Newline_Behavior != SLSMG_NEWLINE_PRINTABLE))
  331. {
  332. newline_flag = 1;
  333. break;
  334. }
  335. else if ((ch == 0x8) && SLsmg_Backspace_Moves)
  336. {
  337. if (len != 0) len--;
  338. }
  339. else
  340. {
  341. if (ch & 0x80)
  342. {
  343. neew = SLSMG_BUILD_CHAR('~',color);
  344. len += 1;
  345. if (len > start_len)
  346. {
  347. if (*p != neew)
  348. {
  349. *p = neew;
  350. flags |= TOUCHED;
  351. }
  352. p++;
  353. if (len == max_len) break;
  354. ch &= 0x7F;
  355. }
  356. }
  357. len += 1;
  358. if (len > start_len)
  359. {
  360. neew = SLSMG_BUILD_CHAR('^',color);
  361. if (*p != neew)
  362. {
  363. *p = neew;
  364. flags |= TOUCHED;
  365. }
  366. p++;
  367. if (len == max_len) break;
  368. }
  369. if (ch == 127) ch = '?'; else ch = ch + '@';
  370. len++;
  371. if (len > start_len)
  372. {
  373. neew = SLSMG_BUILD_CHAR(ch,color);
  374. if (*p != neew)
  375. {
  376. *p = neew;
  377. flags |= TOUCHED;
  378. }
  379. p++;
  380. }
  381. }
  382. }
  383. SL_Screen[This_Row - Start_Row].flags = flags;
  384. This_Col = len;
  385. if (SLsmg_Newline_Behavior == 0)
  386. return;
  387. if (newline_flag == 0)
  388. {
  389. while (str < str_max)
  390. {
  391. if (*str == '\n') break;
  392. str++;
  393. }
  394. if (str == str_max) return;
  395. str++;
  396. }
  397. This_Row++;
  398. This_Col = 0;
  399. if (This_Row == Start_Row + Screen_Rows)
  400. {
  401. if (SLsmg_Newline_Behavior == SLSMG_NEWLINE_SCROLLS) scroll_up ();
  402. }
  403. goto top;
  404. }
  405. void SLsmg_write_char (char ch)
  406. {
  407. SLsmg_write_nchars (&ch, 1);
  408. }
  409. static int Cls_Flag;
  410. void SLsmg_cls (void)
  411. {
  412. int tac;
  413. if (Smg_Inited == 0) return;
  414. tac = This_Alt_Char; This_Alt_Char = 0;
  415. SLsmg_set_color (0);
  416. clear_region (0, Screen_Rows);
  417. This_Alt_Char = tac;
  418. SLsmg_set_color (0);
  419. Cls_Flag = 1;
  420. }
  421. #if 0
  422. static void do_copy (SLsmg_Char_Type *a, SLsmg_Char_Type *b)
  423. {
  424. SLsmg_Char_Type *amax = a + Screen_Cols;
  425. while (a < amax) *a++ = *b++;
  426. }
  427. #endif
  428. #ifndef IBMPC_SYSTEM
  429. int SLsmg_Scroll_Hash_Border = 0;
  430. static unsigned long compute_hash (SLsmg_Char_Type *s, int n)
  431. {
  432. register unsigned long h = 0, g;
  433. register unsigned long sum = 0;
  434. register SLsmg_Char_Type *smax, ch;
  435. int is_blank = 2;
  436. s += SLsmg_Scroll_Hash_Border;
  437. smax = s + (n - SLsmg_Scroll_Hash_Border);
  438. while (s < smax)
  439. {
  440. ch = *s++;
  441. if (is_blank && (SLSMG_EXTRACT_CHAR(ch) != 32)) is_blank--;
  442. sum += ch;
  443. h = sum + (h << 3);
  444. if ((g = h & 0xE0000000UL) != 0)
  445. {
  446. h = h ^ (g >> 24);
  447. h = h ^ g;
  448. }
  449. }
  450. if (is_blank) return 0;
  451. return h;
  452. }
  453. static unsigned long Blank_Hash;
  454. static int try_scroll_down (int rmin, int rmax)
  455. {
  456. int i, r1, r2, di, j;
  457. unsigned long hash;
  458. int did_scroll;
  459. int color;
  460. SLsmg_Char_Type *tmp;
  461. int ignore;
  462. did_scroll = 0;
  463. for (i = rmax; i > rmin; i--)
  464. {
  465. hash = SL_Screen[i].new_hash;
  466. if (hash == Blank_Hash) continue;
  467. if ((hash == SL_Screen[i].old_hash)
  468. #if 0
  469. || ((i + 1 < Screen_Rows) && (hash == SL_Screen[i + 1].old_hash))
  470. || ((i - 1 > rmin) && (SL_Screen[i].old_hash == SL_Screen[i - 1].new_hash))
  471. #endif
  472. )
  473. continue;
  474. for (j = i - 1; j >= rmin; j--)
  475. {
  476. if (hash == SL_Screen[j].old_hash) break;
  477. }
  478. if (j < rmin) continue;
  479. r2 = i; /* end scroll region */
  480. di = i - j;
  481. j--;
  482. ignore = 0;
  483. while ((j >= rmin) && (SL_Screen[j].old_hash == SL_Screen[j + di].new_hash))
  484. {
  485. if (SL_Screen[j].old_hash == Blank_Hash) ignore++;
  486. j--;
  487. }
  488. r1 = j + 1;
  489. /* If this scroll only scrolls this line into place, don't do it.
  490. */
  491. if ((di > 1) && (r1 + di + ignore == r2)) continue;
  492. /* If there is anything in the scrolling region that is ok, abort the
  493. * scroll.
  494. */
  495. for (j = r1; j <= r2; j++)
  496. {
  497. if ((SL_Screen[j].old_hash != Blank_Hash)
  498. && (SL_Screen[j].old_hash == SL_Screen[j].new_hash))
  499. {
  500. /* See if the scroll is happens to scroll this one into place. */
  501. if ((j + di > r2) || (SL_Screen[j].old_hash != SL_Screen[j + di].new_hash))
  502. break;
  503. }
  504. }
  505. if (j <= r2) continue;
  506. color = This_Color; This_Color = 0;
  507. did_scroll = 1;
  508. (*tt_normal_video) ();
  509. (*tt_set_scroll_region) (r1, r2);
  510. (*tt_goto_rc) (0, 0);
  511. (*tt_reverse_index) (di);
  512. (*tt_reset_scroll_region) ();
  513. /* Now we have a hole in the screen.
  514. * Make the virtual screen look like it.
  515. *
  516. * Note that if the terminal does not support BCE, then we have
  517. * no idea what color the hole is. So, for this case, we do not
  518. * want to add Bce_Color_Offset to This_Color since if Bce_Color_Offset
  519. * is non-zero, then This_Color = 0 does not match any valid color
  520. * obtained by adding Bce_Color_Offset.
  521. */
  522. for (j = r1; j <= r2; j++) SL_Screen[j].flags = TOUCHED;
  523. while (di--)
  524. {
  525. tmp = SL_Screen[r2].old;
  526. for (j = r2; j > r1; j--)
  527. {
  528. SL_Screen[j].old = SL_Screen[j - 1].old;
  529. SL_Screen[j].old_hash = SL_Screen[j - 1].old_hash;
  530. }
  531. SL_Screen[r1].old = tmp;
  532. blank_line (SL_Screen[r1].old, Screen_Cols, ' ');
  533. SL_Screen[r1].old_hash = Blank_Hash;
  534. r1++;
  535. }
  536. This_Color = color;
  537. }
  538. return did_scroll;
  539. }
  540. static int try_scroll_up (int rmin, int rmax)
  541. {
  542. int i, r1, r2, di, j;
  543. unsigned long hash;
  544. int did_scroll;
  545. int color;
  546. SLsmg_Char_Type *tmp;
  547. int ignore;
  548. did_scroll = 0;
  549. for (i = rmin; i < rmax; i++)
  550. {
  551. hash = SL_Screen[i].new_hash;
  552. if (hash == Blank_Hash) continue;
  553. if (hash == SL_Screen[i].old_hash)
  554. continue;
  555. /* find a match further down screen */
  556. for (j = i + 1; j <= rmax; j++)
  557. {
  558. if (hash == SL_Screen[j].old_hash) break;
  559. }
  560. if (j > rmax) continue;
  561. r1 = i; /* beg scroll region */
  562. di = j - i; /* number of lines to scroll */
  563. j++; /* since we know this is a match */
  564. /* find end of scroll region */
  565. ignore = 0;
  566. while ((j <= rmax) && (SL_Screen[j].old_hash == SL_Screen[j - di].new_hash))
  567. {
  568. if (SL_Screen[j].old_hash == Blank_Hash) ignore++;
  569. j++;
  570. }
  571. r2 = j - 1; /* end of scroll region */
  572. /* If this scroll only scrolls this line into place, don't do it.
  573. */
  574. if ((di > 1) && (r1 + di + ignore == r2)) continue;
  575. /* If there is anything in the scrolling region that is ok, abort the
  576. * scroll.
  577. */
  578. for (j = r1; j <= r2; j++)
  579. {
  580. if ((SL_Screen[j].old_hash != Blank_Hash)
  581. && (SL_Screen[j].old_hash == SL_Screen[j].new_hash))
  582. {
  583. if ((j - di < r1) || (SL_Screen[j].old_hash != SL_Screen[j - di].new_hash))
  584. break;
  585. }
  586. }
  587. if (j <= r2) continue;
  588. did_scroll = 1;
  589. /* See the above comments about BCE */
  590. color = This_Color; This_Color = 0;
  591. (*tt_normal_video) ();
  592. (*tt_set_scroll_region) (r1, r2);
  593. (*tt_goto_rc) (0, 0); /* relative to scroll region */
  594. (*tt_delete_nlines) (di);
  595. (*tt_reset_scroll_region) ();
  596. /* Now we have a hole in the screen. Make the virtual screen look
  597. * like it.
  598. */
  599. for (j = r1; j <= r2; j++) SL_Screen[j].flags = TOUCHED;
  600. while (di--)
  601. {
  602. tmp = SL_Screen[r1].old;
  603. for (j = r1; j < r2; j++)
  604. {
  605. SL_Screen[j].old = SL_Screen[j + 1].old;
  606. SL_Screen[j].old_hash = SL_Screen[j + 1].old_hash;
  607. }
  608. SL_Screen[r2].old = tmp;
  609. blank_line (SL_Screen[r2].old, Screen_Cols, ' ');
  610. SL_Screen[r2].old_hash = Blank_Hash;
  611. r2--;
  612. }
  613. This_Color = color;
  614. }
  615. return did_scroll;
  616. }
  617. static void try_scroll (void)
  618. {
  619. int r1, rmin, rmax;
  620. int num_up, num_down;
  621. /* find region limits. */
  622. for (rmax = Screen_Rows - 1; rmax > 0; rmax--)
  623. {
  624. if (SL_Screen[rmax].new_hash != SL_Screen[rmax].old_hash)
  625. {
  626. r1 = rmax - 1;
  627. if ((r1 == 0)
  628. || (SL_Screen[r1].new_hash != SL_Screen[r1].old_hash))
  629. break;
  630. rmax = r1;
  631. }
  632. }
  633. for (rmin = 0; rmin < rmax; rmin++)
  634. {
  635. if (SL_Screen[rmin].new_hash != SL_Screen[rmin].old_hash)
  636. {
  637. r1 = rmin + 1;
  638. if ((r1 == rmax)
  639. || (SL_Screen[r1].new_hash != SL_Screen[r1].old_hash))
  640. break;
  641. rmin = r1;
  642. }
  643. }
  644. /* Below, we have two scrolling algorithms. The first has the effect of
  645. * scrolling lines down. This is usually appropriate when one moves
  646. * up the display, e.g., with the UP arrow. The second algorithm is
  647. * appropriate for going the other way. It is important to choose the
  648. * correct one.
  649. */
  650. num_up = 0;
  651. for (r1 = rmin; r1 < rmax; r1++)
  652. {
  653. if (SL_Screen[r1].new_hash == SL_Screen[r1 + 1].old_hash)
  654. num_up++;
  655. }
  656. num_down = 0;
  657. for (r1 = rmax; r1 > rmin; r1--)
  658. {
  659. if (SL_Screen[r1 - 1].old_hash == SL_Screen[r1].new_hash)
  660. num_down++;
  661. }
  662. if (num_up > num_down)
  663. {
  664. if (try_scroll_up (rmin, rmax))
  665. return;
  666. (void) try_scroll_down (rmin, rmax);
  667. }
  668. else
  669. {
  670. if (try_scroll_down (rmin, rmax))
  671. return;
  672. (void) try_scroll_up (rmin, rmax);
  673. }
  674. }
  675. #endif /* NOT IBMPC_SYSTEM */
  676. #ifdef REQUIRES_NON_BCE_SUPPORT
  677. static void adjust_colors (void)
  678. {
  679. int bce;
  680. int i;
  681. bce = Bce_Color_Offset;
  682. Bce_Color_Offset = _SLtt_get_bce_color_offset ();
  683. if (bce == Bce_Color_Offset)
  684. return;
  685. if ((tt_Use_Blink_For_ACS != NULL)
  686. && (*tt_Use_Blink_For_ACS != 0))
  687. return; /* this mode does not support non-BCE
  688. * terminals.
  689. */
  690. for (i = 0; i < Screen_Rows; i++)
  691. {
  692. SLsmg_Char_Type *s, *smax;
  693. SL_Screen[i].flags |= TRASHED;
  694. s = SL_Screen[i].neew;
  695. smax = s + Screen_Cols;
  696. while (s < smax)
  697. {
  698. int color = (int) SLSMG_EXTRACT_COLOR(*s);
  699. int acs;
  700. if (color < 0)
  701. {
  702. s++;
  703. continue;
  704. }
  705. acs = color & 0x80;
  706. color = (color & 0x7F) - bce;
  707. color += Bce_Color_Offset;
  708. if (color >= 0)
  709. {
  710. unsigned char ch = SLSMG_EXTRACT_CHAR(*s);
  711. *s = SLSMG_BUILD_CHAR(ch, ((color&0x7F)|acs));
  712. }
  713. s++;
  714. }
  715. }
  716. }
  717. #endif
  718. void SLsmg_refresh (void)
  719. {
  720. int i;
  721. #ifndef IBMPC_SYSTEM
  722. int trashed = 0;
  723. #endif
  724. if (Smg_Inited == 0) return;
  725. if (Screen_Trashed)
  726. {
  727. Cls_Flag = 1;
  728. for (i = 0; i < Screen_Rows; i++)
  729. SL_Screen[i].flags |= TRASHED;
  730. #ifdef REQUIRES_NON_BCE_SUPPORT
  731. adjust_colors ();
  732. #endif
  733. }
  734. #ifndef IBMPC_SYSTEM
  735. for (i = 0; i < Screen_Rows; i++)
  736. {
  737. if (SL_Screen[i].flags == 0) continue;
  738. SL_Screen[i].new_hash = compute_hash (SL_Screen[i].neew, Screen_Cols);
  739. trashed = 1;
  740. }
  741. #endif
  742. if (Cls_Flag)
  743. {
  744. (*tt_normal_video) (); (*tt_cls) ();
  745. }
  746. #ifndef IBMPC_SYSTEM
  747. else if (trashed && (*tt_Term_Cannot_Scroll == 0)) try_scroll ();
  748. #endif
  749. for (i = 0; i < Screen_Rows; i++)
  750. {
  751. if (SL_Screen[i].flags == 0) continue;
  752. if (Cls_Flag || SL_Screen[i].flags & TRASHED)
  753. {
  754. int color = This_Color;
  755. if (Cls_Flag == 0)
  756. {
  757. (*tt_goto_rc) (i, 0);
  758. (*tt_del_eol) ();
  759. }
  760. This_Color = 0;
  761. blank_line (SL_Screen[i].old, Screen_Cols, ' ');
  762. This_Color = color;
  763. }
  764. SL_Screen[i].old[Screen_Cols] = 0;
  765. SL_Screen[i].neew[Screen_Cols] = 0;
  766. (*tt_smart_puts) (SL_Screen[i].neew, SL_Screen[i].old, Screen_Cols, i);
  767. SLMEMCPY ((char *) SL_Screen[i].old, (char *) SL_Screen[i].neew,
  768. Screen_Cols * sizeof (SLsmg_Char_Type));
  769. SL_Screen[i].flags = 0;
  770. #ifndef IBMPC_SYSTEM
  771. SL_Screen[i].old_hash = SL_Screen[i].new_hash;
  772. #endif
  773. }
  774. if (point_visible (1)) (*tt_goto_rc) (This_Row - Start_Row, This_Col - Start_Col);
  775. (*tt_flush_output) ();
  776. Cls_Flag = 0;
  777. Screen_Trashed = 0;
  778. }
  779. static int compute_clip (int row, int n, int box_start, int box_end,
  780. int *rmin, int *rmax)
  781. {
  782. int row_max;
  783. if (n < 0) return 0;
  784. if (row >= box_end) return 0;
  785. row_max = row + n;
  786. if (row_max <= box_start) return 0;
  787. if (row < box_start) row = box_start;
  788. if (row_max >= box_end) row_max = box_end;
  789. *rmin = row;
  790. *rmax = row_max;
  791. return 1;
  792. }
  793. void SLsmg_touch_lines (int row, unsigned int n)
  794. {
  795. int i;
  796. int r1, r2;
  797. /* Allow this function to be called even when we are not initialied.
  798. * Calling this function is useful after calling SLtt_set_color
  799. * to force the display to be redrawn
  800. */
  801. if (Smg_Inited == 0)
  802. return;
  803. if (0 == compute_clip (row, (int) n, Start_Row, Start_Row + Screen_Rows, &r1, &r2))
  804. return;
  805. r1 -= Start_Row;
  806. r2 -= Start_Row;
  807. for (i = r1; i < r2; i++)
  808. {
  809. SL_Screen[i].flags |= TRASHED;
  810. }
  811. }
  812. void SLsmg_touch_screen (void)
  813. {
  814. Screen_Trashed = 1;
  815. }
  816. #ifndef IBMPC_SYSTEM
  817. static const char Fake_Alt_Char_Pairs [] = "a:j+k+l+m+q-t+u+v+w+x|n+`+f\\g#~o,<+>.v-^h#0#";
  818. static void init_alt_char_set (void)
  819. {
  820. int i;
  821. unsigned const char *p, *pmax;
  822. unsigned char ch;
  823. if (Alt_Char_Set[128] == 128) return;
  824. i = 32;
  825. memset ((char *)Alt_Char_Set, ' ', i);
  826. while (i <= 128)
  827. {
  828. Alt_Char_Set [i] = i;
  829. i++;
  830. }
  831. /* Map to VT100 */
  832. if (*tt_Has_Alt_Charset)
  833. {
  834. if (tt_Graphics_Char_Pairs == NULL) p = NULL;
  835. else p = (unsigned char *) *tt_Graphics_Char_Pairs;
  836. if (p == NULL) return;
  837. }
  838. else p = (unsigned char *) Fake_Alt_Char_Pairs;
  839. pmax = p + strlen ((char *) p);
  840. /* Some systems have messed up entries for this */
  841. while (p < pmax)
  842. {
  843. ch = *p++;
  844. ch &= 0x7F; /* should be unnecessary */
  845. Alt_Char_Set [ch] = *p;
  846. p++;
  847. }
  848. }
  849. #endif
  850. #ifndef IBMPC_SYSTEM
  851. # define BLOCK_SIGNALS SLsig_block_signals ()
  852. # define UNBLOCK_SIGNALS SLsig_unblock_signals ()
  853. #else
  854. # define BLOCK_SIGNALS (void)0
  855. # define UNBLOCK_SIGNALS (void)0
  856. #endif
  857. static int Smg_Suspended;
  858. int SLsmg_suspend_smg (void)
  859. {
  860. BLOCK_SIGNALS;
  861. if (Smg_Suspended == 0)
  862. {
  863. (*tt_reset_video) ();
  864. Smg_Suspended = 1;
  865. }
  866. UNBLOCK_SIGNALS;
  867. return 0;
  868. }
  869. int SLsmg_resume_smg (void)
  870. {
  871. BLOCK_SIGNALS;
  872. if (Smg_Suspended == 0)
  873. {
  874. UNBLOCK_SIGNALS;
  875. return 0;
  876. }
  877. Smg_Suspended = 0;
  878. if (-1 == (*tt_init_video) ())
  879. {
  880. UNBLOCK_SIGNALS;
  881. return -1;
  882. }
  883. Cls_Flag = 1;
  884. SLsmg_touch_screen ();
  885. SLsmg_refresh ();
  886. UNBLOCK_SIGNALS;
  887. return 0;
  888. }
  889. static void reset_smg (void)
  890. {
  891. int i;
  892. if (Smg_Inited == 0)
  893. return;
  894. for (i = 0; i < Screen_Rows; i++)
  895. {
  896. SLfree ((char *)SL_Screen[i].old);
  897. SLfree ((char *)SL_Screen[i].neew);
  898. SL_Screen[i].old = SL_Screen[i].neew = NULL;
  899. }
  900. This_Alt_Char = This_Color = 0;
  901. Smg_Inited = 0;
  902. }
  903. static int init_smg (void)
  904. {
  905. int i, len;
  906. SLsmg_Char_Type *old, *neew;
  907. Smg_Inited = 0;
  908. #ifdef REQUIRES_NON_BCE_SUPPORT
  909. Bce_Color_Offset = _SLtt_get_bce_color_offset ();
  910. #endif
  911. Screen_Rows = *tt_Screen_Rows;
  912. if (Screen_Rows > MAX_SCREEN_SIZE)
  913. Screen_Rows = MAX_SCREEN_SIZE;
  914. Screen_Cols = *tt_Screen_Cols;
  915. This_Col = This_Row = Start_Col = Start_Row = 0;
  916. This_Alt_Char = 0;
  917. SLsmg_set_color (0);
  918. Cls_Flag = 1;
  919. #ifndef IBMPC_SYSTEM
  920. init_alt_char_set ();
  921. #endif
  922. len = Screen_Cols + 3;
  923. for (i = 0; i < Screen_Rows; i++)
  924. {
  925. if ((NULL == (old = (SLsmg_Char_Type *) SLmalloc (sizeof(SLsmg_Char_Type) * len)))
  926. || ((NULL == (neew = (SLsmg_Char_Type *) SLmalloc (sizeof(SLsmg_Char_Type) * len)))))
  927. {
  928. SLfree ((char *) old);
  929. return -1;
  930. }
  931. blank_line (old, len, ' ');
  932. blank_line (neew, len, ' ');
  933. SL_Screen[i].old = old;
  934. SL_Screen[i].neew = neew;
  935. SL_Screen[i].flags = 0;
  936. #ifndef IBMPC_SYSTEM
  937. Blank_Hash = compute_hash (old, Screen_Cols);
  938. SL_Screen[i].new_hash = SL_Screen[i].old_hash = Blank_Hash;
  939. #endif
  940. }
  941. _SLtt_color_changed_hook = SLsmg_touch_screen;
  942. Screen_Trashed = 1;
  943. Smg_Inited = 1;
  944. return 0;
  945. }
  946. int SLsmg_init_smg (void)
  947. {
  948. int ret;
  949. BLOCK_SIGNALS;
  950. if (Smg_Inited)
  951. SLsmg_reset_smg ();
  952. if (-1 == (*tt_init_video) ())
  953. {
  954. UNBLOCK_SIGNALS;
  955. return -1;
  956. }
  957. if (-1 == (ret = init_smg ()))
  958. (void) (*tt_reset_video)();
  959. UNBLOCK_SIGNALS;
  960. return ret;
  961. }
  962. int SLsmg_reinit_smg (void)
  963. {
  964. int ret;
  965. if (Smg_Inited == 0)
  966. return SLsmg_init_smg ();
  967. BLOCK_SIGNALS;
  968. reset_smg ();
  969. ret = init_smg ();
  970. UNBLOCK_SIGNALS;
  971. return ret;
  972. }
  973. void SLsmg_reset_smg (void)
  974. {
  975. if (Smg_Inited == 0)
  976. return;
  977. BLOCK_SIGNALS;
  978. reset_smg ();
  979. (*tt_reset_video)();
  980. UNBLOCK_SIGNALS;
  981. }
  982. SLsmg_Char_Type SLsmg_char_at (void)
  983. {
  984. if (Smg_Inited == 0) return 0;
  985. if (point_visible (1))
  986. {
  987. return SL_Screen[This_Row - Start_Row].neew[This_Col - Start_Col];
  988. }
  989. return 0;
  990. }
  991. void SLsmg_vprintf (char *fmt, va_list ap)
  992. {
  993. char buf[1024];
  994. if (Smg_Inited == 0) return;
  995. (void) _SLvsnprintf (buf, sizeof (buf), fmt, ap);
  996. SLsmg_write_string (buf);
  997. }
  998. void SLsmg_printf (char *fmt, ...)
  999. {
  1000. va_list ap;
  1001. unsigned int len;
  1002. char *f;
  1003. if (Smg_Inited == 0) return;
  1004. va_start(ap, fmt);
  1005. f = fmt;
  1006. while (*f && (*f != '%'))
  1007. f++;
  1008. len = (unsigned int) (f - fmt);
  1009. if (len) SLsmg_write_nchars (fmt, len);
  1010. if (*f != 0)
  1011. SLsmg_vprintf (f, ap);
  1012. va_end (ap);
  1013. }
  1014. void SLsmg_set_screen_start (int *r, int *c)
  1015. {
  1016. int orow = Start_Row, oc = Start_Col;
  1017. if (Smg_Inited == 0) return;
  1018. if (c == NULL) Start_Col = 0;
  1019. else
  1020. {
  1021. Start_Col = *c;
  1022. *c = oc;
  1023. }
  1024. if (r == NULL) Start_Row = 0;
  1025. else
  1026. {
  1027. Start_Row = *r;
  1028. *r = orow;
  1029. }
  1030. }
  1031. void SLsmg_draw_object (int r, int c, unsigned char object)
  1032. {
  1033. This_Row = r; This_Col = c;
  1034. if (Smg_Inited == 0) return;
  1035. if (point_visible (1))
  1036. {
  1037. int color = This_Color;
  1038. This_Color |= ALT_CHAR_FLAG;
  1039. SLsmg_write_char (object);
  1040. This_Color = color;
  1041. }
  1042. This_Col = c + 1;
  1043. }
  1044. void SLsmg_draw_hline (unsigned int n)
  1045. {
  1046. static unsigned char hbuf[16];
  1047. int count;
  1048. int cmin, cmax;
  1049. int final_col = This_Col + (int) n;
  1050. int save_color;
  1051. if (Smg_Inited == 0) return;
  1052. if ((This_Row < Start_Row) || (This_Row >= Start_Row + Screen_Rows)
  1053. || (0 == compute_clip (This_Col, n, Start_Col, Start_Col + Screen_Cols,
  1054. &cmin, &cmax)))
  1055. {
  1056. This_Col = final_col;
  1057. return;
  1058. }
  1059. if (hbuf[0] == 0)
  1060. {
  1061. SLMEMSET ((char *) hbuf, SLSMG_HLINE_CHAR, 16);
  1062. }
  1063. n = (unsigned int)(cmax - cmin);
  1064. count = n / 16;
  1065. save_color = This_Color;
  1066. This_Color |= ALT_CHAR_FLAG;
  1067. This_Col = cmin;
  1068. SLsmg_write_nchars ((char *) hbuf, n % 16);
  1069. while (count-- > 0)
  1070. {
  1071. SLsmg_write_nchars ((char *) hbuf, 16);
  1072. }
  1073. This_Color = save_color;
  1074. This_Col = final_col;
  1075. }
  1076. void SLsmg_draw_vline (int n)
  1077. {
  1078. unsigned char ch = SLSMG_VLINE_CHAR;
  1079. int c = This_Col, rmin, rmax;
  1080. int final_row = This_Row + n;
  1081. int save_color;
  1082. if (Smg_Inited == 0) return;
  1083. if (((c < Start_Col) || (c >= Start_Col + Screen_Cols)) ||
  1084. (0 == compute_clip (This_Row, n, Start_Row, Start_Row + Screen_Rows,
  1085. &rmin, &rmax)))
  1086. {
  1087. This_Row = final_row;
  1088. return;
  1089. }
  1090. save_color = This_Color;
  1091. This_Color |= ALT_CHAR_FLAG;
  1092. for (This_Row = rmin; This_Row < rmax; This_Row++)
  1093. {
  1094. This_Col = c;
  1095. SLsmg_write_nchars ((char *) &ch, 1);
  1096. }
  1097. This_Col = c; This_Row = final_row;
  1098. This_Color = save_color;
  1099. }
  1100. void SLsmg_draw_box (int r, int c, unsigned int dr, unsigned int dc)
  1101. {
  1102. if (Smg_Inited == 0) return;
  1103. if (!dr || !dc) return;
  1104. This_Row = r; This_Col = c;
  1105. dr--; dc--;
  1106. SLsmg_draw_hline (dc);
  1107. SLsmg_draw_vline (dr);
  1108. This_Row = r; This_Col = c;
  1109. SLsmg_draw_vline (dr);
  1110. SLsmg_draw_hline (dc);
  1111. SLsmg_draw_object (r, c, SLSMG_ULCORN_CHAR);
  1112. SLsmg_draw_object (r, c + (int) dc, SLSMG_URCORN_CHAR);
  1113. SLsmg_draw_object (r + (int) dr, c, SLSMG_LLCORN_CHAR);
  1114. SLsmg_draw_object (r + (int) dr, c + (int) dc, SLSMG_LRCORN_CHAR);
  1115. This_Row = r; This_Col = c;
  1116. }
  1117. void SLsmg_fill_region (int r, int c, unsigned int dr, unsigned int dc, unsigned char ch)
  1118. {
  1119. static unsigned char hbuf[16];
  1120. int count;
  1121. int dcmax, rmax;
  1122. if (Smg_Inited == 0) return;
  1123. SLsmg_gotorc (r, c);
  1124. r = This_Row; c = This_Col;
  1125. dcmax = Screen_Cols - This_Col;
  1126. if (dcmax < 0)
  1127. return;
  1128. if (dc > (unsigned int) dcmax) dc = (unsigned int) dcmax;
  1129. rmax = This_Row + dr;
  1130. if (rmax > Screen_Rows) rmax = Screen_Rows;
  1131. #if 0
  1132. ch = Alt_Char_Set[ch];
  1133. #endif
  1134. if (ch != hbuf[0]) SLMEMSET ((char *) hbuf, (char) ch, 16);
  1135. for (This_Row = r; This_Row < rmax; This_Row++)
  1136. {
  1137. This_Col = c;
  1138. count = dc / 16;
  1139. SLsmg_write_nchars ((char *) hbuf, dc % 16);
  1140. while (count-- > 0)
  1141. {
  1142. SLsmg_write_nchars ((char *) hbuf, 16);
  1143. }
  1144. }
  1145. This_Row = r;
  1146. }
  1147. void SLsmg_forward (int n)
  1148. {
  1149. This_Col += n;
  1150. }
  1151. void SLsmg_write_color_chars (SLsmg_Char_Type *s, unsigned int len)
  1152. {
  1153. SLsmg_Char_Type *smax, sh;
  1154. char buf[32], *b, *bmax;
  1155. int color, save_color;
  1156. if (Smg_Inited == 0) return;
  1157. smax = s + len;
  1158. b = buf;
  1159. bmax = b + sizeof (buf);
  1160. save_color = This_Color;
  1161. while (s < smax)
  1162. {
  1163. sh = *s++;
  1164. color = SLSMG_EXTRACT_COLOR(sh);
  1165. #if REQUIRES_NON_BCE_SUPPORT
  1166. if (Bce_Color_Offset)
  1167. {
  1168. if (color & 0x80)
  1169. color = ((color & 0x7F) + Bce_Color_Offset) | 0x80;
  1170. else
  1171. color = ((color & 0x7F) + Bce_Color_Offset) & 0x7F;
  1172. }
  1173. #endif
  1174. if ((color != This_Color) || (b == bmax))
  1175. {
  1176. if (b != buf)
  1177. {
  1178. SLsmg_write_nchars (buf, (int) (b - buf));
  1179. b = buf;
  1180. }
  1181. This_Color = color;
  1182. }
  1183. *b++ = (char) SLSMG_EXTRACT_CHAR(sh);
  1184. }
  1185. if (b != buf)
  1186. SLsmg_write_nchars (buf, (unsigned int) (b - buf));
  1187. This_Color = save_color;
  1188. }
  1189. unsigned int SLsmg_read_raw (SLsmg_Char_Type *buf, unsigned int len)
  1190. {
  1191. unsigned int r, c;
  1192. if (Smg_Inited == 0) return 0;
  1193. if (0 == point_visible (1)) return 0;
  1194. r = (unsigned int) (This_Row - Start_Row);
  1195. c = (unsigned int) (This_Col - Start_Col);
  1196. if (c + len > (unsigned int) Screen_Cols)
  1197. len = (unsigned int) Screen_Cols - c;
  1198. memcpy ((char *) buf, (char *) (SL_Screen[r].neew + c), len * sizeof (SLsmg_Char_Type));
  1199. return len;
  1200. }
  1201. unsigned int SLsmg_write_raw (SLsmg_Char_Type *buf, unsigned int len)
  1202. {
  1203. unsigned int r, c;
  1204. SLsmg_Char_Type *dest;
  1205. if (Smg_Inited == 0) return 0;
  1206. if (0 == point_visible (1)) return 0;
  1207. r = (unsigned int) (This_Row - Start_Row);
  1208. c = (unsigned int) (This_Col - Start_Col);
  1209. if (c + len > (unsigned int) Screen_Cols)
  1210. len = (unsigned int) Screen_Cols - c;
  1211. dest = SL_Screen[r].neew + c;
  1212. if (0 != memcmp ((char *) dest, (char *) buf, len * sizeof (SLsmg_Char_Type)))
  1213. {
  1214. memcpy ((char *) dest, (char *) buf, len * sizeof (SLsmg_Char_Type));
  1215. SL_Screen[r].flags |= TOUCHED;
  1216. }
  1217. return len;
  1218. }
  1219. void
  1220. SLsmg_set_color_in_region (int color, int r, int c, unsigned int dr, unsigned int dc)
  1221. {
  1222. int cmax, rmax;
  1223. SLsmg_Char_Type char_mask;
  1224. if (Smg_Inited == 0) return;
  1225. c -= Start_Col;
  1226. r -= Start_Row;
  1227. cmax = c + (int) dc;
  1228. rmax = r + (int) dr;
  1229. if (cmax > Screen_Cols) cmax = Screen_Cols;
  1230. if (rmax > Screen_Rows) rmax = Screen_Rows;
  1231. if (c < 0) c = 0;
  1232. if (r < 0) r = 0;
  1233. #if REQUIRES_NON_BCE_SUPPORT
  1234. if (Bce_Color_Offset)
  1235. {
  1236. if (color & 0x80)
  1237. color = ((color & 0x7F) + Bce_Color_Offset) | 0x80;
  1238. else
  1239. color = ((color & 0x7F) + Bce_Color_Offset) & 0x7F;
  1240. }
  1241. #endif
  1242. color = color << 8;
  1243. char_mask = 0xFF;
  1244. #ifndef IBMPC_SYSTEM
  1245. if ((tt_Use_Blink_For_ACS == NULL)
  1246. || (0 == *tt_Use_Blink_For_ACS))
  1247. char_mask = 0x80FF;
  1248. #endif
  1249. while (r < rmax)
  1250. {
  1251. SLsmg_Char_Type *s, *smax;
  1252. SL_Screen[r].flags |= TOUCHED;
  1253. s = SL_Screen[r].neew;
  1254. smax = s + cmax;
  1255. s += c;
  1256. while (s < smax)
  1257. {
  1258. *s = (*s & char_mask) | color;
  1259. s++;
  1260. }
  1261. r++;
  1262. }
  1263. }
  1264. void SLsmg_set_terminal_info (SLsmg_Term_Type *tt)
  1265. {
  1266. if (tt == NULL) /* use default */
  1267. return;
  1268. if ((tt->tt_normal_video == NULL)
  1269. || (tt->tt_goto_rc == NULL)
  1270. || (tt->tt_cls == NULL)
  1271. || (tt->tt_del_eol == NULL)
  1272. || (tt->tt_smart_puts == NULL)
  1273. || (tt->tt_flush_output == NULL)
  1274. || (tt->tt_reset_video == NULL)
  1275. || (tt->tt_init_video == NULL)
  1276. #ifndef IBMPC_SYSTEM
  1277. || (tt->tt_set_scroll_region == NULL)
  1278. || (tt->tt_reverse_index == NULL)
  1279. || (tt->tt_reset_scroll_region == NULL)
  1280. || (tt->tt_delete_nlines == NULL)
  1281. /* Variables */
  1282. || (tt->tt_term_cannot_scroll == NULL)
  1283. || (tt->tt_has_alt_charset == NULL)
  1284. #if 0 /* These can be NULL */
  1285. || (tt->tt_use_blink_for_acs == NULL)
  1286. || (tt->tt_graphic_char_pairs == NULL)
  1287. #endif
  1288. || (tt->tt_screen_cols == NULL)
  1289. || (tt->tt_screen_rows == NULL)
  1290. #endif
  1291. )
  1292. SLang_exit_error ("Terminal not powerful enough for SLsmg");
  1293. tt_normal_video = tt->tt_normal_video;
  1294. tt_goto_rc = tt->tt_goto_rc;
  1295. tt_cls = tt->tt_cls;
  1296. tt_del_eol = tt->tt_del_eol;
  1297. tt_smart_puts = tt->tt_smart_puts;
  1298. tt_flush_output = tt->tt_flush_output;
  1299. tt_reset_video = tt->tt_reset_video;
  1300. tt_init_video = tt->tt_init_video;
  1301. #ifndef IBMPC_SYSTEM
  1302. tt_set_scroll_region = tt->tt_set_scroll_region;
  1303. tt_reverse_index = tt->tt_reverse_index;
  1304. tt_reset_scroll_region = tt->tt_reset_scroll_region;
  1305. tt_delete_nlines = tt->tt_delete_nlines;
  1306. tt_Term_Cannot_Scroll = tt->tt_term_cannot_scroll;
  1307. tt_Has_Alt_Charset = tt->tt_has_alt_charset;
  1308. tt_Use_Blink_For_ACS = tt->tt_use_blink_for_acs;
  1309. tt_Graphics_Char_Pairs = tt->tt_graphic_char_pairs;
  1310. #endif
  1311. tt_Screen_Cols = tt->tt_screen_cols;
  1312. tt_Screen_Rows = tt->tt_screen_rows;
  1313. }