slsmg.c 33 KB

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