TermPrimRender.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* *
  24. * (c) Copyright 1993, 1994, 1996 Hewlett-Packard Company *
  25. * (c) Copyright 1993, 1994, 1996 International Business Machines Corp. *
  26. * (c) Copyright 1993, 1994, 1996 Sun Microsystems, Inc. *
  27. * (c) Copyright 1993, 1994, 1996 Novell, Inc. *
  28. * (c) Copyright 1996 Digital Equipment Corporation. *
  29. * (c) Copyright 1996 FUJITSU LIMITED. *
  30. * (c) Copyright 1996 Hitachi. *
  31. */
  32. #include "TermHeader.h"
  33. #include "TermPrimDebug.h"
  34. #include "TermPrimP.h"
  35. #include "TermPrimI.h"
  36. #include "TermPrimData.h"
  37. #include "TermPrimLineDraw.h"
  38. #include "TermPrimBufferP.h"
  39. #include "TermPrimRender.h"
  40. #include "TermPrimRenderP.h"
  41. #include "TermPrimSelect.h"
  42. #include "TermPrimSelectP.h"
  43. #include "TermPrimMessageCatI.h"
  44. #include <limits.h>
  45. #ifdef DKS
  46. void
  47. _termSetRenderFont(Widget w, TermFont *termFont)
  48. {
  49. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
  50. struct termData *tpd = tw->term.tpd;
  51. int i;
  52. /* DKS: ** alert ** alert ** alert ** alert ** alert **
  53. *
  54. * The following opens up a memory leak that needs to be closed.
  55. * we need to free up both the list and any created entries...
  56. */
  57. /* do initialization... */
  58. if (!tpd->renderTermFontsNum) {
  59. tpd->renderTermFontsNum = 4;
  60. tpd->renderTermFonts = (TermFont **) malloc((unsigned)
  61. tpd->renderTermFontsNum * sizeof(TermFont *));
  62. (void) memset(tpd->renderTermFonts, '\0',
  63. tpd->renderTermFontsNum * sizeof(TermFont *));
  64. }
  65. /* replace the font if we already have one defined, else add it... */
  66. for (i = 0; i < tpd->renderTermFontsNum; i++) {
  67. if (tpd->renderTermFonts[i] && (tpd->renderTermFonts[i]->id ==
  68. termFont->id))
  69. /* found an existing font with this id... */
  70. break;
  71. }
  72. if (i >= tpd->renderTermFontsNum) {
  73. /* we didn't find one, so let's add this one to the list... */
  74. for (i = 0; i < tpd->renderTermFontsNum; i++) {
  75. if (!tpd->renderTermFonts[i])
  76. break;
  77. }
  78. /* check for list full... */
  79. if (i >= tpd->renderTermFontsNum) {
  80. (void) fprintf(stderr,
  81. "setRenderFont: list full can't add font to widget 0x%lx\n",
  82. w);
  83. }
  84. /* we found an empty spot and need to malloc storage... */
  85. tpd->renderTermFonts[i] = (TermFont *) malloc(sizeof(TermFont));
  86. /* we don't need to initialize it as we will do a copy below... */
  87. }
  88. (void) memcpy(tpd->renderTermFonts[i], termFont, sizeof(TermFont));
  89. if ((0 == tpd->cellWidth) || ('@' == termFont->id)) {
  90. tpd->cellWidth = termFont->width;
  91. tpd->cellHeight = termFont->height;
  92. tw->term.widthInc = termFont->width;
  93. tw->term.heightInc = termFont->height;
  94. tw->term.ascent = termFont->ascent;
  95. }
  96. }
  97. #endif /* DKS */
  98. void
  99. _DtTermPrimBell(Widget w)
  100. {
  101. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
  102. struct termData *tpd = tw->term.tpd;
  103. int i;
  104. if (tw->term.visualBell) {
  105. /* the speed of this operation is not critical, so we will just
  106. * use the standard text rendering GC and restore it after we
  107. * are done...
  108. */
  109. if (tpd->renderGC.foreground !=
  110. (tw->primitive.foreground ^ tw->core.background_pixel)) {
  111. tpd->renderGC.foreground =
  112. tw->primitive.foreground ^ tw->core.background_pixel;
  113. (void) XSetForeground(XtDisplay(w), tpd->renderGC.gc,
  114. tpd->renderGC.foreground);
  115. }
  116. (void) XSetFunction(XtDisplay(w), tpd->renderGC.gc, GXxor);
  117. for (i = 0; i < 2; i++) {
  118. (void) XFillRectangle(XtDisplay(w), /* Display */
  119. XtWindow(w), /* Drawable */
  120. tpd->renderGC.gc, /* GC */
  121. tpd->offsetX, /* x */
  122. tpd->offsetY, /* y */
  123. tw->term.columns * tpd->cellWidth,
  124. /* width */
  125. tw->term.rows * tpd->cellHeight);
  126. /* height */
  127. (void) XSync(XtDisplay(w), 0);
  128. }
  129. /* restore the GC... */
  130. (void) XSetFunction(XtDisplay(w), tpd->renderGC.gc, GXcopy);
  131. } else {
  132. (void) XBell(XtDisplay(w), 0);
  133. }
  134. }
  135. void
  136. _DtTermPrimRefreshText(Widget w, short startColumn, short startRow,
  137. short endColumn, short endRow)
  138. {
  139. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
  140. struct termData *tpd = tw->term.tpd;
  141. TermBuffer tBuffer = tpd->termBuffer;
  142. int lineWidth;
  143. unsigned char *linePtr;
  144. TermFont termFont;
  145. int currentColorPair = 0;
  146. int currentVideo = 0;
  147. short chunkStartColumn;
  148. short chunkWidth;
  149. enhValues enhancements;
  150. int i;
  151. int lineNum;
  152. unsigned long valueMask;
  153. GC gc;
  154. XGCValues values;
  155. TermEnhInfoRec enhInfo;
  156. Boolean checkSelection = False;
  157. int selectionEnd;
  158. Pixel tmpPixel;
  159. XmTextPosition begin, end;
  160. DebugF('t', 0, fprintf(stderr, ">>_DtTermPrimRefreshText() starting\n"));
  161. DebugF('t', 0, fprintf(stderr,
  162. ">>_DtTermPrimRefreshText() startCol=%hd startRow=%hd endCol=%hd endRow=%hd\n",
  163. startColumn, startRow, endColumn, endRow));
  164. if (tpd->mbCurMax > 1)
  165. {
  166. _DtTermPrimRefreshTextWc(w, startColumn, startRow, endColumn, endRow);
  167. return;
  168. }
  169. /* clip start/end x/y... */
  170. if (startColumn <= 0)
  171. startColumn = 0;
  172. if (startRow <= 0)
  173. startRow = 0;
  174. if (endColumn >= tw->term.columns)
  175. endColumn = tw->term.columns - 1;
  176. if (endRow >= tw->term.rows)
  177. endRow = tw->term.rows - 1;
  178. /*
  179. ** don't display if we are in jump scroll and in the process
  180. ** of scrolling and inside the scroll region...
  181. */
  182. if (tw->term.jumpScroll && tpd->scroll.jump.scrolled) {
  183. /* set all the scrollRefreshRows flags... */
  184. for (; startRow <= endRow; startRow++) {
  185. tpd->scrollRefreshRows[startRow] = True;
  186. }
  187. DebugF('t', 0,
  188. fprintf(stderr,
  189. ">>_DtTermPrimRefreshText() jump scroll in progress, no render\n"));
  190. return;
  191. }
  192. if (!tpd->renderGC.gc) {
  193. /* get a drawImageString GC... */
  194. int i;
  195. XGCValues values;
  196. /***********************************************************
  197. * renderGC...
  198. */
  199. /* set the GC fg and bg... */
  200. values.foreground = tw->term.reverseVideo ?
  201. tw->core.background_pixel : tw->primitive.foreground;
  202. values.background = tw->term.reverseVideo ?
  203. tw->primitive.foreground : tw->core.background_pixel;
  204. tpd->renderGC.gc = XCreateGC(XtDisplay(w), XtWindow(w),
  205. GCForeground | GCBackground, &values);
  206. /* set the GC cache values... */
  207. tpd->renderGC.foreground = values.foreground;
  208. tpd->renderGC.background = values.background;
  209. tpd->renderGC.fid = (Font) 0;
  210. /***********************************************************
  211. * renderReverseGC...
  212. */
  213. values.foreground = tw->term.reverseVideo ?
  214. tw->primitive.foreground : tw->core.background_pixel;
  215. values.background = tw->term.reverseVideo ?
  216. tw->core.background_pixel : tw->primitive.foreground;
  217. tpd->renderReverseGC.gc = XCreateGC(XtDisplay(w), XtWindow(w),
  218. GCForeground | GCBackground, &values);
  219. /* set the GC cache values... */
  220. tpd->renderReverseGC.foreground = values.foreground;
  221. tpd->renderReverseGC.background = values.background;
  222. tpd->renderReverseGC.fid = (Font) 0;
  223. /***********************************************************
  224. * clearGC...
  225. */
  226. values.foreground = tw->term.reverseVideo ?
  227. tw->primitive.foreground : tw->core.background_pixel;
  228. values.background = tw->term.reverseVideo ?
  229. tw->core.background_pixel : tw->primitive.foreground;
  230. tpd->clearGC.gc = XCreateGC(XtDisplay(w), XtWindow(w),
  231. GCForeground | GCBackground, &values);
  232. /* set the GC cache values... */
  233. tpd->clearGC.foreground = values.foreground;
  234. tpd->clearGC.background = values.background;
  235. tpd->clearGC.fid = (Font) 0;
  236. }
  237. #ifdef SUN_MOTIF_PERF
  238. /* use the clear GC... */
  239. gc = tpd->clearGC.gc;
  240. valueMask = (unsigned long) 0;
  241. if (tpd->clearGC.foreground !=
  242. tw->term.reverseVideo ?
  243. tw->core.background_pixel : tw->primitive.foreground) {
  244. values.foreground =
  245. tw->term.reverseVideo ?
  246. tw->primitive.foreground :
  247. tw->core.background_pixel;
  248. tpd->clearGC.foreground = values.foreground;
  249. valueMask |= GCForeground;
  250. }
  251. if (tpd->clearGC.background !=
  252. tw->term.reverseVideo ?
  253. tw->primitive.foreground :
  254. tw->core.background_pixel) {
  255. values.background =
  256. tw->term.reverseVideo ?
  257. tw->core.background_pixel :
  258. tw->primitive.foreground;
  259. tpd->clearGC.background = values.background;
  260. valueMask |= GCBackground;
  261. }
  262. if (valueMask) {
  263. (void) XChangeGC(XtDisplay(w), tpd->clearGC.gc,
  264. valueMask, &values);
  265. }
  266. (void) XFillRectangle(XtDisplay(w), /* Display */
  267. XtWindow(w), /* Drawable */
  268. gc, /* GC */
  269. startColumn * tpd->cellWidth + tpd->offsetX,
  270. /* x */
  271. startRow * tpd->cellHeight + tpd->offsetY,
  272. /* y */
  273. (endColumn - startColumn + 1) * tpd->cellWidth,
  274. /* width */
  275. (endRow - startRow + 1) * tpd->cellHeight);
  276. /* height */
  277. #endif /* SUN_MOTIF_PERF */
  278. for (; startRow <= endRow; startRow++) {
  279. /* if we are refreshing a full line, then we can clear the
  280. * scrollRefreshRows flag for this line...
  281. */
  282. if ((startColumn == 0) && (endColumn >= tw->term.columns - 1)) {
  283. tpd->scrollRefreshRows[startRow] = False;
  284. }
  285. lineNum = startRow + tpd->topRow;
  286. if (!tw->term.jumpScroll && tpd->scroll.nojump.pendingScroll) {
  287. if (lineNum != tpd->scrollLockBottomRow)
  288. {
  289. lineNum -= tpd->scroll.nojump.pendingScrollLines;
  290. }
  291. }
  292. /* are we in the selected area?... */
  293. if (tpd->useHistoryBuffer)
  294. {
  295. if (_DtTermPrimSelectGetSelection(w, &begin, &end) &&
  296. (begin < end) &&
  297. (lineNum >= (begin / (tpd->selectInfo->columns + 1)) -
  298. tpd->lastUsedHistoryRow) &&
  299. (lineNum <= (end / (tpd->selectInfo->columns + 1)) -
  300. tpd->lastUsedHistoryRow)) {
  301. checkSelection = True;
  302. } else {
  303. checkSelection = False;
  304. }
  305. }
  306. else
  307. {
  308. if (_DtTermPrimSelectGetSelection(w, &begin, &end) &&
  309. (begin < end) &&
  310. (lineNum >= (begin / (tpd->selectInfo->columns + 1))) &&
  311. (lineNum <= (end / (tpd->selectInfo->columns + 1)))) {
  312. checkSelection = True;
  313. } else {
  314. checkSelection = False;
  315. }
  316. }
  317. chunkStartColumn = startColumn;
  318. if (startColumn > endColumn) {
  319. /* nothing to render on this line... */
  320. continue;
  321. }
  322. if (lineNum >= tpd->lastUsedRow) {
  323. /* we are pointing to empty screen space below the last used
  324. * line of the display...
  325. */
  326. lineWidth = 0;
  327. linePtr = NULL;
  328. } else if (lineNum < 0) {
  329. if ((tpd->useHistoryBuffer) &&
  330. (-lineNum <= tpd->lastUsedHistoryRow)) {
  331. /* get a line out of the history buffer... */
  332. lineWidth = MAX(0, MIN(endColumn - startColumn + 1,
  333. _DtTermPrimBufferGetLineWidth(tpd->historyBuffer,
  334. tpd->lastUsedHistoryRow + lineNum) - startColumn));
  335. linePtr =
  336. _DtTermPrimBufferGetCharacterPointer(tpd->historyBuffer,
  337. tpd->lastUsedHistoryRow + lineNum, startColumn);
  338. } else {
  339. /* we are above the history buffer. Should not happen, but...
  340. */
  341. lineWidth = 0;
  342. linePtr = NULL;
  343. }
  344. } else {
  345. /* get the line width and a pointer to the data... */
  346. lineWidth = MAX(0, MIN(endColumn - startColumn + 1,
  347. _DtTermPrimBufferGetLineWidth(tBuffer, lineNum) -
  348. startColumn));
  349. linePtr = _DtTermPrimBufferGetCharacterPointer(tBuffer, lineNum,
  350. startColumn);
  351. }
  352. while (lineWidth > 0) {
  353. /* get the enhancement values for the first chunk of the
  354. * string...
  355. */
  356. if (lineNum >= 0) {
  357. (void) _DtTermPrimBufferGetEnhancement(tBuffer,
  358. /* TermBuffer */
  359. lineNum, /* row */
  360. chunkStartColumn, /* col */
  361. &enhancements, /* enhancements */
  362. &chunkWidth, /* width */
  363. countAll); /* countWhich */
  364. } else {
  365. /* get it from the history buffer... */
  366. (void) _DtTermPrimBufferGetEnhancement(tpd->historyBuffer,
  367. /* TermBuffer */
  368. tpd->lastUsedHistoryRow + lineNum,
  369. /* row */
  370. chunkStartColumn, /* col */
  371. &enhancements, /* enhancements */
  372. &chunkWidth, /* width */
  373. countAll); /* countWhich */
  374. }
  375. /* clip chunkWidth... */
  376. if (chunkWidth > lineWidth)
  377. chunkWidth = lineWidth;
  378. /* set reasonable defaults for our render info... */
  379. enhInfo.fg = tw->primitive.foreground;
  380. enhInfo.bg = tw->core.background_pixel;
  381. enhInfo.font = tpd->defaultTermFont;
  382. enhInfo.flags = (unsigned long) 0;
  383. /* set our font and color from the enhancements... */
  384. if (ENH_PROC(tBuffer)) {
  385. (void) (*(ENH_PROC(tBuffer)))(w, enhancements, &enhInfo);
  386. }
  387. /* if we are in reverse video mode... */
  388. if (tw->term.reverseVideo) {
  389. /* flip fg and bg... */
  390. tmpPixel = enhInfo.fg;
  391. enhInfo.fg = enhInfo.bg;
  392. enhInfo.bg = tmpPixel;
  393. }
  394. /* are we in the selection area?... */
  395. if (checkSelection &&
  396. _DtTermPrimSelectIsInSelection(w, lineNum, chunkStartColumn,
  397. chunkWidth, &chunkWidth)) {
  398. /* flip fg and bg... */
  399. tmpPixel = enhInfo.fg;
  400. enhInfo.fg = enhInfo.bg;
  401. enhInfo.bg = tmpPixel;
  402. }
  403. /* if secure, we will use a XFillRectangle, and we need
  404. * foreground set to the background...
  405. */
  406. if (TermIS_SECURE(enhInfo.flags)) {
  407. /* render secure video locally...
  408. */
  409. /* set the renderReverseGC... */
  410. valueMask = (unsigned long) 0;
  411. if (tpd->renderReverseGC.foreground != enhInfo.bg) {
  412. tpd->renderReverseGC.foreground = enhInfo.bg;
  413. values.foreground = enhInfo.bg;
  414. valueMask |= GCForeground;
  415. }
  416. if (valueMask) {
  417. (void) XChangeGC(XtDisplay(w), tpd->renderReverseGC.gc,
  418. valueMask, &values);
  419. }
  420. (void) XFillRectangle(XtDisplay(w),
  421. XtWindow(w),
  422. tpd->renderReverseGC.gc,
  423. chunkStartColumn * tpd->cellWidth + tpd->offsetX,
  424. startRow * tpd->cellHeight + tpd->offsetY,
  425. tpd->cellWidth * chunkWidth,
  426. tpd->cellHeight);
  427. /* underline as well... */
  428. if (TermIS_UNDERLINE(enhInfo.flags)) {
  429. valueMask = (unsigned long) 0;
  430. if (tpd->renderGC.foreground != enhInfo.fg) {
  431. tpd->renderGC.foreground = enhInfo.fg;
  432. values.foreground = enhInfo.fg;
  433. valueMask |= GCForeground;
  434. }
  435. if (valueMask) {
  436. (void) XChangeGC(XtDisplay(w), tpd->renderGC.gc,
  437. valueMask, &values);
  438. }
  439. (void) XDrawLine(XtDisplay(w),
  440. /* Display */
  441. XtWindow(w), /* Drawable */
  442. tpd->renderGC.gc, /* GC */
  443. chunkStartColumn * tpd->cellWidth + tpd->offsetX,
  444. /* X1 */
  445. startRow * tpd->cellHeight + tpd->offsetY +
  446. tpd->cellHeight - 1,
  447. /* Y1 */
  448. (chunkStartColumn + chunkWidth) * tpd->cellWidth +
  449. tpd->offsetX, /* X2 */
  450. startRow * tpd->cellHeight + tpd->offsetY +
  451. tpd->cellHeight - 1);
  452. /* Y2 */
  453. }
  454. } else {
  455. (void) _DtTermPrimRenderText(
  456. w, /* Widget */
  457. enhInfo.font, /* TermFont */
  458. enhInfo.fg, /* fg Pixel */
  459. enhInfo.bg, /* bg Pixel */
  460. enhInfo.flags, /* flags */
  461. chunkStartColumn * tpd->cellWidth + tpd->offsetX,
  462. /* x */
  463. startRow * tpd->cellHeight + tpd->offsetY,
  464. /* y */
  465. linePtr, /* string */
  466. chunkWidth); /* width */
  467. }
  468. chunkStartColumn += chunkWidth;
  469. lineWidth -= chunkWidth;
  470. linePtr += chunkWidth;
  471. }
  472. /* clear any extra space in the line. chunkStartColumn now points to
  473. * the end of the line, and lineWidth == 0...
  474. */
  475. while (endColumn - chunkStartColumn >= 0) {
  476. chunkWidth = endColumn - chunkStartColumn + 1;
  477. if (checkSelection &&
  478. _DtTermPrimSelectIsInSelection(w, lineNum, chunkStartColumn,
  479. chunkWidth, &chunkWidth)) {
  480. /* use the render gc set to the fg color... */
  481. gc = tpd->renderReverseGC.gc;
  482. valueMask = (unsigned long) 0;
  483. if (tpd->renderReverseGC.foreground !=
  484. tw->term.reverseVideo ?
  485. tw->core.background_pixel : tw->primitive.foreground) {
  486. values.foreground =
  487. tw->term.reverseVideo ?
  488. tw->core.background_pixel :
  489. tw->primitive.foreground;
  490. tpd->renderReverseGC.foreground = values.foreground;
  491. valueMask |= GCForeground;
  492. }
  493. if (valueMask) {
  494. (void) XChangeGC(XtDisplay(w), tpd->renderReverseGC.gc,
  495. valueMask, &values);
  496. }
  497. #ifndef SUN_MOTIF_PERF
  498. } else {
  499. /* use the clear GC... */
  500. gc = tpd->clearGC.gc;
  501. valueMask = (unsigned long) 0;
  502. if (tpd->clearGC.foreground !=
  503. tw->term.reverseVideo ?
  504. tw->core.background_pixel : tw->primitive.foreground) {
  505. values.foreground =
  506. tw->term.reverseVideo ?
  507. tw->primitive.foreground :
  508. tw->core.background_pixel;
  509. tpd->clearGC.foreground = values.foreground;
  510. valueMask |= GCForeground;
  511. }
  512. if (tpd->clearGC.background !=
  513. tw->term.reverseVideo ?
  514. tw->primitive.foreground :
  515. tw->core.background_pixel) {
  516. values.background =
  517. tw->term.reverseVideo ?
  518. tw->core.background_pixel :
  519. tw->primitive.foreground;
  520. tpd->clearGC.background = values.background;
  521. valueMask |= GCBackground;
  522. }
  523. if (valueMask) {
  524. (void) XChangeGC(XtDisplay(w), tpd->clearGC.gc,
  525. valueMask, &values);
  526. }
  527. }
  528. #endif /* not SUN_MOTIF_PERF */
  529. if (isDebugFSet('t', 1)) {
  530. #ifdef BBA
  531. #pragma BBA_IGNORE
  532. #endif /*BBA*/
  533. /* Fill in the clear area so we can see what is going to
  534. * be displayed...
  535. */
  536. (void) XFillRectangle(XtDisplay(w),
  537. XtWindow(w),
  538. tpd->renderGC.gc,
  539. chunkStartColumn * tpd->cellWidth + tpd->offsetX,
  540. startRow * tpd->cellHeight + tpd->offsetY,
  541. chunkWidth * tpd->cellWidth,
  542. tpd->cellHeight);
  543. (void) XSync(XtDisplay(w), False);
  544. (void) shortSleep(100000);
  545. }
  546. (void) XFillRectangle(XtDisplay(w),
  547. /* Display */
  548. XtWindow(w), /* Drawable */
  549. gc, /* GC */
  550. chunkStartColumn * tpd->cellWidth + tpd->offsetX,
  551. /* x */
  552. startRow * tpd->cellHeight + tpd->offsetY,
  553. /* y */
  554. chunkWidth * tpd->cellWidth,
  555. /* width */
  556. tpd->cellHeight); /* height */
  557. #ifdef SUN_MOTIF_PERF
  558. }
  559. #endif /* SUN_MOTIF_PERF */
  560. chunkStartColumn += chunkWidth;
  561. }
  562. }
  563. DebugF('t', 0, fprintf(stderr, ">>_DtTermPrimRefreshText() finished\n"));
  564. }
  565. /**************************************************************************
  566. * Function:
  567. * _DtTermPrimExposeText(): expose (refresh) the text in rectangular area
  568. *
  569. * Parameters:
  570. * Widget w: widget to expose
  571. * int x: starting x pixel in window
  572. * int y: starting y pixel in window
  573. * int width: width in pixels
  574. * int height: height in pixels
  575. * Boolean isExposeEvent: true, deal with copyarea / expose overlap
  576. *
  577. * Returns:
  578. * <nothing>
  579. *
  580. * Notes:
  581. * This function will redisplay the text in a rectangular exposure
  582. * area. The x, y, width, and height are in absolute pixels. The
  583. * internal x and y offsets will be accounted for, and the minimum
  584. * number of characters will be displayed. The algorithm has been
  585. * tuned (somewhat) and no longer exposes an extra character at the
  586. * end of the refresh lines and an extra line at the bottom of the
  587. * refresh area. This can be verified by using the 't' and 'e'
  588. * debug flags.
  589. */
  590. void
  591. _DtTermPrimExposeText(Widget w, int x, int y, int width, int height,
  592. Boolean isExposeEvent)
  593. {
  594. struct termData *tpd = ((DtTermPrimitiveWidget) w)->term.tpd;
  595. DebugF('e', 0, fprintf(stderr, ">>exposeText() starting\n"));
  596. DebugF('e', 0, fprintf(stderr,
  597. ">>exposeText() startX=%d startY=%d width=%d height=%d\n",
  598. x, y, width, height));
  599. DebugF('e', 0, fprintf(stderr,
  600. ">> offsetX=%d offsetY=%d cellHeight=%d cellWidth=%d\n",
  601. tpd->offsetX, tpd->offsetY, tpd->cellHeight, tpd->cellWidth));
  602. /* The following "hack" takes care of the problem of an exposure event
  603. * from the server and a copy area from the client crossing. The
  604. * combination of these two events can cause a race condition which
  605. * manifests itself by leaving a hole in the terminal window. What
  606. * happens is this:
  607. *
  608. * A window is partially obscured. The terminal emulator does a copy
  609. * area (scroll) which includes part of obscured area. Before the
  610. * server processes the copy area, the window is unobscured, and the
  611. * server sends an exposure event back to the client.
  612. *
  613. * - The window is partially obscured.
  614. *
  615. * - The terminal emulator does a copy area (scroll) which includes a
  616. * portion of the obscured area. Normally, the server will generate
  617. * a graphics exposure event for the obscured portion that it can't
  618. * copy which will allow the terminal emulator to update the area.
  619. *
  620. * - Before the server receives the copy area request, the server
  621. * unobscures the window and sends an exposure event for the exposed
  622. * area. (This is where the hack comes into play and refreshes the
  623. * scrolled portion of this area (and possibly some extra space as
  624. * well)).
  625. *
  626. * - The server processes the copy area. Since the area in question is
  627. * no longer obscured, the server will copy blank space and not
  628. * generate a graphics exposure event.
  629. *
  630. * - The terminal emulator processes the exposure event and refreshes
  631. * the exposed area. (This is the hack extends the exposure to cover
  632. * the gap).
  633. *
  634. * - You now have a blank chunk of the terminal window where the
  635. * obscured area was scrolled (without the hack).
  636. *
  637. * Our fix is similar to the one used in xterm. The Motif text widget
  638. * uses a more brute force method and simply extends the exposure event
  639. * to the full height (or width) of the screen in the direction of the
  640. * copy area.
  641. */
  642. if (isExposeEvent && tpd->scrollInProgress) {
  643. int bothX1;
  644. int bothX2;
  645. int bothY1;
  646. int bothY2;
  647. bothX1 = MAX(tpd->scrollSrcX, x);
  648. bothY1 = MAX(tpd->scrollSrcY, y);
  649. bothX2 = MIN(tpd->scrollSrcX + tpd->scrollWidth, x + width - 1);
  650. bothY2 = MIN(tpd->scrollSrcY + tpd->scrollHeight, y + height - 1);
  651. if ((bothX2 > bothX1) && (bothY2 > bothY1)) {
  652. (void) _DtTermPrimRefreshText(w,
  653. (x - tpd->offsetX + tpd->scrollDestX - tpd->scrollSrcX) /
  654. tpd->cellWidth,
  655. (y - tpd->offsetY + tpd->scrollDestY - tpd->scrollSrcY) /
  656. tpd->cellHeight,
  657. (x + width - 1 - tpd->offsetX + tpd->scrollDestX -
  658. tpd->scrollSrcX) / tpd->cellWidth,
  659. (y + height - 1 - tpd->offsetY + tpd->scrollDestY -
  660. tpd->scrollSrcY) / tpd->cellHeight);
  661. }
  662. }
  663. /* render the text... */
  664. DebugF('t', 0, fprintf(stderr, ">>exposeText() calling _DtTermPrimRefreshText()\n"));
  665. (void) _DtTermPrimRefreshText(w, (x - tpd->offsetX) / tpd->cellWidth,
  666. (y - tpd->offsetY) / tpd->cellHeight,
  667. (x + width - 1 - tpd->offsetX) / tpd->cellWidth,
  668. (y + height - 1 - tpd->offsetY) / tpd->cellHeight);
  669. DebugF('e', 0, fprintf(stderr, ">>exposeText() finished\n"));
  670. }
  671. void
  672. _DtTermPrimFillScreenGap(Widget w)
  673. {
  674. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
  675. struct termData *tpd = tw->term.tpd;
  676. TermBuffer tBuffer = tpd->termBuffer;
  677. int linesNeeded;
  678. int linesCopied;
  679. int historyLinesNeeded;
  680. int i1;
  681. TermLineSelection selectionFlag;
  682. /* before we insert any text, we need to insure that the cursor is
  683. * on a valid buffer row. If not, we will need to fill in any gap
  684. * between the lastUsedRow and the current cursor postion with 0
  685. * width lines, and get a buffer row for the current cursor row...
  686. */
  687. if (tpd->topRow + tpd->cursorRow >= tpd->lastUsedRow) {
  688. /* if there are lines left in the buffer, just use them...
  689. */
  690. /* this does not effect the position of the selection... */
  691. for (; (tpd->lastUsedRow < tpd->bufferRows) &&
  692. (tpd->topRow + tpd->cursorRow >= tpd->lastUsedRow); ) {
  693. /* all we need to do is clear the line... */
  694. _DtTermPrimBufferClearLine(tBuffer, tpd->lastUsedRow++, 0);
  695. }
  696. /* if we still need more lines, then we will need to steal from
  697. * the top of the buffer...
  698. */
  699. if (tpd->topRow + tpd->cursorRow >= tpd->lastUsedRow) {
  700. if (tpd->memoryLockMode == SCROLL_LOCKprotect) {
  701. if (!tpd->warningDialogMapped) {
  702. (void) _DtTermPrimWarningDialog(w,
  703. GETMESSAGE(NL_SETN_PrimRend, 1,"MEMORY FULL\nPress OK to clear"));
  704. }
  705. } else {
  706. /* figure out how many lines are needed... */
  707. linesNeeded = tpd->topRow + tpd->cursorRow -
  708. (tpd->lastUsedRow - 1);
  709. linesCopied = 0;
  710. /* before we drop them off of the top, if we are
  711. * using a history buffer, we need to transfer them
  712. * into the history buffer...
  713. */
  714. if (tpd->useHistoryBuffer &&
  715. (tpd->scrollLockTopRow == 0) &&
  716. (tpd->scrollLockBottomRow >= (tw->term.rows - 1))) {
  717. /* do we have enough lines in the history buffer, or
  718. * do we need to steal some from the top?...
  719. */
  720. historyLinesNeeded = linesNeeded -
  721. (tpd->historyBufferRows - tpd->lastUsedHistoryRow);
  722. if ((historyLinesNeeded > 0) &&
  723. (tpd->historyBufferRows > 0)) {
  724. /* take them from the top... */
  725. /* we are effectively deleting the lines from the
  726. * top of the combined buffers...
  727. */
  728. (void) _DtTermPrimSelectDeleteLines(w, 0,
  729. historyLinesNeeded);
  730. /* move the lines from the top to the bottom... */
  731. (void) _DtTermPrimBufferInsertLineFromTB(
  732. tpd->historyBuffer, tpd->historyBufferRows - 1,
  733. historyLinesNeeded, insertFromTop);
  734. /* the lines are now freed, adjust the used count... */
  735. tpd->lastUsedHistoryRow -= historyLinesNeeded;
  736. if (tpd->lastUsedHistoryRow < 0) {
  737. tpd->lastUsedHistoryRow = 0;
  738. }
  739. }
  740. /* copy the lines over... */
  741. for (i1 = 0; (i1 < linesNeeded) &&
  742. (tpd->lastUsedHistoryRow < tpd->historyBufferRows);
  743. i1++) {
  744. termChar *c1;
  745. short length;
  746. termChar *overflowChars;
  747. short overflowCount;
  748. /* get the line from the active buffer... */
  749. length = _DtTermPrimBufferGetLineLength(tBuffer,
  750. i1);
  751. /*
  752. ** stuff it into the history buffer...
  753. ** (but only if there is something to copy)
  754. */
  755. if (length > 0) {
  756. unsigned char eIndex;
  757. short eCol;
  758. short eCount;
  759. enhValue *eValues = (enhValue *)NULL;
  760. overflowChars = (termChar *) XtMalloc(BUFSIZ * sizeof (termChar));
  761. /* Perpetuate the enhancements. */
  762. for (eCol = 0; eCol < length; eCol += eCount)
  763. {
  764. if (_DtTermPrimBufferGetEnhancement(tBuffer,
  765. i1, eCol, &eValues,
  766. &eCount, countAll))
  767. {
  768. if ((eValues == (enhValue *)NULL) ||
  769. (eCount <= 0))
  770. break;
  771. for (eIndex = 0;
  772. eIndex < NUM_ENH_FIELDS(tBuffer);
  773. eIndex++)
  774. {
  775. _DtTermPrimBufferSetEnhancement(
  776. tpd->historyBuffer,
  777. tpd->lastUsedHistoryRow,
  778. eCol, eIndex, eValues[eIndex]);
  779. }
  780. c1 = _DtTermPrimBufferGetCharacterPointer(
  781. tBuffer, i1, eCol);
  782. (void) _DtTermPrimBufferInsert(
  783. tpd->historyBuffer,
  784. tpd->lastUsedHistoryRow,
  785. eCol, c1, eCount,
  786. False, &overflowChars,
  787. &overflowCount);
  788. }
  789. else break;
  790. }
  791. if (eCol < length)
  792. {
  793. /* Clear out enhancement values if necessary */
  794. if (eValues != (enhValue *)NULL)
  795. {
  796. for (eIndex = 0;
  797. eIndex < NUM_ENH_FIELDS(tBuffer);
  798. eIndex++)
  799. {
  800. _DtTermPrimBufferSetEnhancement(
  801. tpd->historyBuffer,
  802. tpd->lastUsedHistoryRow,
  803. eCol, eIndex, 0);
  804. }
  805. }
  806. c1 = _DtTermPrimBufferGetCharacterPointer(
  807. tBuffer, i1, eCol);
  808. (void) _DtTermPrimBufferInsert(
  809. tpd->historyBuffer,
  810. tpd->lastUsedHistoryRow,
  811. eCol, c1,
  812. length - eCol,
  813. False, &overflowChars,
  814. &overflowCount);
  815. }
  816. (void) XtFree((char *) overflowChars);
  817. } else {
  818. (void) _DtTermPrimBufferClearLine(
  819. tpd->historyBuffer,
  820. tpd->lastUsedHistoryRow, 0);
  821. }
  822. /* perpetuate the state of the wrap flag... */
  823. (void) _DtTermPrimBufferSetLineWrapFlag(
  824. tpd->historyBuffer,
  825. tpd->lastUsedHistoryRow,
  826. _DtTermPrimBufferTestLineWrapFlag(tBuffer, i1));
  827. /* perpetuate the state of the inSelection flag...
  828. */
  829. selectionFlag =
  830. _DtTermPrimBufferGetInSelectionFlag(
  831. tBuffer, i1);
  832. (void) _DtTermPrimBufferSetInSelectionFlag(
  833. tpd->historyBuffer,
  834. tpd->lastUsedHistoryRow,
  835. selectionFlag);
  836. /* clear the inselection flag before we scroll
  837. * the lines off or we will end up releasing the
  838. * selection...
  839. */
  840. (void) _DtTermPrimBufferSetInSelectionFlag(
  841. tBuffer, i1, (TermLineSelection) 0);
  842. (void) tpd->lastUsedHistoryRow++;
  843. (void) linesCopied++;
  844. }
  845. }
  846. /* take them from the top. If we are about to take lines
  847. * from the top without first copying them into the
  848. * history buffer, we need to adjust the selection
  849. * position...
  850. */
  851. if (linesNeeded > linesCopied) {
  852. (void) _DtTermPrimSelectDeleteLines(w,
  853. tpd->lastUsedHistoryRow + linesCopied,
  854. linesNeeded - linesCopied);
  855. }
  856. (void) _DtTermPrimBufferInsertLineFromTB(tBuffer,
  857. tpd->bufferRows - 1,
  858. linesNeeded, insertFromTop);
  859. /* adjust everything... */
  860. tpd->topRow -= linesNeeded;
  861. }
  862. }
  863. }
  864. }
  865. static short
  866. DoInsert(Widget w, unsigned char *buffer, int length, Boolean *wrapped)
  867. {
  868. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
  869. struct termData *tpd = tw->term.tpd;
  870. TermBuffer tBuffer = tpd->termBuffer;
  871. short newWidth;
  872. termChar *returnChars;
  873. short returnCount;
  874. /* before we insert any text, we need to insure that the cursor is on
  875. * a valid buffer row...
  876. */
  877. if (tpd->topRow + tpd->cursorRow >= tpd->lastUsedRow) {
  878. (void) _DtTermPrimFillScreenGap(w);
  879. }
  880. /* insert the text... */
  881. returnChars = (termChar *) XtMalloc(BUFSIZ * sizeof (termChar));
  882. newWidth = _DtTermPrimBufferInsert(tBuffer, /* TermBuffer */
  883. tpd->topRow + tpd->cursorRow, /* row */
  884. tpd->cursorColumn, /* column */
  885. (termChar *) buffer, /* newChars */
  886. length, /* numChars */
  887. tpd->insertCharMode != DtTERM_INSERT_CHAR_OFF,
  888. /* insert flag */
  889. &returnChars, /* return char ptr */
  890. &returnCount); /* return count ptr */
  891. if ((tpd->insertCharMode != DtTERM_INSERT_CHAR_ON_WRAP) || (returnCount <= 0)) {
  892. (void) XtFree((char *) returnChars);
  893. return(newWidth);
  894. }
  895. /* we are in insert char mode with wrap and we wrapped text off of
  896. * the line...
  897. */
  898. *wrapped = True;
  899. /* wrap the inserted characters into the following line... */
  900. if (tpd->topRow + tpd->cursorRow + 1 >= tpd->lastUsedRow) {
  901. /* fake cursorRow... */
  902. (void) tpd->cursorRow++;
  903. (void) _DtTermPrimFillScreenGap(w);
  904. (void) tpd->cursorRow--;
  905. }
  906. /* we will allocate a temporary buffer so we don't have to worry
  907. * about _DtTermPrimBufferInsert tromping over its overflow buffer...
  908. */
  909. length = returnCount;
  910. buffer = (unsigned char *) XtMalloc(length);
  911. (void) memcpy(buffer, returnChars, length);
  912. /* insert the text into the next line... */
  913. newWidth = _DtTermPrimBufferInsert(tBuffer, /* TermBuffer */
  914. tpd->topRow + tpd->cursorRow + 1,
  915. /* row */
  916. 0, /* column */
  917. (termChar *) buffer, /* newChars */
  918. length, /* numChars */
  919. tpd->insertCharMode != DtTERM_INSERT_CHAR_OFF,
  920. /* insert flag */
  921. &returnChars, /* return char ptr */
  922. &returnCount); /* return count ptr */
  923. (void) XtFree((char *) buffer);
  924. (void) XtFree((char *) returnChars);
  925. return(newWidth);
  926. }
  927. int
  928. _DtTermPrimInsertText(Widget w, unsigned char *buffer, int length)
  929. {
  930. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
  931. struct termData *tpd = tw->term.tpd;
  932. TermBuffer tBuffer = tpd->termBuffer;
  933. int i;
  934. short renderStartX;
  935. short renderEndX;
  936. short insertStartX;
  937. short insertCharCount;
  938. short newWidth;
  939. Boolean needToRender = False;
  940. Boolean wrapped = False;
  941. if (tpd->mbCurMax > 1)
  942. {
  943. short wcBufferLen;
  944. wchar_t *wcBuffer;
  945. wchar_t *pwc;
  946. int i;
  947. int mbLen;
  948. char *pmb;
  949. #ifdef NOCODE
  950. /*
  951. ** It would be nice if the calling function supplied us with a count
  952. ** of the number of mb characters in the buffer, then we wouldn't
  953. ** have to count them again.
  954. */
  955. /*
  956. ** we could use this if the multi-byte buffer was null terminated
  957. */
  958. wcBufferLen = mbstowcs((wchar_t *)NULL, (char *)buffer, length);
  959. #else /* NOCODE */
  960. i = 0;
  961. pmb = (char *)buffer;
  962. /*
  963. ** we should never need more than length * sizeof(wchar_t)
  964. ** bytes to store the wide char equivalent of the incoming mb string
  965. */
  966. wcBuffer = (wchar_t *)XtMalloc(length * sizeof(wchar_t));
  967. pwc = wcBuffer;
  968. wcBufferLen = 0;
  969. while (i < length)
  970. {
  971. switch (mbLen = mbtowc(pwc, pmb, MIN(((int)MB_CUR_MAX), length - i)))
  972. {
  973. case -1:
  974. if ((int)MB_CUR_MAX <= length - i) {
  975. /* we have a bogus multi-byte character. Throw away
  976. * the first byte and rescan (TM 12/14/93)...
  977. */
  978. /*
  979. ** in this case, we move the remaining length - i - 1
  980. ** bytes one byte to the left (to overwrite the bogus
  981. ** byte)
  982. */
  983. memmove(pmb, pmb + 1, length - i - 1);
  984. length--;
  985. continue;
  986. }
  987. break;
  988. case 0:
  989. /*
  990. ** treat null character same as any other character...
  991. */
  992. mbLen = 1;
  993. default:
  994. i += mbLen;
  995. pmb += mbLen;
  996. pwc++;
  997. wcBufferLen++;
  998. }
  999. }
  1000. #endif /* NOCODE */
  1001. i = _DtTermPrimInsertTextWc(w, wcBuffer, wcBufferLen);
  1002. /* convert back from a wide character count to a multibyte
  1003. * character count...
  1004. */
  1005. pmb = (char *)buffer;
  1006. wcBufferLen = i;
  1007. i = 0;
  1008. while (i < wcBufferLen)
  1009. {
  1010. switch (mbLen = mblen(pmb, MIN(((int)MB_CUR_MAX), length - i)))
  1011. {
  1012. case -1:
  1013. case 0:
  1014. /*
  1015. ** treat null character same as any other character...
  1016. */
  1017. mbLen = 1;
  1018. default:
  1019. i ++;
  1020. pmb += mbLen;
  1021. }
  1022. }
  1023. XtFree((char *)wcBuffer);
  1024. return(pmb - (char *) buffer);
  1025. }
  1026. /* turn off the cursor... */
  1027. if (CURSORoff != tpd->cursorState) {
  1028. (void) _DtTermPrimCursorOff(w);
  1029. }
  1030. /* we support two different types of autowrap. The HP style one is where
  1031. * you display the character and then wrap if you are at the end of the
  1032. * line. The ANSI style one is where you insert the character at the end
  1033. * of the line and don't autowrap until you try to insert another
  1034. * character...
  1035. */
  1036. renderStartX = tpd->cursorColumn;
  1037. renderEndX = tpd->cursorColumn;
  1038. insertStartX = tpd->cursorColumn;
  1039. insertCharCount = 0;
  1040. for (i = 0; (i < length) && tpd->ptyInputId; i++) {
  1041. /* the following code performs two functions. If we are in
  1042. * autowrap, it performs a sanity check on the insert position.
  1043. * if we are not in autowrap, it will insure that characters
  1044. * inserted after the last position will replace the last
  1045. * character...
  1046. */
  1047. if ((tpd->cursorColumn >= tw->term.columns) &&
  1048. !(tpd->autoWrapRight && !tpd->wrapRightAfterInsert)) {
  1049. /* blow away the previous character...
  1050. */
  1051. tpd->cursorColumn = tw->term.columns - 1;
  1052. renderStartX = MIN(renderStartX, tpd->cursorColumn);
  1053. tpd->wrapState = WRAPpastRightMargin;
  1054. }
  1055. /* for emulations that wrap after inserting the character, we
  1056. * will insert the character and then check for wrap...
  1057. */
  1058. if (tpd->wrapRightAfterInsert) {
  1059. if (insertCharCount == 0) {
  1060. insertStartX = i;
  1061. }
  1062. (void) insertCharCount++;
  1063. }
  1064. if (((tpd->cursorColumn + insertCharCount) >= tw->term.columns) ||
  1065. ((tpd->cursorColumn + insertCharCount) ==
  1066. tpd->rightMargin + 1)) {
  1067. if (tpd->autoWrapRight) {
  1068. /* perform an auto wrap...
  1069. */
  1070. /* we need to insert any pending characters, and
  1071. * render them...
  1072. */
  1073. if (insertCharCount) {
  1074. newWidth = DoInsert(w, &buffer[insertStartX],
  1075. insertCharCount, &wrapped);
  1076. tpd->cursorColumn += insertCharCount;
  1077. insertCharCount = 0;
  1078. if (tpd->insertCharMode != DtTERM_INSERT_CHAR_OFF) {
  1079. renderEndX = newWidth;
  1080. } else {
  1081. renderEndX = MAX(renderEndX, tpd->cursorColumn);
  1082. }
  1083. needToRender = True;
  1084. }
  1085. if (needToRender) {
  1086. DebugF('t', 0, fprintf(stderr,
  1087. ">>termInsertText() calling[2] _DtTermPrimRefreshText()\n"));
  1088. (void) _DtTermPrimRefreshText(w, renderStartX, tpd->cursorRow,
  1089. wrapped ? tw->term.columns : MAX(renderEndX, 0),
  1090. tpd->cursorRow);
  1091. if (wrapped && (tpd->cursorRow + 1 < tw->term.rows)) {
  1092. (void) _DtTermPrimRefreshText(w, 0, tpd->cursorRow + 1,
  1093. renderEndX, tpd->cursorRow + 1);
  1094. }
  1095. wrapped = False;
  1096. needToRender = False;
  1097. }
  1098. tpd->cursorColumn = tpd->leftMargin;
  1099. tpd->wrapState = WRAPbetweenMargins;
  1100. renderEndX = 0;
  1101. _DtTermPrimBufferSetLineWrapFlag(tBuffer,
  1102. tpd->topRow + tpd->cursorRow,
  1103. True);
  1104. if (tpd->cursorRow == tpd->scrollLockBottomRow) {
  1105. /* scroll at the bottom of the lock area... */
  1106. (void) _DtTermPrimScrollText(w, 1);
  1107. (void) _DtTermPrimFillScreenGap(w);
  1108. } else if (++tpd->cursorRow >= tw->term.rows) {
  1109. /* we are at the bottom row of the locked region.
  1110. * Wrap to the beginning of this line...
  1111. */
  1112. tpd->cursorRow = tw->term.rows - 1;
  1113. } else {
  1114. /* we are not at the bottom row. We already have
  1115. * wrapped down one line...
  1116. */
  1117. }
  1118. renderStartX = tpd->cursorColumn;
  1119. if (tpd->scroll.nojump.pendingScroll) {
  1120. /* If we wrapped the screen, bail out now and we
  1121. * will take care of this character when we
  1122. * finish the scroll. If we are in wrap after,
  1123. * then we need to skip past this character so
  1124. * that it doesn't get processed twice...
  1125. */
  1126. if (tpd->wrapRightAfterInsert)
  1127. (void) i++;
  1128. break;
  1129. }
  1130. } else {
  1131. /* overwrite the last character(s) on the line... */
  1132. if (insertCharCount > 0) {
  1133. newWidth = DoInsert(w, &buffer[insertStartX],
  1134. insertCharCount, &wrapped);
  1135. tpd->cursorColumn += insertCharCount;
  1136. insertCharCount = 0;
  1137. if (tpd->insertCharMode != DtTERM_INSERT_CHAR_OFF) {
  1138. renderEndX = newWidth;
  1139. } else {
  1140. renderEndX = MAX(renderEndX, tpd->cursorColumn);
  1141. }
  1142. needToRender = True;
  1143. }
  1144. if (tpd->cursorColumn == tpd->rightMargin + 1)
  1145. tpd->cursorColumn = tpd->rightMargin;
  1146. else
  1147. tpd->cursorColumn = tw->term.columns - 1;
  1148. }
  1149. }
  1150. /* for emulations that wrap before inserting the character, we
  1151. * will insert the character and then check for wrap...
  1152. */
  1153. if (!tpd->wrapRightAfterInsert) {
  1154. /* before we insert any text, we need to insure that the
  1155. * cursor is on a valid buffer row...
  1156. */
  1157. if (insertCharCount == 0) {
  1158. insertStartX = i;
  1159. }
  1160. (void) insertCharCount++;
  1161. }
  1162. }
  1163. /* insert and render any remaining text... */
  1164. if (insertCharCount > 0) {
  1165. newWidth = DoInsert(w, &buffer[insertStartX],
  1166. insertCharCount, &wrapped);
  1167. tpd->cursorColumn += insertCharCount;
  1168. if (tpd->insertCharMode != DtTERM_INSERT_CHAR_OFF) {
  1169. renderEndX = newWidth;
  1170. } else {
  1171. renderEndX = MAX(renderEndX, tpd->cursorColumn);
  1172. }
  1173. needToRender = True;
  1174. }
  1175. if (needToRender) {
  1176. renderEndX = MAX(renderEndX, tpd->cursorColumn);
  1177. DebugF('t', 0, fprintf(stderr,
  1178. ">>termInsertText() calling _DtTermPrimRefreshText()\n"));
  1179. (void) _DtTermPrimRefreshText(w, renderStartX, tpd->cursorRow,
  1180. wrapped ? tw->term.columns : MAX(renderEndX, 0),
  1181. tpd->cursorRow);
  1182. if (wrapped && (tpd->cursorRow + 1 < tw->term.rows)) {
  1183. (void) _DtTermPrimRefreshText(w, 0, tpd->cursorRow + 1,
  1184. renderEndX, tpd->cursorRow + 1);
  1185. }
  1186. }
  1187. return(i);
  1188. }
  1189. static void
  1190. buildDangleBuffer
  1191. (
  1192. unsigned char *buffer,
  1193. int bufferLen,
  1194. unsigned char *mbPartialChar,
  1195. int *mbPartialCharLen,
  1196. int writeLen,
  1197. unsigned char **dangleBuffer,
  1198. int *dangleBufferLen
  1199. )
  1200. {
  1201. /* malloc our dangle buffer... */
  1202. *dangleBufferLen = bufferLen + *mbPartialCharLen - writeLen;
  1203. *dangleBuffer = (unsigned char *) XtMalloc(*dangleBufferLen);
  1204. /* copy over the unwritten part of the original buffer... */
  1205. (void) memmove(*dangleBuffer, buffer + writeLen, bufferLen - writeLen);
  1206. if (*mbPartialCharLen) {
  1207. (void) memmove(*dangleBuffer + bufferLen - writeLen, mbPartialChar,
  1208. *mbPartialCharLen);
  1209. *mbPartialCharLen = 0;
  1210. }
  1211. return;
  1212. }
  1213. Boolean
  1214. _DtTermPrimParseInput
  1215. (
  1216. Widget w,
  1217. unsigned char *buffer,
  1218. int len,
  1219. unsigned char **dangleBuffer,
  1220. int *dangleBufferLen
  1221. )
  1222. {
  1223. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
  1224. struct termData *tpd = tw->term.tpd;
  1225. DtTermPrimitiveClassPart *termClassPart = &(((DtTermPrimitiveClassRec *)
  1226. (tw->core.widget_class))->term_primitive_class);
  1227. int i;
  1228. short insertStart;
  1229. short insertByteCount;
  1230. short returnLen;
  1231. Boolean turnCursorOn = False;
  1232. unsigned char *tmpBuffer = (unsigned char *) 0;
  1233. unsigned char mbChar[MB_LEN_MAX];
  1234. int mbCharLen = 1;
  1235. static Boolean *preParseTable = (Boolean *) 0;
  1236. *dangleBuffer = (unsigned char *) 0;
  1237. insertStart = 0;
  1238. insertByteCount = 0;
  1239. /* initialize the preParseTable if necessary... */
  1240. _DtTermProcessLock();
  1241. if (!preParseTable) {
  1242. preParseTable = (Boolean *) XtMalloc(256 * sizeof(Boolean));
  1243. (void) memset(preParseTable, '\0', 256 * sizeof(Boolean));
  1244. for (i = 0x00; i <= 0x1f; i++) {
  1245. preParseTable[i] = True;
  1246. }
  1247. for (i = 0x80; i <= 0x9f; i++) {
  1248. preParseTable[i] = True;
  1249. }
  1250. }
  1251. _DtTermProcessUnlock();
  1252. /* check for partial multibyte character... */
  1253. if (tpd->mbPartialCharLen > 0) {
  1254. tmpBuffer = (unsigned char *) XtMalloc(len + tpd->mbPartialCharLen);
  1255. (void) memcpy(tmpBuffer, tpd->mbPartialChar, tpd->mbPartialCharLen);
  1256. (void) memcpy(tmpBuffer + tpd->mbPartialCharLen, buffer, len);
  1257. buffer = tmpBuffer;
  1258. len += tpd->mbPartialCharLen;
  1259. tpd->mbPartialCharLen = 0;
  1260. }
  1261. /* turn off the cursor... */
  1262. if (CURSORoff != tpd->cursorState) {
  1263. (void) _DtTermPrimCursorOff(w);
  1264. turnCursorOn = True;
  1265. }
  1266. for (i = 0; (i < len) && tpd->ptyInputId; ) {
  1267. if (tpd->mbCurMax > 1) {
  1268. switch (mbCharLen =
  1269. mblen((char *) &buffer[i], MIN(((int)MB_CUR_MAX), len - i)))
  1270. {
  1271. case -1:
  1272. if ((int)MB_CUR_MAX <= len - i)
  1273. {
  1274. /* we have a bogus multi-byte character. Throw away
  1275. * the first byte and rescan (TM 12/14/93)...
  1276. */
  1277. /* dump what we know we want to insert... */
  1278. if (insertByteCount > 0) {
  1279. returnLen = (*(termClassPart->term_insert_proc))(w,
  1280. &buffer[insertStart], insertByteCount);
  1281. if (returnLen != insertByteCount) {
  1282. (void) buildDangleBuffer(buffer, len,
  1283. tpd->mbPartialChar,
  1284. &tpd->mbPartialCharLen,
  1285. insertStart + returnLen,
  1286. dangleBuffer, dangleBufferLen);
  1287. insertByteCount = 0;
  1288. break;
  1289. }
  1290. insertByteCount = 0;
  1291. }
  1292. /* skip over the bogus char's first byte... */
  1293. (void) i++;
  1294. insertStart = i;
  1295. continue;
  1296. } else {
  1297. /* we have a dangling partial multi-byte character... */
  1298. (void) memmove(tpd->mbPartialChar, &buffer[i], len - i);
  1299. tpd->mbPartialCharLen = len - i;
  1300. /* remove the partial char from the buffer and adjust
  1301. * the buffer len...
  1302. */
  1303. len = i;
  1304. continue;
  1305. }
  1306. break;
  1307. case 0:
  1308. mbCharLen = 1;
  1309. /* fall through */
  1310. default:
  1311. break;
  1312. }
  1313. }
  1314. if (((mbCharLen == 1) && preParseTable[buffer[i]]) ||
  1315. tpd->parserNotInStartState) {
  1316. /*
  1317. ** It's either a control code or we are not in the start state.
  1318. ** If we have any text to insert, insert
  1319. ** the text, display any added text, then send it down through
  1320. ** the parser.
  1321. */
  1322. if (insertByteCount > 0) {
  1323. returnLen = (*(termClassPart->term_insert_proc))(w,
  1324. &buffer[insertStart], insertByteCount);
  1325. if (returnLen != insertByteCount) {
  1326. (void) buildDangleBuffer(buffer, len,
  1327. tpd->mbPartialChar,
  1328. &tpd->mbPartialCharLen,
  1329. insertStart + returnLen,
  1330. dangleBuffer, dangleBufferLen);
  1331. insertByteCount = 0;
  1332. break;
  1333. }
  1334. insertByteCount = 0;
  1335. }
  1336. tpd->parserNotInStartState = _DtTermPrimParse(w, &buffer[i],
  1337. mbCharLen);
  1338. i += mbCharLen;
  1339. insertStart = i;
  1340. } else {
  1341. /* queue up text to insert into the buffer... */
  1342. insertByteCount += mbCharLen;
  1343. i += mbCharLen;
  1344. }
  1345. }
  1346. if (insertByteCount > 0)
  1347. {
  1348. returnLen = (*(termClassPart->term_insert_proc))(w,
  1349. &buffer[insertStart],
  1350. insertByteCount);
  1351. if (returnLen != insertByteCount)
  1352. {
  1353. (void) buildDangleBuffer(buffer, len,
  1354. tpd->mbPartialChar,
  1355. &tpd->mbPartialCharLen,
  1356. insertStart + returnLen,
  1357. dangleBuffer, dangleBufferLen);
  1358. }
  1359. }
  1360. else
  1361. {
  1362. /*
  1363. ** insertByteCount <= 0, check to make sure we haven't
  1364. ** already saved any remaining text in the dangleBuffer...
  1365. */
  1366. if (i < len && !*dangleBuffer)
  1367. {
  1368. (void) buildDangleBuffer(buffer, len,
  1369. tpd->mbPartialChar,
  1370. &tpd->mbPartialCharLen,
  1371. i, dangleBuffer, dangleBufferLen);
  1372. }
  1373. }
  1374. /* if we turned the cursor off, turn the cursor back on... */
  1375. if (turnCursorOn) {
  1376. (void) _DtTermPrimCursorOn(w);
  1377. }
  1378. if (tmpBuffer) {
  1379. (void) XtFree((char *) tmpBuffer);
  1380. }
  1381. if (*dangleBuffer) {
  1382. return(False);
  1383. }
  1384. return(True);
  1385. }
  1386. /*
  1387. ** Pad the current line from the current end of line up to (and
  1388. ** including) the current cursor column.
  1389. */
  1390. void
  1391. _DtTermPrimRenderPadLine
  1392. (
  1393. Widget w
  1394. )
  1395. {
  1396. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
  1397. struct termData *tpd = tw->term.tpd;
  1398. TermBuffer tBuffer = tpd->termBuffer;
  1399. short currentWidth;
  1400. /* before we pad this line, we need to insure that cursor is on a
  1401. * valid buffer row...
  1402. */
  1403. (void) _DtTermPrimFillScreenGap(w);
  1404. currentWidth = _DtTermPrimBufferGetLineWidth(tBuffer,
  1405. tpd->topRow + tpd->cursorRow);
  1406. if (tpd->cursorColumn >= currentWidth)
  1407. {
  1408. /*
  1409. ** Pad the line and refresh it.
  1410. */
  1411. if (tpd->mbCurMax > 1) {
  1412. _DtTermPrimBufferPadLineWc(tBuffer, tpd->topRow + tpd->cursorRow,
  1413. tpd->cursorColumn + 1);
  1414. } else {
  1415. _DtTermPrimBufferPadLine(tBuffer, tpd->topRow + tpd->cursorRow,
  1416. tpd->cursorColumn + 1);
  1417. }
  1418. _DtTermPrimRefreshText(w, currentWidth, tpd->cursorRow, tpd->cursorColumn,
  1419. tpd->cursorRow);
  1420. }
  1421. }
  1422. void
  1423. _DtTermPrimDestroyFont(
  1424. Widget w,
  1425. TermFont font
  1426. )
  1427. {
  1428. if (font) (void) (*(font->destroyFunction))(w, font);
  1429. return;
  1430. }
  1431. void
  1432. _DtTermPrimRenderText(
  1433. Widget w,
  1434. TermFont font,
  1435. Pixel fg,
  1436. Pixel bg,
  1437. unsigned long flags,
  1438. int x,
  1439. int y,
  1440. unsigned char *string,
  1441. int len
  1442. )
  1443. {
  1444. (void) (*(font->renderFunction))(w, font, fg, bg, flags, x, y, string, len);
  1445. return;
  1446. }