1 /* Copyright (c) 1993-2002
2 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
3 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
4 * Copyright (c) 1987 Oliver Laumann
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3, or (at your option)
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program (see the file COPYING); if not, see
18 * http://www.gnu.org/licenses/, or contact Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
21 ****************************************************************
24 #include <sys/types.h>
28 # include <sys/ioctl.h>
36 static int CountChars
__P((int));
37 static int DoAddChar
__P((int));
38 static int BlankResize
__P((int, int));
39 static int CallRewrite
__P((int, int, int, int));
40 static void FreeCanvas
__P((struct canvas
*));
41 static void disp_readev_fn
__P((struct event
*, char *));
42 static void disp_writeev_fn
__P((struct event
*, char *));
44 static void disp_writeev_eagain
__P((struct event
*, char *));
46 static void disp_status_fn
__P((struct event
*, char *));
47 static void disp_hstatus_fn
__P((struct event
*, char *));
48 static void disp_blocked_fn
__P((struct event
*, char *));
49 static void cv_winid_fn
__P((struct event
*, char *));
51 static void disp_map_fn
__P((struct event
*, char *));
53 static void disp_idle_fn
__P((struct event
*, char *));
55 static void disp_blanker_fn
__P((struct event
*, char *));
57 static void WriteLP
__P((int, int));
58 static void INSERTCHAR
__P((int));
59 static void RAW_PUTCHAR
__P((int));
61 static void SetBackColor
__P((int));
63 static void FreePerp
__P((struct canvas
*));
64 static struct canvas
*AddPerp
__P((struct canvas
*));
67 extern struct layer
*flayer
;
68 extern struct win
*windows
, *fore
;
69 extern struct LayFuncs WinLf
;
71 extern int use_hardstatus
;
72 extern int MsgWait
, MsgMinWait
;
73 extern int Z0width
, Z1width
;
74 extern unsigned char *blank
, *null
;
75 extern struct mline mline_blank
, mline_null
, mline_old
;
76 extern struct mchar mchar_null
, mchar_blank
, mchar_so
;
77 extern struct NewWindow nwin_default
;
78 extern struct action idleaction
;
80 /* XXX shouldn't be here */
81 extern char *hstatusstring
;
82 extern char *captionstring
;
88 extern int pty_preopen
;
89 #if defined(TIOCSWINSZ) || defined(TIOCGWINSZ)
90 extern struct winsize glwz
;
93 extern int real_uid
, real_gid
;
97 * tputs needs this to calculate the padding
101 #endif /* NEED_OSPEED */
105 struct display
*display
, *displays
;
107 int attr2color
[8][4];
112 struct display TheDisplay
;
118 int defobuflimit
= OBUF_MAX
;
119 int defnonblock
= -1;
124 int hardstatusemu
= HSTATUS_IGNORE
;
126 int focusminwidth
, focusminheight
;
129 * Default layer management
133 DefProcess(bufp
, lenp
)
142 DefRedisplayLine(y
, xs
, xe
, isblank
)
143 int y
, xs
, xe
, isblank
;
145 if (isblank
== 0 && y
>= 0)
146 DefClearLine(y
, xs
, xe
, 0);
150 DefClearLine(y
, xs
, xe
, bce
)
153 LClearLine(flayer
, y
, xs
, xe
, bce
, (struct mline
*)0);
158 DefRewrite(y
, xs
, xe
, rend
, doit
)
176 LAY_DISPLAYS(flayer
, InsertMode(0));
177 /* ChangeScrollRegion(0, D_height - 1); */
178 LKeypadMode(flayer
, 0);
179 LCursorkeysMode(flayer
, 0);
180 LCursorVisibility(flayer
, 0);
181 LMouseMode(flayer
, 0);
182 LSetRendition(flayer
, &mchar_null
);
183 LSetFlow(flayer
, nwin_default
.flowflag
& FLOW_NOW
);
187 * Blank layer management
190 struct LayFuncs BlankLf
=
206 flayer
->l_width
= wi
;
207 flayer
->l_height
= he
;
213 * Generate new display, start with a blank layer.
214 * The termcap arrays are not initialised here.
215 * The new display is placed in the displays list.
219 MakeDisplay(uname
, utty
, term
, fd
, pid
, Mode
)
220 char *uname
, *utty
, *term
;
225 struct baud_values
*b
;
227 if (!*(u
= FindUserPtr(uname
)) && UserAdd(uname
, (char *)0, u
))
228 return 0; /* could not find or add user */
231 if ((display
= (struct display
*)calloc(1, sizeof(*display
))) == 0)
236 bzero((char *)&TheDisplay
, sizeof(TheDisplay
));
237 display
= &TheDisplay
;
239 display
->d_next
= displays
;
242 D_nonblock
= defnonblock
;
244 D_readev
.fd
= D_writeev
.fd
= fd
;
245 D_readev
.type
= EV_READ
;
246 D_writeev
.type
= EV_WRITE
;
247 D_readev
.data
= D_writeev
.data
= (char *)display
;
248 D_readev
.handler
= disp_readev_fn
;
249 D_writeev
.handler
= disp_writeev_fn
;
251 D_writeev
.condpos
= &D_obuflen
;
252 D_writeev
.condneg
= &D_obuffree
;
254 D_statusev
.type
= EV_TIMEOUT
;
255 D_statusev
.data
= (char *)display
;
256 D_statusev
.handler
= disp_status_fn
;
257 D_hstatusev
.type
= EV_TIMEOUT
;
258 D_hstatusev
.data
= (char *)display
;
259 D_hstatusev
.handler
= disp_hstatus_fn
;
260 D_blockedev
.type
= EV_TIMEOUT
;
261 D_blockedev
.data
= (char *)display
;
262 D_blockedev
.handler
= disp_blocked_fn
;
263 D_blockedev
.condpos
= &D_obuffree
;
264 D_blockedev
.condneg
= &D_obuflenmax
;
265 D_hstatusev
.handler
= disp_hstatus_fn
;
267 D_mapev
.type
= EV_TIMEOUT
;
268 D_mapev
.data
= (char *)display
;
269 D_mapev
.handler
= disp_map_fn
;
271 D_idleev
.type
= EV_TIMEOUT
;
272 D_idleev
.data
= (char *)display
;
273 D_idleev
.handler
= disp_idle_fn
;
275 D_blankerev
.type
= EV_READ
;
276 D_blankerev
.data
= (char *)display
;
277 D_blankerev
.handler
= disp_blanker_fn
;
281 D_status_obuffree
= -1;
282 Resize_obuf(); /* Allocate memory for buffer */
283 D_obufmax
= defobuflimit
;
284 D_obuflenmax
= D_obuflen
- D_obufmax
;
286 D_auto_nuke
= defautonuke
;
293 if ((b
= lookup_baud((int)cfgetospeed(&D_OldMode
.tio
))))
297 if ((b
= lookup_baud(D_OldMode
.tio
.c_cflag
& CBAUD
)))
300 D_dospeed
= (short)D_OldMode
.m_ttyb
.sg_ospeed
;
303 debug1("New displays ospeed = %d\n", D_dospeed
);
305 strncpy(D_usertty
, utty
, sizeof(D_usertty
) - 1);
306 D_usertty
[sizeof(D_usertty
) - 1] = 0;
307 strncpy(D_termname
, term
, sizeof(D_termname
) - 1);
308 D_termname
[sizeof(D_termname
) - 1] = 0;
310 D_processinput
= ProcessInput
;
320 struct display
*d
, **dp
;
334 SetTTY(D_userfd
, &D_OldMode
);
335 fcntl(D_userfd
, F_SETFL
, 0);
341 if (D_processinputdata
)
342 free(D_processinputdata
);
343 D_processinputdata
= 0;
368 if (bd
.bd_dpy
== display
)
370 bd
.bd_start_braille
= 0;
376 for (dp
= &displays
; (d
= *dp
) ; dp
= &d
->d_next
)
380 if (D_status_lastmsg
)
381 free(D_status_lastmsg
);
384 *dp
= display
->d_next
;
386 ASSERT(display
== displays
);
387 ASSERT(display
== &TheDisplay
);
391 while (D_canvas
.c_slperp
)
392 FreeCanvas(D_canvas
.c_slperp
);
395 for (p
= windows
; p
; p
= p
->w_next
)
397 if (p
->w_pdisplay
== display
)
399 if (p
->w_lastdisp
== display
)
401 if (p
->w_readev
.condneg
== &D_status
|| p
->w_readev
.condneg
== &D_obuflenmax
)
402 p
->w_readev
.condpos
= p
->w_readev
.condneg
= 0;
405 for (p
= windows
; p
; p
= p
->w_next
)
406 if (p
->w_zdisplay
== display
)
410 free((char *)display
);
419 cv
->c_blank
.l_cvlist
= cv
;
420 cv
->c_blank
.l_width
= cv
->c_xe
- cv
->c_xs
+ 1;
421 cv
->c_blank
.l_height
= cv
->c_ye
- cv
->c_ys
+ 1;
422 cv
->c_blank
.l_x
= cv
->c_blank
.l_y
= 0;
423 cv
->c_blank
.l_layfn
= &BlankLf
;
424 cv
->c_blank
.l_data
= 0;
425 cv
->c_blank
.l_next
= 0;
426 cv
->c_blank
.l_bottom
= &cv
->c_blank
;
427 cv
->c_blank
.l_blocking
= 0;
428 cv
->c_layer
= &cv
->c_blank
;
437 if ((cv
= (struct canvas
*)calloc(1, sizeof *cv
)) == 0)
440 cv
->c_xe
= D_width
- 1;
442 cv
->c_ye
= D_height
- 1 - (D_has_hstatus
== HSTATUS_LASTLINE
) - captionalways
;
443 debug2("MakeDefaultCanvas 0,0 %d,%d\n", cv
->c_xe
, cv
->c_ye
);
447 cv
->c_display
= display
;
453 cv
->c_slback
= &D_canvas
;
454 D_canvas
.c_slperp
= cv
;
455 D_canvas
.c_xs
= cv
->c_xs
;
456 D_canvas
.c_xe
= cv
->c_xe
;
457 D_canvas
.c_ys
= cv
->c_ys
;
458 D_canvas
.c_ye
= cv
->c_ye
;
459 cv
->c_slorient
= SLICE_UNKN
;
460 cv
->c_captev
.type
= EV_TIMEOUT
;
461 cv
->c_captev
.data
= (char *)cv
;
462 cv
->c_captev
.handler
= cv_winid_fn
;
468 RethinkDisplayViewports();
469 D_forecv
= cv
; /* default input focus */
473 static struct canvas
**
474 CreateCanvasChainRec(cv
, cvp
)
478 for (; cv
; cv
= cv
->c_slnext
)
481 cvp
= CreateCanvasChainRec(cv
->c_slperp
, cvp
);
492 RecreateCanvasChain()
495 cvp
= CreateCanvasChainRec(D_canvas
.c_slperp
, &D_cvlist
);
503 struct viewport
*vp
, *nvp
;
508 cv
->c_slprev
->c_slnext
= cv
->c_slnext
;
510 cv
->c_slnext
->c_slprev
= cv
->c_slprev
;
511 if (cv
->c_slback
&& cv
->c_slback
->c_slperp
== cv
)
512 cv
->c_slback
->c_slperp
= cv
->c_slnext
? cv
->c_slnext
: cv
->c_slprev
;
516 FreeCanvas(cv
->c_slperp
);
525 /* remove from canvas chain as SetCanvasWindow might call
526 * some layer function */
527 for (cvp
= &D_cvlist
; *cvp
; cvp
= &(*cvp
)->c_next
)
534 p
= cv
->c_layer
? Layer2Window(cv
->c_layer
) : 0;
535 SetCanvasWindow(cv
, 0);
537 WindowChanged(p
, 'u');
538 if (flayer
== cv
->c_layer
)
540 for (vp
= cv
->c_vplist
; vp
; vp
= nvp
)
547 evdeq(&cv
->c_captev
);
556 for (; cv
; cv
= cv
->c_slnext
)
562 for (cvp
= cv
->c_slperp
; cvp
; cvp
= cvp
->c_slnext
)
565 n
= CountCanvas(cvp
->c_slperp
);
583 for (cvp
= cv
->c_slperp
; cvp
; cvp
= cvp
->c_slnext
)
586 n
= CountCanvas(cvp
->c_slperp
);
594 EqualizeCanvas(cv
, gflag
)
599 for (; cv
; cv
= cv
->c_slnext
)
601 if (cv
->c_slperp
&& gflag
)
603 cv
->c_slweight
= CountCanvasPerp(cv
);
604 for (cv2
= cv
->c_slperp
; cv2
; cv2
= cv2
->c_slnext
)
606 EqualizeCanvas(cv2
->c_slperp
, gflag
);
617 struct canvas
*cv2
, *cvn
, *fcv
;
618 int nh
, i
, maxi
, hh
, m
, w
, wsum
;
628 debug2("ResizeCanvas: %d,%d", xs
, ys
);
629 debug2(" %d,%d\n", xe
, ye
);
632 if (cv
->c_slorient
== SLICE_UNKN
)
634 ASSERT(!cv
->c_slnext
&& !cv
->c_slperp
);
639 cv
->c_xoff
= cv
->c_xs
;
640 cv
->c_yoff
= cv
->c_ys
;
641 cv
->c_blank
.l_width
= cv
->c_xe
- cv
->c_xs
+ 1;
642 cv
->c_blank
.l_height
= cv
->c_ye
- cv
->c_ys
+ 1;
647 if (focusminwidth
|| focusminheight
)
649 debug("searching for focus canvas\n");
651 while (cv2
->c_slback
)
653 if (cv2
->c_slback
== cv
->c_slback
)
656 focusmin
= cv
->c_slorient
== SLICE_VERT
? focusminheight
: focusminwidth
;
659 else if (focusmin
< 0)
660 focusmin
= cv
->c_slorient
== SLICE_VERT
? ye
- ys
+ 2 : xe
- xs
+ 2;
661 debug1("found, focusmin=%d\n", focusmin
);
668 m
= CountCanvas(cv
) * 2;
669 nh
= cv
->c_slorient
== SLICE_VERT
? ye
- ys
+ 2 : xe
- xs
+ 2;
675 debug1("corrected to %d\n", focusmin
);
678 /* pass 1: calculate weight sum */
679 for (cv2
= cv
, wsum
= 0; cv2
; cv2
= cv2
->c_slnext
)
681 debug1(" weight %d\n", cv2
->c_slweight
);
682 wsum
+= cv2
->c_slweight
;
684 debug1("wsum = %d\n", wsum
);
689 /* pass 2: calculate need/excess space */
690 nh
= cv
->c_slorient
== SLICE_VERT
? ye
- ys
+ 2 : xe
- xs
+ 2;
691 for (cv2
= cv
, need
= got
= 0; cv2
; cv2
= cv2
->c_slnext
)
693 m
= cv2
->c_slperp
? CountCanvasPerp(cv2
) * 2 - 1 : 1;
696 hh
= cv2
->c_slweight
? nh
* cv2
->c_slweight
/ w
: 0;
697 w
-= cv2
->c_slweight
;
699 debug2(" should %d min %d\n", hh
, m
);
705 debug2("need: %d, got %d\n", need
, got
);
709 /* pass 3: distribute space */
710 nh
= cv
->c_slorient
== SLICE_VERT
? ye
- ys
+ 2 : xe
- xs
+ 2;
711 i
= cv
->c_slorient
== SLICE_VERT
? ys
: xs
;
712 maxi
= cv
->c_slorient
== SLICE_VERT
? ye
: xe
;
719 if (cv
->c_slprev
&& !cv
->c_slback
->c_slback
&& !cv
->c_slprev
->c_slperp
&& !cv
->c_slprev
->c_slprev
)
721 cv
->c_slprev
->c_slorient
= SLICE_UNKN
;
724 cv
->c_slback
->c_ye
++;
725 cv
->c_slprev
->c_ye
++;
728 SetCanvasWindow(cv
, 0);
732 m
= cv
->c_slperp
? CountCanvasPerp(cv
) * 2 - 1 : 1;
735 hh
= cv
->c_slweight
? nh
* cv
->c_slweight
/ w
: 0;
738 debug2(" should %d min %d\n", hh
, m
);
742 debug1(" -> %d\n", hh
);
746 int hx
= need
* (hh
- m
- 1) / got
;
747 debug3(" -> %d - %d = %d\n", hh
, hx
, hh
- hx
);
751 debug2(" now need=%d got=%d\n", need
, got
);
754 /* hh is window size plus pation line */
755 if (i
+ hh
> maxi
+ 2)
758 debug1(" not enough space, reducing to %d\n", hh
);
760 if (i
+ hh
== maxi
+ 1)
763 debug(" incrementing as no other canvas will fit\n");
765 if (cv
->c_slorient
== SLICE_VERT
)
770 cv
->c_ye
= i
+ hh
- 2;
777 cv
->c_xe
= i
+ hh
- 2;
783 cv
->c_xoff
= cv
->c_xs
;
784 cv
->c_yoff
= cv
->c_ys
;
785 cv
->c_blank
.l_width
= cv
->c_xe
- cv
->c_xs
+ 1;
786 cv
->c_blank
.l_height
= cv
->c_ye
- cv
->c_ys
+ 1;
790 if (!cv
->c_slperp
->c_slnext
)
792 debug("deleting perp node\n");
793 FreePerp(cv
->c_slperp
);
801 static struct canvas
*
806 debug("Creating new perp node\n");
808 if ((pcv
= (struct canvas
*)calloc(1, sizeof *cv
)) == 0)
811 pcv
->c_display
= cv
->c_display
;
812 pcv
->c_slnext
= cv
->c_slnext
;
813 pcv
->c_slprev
= cv
->c_slprev
;
815 pcv
->c_slback
= cv
->c_slback
;
816 if (cv
->c_slback
&& cv
->c_slback
->c_slperp
== cv
)
817 cv
->c_slback
->c_slperp
= pcv
;
818 pcv
->c_slorient
= cv
->c_slorient
;
821 pcv
->c_xs
= cv
->c_xs
;
822 pcv
->c_xe
= cv
->c_xe
;
823 pcv
->c_ys
= cv
->c_ys
;
824 pcv
->c_ye
= cv
->c_ye
;
826 pcv
->c_slnext
->c_slprev
= pcv
;
828 pcv
->c_slprev
->c_slnext
= pcv
;
829 pcv
->c_slweight
= cv
->c_slweight
;
830 CanvasInitBlank(pcv
);
836 cv
->c_slorient
= SLICE_UNKN
;
849 cv
->c_slprev
= pcv
->c_slprev
;
851 cv
->c_slprev
->c_slnext
= cv
;
852 cv
->c_slback
= pcv
->c_slback
;
853 if (cv
->c_slback
&& cv
->c_slback
->c_slperp
== pcv
)
854 cv
->c_slback
->c_slperp
= cv
;
855 cv
->c_slorient
= pcv
->c_slorient
;
856 cv
->c_slweight
= pcv
->c_slweight
;
860 cv
->c_slorient
= pcv
->c_slorient
;
861 cv
->c_slback
= pcv
->c_slback
;
862 cv
->c_slweight
= pcv
->c_slweight
;
864 cv
->c_slnext
= pcv
->c_slnext
;
866 cv
->c_slnext
->c_slprev
= cv
;
879 debug2("AddCanvas orient %d, forecv is %d\n", orient
, cv
->c_slorient
);
881 if (cv
->c_slorient
!= SLICE_UNKN
&& cv
->c_slorient
!= orient
)
886 xs
= cv
->c_slback
->c_xs
;
887 xe
= cv
->c_slback
->c_xe
;
888 ys
= cv
->c_slback
->c_ys
;
889 ye
= cv
->c_slback
->c_ye
;
890 if (!captionalways
&& cv
== D_canvas
.c_slperp
&& !cv
->c_slnext
)
891 ye
--; /* need space for caption */
892 debug2("Adding Canvas to slice %d,%d ", xs
, ys
);
893 debug2("%d,%d\n", xe
, ye
);
895 num
= CountCanvas(cv
->c_slback
->c_slperp
) + 1;
896 debug1("Num = %d\n", num
);
897 if (orient
== SLICE_VERT
)
904 return -1; /* can't fit in */
906 if ((cv
= (struct canvas
*)calloc(1, sizeof *cv
)) == 0)
909 D_forecv
->c_slback
->c_ye
= ye
; /* in case we modified it above */
910 D_forecv
->c_slorient
= orient
; /* in case it was UNKN */
911 cv
->c_slnext
= D_forecv
->c_slnext
;
912 cv
->c_slprev
= D_forecv
;
913 D_forecv
->c_slnext
= cv
;
915 cv
->c_slnext
->c_slprev
= cv
;
916 cv
->c_slorient
= orient
;
917 cv
->c_slback
= D_forecv
->c_slback
;
925 cv
->c_display
= display
;
927 cv
->c_captev
.type
= EV_TIMEOUT
;
928 cv
->c_captev
.data
= (char *)cv
;
929 cv
->c_captev
.handler
= cv_winid_fn
;
937 EqualizeCanvas(cv
->c_slperp
, 0);
939 RecreateCanvasChain();
940 RethinkDisplayViewports();
941 ResizeLayersToCanvases();
951 debug("RemCanvas\n");
953 if (cv
->c_slorient
== SLICE_UNKN
)
959 if (!cv
->c_slnext
->c_slnext
&& cv
->c_slback
->c_slback
)
961 /* two canvases in slice, kill perp node */
963 debug("deleting perp node\n");
964 FreePerp(cv
->c_slprev
? cv
->c_slprev
: cv
->c_slnext
);
965 FreePerp(cv
->c_slback
);
967 xs
= cv
->c_slback
->c_xs
;
968 xe
= cv
->c_slback
->c_xe
;
969 ys
= cv
->c_slback
->c_ys
;
970 ye
= cv
->c_slback
->c_ye
;
973 D_forecv
= cv
->c_slprev
;
975 D_forecv
= cv
->c_slnext
;
979 while (D_forecv
->c_slperp
)
980 D_forecv
= D_forecv
->c_slperp
;
982 /* if only one canvas left, set orient back to unknown */
983 if (!cv
->c_slnext
&& !cv
->c_slprev
&& !cv
->c_slback
->c_slback
&& !cv
->c_slperp
)
985 cv
->c_slorient
= SLICE_UNKN
;
987 cv
->c_slback
->c_ye
= ++ye
; /* caption line no longer needed */
990 EqualizeCanvas(cv
->c_slperp
, 0);
993 D_fore
= Layer2Window(D_forecv
->c_layer
);
994 flayer
= D_forecv
->c_layer
;
996 RecreateCanvasChain();
997 RethinkDisplayViewports();
998 ResizeLayersToCanvases();
1004 struct canvas
*cv
= D_forecv
, *ocv
= 0;
1009 cv
->c_slprev
->c_slnext
= cv
->c_slnext
;
1014 cv
->c_slnext
->c_slprev
= cv
->c_slprev
;
1018 if (cv
->c_slback
&& cv
->c_slback
->c_slperp
== cv
)
1019 cv
->c_slback
->c_slperp
= ocv
;
1020 cv
->c_slorient
= SLICE_UNKN
;
1021 while (D_canvas
.c_slperp
)
1022 FreeCanvas(D_canvas
.c_slperp
);
1024 D_canvas
.c_slperp
= cv
;
1025 cv
->c_slback
= &D_canvas
;
1028 ASSERT(!cv
->c_slperp
);
1030 D_canvas
.c_ye
++; /* caption line no longer needed */
1031 ResizeCanvas(&D_canvas
);
1032 RecreateCanvasChain();
1033 RethinkDisplayViewports();
1034 ResizeLayersToCanvases();
1038 RethinkDisplayViewports()
1041 struct viewport
*vp
, *vpn
;
1043 /* free old viewports */
1044 for (cv
= display
->d_cvlist
; cv
; cv
= cv
->c_next
)
1046 for (vp
= cv
->c_vplist
; vp
; vp
= vpn
)
1050 bzero((char *)vp
, sizeof(*vp
));
1055 display
->d_vpxmin
= -1;
1056 display
->d_vpxmax
= -1;
1058 for (cv
= display
->d_cvlist
; cv
; cv
= cv
->c_next
)
1060 if ((vp
= (struct viewport
*)malloc(sizeof *vp
)) == 0)
1064 vp
->v_xs
= cv
->c_xs
;
1065 vp
->v_ys
= (cv
->c_ys
+ cv
->c_ye
) / 2;
1066 vp
->v_xe
= cv
->c_xe
;
1067 vp
->v_ye
= cv
->c_ye
;
1068 vp
->v_xoff
= cv
->c_xoff
;
1069 vp
->v_yoff
= cv
->c_yoff
;
1070 vp
->v_next
= cv
->c_vplist
;
1073 if ((vp
= (struct viewport
*)malloc(sizeof *vp
)) == 0)
1076 vp
->v_xs
= (cv
->c_xs
+ cv
->c_xe
) / 2;
1077 vp
->v_ys
= (3 * cv
->c_ys
+ cv
->c_ye
) / 4;
1078 vp
->v_xe
= cv
->c_xe
;
1079 vp
->v_ye
= (cv
->c_ys
+ cv
->c_ye
) / 2 - 1;
1080 vp
->v_xoff
= cv
->c_xoff
;
1081 vp
->v_yoff
= cv
->c_yoff
;
1082 vp
->v_next
= cv
->c_vplist
;
1085 if ((vp
= (struct viewport
*)malloc(sizeof *vp
)) == 0)
1088 vp
->v_xs
= cv
->c_xs
;
1089 vp
->v_ys
= (3 * cv
->c_ys
+ cv
->c_ye
) / 4;
1090 vp
->v_xe
= (3 * cv
->c_xs
+ cv
->c_xe
) / 4 - 1;
1091 vp
->v_ye
= (cv
->c_ys
+ cv
->c_ye
) / 2 - 1;
1092 vp
->v_xoff
= cv
->c_xoff
;
1093 vp
->v_yoff
= cv
->c_yoff
;
1094 vp
->v_next
= cv
->c_vplist
;
1097 if ((vp
= (struct viewport
*)malloc(sizeof *vp
)) == 0)
1100 vp
->v_xs
= cv
->c_xs
;
1101 vp
->v_ys
= cv
->c_ys
;
1102 vp
->v_xe
= cv
->c_xe
;
1103 vp
->v_ye
= (3 * cv
->c_ys
+ cv
->c_ye
) / 4 - 1;
1104 vp
->v_xoff
= cv
->c_xoff
;
1105 vp
->v_yoff
= cv
->c_yoff
;
1106 vp
->v_next
= cv
->c_vplist
;
1110 vp
->v_xs
= cv
->c_xs
;
1111 vp
->v_ys
= cv
->c_ys
;
1112 vp
->v_xe
= cv
->c_xe
;
1113 vp
->v_ye
= cv
->c_ye
;
1114 vp
->v_xoff
= cv
->c_xoff
;
1115 vp
->v_yoff
= cv
->c_yoff
;
1116 vp
->v_next
= cv
->c_vplist
;
1120 if (cv
->c_xs
< display
->d_vpxmin
|| display
->d_vpxmin
== -1)
1121 display
->d_vpxmin
= cv
->c_xs
;
1122 if (cv
->c_xe
> display
->d_vpxmax
|| display
->d_vpxmax
== -1)
1123 display
->d_vpxmax
= cv
->c_xe
;
1129 RethinkViewportOffsets(cv
)
1132 struct viewport
*vp
;
1134 for (vp
= cv
->c_vplist
; vp
; vp
= vp
->v_next
)
1136 vp
->v_xoff
= cv
->c_xoff
;
1137 vp
->v_yoff
= cv
->c_yoff
;
1142 * if the adaptflag is on, we keep the size of this display, else
1143 * we may try to restore our old window sizes.
1154 /* Check for toggle */
1155 if (D_IM
&& strcmp(D_IM
, D_EI
))
1162 /* Check for toggle */
1163 if (D_KS
&& strcmp(D_KS
, D_KE
))
1165 if (D_CCS
&& strcmp(D_CCS
, D_CCE
))
1173 D_rend
= mchar_null
;
1176 ResizeDisplay(D_defwidth
, D_defheight
);
1177 ChangeScrollRegion(0, D_height
- 1);
1181 debug1("we %swant to adapt all our windows to the display\n",
1182 (adapt
) ? "" : "don't ");
1183 /* In case the size was changed by a init sequence */
1184 CheckScreenSize((adapt
) ? 2 : 0);
1196 ResizeDisplay(D_defwidth
, D_defheight
);
1198 ChangeScrollRegion(0, D_height
- 1);
1201 CursorVisibility(0);
1203 SetRendition(&mchar_null
);
1210 ShowHStatus((char *)0);
1215 GotoPos(0, D_height
- 1);
1229 if (!D_insert
&& D_x
< D_width
- 1)
1243 RefreshLine(D_y
, D_x
, D_width
-1, 0);
1255 if (D_insert
&& D_x
< D_width
- 1)
1264 if (D_x
< D_width
- 1)
1271 if (D_CLP
|| D_y
!= D_bot
)
1276 GotoPos(D_width
- 1, y
);
1279 debug("PUTCHARLP: lp_missing!\n");
1284 /* XXX -> PutChar ? */
1288 D_lpchar
.image
= D_mbcs
;
1296 * RAW_PUTCHAR() is for all text that will be displayed.
1297 * NOTE: charset Nr. 0 has a conversion table, but c1, c2, ... don't.
1308 if (D_encoding
== UTF8
)
1310 c
= (c
& 255) | (unsigned char)D_rend
.font
<< 8;
1316 D_x
+= D_AM
? 1 : -1;
1319 else if (utf8_isdouble(c
))
1328 AddCStr2(D_CS0
, '0');
1338 if (is_dw_font(D_rend
.font
))
1348 if (D_x
== D_width
- 1)
1349 D_x
+= D_AM
? 1 : -1;
1354 # if defined(ENCODINGS) && defined(DW_CHARS)
1356 c
= PrepareEncodedChar(c
);
1361 if (D_xtable
&& D_xtable
[(int)(unsigned char)D_rend
.font
] && D_xtable
[(int)(unsigned char)D_rend
.font
][(int)(unsigned char)c
])
1362 AddStr(D_xtable
[(int)(unsigned char)D_rend
.font
][(int)(unsigned char)c
]);
1364 AddChar(D_rend
.font
!= '0' ? c
: D_c0_tab
[(int)(unsigned char)c
]);
1372 if (++D_x
>= D_width
)
1376 else if (!D_CLP
|| D_x
> D_width
)
1379 if (D_y
< D_height
-1 && D_y
!= D_bot
)
1397 /* this is for ESC-sequences only (AddChar is a macro) */
1406 if (display
&& s
&& *s
)
1409 tputs(s
, 1, DoAddChar
);
1418 if (display
&& s
&& *s
)
1421 tputs(tgoto(s
, 0, c
), 1, DoAddChar
);
1426 /* Insert mode is a toggle on some terminals, so we need this hack:
1432 if (display
&& on
!= D_insert
&& D_IM
)
1442 /* ...and maybe keypad application mode is a toggle, too:
1452 if (display
&& D_keypad
!= on
&& D_KS
)
1471 if (display
&& D_cursorkeys
!= on
&& D_CCS
)
1486 if (display
&& D_revvid
!= on
&& D_CVR
)
1500 if (display
&& D_curvis
!= v
)
1503 AddCStr(D_VE
); /* do this always, just to be safe */
1505 if (v
== -1 && D_VI
)
1507 else if (v
== 1 && D_VS
)
1519 if (display
&& D_mouse
!= mode
)
1526 sprintf(mousebuf
, "\033[?%dl", D_mouse
);
1531 sprintf(mousebuf
, "\033[?%dh", mode
);
1558 tputs(s
, 1, CountChars
);
1566 CallRewrite(y
, xs
, xe
, doit
)
1567 int y
, xs
, xe
, doit
;
1569 struct canvas
*cv
, *cvlist
, *cvlnext
;
1570 struct viewport
*vp
;
1571 struct layer
*oldflayer
;
1574 debug3("CallRewrite %d %d %d\n", y
, xs
, xe
);
1579 for (cv
= D_cvlist
; cv
; cv
= cv
->c_next
)
1581 if (y
< cv
->c_ys
|| y
> cv
->c_ye
|| xe
< cv
->c_xs
|| xs
> cv
->c_xe
)
1583 for (vp
= cv
->c_vplist
; vp
; vp
= vp
->v_next
)
1584 if (y
>= vp
->v_ys
&& y
<= vp
->v_ye
&& xe
>= vp
->v_xs
&& xs
<= vp
->v_xe
)
1592 flayer
= cv
->c_layer
;
1593 cvlist
= flayer
->l_cvlist
;
1594 cvlnext
= cv
->c_lnext
;
1595 flayer
->l_cvlist
= cv
;
1597 LayRewrite(y
- vp
->v_yoff
, xs
- vp
->v_xoff
, xe
- vp
->v_xoff
, &D_rend
, 1);
1598 flayer
->l_cvlist
= cvlist
;
1599 cv
->c_lnext
= cvlnext
;
1603 if (cv
== 0 || cv
->c_layer
== 0)
1604 return EXPENSIVE
; /* not found or nothing on it */
1605 if (xs
< vp
->v_xs
|| xe
> vp
->v_xe
)
1606 return EXPENSIVE
; /* crosses viewport boundaries */
1607 if (y
- vp
->v_yoff
< 0 || y
- vp
->v_yoff
>= cv
->c_layer
->l_height
)
1608 return EXPENSIVE
; /* line not on layer */
1609 if (xs
- vp
->v_xoff
< 0 || xe
- vp
->v_xoff
>= cv
->c_layer
->l_width
)
1610 return EXPENSIVE
; /* line not on layer */
1612 if (D_encoding
== UTF8
)
1616 flayer
= cv
->c_layer
;
1617 debug3("Calling Rewrite %d %d %d\n", y
- vp
->v_yoff
, xs
- vp
->v_xoff
, xe
- vp
->v_xoff
);
1618 cost
= LayRewrite(y
- vp
->v_yoff
, xs
- vp
->v_xoff
, xe
- vp
->v_xoff
, &D_rend
, 0);
1621 cost
+= D_EIcost
+ D_IMcost
;
1630 register int dy
, dx
, x1
, y1
;
1631 register int costx
, costy
;
1635 enum move_t xm
= M_NONE
, ym
= M_NONE
;
1646 x1
= -1; /* don't know how the terminal treats this */
1654 if (dy
== 0 && dx
== 0)
1656 debug2("GotoPos (%d,%d)", x1
, y1
);
1657 debug2(" -> (%d,%d)\n", x2
, y2
);
1658 if (!D_MS
) /* Safe to move ? */
1659 SetRendition(&mchar_null
);
1660 if (y1
< 0 /* don't know the y position */
1661 || (y2
> D_bot
&& y1
<= D_bot
) /* have to cross border */
1662 || (y2
< D_top
&& y1
>= D_top
)) /* of scrollregion ? */
1665 if (D_HO
&& !x2
&& !y2
)
1668 AddCStr(tgoto(D_CM
, x2
, y2
));
1674 /* some scrollregion implementations don't allow movements
1675 * away from the region. sigh.
1677 if ((y1
> D_bot
&& y2
> y1
) || (y1
< D_top
&& y2
< y1
))
1680 /* Calculate CMcost */
1681 if (D_HO
&& !x2
&& !y2
)
1684 s
= tgoto(D_CM
, x2
, y2
);
1685 CMcost
= CalcCost(s
);
1687 /* Calculate the cost to move the cursor to the right x position */
1689 if (x1
>= 0) /* relativ x positioning only if we know where we are */
1693 if (D_CRI
&& (dx
> 1 || !D_ND
))
1695 costx
= CalcCost(tgoto(D_CRI
, 0, dx
));
1698 if ((m
= D_NDcost
* dx
) < costx
)
1703 /* Speedup: dx <= LayRewrite() */
1704 if (dx
< costx
&& (m
= CallRewrite(y1
, x1
, x2
- 1, 0)) < costx
)
1712 if (D_CLE
&& (dx
< -1 || !D_BC
))
1714 costx
= CalcCost(tgoto(D_CLE
, 0, -dx
));
1717 if ((m
= -dx
* D_LEcost
) < costx
)
1726 /* Speedup: LayRewrite() >= x2 */
1727 if (x2
+ D_CRcost
< costx
&& (m
= (x2
? CallRewrite(y1
, 0, x2
- 1, 0) : 0) + D_CRcost
) < costx
)
1733 /* Check if it is already cheaper to do CM */
1734 if (costx
>= CMcost
)
1737 /* Calculate the cost to move the cursor to the right y position */
1741 if (D_CDO
&& dy
> 1) /* DO & NL are always != 0 */
1743 costy
= CalcCost(tgoto(D_CDO
, 0, dy
));
1746 if ((m
= dy
* ((x2
== 0) ? D_NLcost
: D_DOcost
)) < costy
)
1754 if (D_CUP
&& (dy
< -1 || !D_UP
))
1756 costy
= CalcCost(tgoto(D_CUP
, 0, -dy
));
1759 if ((m
= -dy
* D_UPcost
) < costy
)
1768 /* Finally check if it is cheaper to do CM */
1769 if (costx
+ costy
>= CMcost
)
1779 AddCStr2(D_CLE
, -dx
);
1786 AddCStr2(D_CRI
, dx
);
1795 (void) CallRewrite(y1
, x1
, x2
- 1, 1);
1808 AddCStr2(D_CUP
, -dy
);
1811 s
= (x2
== 0) ? D_NL
: D_DO
;
1816 AddCStr2(D_CDO
, dy
);
1829 ClearArea(0, 0, 0, D_width
- 1, D_width
- 1, D_height
- 1, 0, 0);
1833 ClearArea(x1
, y1
, xs
, xe
, x2
, y2
, bce
, uselayfn
)
1834 int x1
, y1
, xs
, xe
, x2
, y2
, bce
, uselayfn
;
1838 struct viewport
*vp
;
1840 debug2("Clear %d,%d", x1
, y1
);
1841 debug2(" %d-%d", xs
, xe
);
1842 debug2(" %d,%d", x2
, y2
);
1843 debug2(" uselayfn=%d bce=%d\n", uselayfn
, bce
);
1853 if (D_UT
) /* Safe to erase ? */
1854 SetRendition(&mchar_null
);
1859 if (D_lp_missing
&& y1
<= D_bot
&& xe
>= D_width
- 1)
1861 if (y2
> D_bot
|| (y2
== D_bot
&& x2
>= D_width
- 1))
1864 if (x2
== D_width
- 1 && (xs
== 0 || y1
== y2
) && xe
== D_width
- 1 && y2
== D_height
- 1 && (!bce
|| D_BE
))
1867 if (x1
== 0 && y1
== 0 && D_auto_nuke
)
1870 if (x1
== 0 && y1
== 0 && D_CL
)
1877 * Workaround a hp700/22 terminal bug. Do not use CD where CE
1878 * is also appropriate.
1880 if (D_CD
&& (y1
< y2
|| !D_CE
))
1887 if (x1
== 0 && xs
== 0 && (xe
== D_width
- 1 || y1
== y2
) && y1
== 0 && D_CCD
&& (!bce
|| D_BE
))
1894 for (y
= y1
; y
<= y2
; y
++, x1
= xs
)
1898 if (x1
== 0 && D_CB
&& (xxe
!= D_width
- 1 || (D_x
== xxe
&& D_y
== y
)) && (!bce
|| D_BE
))
1904 if (xxe
== D_width
- 1 && D_CE
&& (!bce
|| D_BE
))
1913 for (cv
= D_cvlist
; cv
; cv
= cv
->c_next
)
1915 if (y
< cv
->c_ys
|| y
> cv
->c_ye
|| xxe
< cv
->c_xs
|| x1
> cv
->c_xe
)
1917 for (vp
= cv
->c_vplist
; vp
; vp
= vp
->v_next
)
1918 if (y
>= vp
->v_ys
&& y
<= vp
->v_ye
&& xxe
>= vp
->v_xs
&& x1
<= vp
->v_xe
)
1923 if (cv
&& cv
->c_layer
&& x1
>= vp
->v_xs
&& xxe
<= vp
->v_xe
&&
1924 y
- vp
->v_yoff
>= 0 && y
- vp
->v_yoff
< cv
->c_layer
->l_height
&&
1925 xxe
- vp
->v_xoff
>= 0 && x1
- vp
->v_xoff
< cv
->c_layer
->l_width
)
1927 struct layer
*oldflayer
= flayer
;
1928 struct canvas
*cvlist
, *cvlnext
;
1929 flayer
= cv
->c_layer
;
1930 cvlist
= flayer
->l_cvlist
;
1931 cvlnext
= cv
->c_lnext
;
1932 flayer
->l_cvlist
= cv
;
1934 LayClearLine(y
- vp
->v_yoff
, x1
- vp
->v_xoff
, xxe
- vp
->v_xoff
, bce
);
1935 flayer
->l_cvlist
= cvlist
;
1936 cv
->c_lnext
= cvlnext
;
1941 ClearLine((struct mline
*)0, y
, x1
, xxe
, bce
);
1947 * if cur_only > 0, we only redisplay current line, as a full refresh is
1948 * too expensive over a low baud line.
1956 /* XXX do em all? */
1958 ChangeScrollRegion(0, D_height
- 1);
1961 CursorVisibility(0);
1963 SetRendition(&mchar_null
);
1970 if (cur_only
> 0 && D_fore
)
1971 RefreshArea(0, D_fore
->w_y
, D_width
- 1, D_fore
->w_y
, 1);
1975 CV_CALL(D_forecv
, LayRestore();LaySetCursor());
1979 RedisplayDisplays(cur_only
)
1982 struct display
*olddisplay
= display
;
1983 for (display
= displays
; display
; display
= display
->d_next
)
1984 Redisplay(cur_only
);
1985 display
= olddisplay
;
1991 ScrollH(y
, xs
, xe
, n
, bce
, oml
)
1992 int y
, xs
, xe
, n
, bce
;
1999 if (xe
!= D_width
- 1)
2001 RefreshLine(y
, xs
, xe
, 0);
2002 /* UpdateLine(oml, y, xs, xe); */
2007 SetRendition(&mchar_null
);
2014 if (n
>= xe
- xs
+ 1)
2016 if (D_CDC
&& !(n
== 1 && D_DC
))
2025 RefreshLine(y
, xs
, xe
, 0);
2026 /* UpdateLine(oml, y, xs, xe); */
2032 if (-n
>= xe
- xs
+ 1)
2036 if (D_CIC
&& !(n
== -1 && D_IC
))
2037 AddCStr2(D_CIC
, -n
);
2046 SetRendition(&mchar_null
);
2052 bce
= 0; /* all done */
2056 /* UpdateLine(oml, y, xs, xe); */
2057 RefreshLine(y
, xs
, xe
, 0);
2063 SetRendition(&mchar_null
);
2069 bce
= 0; /* all done */
2075 ClearLine((struct mline
*)0, y
, xe
- n
+ 1, xe
, bce
);
2077 ClearLine((struct mline
*)0, y
, xs
, xs
- n
- 1, bce
);
2079 if (D_lp_missing
&& y
== D_bot
)
2082 WriteLP(D_width
- 1 - n
, y
);
2088 ScrollV(xs
, ys
, xe
, ye
, n
, bce
)
2089 int xs
, ys
, xe
, ye
, n
, bce
;
2094 int alok
, dlok
, aldlfaster
;
2100 if (n
>= ye
- ys
+ 1 || -n
>= ye
- ys
+ 1)
2102 ClearArea(xs
, ys
, xs
, xe
, xe
, ye
, bce
, 0);
2105 if (xs
> D_vpxmin
|| xe
< D_vpxmax
)
2107 RefreshArea(xs
, ys
, xe
, ye
, 0);
2113 if (D_bot
> ye
|| D_bot
< ys
)
2118 if (missy
> ye
|| missy
< ys
)
2129 if (n
>= ye
- ys
+ 1)
2134 if (ys
< D_top
|| D_bot
!= ye
)
2135 ChangeScrollRegion(ys
, ye
);
2136 alok
= (D_AL
|| D_CAL
|| (ys
>= D_top
&& ye
== D_bot
&& up
));
2137 dlok
= (D_DL
|| D_CDL
|| (ys
>= D_top
&& ye
== D_bot
&& !up
));
2138 if (D_top
!= ys
&& !(alok
&& dlok
))
2139 ChangeScrollRegion(ys
, ye
);
2143 (oldbot
== D_bot
&& up
&& D_top
== ys
&& D_bot
== ye
)))
2145 WriteLP(D_width
- 1, oldbot
);
2146 if (oldbot
== D_bot
) /* have scrolled */
2151 ChangeScrollRegion(oldtop, oldbot);
2154 ClearLine((struct mline
*)0, ye
, xs
, xe
, bce
);
2161 SetRendition(&mchar_null
);
2167 aldlfaster
= (n
> 1 && ys
>= D_top
&& ye
== D_bot
&& ((up
&& D_CDL
) || (!up
&& D_CAL
)));
2169 if ((up
|| D_SR
) && D_top
== ys
&& D_bot
== ye
&& !aldlfaster
)
2174 for(i
= n
; i
-- > 0; )
2175 AddCStr(D_NL
); /* was SF, I think NL is faster */
2180 for(i
= n
; i
-- > 0; )
2184 else if (alok
&& dlok
)
2186 if (up
|| ye
!= D_bot
)
2188 GotoPos(0, up
? ys
: ye
+1-n
);
2189 if (D_CDL
&& !(n
== 1 && D_DL
))
2195 if (!up
|| ye
!= D_bot
)
2197 GotoPos(0, up
? ye
+1-n
: ys
);
2198 if (D_CAL
&& !(n
== 1 && D_AL
))
2207 RefreshArea(xs
, ys
, xe
, ye
, 0);
2213 ClearArea(xs
, ye
- n
+ 1, xs
, xe
, xe
, ye
, bce
, 0);
2215 ClearArea(xs
, ys
, xs
, xe
, xe
, ys
+ n
- 1, bce
, 0);
2217 if (D_lp_missing
&& missy
!= D_bot
)
2218 WriteLP(D_width
- 1, missy
);
2220 ChangeScrollRegion(oldtop, oldbot);
2221 if (D_lp_missing && missy != D_bot)
2222 WriteLP(D_width - 1, missy);
2230 register int i
, j
, old
, typ
;
2232 if (!display
|| (old
= D_rend
.attr
) == new)
2235 D_col16change
= (old
^ new) & (A_BFG
| A_BBG
);
2236 new ^= D_col16change
;
2240 #if defined(TERMINFO) && defined(USE_SGR)
2246 tputs(tparm(D_SA
, new & A_SO
, new & A_US
, new & A_RV
, new & A_BL
,
2247 new & A_DI
, new & A_BD
, 0 , 0 ,
2253 rend_setdefault(&D_rend
);
2260 if ((new & old
) != old
)
2270 /* ansi attrib handling: \E[m resets color, too */
2272 rend_setdefault(&D_rend
);
2277 /* D_ME may also reset the alternate charset */
2289 for (i
= 0, j
= 1; old
&& i
< NATTR
; i
++, j
<<= 1)
2296 AddCStr(D_attrtab
[i
]);
2297 typ
|= D_attrtyp
[i
];
2308 int old
= D_rend
.font
;
2309 if (!display
|| old
== new)
2313 if (D_encoding
&& CanEncodeFont(D_encoding
, new))
2315 if (new == D_realfont
)
2319 if (D_xtable
&& D_xtable
[(int)(unsigned char)new] &&
2320 D_xtable
[(int)(unsigned char)new][256])
2322 AddCStr(D_xtable
[(int)(unsigned char)new][256]);
2326 if (!D_CG0
&& new != '0')
2345 AddCStr2(D_CS0
, new);
2360 jj
= (jj
- 232) / 6;
2361 jj
= (jj
& 1) << 3 | (jj
& 2 ? 7 : 0);
2369 min
= r
< g
? (r
< b
? r
: b
) : (g
< b
? g
: b
);
2370 max
= r
> g
? (r
> b
? r
: b
) : (g
> b
? g
: b
);
2372 jj
= ((max
+ 1) & 2) << 2 | ((max
+ 1) & 4 ? 7 : 0);
2374 jj
= (b
- min
) / (max
- min
) << 2 | (g
- min
) / (max
- min
) << 1 | (r
-
2375 min
) / (max
- min
) | (max
> 3 ? 8 : 0);
2388 return (jj
- 232) / 3 + 80;
2395 return ((r
+ 1) / 2) * 16 + ((g
+ 1) / 2) * 4 + ((b
+ 1) / 2) + 16;
2406 static unsigned char sftrans
[8] = {0,4,2,6,1,5,3,7};
2411 of
= rend_getfg(&D_rend
);
2412 ob
= rend_getbg(&D_rend
);
2415 /* intense default not invented yet */
2421 debug2("SetColor %d %d", coli2e(of
), coli2e(ob
));
2422 debug2(" -> %d %d\n", coli2e(f
), coli2e(b
));
2423 debug2("(%d %d", of
, ob
);
2424 debug2(" -> %d %d)\n", f
, b
);
2426 if (!D_CAX
&& D_hascolor
&& ((f
== 0 && f
!= of
) || (b
== 0 && b
!= ob
)))
2433 oattr
= D_rend
.attr
;
2434 AddCStr(D_ME
? D_ME
: "\033[m");
2438 /* D_ME may also reset the alternate charset */
2451 rend_setfg(&D_rend
, f
);
2452 rend_setbg(&D_rend
, b
);
2458 f
= f
? coli2e(f
) : -1;
2459 b
= b
? coli2e(b
) : -1;
2460 of
= of
? coli2e(of
) : -1;
2461 ob
= ob
? coli2e(ob
) : -1;
2463 if (f
!= of
&& f
> 15 && D_CCO
!= 256)
2464 f
= D_CCO
== 88 && D_CAF
? color256to88(f
) : color256to16(f
);
2465 if (f
!= of
&& f
> 15 && D_CAF
)
2470 if (b
!= ob
&& b
> 15 && D_CCO
!= 256)
2471 b
= D_CCO
== 88 && D_CAB
? color256to88(b
) : color256to16(b
);
2472 if (b
!= ob
&& b
> 15 && D_CAB
)
2478 if (f
!= of
&& f
!= (of
| 8))
2481 AddCStr("\033[39m"); /* works because AX is set */
2483 AddCStr2(D_CAF
, f
& 7);
2485 AddCStr2(D_CSF
, sftrans
[f
& 7]);
2487 if (b
!= ob
&& b
!= (ob
| 8))
2490 AddCStr("\033[49m"); /* works because AX is set */
2492 AddCStr2(D_CAB
, b
& 7);
2494 AddCStr2(D_CSB
, sftrans
[b
& 7]);
2497 if (f
!= of
&& D_CXT
&& (f
& 8) != 0 && f
!= -1)
2500 AddCStr2("\033[9%p1%dm", f
& 7);
2502 AddCStr2("\033[9%dm", f
& 7);
2505 if (b
!= ob
&& D_CXT
&& (b
& 8) != 0 && b
!= -1)
2508 AddCStr2("\033[10%p1%dm", b
& 7);
2510 AddCStr2("\033[10%dm", b
& 7);
2522 SetColor(rend_getfg(&D_rend
), new);
2532 if (nattr2color
&& D_hascolor
&& (mc
->attr
& nattr2color
) != 0)
2534 static struct mchar mmc
;
2537 for (i
= 0; i
< 8; i
++)
2538 if (attr2color
[i
] && (mc
->attr
& (1 << i
)) != 0)
2540 if (mc
->color
== 0 && attr2color
[i
][3])
2541 ApplyAttrColor(attr2color
[i
][3], &mmc
);
2542 else if ((mc
->color
& 0x0f) == 0 && attr2color
[i
][2])
2543 ApplyAttrColor(attr2color
[i
][2], &mmc
);
2544 else if ((mc
->color
& 0xf0) == 0 && attr2color
[i
][1])
2545 ApplyAttrColor(attr2color
[i
][1], &mmc
);
2547 ApplyAttrColor(attr2color
[i
][0], &mmc
);
2550 debug2("SetRendition: mapped to %02x %02x\n", (unsigned char)mc
->attr
, 0x99 - (unsigned char)mc
->color
);
2552 if (D_hascolor
&& D_CC8
&& (mc
->attr
& (A_BFG
|A_BBG
)))
2555 if ((mc
->attr
& A_BFG
) && D_MD
)
2557 if ((mc
->attr
& A_BBG
) && D_MB
)
2559 if (D_rend
.attr
!= a
)
2562 else if (D_rend
.attr
!= mc
->attr
)
2565 if (D_rend
.color
!= mc
->color
2567 || D_rend
.colorx
!= mc
->colorx
2573 SetColor(rend_getfg(mc
), rend_getbg(mc
));
2576 if (D_rend
.font
!= mc
->font
)
2582 SetRenditionMline(ml
, x
)
2588 if (nattr2color
&& D_hascolor
&& (ml
->attr
[x
] & nattr2color
) != 0)
2591 copy_mline2mchar(&mc
, ml
, x
);
2595 if (D_hascolor
&& D_CC8
&& (ml
->attr
[x
] & (A_BFG
|A_BBG
)))
2597 int a
= ml
->attr
[x
];
2598 if ((ml
->attr
[x
] & A_BFG
) && D_MD
)
2600 if ((ml
->attr
[x
] & A_BBG
) && D_MB
)
2602 if (D_rend
.attr
!= a
)
2605 else if (D_rend
.attr
!= ml
->attr
[x
])
2606 SetAttr(ml
->attr
[x
]);
2608 if (D_rend
.color
!= ml
->color
[x
]
2610 || D_rend
.colorx
!= ml
->colorx
[x
]
2618 copy_mline2mchar(&mc
, ml
, x
);
2619 SetColor(rend_getfg(&mc
), rend_getbg(&mc
));
2623 if (D_rend
.font
!= ml
->font
[x
])
2624 SetFont(ml
->font
[x
]);
2632 register char *s
, *t
;
2642 debug("tc not inited, just writing msg\n");
2643 if (D_processinputdata
)
2644 return; /* XXX: better */
2650 if (!use_hardstatus
|| !D_HS
)
2657 max
= D_WS
> 0 ? D_WS
: (D_width
- !D_CLP
);
2661 if (strcmp(msg
, D_status_lastmsg
) == 0)
2663 debug("same message - increase timeout");
2664 SetTimeout(&D_statusev
, MsgWait
);
2671 gettimeofday(&now
, NULL
);
2672 ti
= (now
.tv_sec
- D_status_time
.tv_sec
) * 1000 + (now
.tv_usec
- D_status_time
.tv_usec
) / 1000;
2673 if (ti
< MsgMinWait
)
2674 DisplaySleep1000(MsgMinWait
- ti
, 0);
2678 for (s
= t
= msg
; *s
&& t
- msg
< max
; ++s
)
2681 else if ((unsigned char)*s
>= ' ' && *s
!= 0177)
2686 if (t
- msg
>= D_status_buflen
)
2689 if (D_status_lastmsg
)
2690 buf
= realloc(D_status_lastmsg
, t
- msg
+ 1);
2692 buf
= malloc(t
- msg
+ 1);
2695 D_status_lastmsg
= buf
;
2696 D_status_buflen
= t
- msg
+ 1;
2699 if (t
- msg
< D_status_buflen
)
2700 strcpy(D_status_lastmsg
, msg
);
2701 D_status_len
= t
- msg
;
2702 D_status_lastx
= D_x
;
2703 D_status_lasty
= D_y
;
2704 if (!use_hardstatus
|| D_has_hstatus
== HSTATUS_IGNORE
|| D_has_hstatus
== HSTATUS_MESSAGE
)
2706 D_status
= STATUS_ON_WIN
;
2707 debug1("using STATLINE %d\n", STATLINE
);
2708 GotoPos(0, STATLINE
);
2709 SetRendition(&mchar_so
);
2712 if (D_status_len
< max
)
2714 /* Wayne Davison: add extra space for readability */
2716 SetRendition(&mchar_null
);
2718 if (D_status_len
< max
)
2730 D_status
= STATUS_ON_HS
;
2736 if (D_status
== STATUS_ON_WIN
)
2738 struct display
*olddisplay
= display
;
2739 struct layer
*oldflayer
= flayer
;
2741 ASSERT(D_obuffree
== D_obuflen
);
2742 /* this is copied over from RemoveStatus() */
2744 GotoPos(0, STATLINE
);
2745 RefreshLine(STATLINE
, 0, D_status_len
- 1, 0);
2746 GotoPos(D_status_lastx
, D_status_lasty
);
2747 flayer
= D_forecv
? D_forecv
->c_layer
: 0;
2750 display
= olddisplay
;
2752 D_status_obuflen
= D_obuflen
;
2753 D_status_obuffree
= D_obuffree
;
2754 D_obuffree
= D_obuflen
= 0;
2755 D_status
= STATUS_ON_WIN
;
2757 gettimeofday(&D_status_time
, NULL
);
2758 SetTimeout(&D_statusev
, MsgWait
);
2761 RefreshBraille(); /* let user see multiple Msg()s */
2768 struct display
*olddisplay
;
2769 struct layer
*oldflayer
;
2774 if (!(where
= D_status
))
2777 debug("RemoveStatus\n");
2778 if (D_status_obuffree
>= 0)
2780 D_obuflen
= D_status_obuflen
;
2781 D_obuffree
= D_status_obuffree
;
2782 D_status_obuffree
= -1;
2787 olddisplay
= display
;
2789 if (where
== STATUS_ON_WIN
)
2791 if (captionalways
|| (D_canvas
.c_slperp
&& D_canvas
.c_slperp
->c_slnext
))
2793 GotoPos(0, STATLINE
);
2794 RefreshLine(STATLINE
, 0, D_status_len
- 1, 0);
2795 GotoPos(D_status_lastx
, D_status_lasty
);
2800 flayer
= D_forecv
? D_forecv
->c_layer
: 0;
2803 display
= olddisplay
;
2807 /* refresh the display's hstatus line */
2814 if (D_status
== STATUS_ON_WIN
&& D_has_hstatus
== HSTATUS_LASTLINE
&& STATLINE
== D_height
-1)
2815 return; /* sorry, in use */
2819 if (D_HS
&& D_has_hstatus
== HSTATUS_HS
)
2821 if (!D_hstatus
&& (str
== 0 || *str
== 0))
2823 debug("ShowHStatus: using HS\n");
2824 SetRendition(&mchar_null
);
2829 if (str
== 0 || *str
== 0)
2832 max
= D_WS
> 0 ? D_WS
: (D_width
- !D_CLP
);
2833 if ((int)strlen(str
) > max
)
2840 else if (D_has_hstatus
== HSTATUS_LASTLINE
)
2842 debug("ShowHStatus: using last line\n");
2845 str
= str
? str
: "";
2849 GotoPos(0, D_height
- 1);
2850 SetRendition(captionalways
|| D_cvlist
== 0 || D_cvlist
->c_next
? &mchar_null
: &mchar_so
);
2851 PutWinMsg(str
, 0, l
);
2852 if (!captionalways
&& D_cvlist
&& !D_cvlist
->c_next
)
2853 while (l
++ < D_width
)
2856 ClearArea(l
, D_height
- 1, l
, D_width
- 1, D_width
- 1, D_height
- 1, 0, 0);
2857 if (ox
!= -1 && oy
!= -1)
2859 D_hstatus
= *str
? 1 : 0;
2860 SetRendition(&mchar_null
);
2862 else if (str
&& *str
&& D_has_hstatus
== HSTATUS_MESSAGE
)
2864 debug("ShowHStatus: using message\n");
2871 * Refreshes the harstatus of the fore window. Shouldn't be here...
2878 evdeq(&D_hstatusev
);
2879 if (D_status
== STATUS_ON_HS
)
2881 buf
= MakeWinMsgEv(hstatusstring
, D_fore
, '%', (D_HS
&& D_has_hstatus
== HSTATUS_HS
&& D_WS
> 0) ? D_WS
: D_width
- !D_CLP
, &D_hstatusev
, 0);
2885 if (D_has_hstatus
!= HSTATUS_IGNORE
&& D_hstatusev
.timeout
.tv_sec
)
2886 evenq(&D_hstatusev
);
2889 ShowHStatus((char *)0);
2892 /*********************************************************************/
2894 * Here come the routines that refresh an arbitrary part of the screen.
2904 debug("Signalling full refresh!\n");
2905 for (cv
= D_cvlist
; cv
; cv
= cv
->c_next
)
2907 CV_CALL(cv
, LayRedisplayLine(-1, -1, -1, isblank
));
2908 display
= cv
->c_display
; /* just in case! */
2910 RefreshArea(0, 0, D_width
- 1, D_height
- 1, isblank
);
2914 RefreshArea(xs
, ys
, xe
, ye
, isblank
)
2915 int xs
, ys
, xe
, ye
, isblank
;
2919 debug2("Refresh Area: %d,%d", xs
, ys
);
2920 debug3(" - %d,%d (isblank=%d)\n", xe
, ye
, isblank
);
2921 if (!isblank
&& xs
== 0 && xe
== D_width
- 1 && ye
== D_height
- 1 && (ys
== 0 || D_CD
))
2923 ClearArea(xs
, ys
, xs
, xe
, xe
, ye
, 0, 0);
2926 for (y
= ys
; y
<= ye
; y
++)
2927 RefreshLine(y
, xs
, xe
, isblank
);
2931 RefreshLine(y
, from
, to
, isblank
)
2932 int y
, from
, to
, isblank
;
2934 struct viewport
*vp
, *lvp
;
2935 struct canvas
*cv
, *lcv
, *cvlist
, *cvlnext
;
2936 struct layer
*oldflayer
;
2943 debug2("RefreshLine %d %d", y
, from
);
2944 debug2(" %d %d\n", to
, isblank
);
2946 if (D_status
== STATUS_ON_WIN
&& y
== STATLINE
)
2948 if (to
>= D_status_len
)
2949 D_status_len
= to
+ 1;
2950 return; /* can't refresh status */
2953 /* The following check makes plenty of sense. Unfortunately,
2954 vte-based terminals (notably gnome-terminal) experience a quirk
2955 that causes the final line not to update properly when it falls outside
2956 the scroll region; clearing the line with D_CE avoids the glitch,
2957 so we'll disable this perfectly sensible shortcut until such a time
2958 as widespread vte installations lack the glitch.
2960 See http://bugzilla.gnome.org/show_bug.cgi?id=542087 for current
2961 status of the VTE bug report, and
2962 https://savannah.gnu.org/bugs/index.php?23699 for the history from
2963 the Savannah BTS. */
2965 if (y
== D_height
- 1 && D_has_hstatus
== HSTATUS_LASTLINE
)
2972 if (isblank
== 0 && D_CE
&& to
== D_width
- 1 && from
< to
)
2976 SetRendition(&mchar_null
);
2984 for (cv
= display
->d_cvlist
; cv
; cv
= cv
->c_next
)
2986 if (y
== cv
->c_ye
+ 1 && from
>= cv
->c_xs
&& from
<= cv
->c_xe
)
2988 p
= Layer2Window(cv
->c_layer
);
2989 buf
= MakeWinMsgEv(captionstring
, p
, '%', cv
->c_xe
- cv
->c_xs
+ (cv
->c_xe
+ 1 < D_width
|| D_CLP
), &cv
->c_captev
, 0);
2990 if (cv
->c_captev
.timeout
.tv_sec
)
2991 evenq(&cv
->c_captev
);
2992 xx
= to
> cv
->c_xe
? cv
->c_xe
: to
;
2995 SetRendition(&mchar_so
);
2996 if (l
> xx
- cv
->c_xs
+ 1)
2997 l
= xx
- cv
->c_xs
+ 1;
2998 PutWinMsg(buf
, from
- cv
->c_xs
, l
);
2999 from
= cv
->c_xs
+ l
;
3000 for (; from
<= xx
; from
++)
3004 if (from
== cv
->c_xe
+ 1 && y
>= cv
->c_ys
&& y
<= cv
->c_ye
+ 1)
3007 SetRendition(&mchar_so
);
3012 if (y
< cv
->c_ys
|| y
> cv
->c_ye
|| to
< cv
->c_xs
|| from
> cv
->c_xe
)
3014 debug2("- canvas hit: %d %d", cv
->c_xs
, cv
->c_ys
);
3015 debug2(" %d %d\n", cv
->c_xe
, cv
->c_ye
);
3016 for (vp
= cv
->c_vplist
; vp
; vp
= vp
->v_next
)
3018 debug2(" - vp: %d %d", vp
->v_xs
, vp
->v_ys
);
3019 debug2(" %d %d\n", vp
->v_xe
, vp
->v_ye
);
3020 /* find leftmost overlapping vp */
3021 if (y
>= vp
->v_ys
&& y
<= vp
->v_ye
&& from
<= vp
->v_xe
&& to
>= vp
->v_xs
&& (lvp
== 0 || lvp
->v_xs
> vp
->v_xs
))
3029 continue; /* we advanced from */
3032 if (from
< lvp
->v_xs
)
3035 DisplayLine(&mline_null
, &mline_blank
, y
, from
, lvp
->v_xs
- 1);
3039 /* call LayRedisplayLine on canvas lcv viewport lvp */
3040 yy
= y
- lvp
->v_yoff
;
3041 xx
= to
< lvp
->v_xe
? to
: lvp
->v_xe
;
3043 if (lcv
->c_layer
&& yy
== lcv
->c_layer
->l_height
)
3046 SetRendition(&mchar_blank
);
3047 while (from
<= lvp
->v_xe
&& from
- lvp
->v_xoff
< lcv
->c_layer
->l_width
)
3052 if (from
>= lvp
->v_xe
+ 1)
3055 if (lcv
->c_layer
== 0 || yy
>= lcv
->c_layer
->l_height
|| from
- lvp
->v_xoff
>= lcv
->c_layer
->l_width
)
3058 DisplayLine(&mline_null
, &mline_blank
, y
, from
, lvp
->v_xe
);
3059 from
= lvp
->v_xe
+ 1;
3063 if (xx
- lvp
->v_xoff
>= lcv
->c_layer
->l_width
)
3064 xx
= lcv
->c_layer
->l_width
+ lvp
->v_xoff
- 1;
3066 flayer
= lcv
->c_layer
;
3067 cvlist
= flayer
->l_cvlist
;
3068 cvlnext
= lcv
->c_lnext
;
3069 flayer
->l_cvlist
= lcv
;
3071 LayRedisplayLine(yy
, from
- lvp
->v_xoff
, xx
- lvp
->v_xoff
, isblank
);
3072 flayer
->l_cvlist
= cvlist
;
3073 lcv
->c_lnext
= cvlnext
;
3078 if (!isblank
&& from
<= to
)
3079 DisplayLine(&mline_null
, &mline_blank
, y
, from
, to
);
3082 /*********************************************************************/
3084 /* clear lp_missing by writing the char on the screen. The
3085 * position must be safe.
3091 struct mchar oldrend
;
3094 ASSERT(D_lp_missing
);
3096 debug2("WriteLP(%d,%d)\n", x2
, y2
);
3103 D_lpchar
= mchar_blank
;
3106 /* Can't use PutChar */
3108 SetRendition(&D_lpchar
);
3109 PUTCHAR(D_lpchar
.image
);
3112 PUTCHAR(D_lpchar
.mbcs
);
3115 SetRendition(&oldrend
);
3119 ClearLine(oml
, y
, from
, to
, bce
)
3121 int from
, to
, y
, bce
;
3125 struct mchar bcechar
;
3128 debug3("ClearLine %d,%d-%d\n", y
, from
, to
);
3129 if (D_UT
) /* Safe to erase ? */
3130 SetRendition(&mchar_null
);
3135 if (from
== 0 && D_CB
&& (to
!= D_width
- 1 || (D_x
== to
&& D_y
== y
)) && (!bce
|| D_BE
))
3141 if (to
== D_width
- 1 && D_CE
&& (!bce
|| D_BE
))
3152 DisplayLine(oml
, &mline_blank
, y
, from
, to
);
3155 bcechar
= mchar_blank
;
3156 rend_setbg(&bcechar
, bce
);
3157 for (x
= from
; x
<= to
; x
++)
3158 copy_mchar2mline(&bcechar
, &mline_old
, x
);
3159 DisplayLine(oml
, &mline_old
, y
, from
, to
);
3161 DisplayLine(oml
, &mline_blank
, y
, from
, to
);
3166 DisplayLine(oml
, ml
, y
, from
, to
)
3167 struct mline
*oml
, *ml
;
3171 int last2flag
= 0, delete_lp
= 0;
3174 ASSERT(y
>= 0 && y
< D_height
);
3175 ASSERT(from
>= 0 && from
< D_width
);
3176 ASSERT(to
>= 0 && to
< D_width
);
3177 if (!D_CLP
&& y
== D_bot
&& to
== D_width
- 1)
3179 if (D_lp_missing
|| !cmp_mline(oml
, ml
, to
))
3182 if ((D_IC
|| D_IM
) && from
< to
&& !dw_left(ml
, to
, D_encoding
))
3184 if ((D_IC
|| D_IM
) && from
< to
)
3193 delete_lp
= !cmp_mchar_mline(&mchar_blank
, oml
, to
) && (D_CE
|| D_DC
|| D_CDC
);
3194 D_lp_missing
= !cmp_mchar_mline(&mchar_blank
, ml
, to
);
3195 copy_mline2mchar(&D_lpchar
, ml
, to
);
3203 /* finish dw-char (can happen after a wrap) */
3204 debug("DisplayLine finishing kanji\n");
3205 SetRenditionMline(ml
, from
);
3206 PUTCHAR(ml
->image
[from
]);
3210 for (x
= from
; x
<= to
; x
++)
3212 #if 0 /* no longer needed */
3213 if (x
|| D_x
!= D_width
|| D_y
!= y
- 1)
3216 if (x
< to
|| x
!= D_width
- 1 || ml
->image
[x
+ 1])
3217 if (cmp_mline(oml
, ml
, x
))
3222 if (dw_right(ml
, x
, D_encoding
))
3225 debug1("DisplayLine on right side of dw char- x now %d\n", x
);
3228 if (x
== to
&& dw_left(ml
, x
, D_encoding
))
3229 break; /* don't start new kanji */
3231 SetRenditionMline(ml
, x
);
3232 PUTCHAR(ml
->image
[x
]);
3234 if (dw_left(ml
, x
, D_encoding
))
3235 PUTCHAR(ml
->image
[++x
]);
3238 #if 0 /* not needed any longer */
3239 /* compare != 0 because ' ' can happen when clipping occures */
3240 if (to
== D_width
- 1 && y
< D_height
- 1 && D_x
== D_width
&& ml
->image
[to
+ 1])
3246 SetRenditionMline(ml
, x
+ 1);
3247 PUTCHAR(ml
->image
[x
+ 1]);
3249 SetRenditionMline(ml
, x
);
3250 INSERTCHAR(ml
->image
[x
]);
3255 SetRendition(&mchar_null
);
3272 PUTCHARLP(c
->image
);
3277 if (D_encoding
== UTF8
)
3286 InsChar(c
, x
, xe
, y
, oml
)
3292 if (y
== D_bot
&& !D_CLP
)
3294 if (x
== D_width
- 1)
3300 if (xe
== D_width
- 1)
3306 PUTCHARLP(c
->image
);
3309 if (!(D_IC
|| D_CIC
|| D_IM
) || xe
!= D_width
- 1)
3311 RefreshLine(y
, x
, xe
, 0);
3313 /* UpdateLine(oml, y, x, xe); */
3320 if (c
->mbcs
&& D_IC
)
3325 AddCStr2(D_CIC
, c
->mbcs
? 2 : 1);
3334 RAW_PUTCHAR(c
->image
);
3339 if (D_encoding
== UTF8
)
3342 if (D_x
== D_width
- 1)
3345 RAW_PUTCHAR(c
->mbcs
);
3351 WrapChar(c
, x
, y
, xs
, ys
, xe
, ye
, ins
)
3360 bce
= rend_getbg(c
);
3365 debug2(" x %d y %d", x
, y
);
3366 debug2(" Dx %d Dy %d", D_x
, D_y
);
3367 debug2(" xs %d ys %d", xs
, ys
);
3368 debug3(" xe %d ye %d ins %d\n", xe
, ye
, ins
);
3369 if (xs
!= 0 || x
!= D_width
|| !D_AM
)
3372 ScrollV(xs
, ys
, xe
, ye
, 1, bce
);
3373 else if (y
< D_height
- 1)
3376 InsChar(c
, xs
, xe
, y
, 0);
3381 if (y
== ye
) /* we have to scroll */
3383 debug("- scrolling\n");
3384 ChangeScrollRegion(ys
, ye
);
3385 if (D_bot
!= y
|| D_x
!= D_width
|| (!bce
&& !D_BE
))
3387 debug("- have to call ScrollV\n");
3388 ScrollV(xs
, ys
, xe
, ye
, 1, bce
);
3392 else if (y
== D_bot
) /* remove unusable region? */
3393 ChangeScrollRegion(0, D_height
- 1);
3394 if (D_x
!= D_width
|| D_y
!= y
)
3396 if (D_CLP
&& y
>= 0) /* don't even try if !LP */
3397 RefreshLine(y
, D_width
- 1, D_width
- 1, 0);
3398 debug2("- refresh last char -> x,y now %d,%d\n", D_x
, D_y
);
3399 if (D_x
!= D_width
|| D_y
!= y
) /* sorry, no bonus */
3402 ScrollV(xs
, ys
, xe
, ye
, 1, bce
);
3403 GotoPos(xs
, y
== ye
|| y
== D_height
- 1 ? y
: y
+ 1);
3406 debug("- writeing new char");
3407 if (y
!= ye
&& y
< D_height
- 1)
3409 if (ins
!= D_insert
)
3411 if (ins
&& !D_insert
)
3413 InsChar(c
, 0, xe
, y
, 0);
3414 debug2(" -> done with insert (%d,%d)\n", D_x
, D_y
);
3420 RAW_PUTCHAR(c
->image
);
3425 if (D_encoding
== UTF8
)
3428 RAW_PUTCHAR(c
->mbcs
);
3431 debug2(" -> done (%d,%d)\n", D_x
, D_y
);
3435 ResizeDisplay(wi
, he
)
3439 debug2("ResizeDisplay: to (%d,%d).\n", wi
, he
);
3440 if (D_width
== wi
&& D_height
== he
)
3442 debug("ResizeDisplay: No change\n");
3445 if (D_width
!= wi
&& (D_height
== he
|| !D_CWS
) && D_CZ0
&& (wi
== Z0width
|| wi
== Z1width
))
3447 debug("ResizeDisplay: using Z0/Z1\n");
3448 AddCStr(wi
== Z0width
? D_CZ0
: D_CZ1
);
3449 ChangeScreenSize(wi
, D_height
, 0);
3450 return (he
== D_height
) ? 0 : -1;
3454 debug("ResizeDisplay: using WS\n");
3455 AddCStr(tgoto(D_CWS
, wi
, he
));
3456 ChangeScreenSize(wi
, he
, 0);
3463 ChangeScrollRegion(newtop
, newbot
)
3468 if (newtop
== newbot
)
3469 return; /* xterm etc can't do it */
3473 newbot
= D_height
- 1;
3477 D_bot
= D_height
- 1;
3480 if (D_top
== newtop
&& D_bot
== newbot
)
3482 debug2("ChangeScrollRegion: (%d - %d)\n", newtop
, newbot
);
3483 AddCStr(tgoto(D_CS
, newbot
, newtop
));
3486 D_y
= D_x
= -1; /* Just in case... */
3495 static char oscs
[] = "1;\000\00020;\00039;\00049;\000";
3502 if (!D_xtermosc
[i
] && !*s
)
3505 s
= "screen"; /* always set icon name */
3507 s
= ""; /* no background */
3509 s
= "black"; /* black text */
3511 s
= "white"; /* on white background */
3514 AddStr(oscs
+ i
* 4);
3523 for (i
= 3; i
>= 0; i
--)
3529 * Output buffering routines
3541 if (D_encoding
== UTF8
)
3543 while ((c
= *str
++))
3544 AddUtf8((unsigned char)c
);
3548 while ((c
= *str
++))
3561 if (D_encoding
== UTF8
)
3563 while ((c
= *str
++) && n
-- > 0)
3564 AddUtf8((unsigned char)c
);
3568 while ((c
= *str
++) && n
-- > 0)
3581 l
= D_obufp
- D_obuf
;
3582 debug1("Flush(): %d\n", l
);
3585 ASSERT(l
+ D_obuffree
== D_obuflen
);
3593 if (fcntl(D_userfd
, F_SETFL
, 0))
3594 debug1("Warning: BLOCK fcntl failed: %d\n", errno
);
3598 wr
= write(D_userfd
, p
, l
);
3603 debug1("Writing to display: %d\n", errno
);
3614 if (fcntl(D_userfd
, F_SETFL
, FNBLOCK
))
3615 debug1("Warning: NBLOCK fcntl failed: %d\n", errno
);
3626 debug1("did freetty %d\n", D_userfd
);
3634 D_obuflenmax
= -D_obufmax
;
3640 * Asynchronous output routines by
3641 * Tim MacKenzie (tym@dibbler.cs.monash.edu.au)
3650 if (D_status_obuffree
>= 0)
3652 ASSERT(D_obuffree
== -1);
3657 gettimeofday(&now
, NULL
);
3658 ti
= (now
.tv_sec
- D_status_time
.tv_sec
) * 1000 + (now
.tv_usec
- D_status_time
.tv_usec
) / 1000;
3659 if (ti
< MsgMinWait
)
3660 DisplaySleep1000(MsgMinWait
- ti
, 0);
3663 if (--D_obuffree
> 0) /* redo AddChar decrement */
3666 if (D_obuflen
&& D_obuf
)
3668 ind
= D_obufp
- D_obuf
;
3670 D_obuffree
+= GRAIN
;
3671 D_obuf
= realloc(D_obuf
, D_obuflen
);
3678 D_obuf
= malloc(D_obuflen
);
3681 Panic(0, "Out of memory");
3682 D_obufp
= D_obuf
+ ind
;
3683 D_obuflenmax
= D_obuflen
- D_obufmax
;
3684 debug1("ResizeObuf: resized to %d\n", D_obuflen
);
3688 DisplaySleep1000(n
, eat
)
3700 debug("DisplaySleep has no display sigh\n");
3704 t
.tv_usec
= (n
% 1000) * 1000;
3705 t
.tv_sec
= n
/ 1000;
3707 FD_SET(D_userfd
, &r
);
3708 if (select(FD_SETSIZE
, &r
, (fd_set
*)0, (fd_set
*)0, &t
) > 0)
3710 debug("display activity stopped sleep\n");
3712 read(D_userfd
, &buf
, 1);
3714 debug2("DisplaySleep(%d) ending, eat was %d\n", n
, eat
);
3720 {/* Nuke pending output in current display, clear screen */
3722 int oldtop
= D_top
, oldbot
= D_bot
;
3723 struct mchar oldrend
;
3724 int oldkeypad
= D_keypad
, oldcursorkeys
= D_cursorkeys
;
3725 int oldcurvis
= D_curvis
;
3726 int oldmouse
= D_mouse
;
3729 len
= D_obufp
- D_obuf
;
3730 debug1("NukePending: nuking %d chars\n", len
);
3732 /* Throw away any output that we can... */
3734 tcflush(D_userfd
, TCOFLUSH
);
3737 (void) ioctl(D_userfd
, TCFLSH
, (char *) 1);
3746 /* Turn off all attributes. (Tim MacKenzie) */
3753 AddStr("\033[m"); /* why is D_ME not set? */
3758 /* Check for toggle */
3759 if (D_IM
&& strcmp(D_IM
, D_EI
))
3762 /* Check for toggle */
3764 if (D_KS
&& strcmp(D_KS
, D_KE
))
3766 if (D_CCS
&& strcmp(D_CCS
, D_CCE
))
3769 if (D_KS
&& strcmp(D_KS
, D_KE
))
3772 if (D_CCS
&& strcmp(D_CCS
, D_CCE
))
3777 D_rend
= mchar_null
;
3783 ChangeScrollRegion(oldtop
, oldbot
);
3784 SetRendition(&oldrend
);
3785 KeypadMode(oldkeypad
);
3786 CursorkeysMode(oldcursorkeys
);
3787 CursorVisibility(oldcurvis
);
3788 MouseMode(oldmouse
);
3791 debug("ResizeDisplay: using WS\n");
3792 AddCStr(tgoto(D_CWS
, D_width
, D_height
));
3794 else if (D_CZ0
&& (D_width
== Z0width
|| D_width
== Z1width
))
3796 debug("ResizeDisplay: using Z0/Z1\n");
3797 AddCStr(D_width
== Z0width
? D_CZ0
: D_CZ1
);
3800 #endif /* AUTO_NUKE */
3803 /* linux' select can't handle flow control, so wait 100ms if
3807 disp_writeev_eagain(ev
, data
)
3811 display
= (struct display
*)data
;
3813 D_writeev
.type
= EV_WRITE
;
3814 D_writeev
.handler
= disp_writeev_fn
;
3820 disp_writeev_fn(ev
, data
)
3824 int len
, size
= OUTPUT_BLOCK_SIZE
;
3826 display
= (struct display
*)data
;
3827 len
= D_obufp
- D_obuf
;
3831 size
= write(D_userfd
, D_obuf
, size
);
3837 bcopy(D_obuf
+ size
, D_obuf
, len
);
3838 debug2("ASYNC: wrote %d - remaining %d\n", size
, len
);
3844 D_blocked_fuzz
-= size
;
3845 if (D_blocked_fuzz
< 0)
3848 if (D_blockedev
.queued
)
3850 if (D_obufp
- D_obuf
> D_obufmax
/ 2)
3852 debug2("%s: resetting timeout to %g secs\n", D_usertty
, D_nonblock
/1000.);
3853 SetTimeout(&D_blockedev
, D_nonblock
);
3857 debug1("%s: deleting blocked timeout\n", D_usertty
);
3858 evdeq(&D_blockedev
);
3861 if (D_blocked
== 1 && D_obuf
== D_obufp
)
3863 /* empty again, restart output */
3864 debug1("%s: buffer empty, unblocking\n", D_usertty
);
3866 Activate(D_fore
? D_fore
->w_norefresh
: 0);
3867 D_blocked_fuzz
= D_obufp
- D_obuf
;
3873 /* linux flow control is badly broken */
3874 if (errno
== EAGAIN
)
3877 D_writeev
.type
= EV_TIMEOUT
;
3878 D_writeev
.handler
= disp_writeev_eagain
;
3879 SetTimeout(&D_writeev
, 100);
3883 if (errno
!= EINTR
&& errno
!= EAGAIN
)
3884 #if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
3885 if (errno
!= EWOULDBLOCK
)
3887 Msg(errno
, "Error writing output to display");
3892 disp_readev_fn(ev
, data
)
3900 display
= (struct display
*)data
;
3902 /* Hmmmm... a bit ugly... */
3904 for (cv
= D_forecv
->c_layer
->l_cvlist
; cv
; cv
= cv
->c_lnext
)
3906 display
= cv
->c_display
;
3907 if (D_status
== STATUS_ON_WIN
)
3911 display
= (struct display
*)data
;
3918 size
= sizeof(D_fore
->w_pwin
->p_inbuf
) - D_fore
->w_pwin
->p_inlen
;
3921 size
= sizeof(D_fore
->w_inbuf
) - D_fore
->w_inlen
;
3927 size
= 1; /* Always allow one char for command keys */
3929 size
= read(D_userfd
, buf
, size
);
3932 if (errno
== EINTR
|| errno
== EAGAIN
)
3934 #if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
3935 if (errno
== EWOULDBLOCK
)
3938 debug1("Read error: %d - hangup!\n", errno
);
3945 debug("Found EOF - hangup!\n");
3956 Activate(D_fore
? D_fore
->w_norefresh
: 0);
3961 if (D_blocked
> 1) /* 2, 3 */
3967 for (p
= windows
; p
; p
= p
->w_next
)
3968 if (p
->w_zdisplay
== display
)
3970 flayer
= &p
->w_layer
;
3973 LayProcess(&bufp
, &size
);
3976 debug("zmodem window gone, deblocking display");
3977 zmodem_abort(0, display
);
3983 D_fore
->w_lastdisp
= display
;
3984 if (D_mouse
&& D_forecv
)
3986 unsigned char *bp
= (unsigned char *)buf
;
3989 /* XXX this assumes that the string is read in as a whole... */
3990 for (i
= size
; i
> 0; i
--, bp
++)
3992 if (i
> 5 && bp
[0] == 033 && bp
[1] == '[' && bp
[2] == 'M')
3997 else if (i
< 5 || bp
[0] != 0233 || bp
[1] != 'M')
4001 if (x
>= D_forecv
->c_xs
&& x
<= D_forecv
->c_xe
&& y
>= D_forecv
->c_ys
&& y
<= D_forecv
->c_ye
)
4003 x
-= D_forecv
->c_xoff
;
4004 y
-= D_forecv
->c_yoff
;
4005 if (x
>= 0 && x
< D_forecv
->c_layer
->l_width
&& y
>= 0 && y
< D_forecv
->c_layer
->l_height
)
4016 bcopy((char *)bp
+ 1, (char *)bp
, i
);
4021 bcopy((char *)bp
+ 5, (char *)bp
, i
- 5);
4028 if (D_encoding
!= (D_forecv
? D_forecv
->c_layer
->l_encoding
: 0))
4031 char buf2
[IOSIZE
* 2 + 10];
4032 enc
= D_forecv
? D_forecv
->c_layer
->l_encoding
: 0;
4033 for (i
= j
= 0; i
< size
; i
++)
4035 c
= ((unsigned char *)buf
)[i
];
4036 c
= DecodeChar(c
, D_encoding
, &D_decodestate
);
4038 i
--; /* try char again */
4044 j
+= EncodeChar(buf2
+ j
, c
, enc
, &font
);
4045 j
+= EncodeChar(buf2
+ j
, -1, enc
, &font
);
4048 j
+= EncodeChar(buf2
+ j
, c
, enc
, 0);
4049 if (j
> (int)sizeof(buf2
) - 10) /* just in case... */
4052 (*D_processinput
)(buf2
, j
);
4056 (*D_processinput
)(buf
, size
);
4060 disp_status_fn(ev
, data
)
4064 display
= (struct display
*)data
;
4065 debug1("disp_status_fn for display %x\n", (int)display
);
4071 disp_hstatus_fn(ev
, data
)
4075 display
= (struct display
*)data
;
4076 if (D_status
== STATUS_ON_HS
)
4086 disp_blocked_fn(ev
, data
)
4092 display
= (struct display
*)data
;
4093 debug1("blocked timeout %s\n", D_usertty
);
4094 if (D_obufp
- D_obuf
> D_obufmax
+ D_blocked_fuzz
)
4096 debug("stopping output to display\n");
4098 /* re-enable all windows */
4099 for (p
= windows
; p
; p
= p
->w_next
)
4100 if (p
->w_readev
.condneg
== &D_obuflenmax
)
4102 debug1("freeing window #%d\n", p
->w_number
);
4103 p
->w_readev
.condpos
= p
->w_readev
.condneg
= 0;
4109 cv_winid_fn(ev
, data
)
4114 struct canvas
*cv
= (struct canvas
*)data
;
4116 display
= cv
->c_display
;
4117 if (D_status
== STATUS_ON_WIN
)
4125 if (cv
->c_ye
+ 1 < D_height
)
4126 RefreshLine(cv
->c_ye
+ 1, 0, D_width
- 1, 0);
4127 if (ox
!= -1 && oy
!= -1)
4133 disp_map_fn(ev
, data
)
4140 display
= (struct display
*)data
;
4141 debug("Flushing map sequence\n");
4144 p
= (char *)D_seqp
- l
;
4145 D_seqp
= D_kmaps
+ 3;
4147 if ((q
= D_seqh
) != 0)
4150 i
= q
[0] << 8 | q
[1];
4151 i
&= ~KMAP_NOTIMEOUT
;
4152 debug1("Mapping former hit #%d - ", i
);
4153 debug2("%d(%s) - ", q
[2], q
+ 3);
4155 ProcessInput2((char *)q
+ 3, q
[2]);
4168 disp_idle_fn(ev
, data
)
4172 struct display
*olddisplay
;
4173 display
= (struct display
*)data
;
4174 debug("idle timeout\n");
4175 if (idletimo
<= 0 || idleaction
.nr
== RC_ILLEGAL
)
4177 olddisplay
= display
;
4178 flayer
= D_forecv
->c_layer
;
4180 DoAction(&idleaction
, -1);
4181 if (idleaction
.nr
== RC_BLANKER
)
4183 for (display
= displays
; display
; display
= display
->d_next
)
4184 if (olddisplay
== display
)
4195 SetTimeout(&D_idleev
, idletimo
);
4196 if (!D_idleev
.queued
)
4207 disp_blanker_fn(ev
, data
)
4211 char buf
[IOSIZE
], *b
;
4214 display
= (struct display
*)data
;
4215 size
= read(D_blankerev
.fd
, buf
, IOSIZE
);
4218 evdeq(&D_blankerev
);
4219 close(D_blankerev
.fd
);
4220 D_blankerev
.fd
= -1;
4223 for (b
= buf
; size
; size
--)
4230 int oldtop
= D_top
, oldbot
= D_bot
;
4231 struct mchar oldrend
;
4233 if (D_blankerev
.fd
== -1)
4237 evdeq(&D_blankerev
);
4238 close(D_blankerev
.fd
);
4239 D_blankerev
.fd
= -1;
4240 Kill(D_blankerpid
, SIGHUP
);
4252 AddStr("\033[m\033[m"); /* why is D_ME not set? */
4259 D_rend
= mchar_null
;
4263 ChangeScrollRegion(oldtop
, oldbot
);
4264 SetRendition(&oldrend
);
4277 char libuf
[20], cobuf
[20];
4281 strcpy(termname
, "TERM=");
4282 strncpy(termname
+ 5, D_termname
, sizeof(termname
) - 6);
4283 termname
[sizeof(termname
) - 1] = 0;
4286 if ((D_blankerev
.fd
= OpenPTY(&m
)) == -1)
4288 Msg(0, "OpenPty failed");
4294 if ((slave
= open(m
, O_RDWR
|O_NOCTTY
)) == -1)
4296 Msg(errno
, "%s", m
);
4297 close(D_blankerev
.fd
);
4298 D_blankerev
.fd
= -1;
4303 switch (pid
= (int)fork())
4307 close(D_blankerev
.fd
);
4308 D_blankerev
.fd
= -1;
4313 if (dfp
&& dfp
!= stderr
)
4319 if (setgid(real_gid
) || setuid(real_uid
))
4320 Panic(errno
, "setuid/setgid");
4326 closeallfiles(slave
);
4327 if (open(m
, O_RDWR
))
4328 Panic(errno
, "Cannot open %s", m
);
4335 SetTTY(0, &D_OldMode
);
4340 glwz
.ws_col
= D_width
;
4341 glwz
.ws_row
= D_height
;
4342 (void)ioctl(0, TIOCSWINSZ
, (char *)&glwz
);
4344 sprintf(libuf
, "LINES=%d", D_height
);
4345 sprintf(cobuf
, "COLUMNS=%d", D_width
);
4350 signal(SIGPIPE
, SIG_DFL
);
4353 execvpe(*cmdv
, cmdv
, NewEnv
+ 3);
4354 Panic(errno
, *cmdv
);
4359 evenq(&D_blankerev
);
4366 struct layout
*layouts
;
4367 struct layout
*laytab
[MAXLAY
];
4368 struct layout
*layout_last
, layout_last_marker
;
4369 struct layout
*layout_attach
= &layout_last_marker
;
4375 struct canvas
*cnext
, *c
= cv
;
4376 for (; cv
; cv
= cnext
)
4380 FreeLayoutCv(cv
->c_slperp
);
4384 cnext
= cv
->c_slnext
;
4392 DupLayoutCv(cvf
, cvt
, save
)
4393 struct canvas
*cvf
, *cvt
;
4398 cvt
->c_slorient
= cvf
->c_slorient
;
4399 cvt
->c_slweight
= cvf
->c_slweight
;
4400 if (cvf
== D_forecv
)
4404 cvt
->c_display
= display
;
4407 cvt
->c_captev
.type
= EV_TIMEOUT
;
4408 cvt
->c_captev
.data
= (char *)cvt
;
4409 cvt
->c_captev
.handler
= cv_winid_fn
;
4410 cvt
->c_blank
.l_cvlist
= 0;
4411 cvt
->c_blank
.l_layfn
= &BlankLf
;
4412 cvt
->c_blank
.l_bottom
= &cvt
->c_blank
;
4414 cvt
->c_layer
= cvf
->c_layer
;
4418 struct win
*p
= cvf
->c_layer
? Layer2Window(cvf
->c_layer
) : 0;
4419 cvt
->c_layer
= p
? &p
->w_layer
: 0;
4423 cvt
->c_slperp
= (struct canvas
*)calloc(1, sizeof(struct canvas
));
4424 cvt
->c_slperp
->c_slback
= cvt
;
4425 CanvasInitBlank(cvt
->c_slperp
);
4426 DupLayoutCv(cvf
->c_slperp
, cvt
->c_slperp
, save
);
4430 cvt
->c_slnext
= (struct canvas
*)calloc(1, sizeof(struct canvas
));
4431 cvt
->c_slnext
->c_slprev
= cvt
;
4432 cvt
->c_slnext
->c_slback
= cvt
->c_slback
;
4433 CanvasInitBlank(cvt
->c_slnext
);
4435 cvf
= cvf
->c_slnext
;
4436 cvt
= cvt
->c_slnext
;
4445 for (; cv
; cv
= cv
->c_slnext
)
4449 PutWindowCv(cv
->c_slperp
);
4452 p
= cv
->c_layer
? (struct win
*)cv
->c_layer
->l_data
: 0;
4454 SetCanvasWindow(cv
, p
);
4459 CreateLayout(title
, startat
)
4466 if (startat
>= MAXLAY
|| startat
< 0)
4468 for (i
= startat
; ;)
4476 Msg(0, "No more layouts\n");
4480 lay
= (struct layout
*)calloc(1, sizeof(*lay
));
4481 lay
->lay_title
= SaveStr(title
);
4482 lay
->lay_autosave
= 1;
4483 lay
->lay_number
= i
;
4485 lay
->lay_next
= layouts
;
4491 SaveLayout(name
, cv
)
4497 for (lay
= layouts
; lay
; lay
= lay
->lay_next
)
4498 if (!strcmp(lay
->lay_title
, name
))
4501 FreeLayoutCv(&lay
->lay_canvas
);
4503 lay
= CreateLayout(name
, 0);
4507 DupLayoutCv(cv
, &lay
->lay_canvas
, 1);
4508 lay
->lay_forecv
= D_forecv
;
4518 if (!lay
|| !lay
->lay_autosave
)
4520 FreeLayoutCv(&lay
->lay_canvas
);
4522 DupLayoutCv(&D_canvas
, &lay
->lay_canvas
, 1);
4523 lay
->lay_forecv
= D_forecv
;
4534 for (i
= 0, s
= name
; *s
>= '0' && *s
<= '9'; s
++)
4535 i
= i
* 10 + (*s
- '0');
4536 if (!*s
&& s
!= name
&& i
>= 0 && i
< MAXLAY
)
4538 for (lay
= layouts
; lay
; lay
= lay
->lay_next
)
4539 if (!strcmp(lay
->lay_title
, name
))
4549 AutosaveLayout(D_layout
);
4552 while (D_canvas
.c_slperp
)
4553 FreeCanvas(D_canvas
.c_slperp
);
4554 MakeDefaultCanvas();
4555 SetCanvasWindow(D_forecv
, 0);
4559 while (D_canvas
.c_slperp
)
4560 FreeCanvas(D_canvas
.c_slperp
);
4562 D_forecv
= lay
->lay_forecv
;
4563 DupLayoutCv(&lay
->lay_canvas
, &D_canvas
, 0);
4564 D_canvas
.c_ye
= D_height
- 1 - ((D_canvas
.c_slperp
&& D_canvas
.c_slperp
->c_slnext
) || captionalways
) - (D_has_hstatus
== HSTATUS_LASTLINE
);
4565 ResizeCanvas(&D_canvas
);
4566 RecreateCanvasChain();
4567 RethinkDisplayViewports();
4568 PutWindowCv(&D_canvas
);
4569 ResizeLayersToCanvases();
4574 NewLayout(title
, startat
)
4581 lay
= CreateLayout(title
, startat
);
4584 LoadLayout(0, &D_canvas
);
4586 DupLayoutCv(&D_canvas
, &lay
->lay_canvas
, 1);
4587 lay
->lay_forecv
= D_forecv
;
4590 lay
->lay_autosave
= 1;
4594 AddLayoutsInfo(buf
, len
, where
)
4600 struct layout
*p
, **pp
;
4604 for (pp
= laytab
; pp
< laytab
+ MAXLAY
; pp
++)
4606 if (pp
- laytab
== where
&& ss
== buf
)
4614 if (s
- buf
+ l
> len
- 24)
4621 sprintf(s
, "%d", p
->lay_number
);
4622 if (p
->lay_number
== where
)
4625 if (display
&& p
== D_layout
)
4646 Msg(0, "No layouts defined\n");
4649 if (where
== -1 && D_layout
)
4650 where
= D_layout
->lay_number
;
4651 ss
= AddLayoutsInfo(buf
, sizeof(buf
), where
);
4652 s
= buf
+ strlen(buf
);
4653 if (ss
- buf
> D_width
/ 2)
4656 if (s
- ss
< D_width
)
4672 struct layout
**layp
= &layouts
;
4674 for (; *layp
; layp
= &(*layp
)->lay_next
)
4678 *layp
= lay
->lay_next
;
4682 laytab
[lay
->lay_number
] = (struct layout
*)0;
4684 if (display
&& D_layout
== lay
)
4685 D_layout
= (struct layout
*)0;
4687 FreeLayoutCv(&lay
->lay_canvas
);
4690 free(lay
->lay_title
);
4694 LoadLayout((display
&& D_layout
) ? D_layout
: *layp
? *layp
: layouts
,
4695 display
? &D_canvas
: (struct canvas
*)0);