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 2, 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, write to the
18 * Free Software Foundation, Inc.,
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
);
421 if ((cv
= (struct canvas
*)calloc(1, sizeof *cv
)) == 0)
424 cv
->c_xe
= D_width
- 1;
426 cv
->c_ye
= D_height
- 1 - (D_has_hstatus
== HSTATUS_LASTLINE
) - captionalways
;
427 debug2("MakeDefaultCanvas 0,0 %d,%d\n", cv
->c_xe
, cv
->c_ye
);
431 cv
->c_display
= display
;
437 cv
->c_slback
= &D_canvas
;
438 D_canvas
.c_slperp
= cv
;
439 D_canvas
.c_xs
= cv
->c_xs
;
440 D_canvas
.c_xe
= cv
->c_xe
;
441 D_canvas
.c_ys
= cv
->c_ys
;
442 D_canvas
.c_ye
= cv
->c_ye
;
443 cv
->c_slorient
= SLICE_UNKN
;
444 cv
->c_captev
.type
= EV_TIMEOUT
;
445 cv
->c_captev
.data
= (char *)cv
;
446 cv
->c_captev
.handler
= cv_winid_fn
;
448 cv
->c_blank
.l_cvlist
= cv
;
449 cv
->c_blank
.l_width
= cv
->c_xe
- cv
->c_xs
+ 1;
450 cv
->c_blank
.l_height
= cv
->c_ye
- cv
->c_ys
+ 1;
451 cv
->c_blank
.l_x
= cv
->c_blank
.l_y
= 0;
452 cv
->c_blank
.l_layfn
= &BlankLf
;
453 cv
->c_blank
.l_data
= 0;
454 cv
->c_blank
.l_next
= 0;
455 cv
->c_blank
.l_bottom
= &cv
->c_blank
;
456 cv
->c_blank
.l_blocking
= 0;
457 cv
->c_layer
= &cv
->c_blank
;
461 RethinkDisplayViewports();
462 D_forecv
= cv
; /* default input focus */
466 static struct canvas
**
467 CreateCanvasChainRec(cv
, cvp
)
471 for (; cv
; cv
= cv
->c_slnext
)
474 cvp
= CreateCanvasChainRec(cv
->c_slperp
, cvp
);
485 RecreateCanvasChain()
488 cvp
= CreateCanvasChainRec(D_canvas
.c_slperp
, &D_cvlist
);
496 struct viewport
*vp
, *nvp
;
501 cv
->c_slprev
->c_slnext
= cv
->c_slnext
;
503 cv
->c_slnext
->c_slprev
= cv
->c_slprev
;
504 if (cv
->c_slback
&& cv
->c_slback
->c_slperp
== cv
)
505 cv
->c_slback
->c_slperp
= cv
->c_slnext
? cv
->c_slnext
: cv
->c_slprev
;
509 FreeCanvas(cv
->c_slperp
);
518 /* remove from canvas chain as SetCanvasWindow might call
519 * some layer function */
520 for (cvp
= &D_cvlist
; *cvp
; cvp
= &(*cvp
)->c_next
)
527 p
= cv
->c_layer
? Layer2Window(cv
->c_layer
) : 0;
528 SetCanvasWindow(cv
, 0);
530 WindowChanged(p
, 'u');
531 if (flayer
== cv
->c_layer
)
533 for (vp
= cv
->c_vplist
; vp
; vp
= nvp
)
540 evdeq(&cv
->c_captev
);
549 for (; cv
; cv
= cv
->c_slnext
)
555 for (cvp
= cv
->c_slperp
; cvp
; cvp
= cvp
->c_slnext
)
558 n
= CountCanvas(cvp
->c_slperp
);
576 for (cvp
= cv
->c_slperp
; cvp
; cvp
= cvp
->c_slnext
)
579 n
= CountCanvas(cvp
->c_slperp
);
587 EqualizeCanvas(cv
, gflag
)
592 for (; cv
; cv
= cv
->c_slnext
)
594 if (cv
->c_slperp
&& gflag
)
596 cv
->c_slweight
= CountCanvasPerp(cv
);
597 for (cv2
= cv
->c_slperp
; cv2
; cv2
= cv2
->c_slnext
)
599 EqualizeCanvas(cv2
->c_slperp
, gflag
);
610 struct canvas
*cv2
, *cvn
, *fcv
;
611 int nh
, i
, maxi
, hh
, m
, w
, wsum
;
621 debug2("ResizeCanvas: %d,%d", xs
, ys
);
622 debug2(" %d,%d\n", xe
, ye
);
625 if (cv
->c_slorient
== SLICE_UNKN
)
627 ASSERT(!cv
->c_slnext
&& !cv
->c_slperp
);
632 cv
->c_xoff
= cv
->c_xs
;
633 cv
->c_yoff
= cv
->c_ys
;
634 cv
->c_blank
.l_width
= cv
->c_xe
- cv
->c_xs
+ 1;
635 cv
->c_blank
.l_height
= cv
->c_ye
- cv
->c_ys
+ 1;
640 if (focusminwidth
|| focusminheight
)
642 debug("searching for focus canvas\n");
644 while (cv2
->c_slback
)
646 if (cv2
->c_slback
== cv
->c_slback
)
649 focusmin
= cv
->c_slorient
== SLICE_VERT
? focusminheight
: focusminwidth
;
652 else if (focusmin
< 0)
653 focusmin
= cv
->c_slorient
== SLICE_VERT
? ye
- ys
+ 2 : xe
- xs
+ 2;
654 debug1("found, focusmin=%d\n", focusmin
);
661 m
= CountCanvas(cv
) * 2;
662 nh
= cv
->c_slorient
== SLICE_VERT
? ye
- ys
+ 2 : xe
- xs
+ 2;
668 debug1("corrected to %d\n", focusmin
);
671 /* pass 1: calculate weight sum */
672 for (cv2
= cv
, wsum
= 0; cv2
; cv2
= cv2
->c_slnext
)
674 debug1(" weight %d\n", cv2
->c_slweight
);
675 wsum
+= cv2
->c_slweight
;
677 debug1("wsum = %d\n", wsum
);
682 /* pass 2: calculate need/excess space */
683 nh
= cv
->c_slorient
== SLICE_VERT
? ye
- ys
+ 2 : xe
- xs
+ 2;
684 for (cv2
= cv
, need
= got
= 0; cv2
; cv2
= cv2
->c_slnext
)
686 m
= cv2
->c_slperp
? CountCanvasPerp(cv2
) * 2 - 1 : 1;
689 hh
= cv2
->c_slweight
? nh
* cv2
->c_slweight
/ w
: 0;
690 w
-= cv2
->c_slweight
;
692 debug2(" should %d min %d\n", hh
, m
);
698 debug2("need: %d, got %d\n", need
, got
);
702 /* pass 3: distribute space */
703 nh
= cv
->c_slorient
== SLICE_VERT
? ye
- ys
+ 2 : xe
- xs
+ 2;
704 i
= cv
->c_slorient
== SLICE_VERT
? ys
: xs
;
705 maxi
= cv
->c_slorient
== SLICE_VERT
? ye
: xe
;
712 if (cv
->c_slprev
&& !cv
->c_slback
->c_slback
&& !cv
->c_slprev
->c_slperp
&& !cv
->c_slprev
->c_slprev
)
714 cv
->c_slprev
->c_slorient
= SLICE_UNKN
;
717 cv
->c_slback
->c_ye
++;
718 cv
->c_slprev
->c_ye
++;
721 SetCanvasWindow(cv
, 0);
725 m
= cv
->c_slperp
? CountCanvasPerp(cv
) * 2 - 1 : 1;
728 hh
= cv
->c_slweight
? nh
* cv
->c_slweight
/ w
: 0;
731 debug2(" should %d min %d\n", hh
, m
);
735 debug1(" -> %d\n", hh
);
739 int hx
= need
* (hh
- m
- 1) / got
;
740 debug3(" -> %d - %d = %d\n", hh
, hx
, hh
- hx
);
744 debug2(" now need=%d got=%d\n", need
, got
);
747 /* hh is window size plus pation line */
748 if (i
+ hh
> maxi
+ 2)
751 debug1(" not enough space, reducing to %d\n", hh
);
753 if (i
+ hh
== maxi
+ 1)
756 debug(" incrementing as no other canvas will fit\n");
758 if (cv
->c_slorient
== SLICE_VERT
)
763 cv
->c_ye
= i
+ hh
- 2;
770 cv
->c_xe
= i
+ hh
- 2;
776 cv
->c_xoff
= cv
->c_xs
;
777 cv
->c_yoff
= cv
->c_ys
;
778 cv
->c_blank
.l_width
= cv
->c_xe
- cv
->c_xs
+ 1;
779 cv
->c_blank
.l_height
= cv
->c_ye
- cv
->c_ys
+ 1;
783 if (!cv
->c_slperp
->c_slnext
)
785 debug("deleting perp node\n");
786 FreePerp(cv
->c_slperp
);
794 static struct canvas
*
799 debug("Creating new perp node\n");
801 if ((pcv
= (struct canvas
*)calloc(1, sizeof *cv
)) == 0)
804 pcv
->c_display
= cv
->c_display
;
805 pcv
->c_slnext
= cv
->c_slnext
;
806 pcv
->c_slprev
= cv
->c_slprev
;
808 pcv
->c_slback
= cv
->c_slback
;
809 if (cv
->c_slback
&& cv
->c_slback
->c_slperp
== cv
)
810 cv
->c_slback
->c_slperp
= pcv
;
811 pcv
->c_slorient
= cv
->c_slorient
;
814 pcv
->c_xs
= cv
->c_xs
;
815 pcv
->c_xe
= cv
->c_xe
;
816 pcv
->c_ys
= cv
->c_ys
;
817 pcv
->c_ye
= cv
->c_ye
;
819 pcv
->c_slnext
->c_slprev
= pcv
;
821 pcv
->c_slprev
->c_slnext
= pcv
;
822 pcv
->c_slweight
= cv
->c_slweight
;
828 cv
->c_slorient
= SLICE_UNKN
;
841 cv
->c_slprev
= pcv
->c_slprev
;
843 cv
->c_slprev
->c_slnext
= cv
;
844 cv
->c_slback
= pcv
->c_slback
;
845 if (cv
->c_slback
&& cv
->c_slback
->c_slperp
== pcv
)
846 cv
->c_slback
->c_slperp
= cv
;
847 cv
->c_slorient
= pcv
->c_slorient
;
848 cv
->c_slweight
= pcv
->c_slweight
;
852 cv
->c_slorient
= pcv
->c_slorient
;
853 cv
->c_slback
= pcv
->c_slback
;
854 cv
->c_slweight
= pcv
->c_slweight
;
856 cv
->c_slnext
= pcv
->c_slnext
;
858 cv
->c_slnext
->c_slprev
= cv
;
871 debug2("AddCanvas orient %d, forecv is %d\n", orient
, cv
->c_slorient
);
873 if (cv
->c_slorient
!= SLICE_UNKN
&& cv
->c_slorient
!= orient
)
878 xs
= cv
->c_slback
->c_xs
;
879 xe
= cv
->c_slback
->c_xe
;
880 ys
= cv
->c_slback
->c_ys
;
881 ye
= cv
->c_slback
->c_ye
;
882 if (!captionalways
&& cv
== D_canvas
.c_slperp
&& !cv
->c_slnext
)
883 ye
--; /* need space for caption */
884 debug2("Adding Canvas to slice %d,%d ", xs
, ys
);
885 debug2("%d,%d\n", xe
, ye
);
887 num
= CountCanvas(cv
->c_slback
->c_slperp
) + 1;
888 debug1("Num = %d\n", num
);
889 if (orient
== SLICE_VERT
)
896 return -1; /* can't fit in */
898 if ((cv
= (struct canvas
*)calloc(1, sizeof *cv
)) == 0)
901 D_forecv
->c_slback
->c_ye
= ye
; /* in case we modified it above */
902 D_forecv
->c_slorient
= orient
; /* in case it was UNKN */
903 cv
->c_slnext
= D_forecv
->c_slnext
;
904 cv
->c_slprev
= D_forecv
;
905 D_forecv
->c_slnext
= cv
;
907 cv
->c_slnext
->c_slprev
= cv
;
908 cv
->c_slorient
= orient
;
909 cv
->c_slback
= D_forecv
->c_slback
;
917 cv
->c_display
= display
;
919 cv
->c_captev
.type
= EV_TIMEOUT
;
920 cv
->c_captev
.data
= (char *)cv
;
921 cv
->c_captev
.handler
= cv_winid_fn
;
923 cv
->c_blank
.l_cvlist
= cv
;
924 cv
->c_blank
.l_width
= cv
->c_xe
- cv
->c_xs
+ 1;
925 cv
->c_blank
.l_height
= cv
->c_ye
- cv
->c_ys
+ 1;
926 cv
->c_blank
.l_x
= cv
->c_blank
.l_y
= 0;
927 cv
->c_blank
.l_layfn
= &BlankLf
;
928 cv
->c_blank
.l_data
= 0;
929 cv
->c_blank
.l_next
= 0;
930 cv
->c_blank
.l_bottom
= &cv
->c_blank
;
931 cv
->c_blank
.l_blocking
= 0;
932 cv
->c_layer
= &cv
->c_blank
;
938 EqualizeCanvas(cv
->c_slperp
, 0);
940 RecreateCanvasChain();
941 RethinkDisplayViewports();
942 ResizeLayersToCanvases();
952 debug("RemCanvas\n");
954 if (cv
->c_slorient
== SLICE_UNKN
)
960 if (!cv
->c_slnext
->c_slnext
&& cv
->c_slback
->c_slback
)
962 /* two canvases in slice, kill perp node */
964 debug("deleting perp node\n");
965 FreePerp(cv
->c_slprev
? cv
->c_slprev
: cv
->c_slnext
);
966 FreePerp(cv
->c_slback
);
968 xs
= cv
->c_slback
->c_xs
;
969 xe
= cv
->c_slback
->c_xe
;
970 ys
= cv
->c_slback
->c_ys
;
971 ye
= cv
->c_slback
->c_ye
;
974 D_forecv
= cv
->c_slprev
;
976 D_forecv
= cv
->c_slnext
;
980 while (D_forecv
->c_slperp
)
981 D_forecv
= D_forecv
->c_slperp
;
983 /* if only one canvas left, set orient back to unknown */
984 if (!cv
->c_slnext
&& !cv
->c_slprev
&& !cv
->c_slback
->c_slback
&& !cv
->c_slperp
)
986 cv
->c_slorient
= SLICE_UNKN
;
988 cv
->c_slback
->c_ye
= ++ye
; /* caption line no longer needed */
991 EqualizeCanvas(cv
->c_slperp
, 0);
994 D_fore
= Layer2Window(D_forecv
->c_layer
);
995 flayer
= D_forecv
->c_layer
;
997 RecreateCanvasChain();
998 RethinkDisplayViewports();
999 ResizeLayersToCanvases();
1005 struct canvas
*cv
= D_forecv
, *ocv
= 0;
1010 cv
->c_slprev
->c_slnext
= cv
->c_slnext
;
1015 cv
->c_slnext
->c_slprev
= cv
->c_slprev
;
1019 if (cv
->c_slback
&& cv
->c_slback
->c_slperp
== cv
)
1020 cv
->c_slback
->c_slperp
= ocv
;
1021 cv
->c_slorient
= SLICE_UNKN
;
1022 while (D_canvas
.c_slperp
)
1023 FreeCanvas(D_canvas
.c_slperp
);
1025 D_canvas
.c_slperp
= cv
;
1026 cv
->c_slback
= &D_canvas
;
1029 ASSERT(!cv
->c_slperp
);
1031 D_canvas
.c_ye
++; /* caption line no longer needed */
1032 ResizeCanvas(&D_canvas
);
1033 RecreateCanvasChain();
1034 RethinkDisplayViewports();
1035 ResizeLayersToCanvases();
1039 RethinkDisplayViewports()
1042 struct viewport
*vp
, *vpn
;
1044 /* free old viewports */
1045 for (cv
= display
->d_cvlist
; cv
; cv
= cv
->c_next
)
1047 for (vp
= cv
->c_vplist
; vp
; vp
= vpn
)
1051 bzero((char *)vp
, sizeof(*vp
));
1056 display
->d_vpxmin
= -1;
1057 display
->d_vpxmax
= -1;
1059 for (cv
= display
->d_cvlist
; cv
; cv
= cv
->c_next
)
1061 if ((vp
= (struct viewport
*)malloc(sizeof *vp
)) == 0)
1065 vp
->v_xs
= cv
->c_xs
;
1066 vp
->v_ys
= (cv
->c_ys
+ cv
->c_ye
) / 2;
1067 vp
->v_xe
= cv
->c_xe
;
1068 vp
->v_ye
= cv
->c_ye
;
1069 vp
->v_xoff
= cv
->c_xoff
;
1070 vp
->v_yoff
= cv
->c_yoff
;
1071 vp
->v_next
= cv
->c_vplist
;
1074 if ((vp
= (struct viewport
*)malloc(sizeof *vp
)) == 0)
1077 vp
->v_xs
= (cv
->c_xs
+ cv
->c_xe
) / 2;
1078 vp
->v_ys
= (3 * cv
->c_ys
+ cv
->c_ye
) / 4;
1079 vp
->v_xe
= cv
->c_xe
;
1080 vp
->v_ye
= (cv
->c_ys
+ cv
->c_ye
) / 2 - 1;
1081 vp
->v_xoff
= cv
->c_xoff
;
1082 vp
->v_yoff
= cv
->c_yoff
;
1083 vp
->v_next
= cv
->c_vplist
;
1086 if ((vp
= (struct viewport
*)malloc(sizeof *vp
)) == 0)
1089 vp
->v_xs
= cv
->c_xs
;
1090 vp
->v_ys
= (3 * cv
->c_ys
+ cv
->c_ye
) / 4;
1091 vp
->v_xe
= (3 * cv
->c_xs
+ cv
->c_xe
) / 4 - 1;
1092 vp
->v_ye
= (cv
->c_ys
+ cv
->c_ye
) / 2 - 1;
1093 vp
->v_xoff
= cv
->c_xoff
;
1094 vp
->v_yoff
= cv
->c_yoff
;
1095 vp
->v_next
= cv
->c_vplist
;
1098 if ((vp
= (struct viewport
*)malloc(sizeof *vp
)) == 0)
1101 vp
->v_xs
= cv
->c_xs
;
1102 vp
->v_ys
= cv
->c_ys
;
1103 vp
->v_xe
= cv
->c_xe
;
1104 vp
->v_ye
= (3 * cv
->c_ys
+ cv
->c_ye
) / 4 - 1;
1105 vp
->v_xoff
= cv
->c_xoff
;
1106 vp
->v_yoff
= cv
->c_yoff
;
1107 vp
->v_next
= cv
->c_vplist
;
1111 vp
->v_xs
= cv
->c_xs
;
1112 vp
->v_ys
= cv
->c_ys
;
1113 vp
->v_xe
= cv
->c_xe
;
1114 vp
->v_ye
= cv
->c_ye
;
1115 vp
->v_xoff
= cv
->c_xoff
;
1116 vp
->v_yoff
= cv
->c_yoff
;
1117 vp
->v_next
= cv
->c_vplist
;
1121 if (cv
->c_xs
< display
->d_vpxmin
|| display
->d_vpxmin
== -1)
1122 display
->d_vpxmin
= cv
->c_xs
;
1123 if (cv
->c_xe
> display
->d_vpxmax
|| display
->d_vpxmax
== -1)
1124 display
->d_vpxmax
= cv
->c_xe
;
1130 RethinkViewportOffsets(cv
)
1133 struct viewport
*vp
;
1135 for (vp
= cv
->c_vplist
; vp
; vp
= vp
->v_next
)
1137 vp
->v_xoff
= cv
->c_xoff
;
1138 vp
->v_yoff
= cv
->c_yoff
;
1143 * if the adaptflag is on, we keep the size of this display, else
1144 * we may try to restore our old window sizes.
1155 /* Check for toggle */
1156 if (D_IM
&& strcmp(D_IM
, D_EI
))
1163 /* Check for toggle */
1164 if (D_KS
&& strcmp(D_KS
, D_KE
))
1166 if (D_CCS
&& strcmp(D_CCS
, D_CCE
))
1174 D_rend
= mchar_null
;
1177 ResizeDisplay(D_defwidth
, D_defheight
);
1178 ChangeScrollRegion(0, D_height
- 1);
1182 debug1("we %swant to adapt all our windows to the display\n",
1183 (adapt
) ? "" : "don't ");
1184 /* In case the size was changed by a init sequence */
1185 CheckScreenSize((adapt
) ? 2 : 0);
1197 ResizeDisplay(D_defwidth
, D_defheight
);
1199 ChangeScrollRegion(0, D_height
- 1);
1202 CursorVisibility(0);
1204 SetRendition(&mchar_null
);
1211 ShowHStatus((char *)0);
1216 GotoPos(0, D_height
- 1);
1230 if (!D_insert
&& D_x
< D_width
- 1)
1244 RefreshLine(D_y
, D_x
, D_width
-1, 0);
1256 if (D_insert
&& D_x
< D_width
- 1)
1265 if (D_x
< D_width
- 1)
1272 if (D_CLP
|| D_y
!= D_bot
)
1277 GotoPos(D_width
- 1, y
);
1280 debug("PUTCHARLP: lp_missing!\n");
1285 /* XXX -> PutChar ? */
1289 D_lpchar
.image
= D_mbcs
;
1297 * RAW_PUTCHAR() is for all text that will be displayed.
1298 * NOTE: charset Nr. 0 has a conversion table, but c1, c2, ... don't.
1309 if (D_encoding
== UTF8
)
1311 c
= (c
& 255) | (unsigned char)D_rend
.font
<< 8;
1317 D_x
+= D_AM
? 1 : -1;
1320 else if (utf8_isdouble(c
))
1329 AddCStr2(D_CS0
, '0');
1339 if (is_dw_font(D_rend
.font
))
1349 if (D_x
== D_width
- 1)
1350 D_x
+= D_AM
? 1 : -1;
1355 # if defined(ENCODINGS) && defined(DW_CHARS)
1357 c
= PrepareEncodedChar(c
);
1362 if (D_xtable
&& D_xtable
[(int)(unsigned char)D_rend
.font
] && D_xtable
[(int)(unsigned char)D_rend
.font
][(int)(unsigned char)c
])
1363 AddStr(D_xtable
[(int)(unsigned char)D_rend
.font
][(int)(unsigned char)c
]);
1365 AddChar(D_rend
.font
!= '0' ? c
: D_c0_tab
[(int)(unsigned char)c
]);
1373 if (++D_x
>= D_width
)
1377 else if (!D_CLP
|| D_x
> D_width
)
1380 if (D_y
< D_height
-1 && D_y
!= D_bot
)
1398 /* this is for ESC-sequences only (AddChar is a macro) */
1407 if (display
&& s
&& *s
)
1410 tputs(s
, 1, DoAddChar
);
1419 if (display
&& s
&& *s
)
1422 tputs(tgoto(s
, 0, c
), 1, DoAddChar
);
1427 /* Insert mode is a toggle on some terminals, so we need this hack:
1433 if (display
&& on
!= D_insert
&& D_IM
)
1443 /* ...and maybe keypad application mode is a toggle, too:
1453 if (display
&& D_keypad
!= on
&& D_KS
)
1472 if (display
&& D_cursorkeys
!= on
&& D_CCS
)
1487 if (display
&& D_revvid
!= on
&& D_CVR
)
1501 if (display
&& D_curvis
!= v
)
1504 AddCStr(D_VE
); /* do this always, just to be safe */
1506 if (v
== -1 && D_VI
)
1508 else if (v
== 1 && D_VS
)
1520 if (display
&& D_mouse
!= mode
)
1527 sprintf(mousebuf
, "\033[?%dl", D_mouse
);
1532 sprintf(mousebuf
, "\033[?%dh", mode
);
1559 tputs(s
, 1, CountChars
);
1567 CallRewrite(y
, xs
, xe
, doit
)
1568 int y
, xs
, xe
, doit
;
1570 struct canvas
*cv
, *cvlist
, *cvlnext
;
1571 struct viewport
*vp
;
1572 struct layer
*oldflayer
;
1575 debug3("CallRewrite %d %d %d\n", y
, xs
, xe
);
1580 for (cv
= D_cvlist
; cv
; cv
= cv
->c_next
)
1582 if (y
< cv
->c_ys
|| y
> cv
->c_ye
|| xe
< cv
->c_xs
|| xs
> cv
->c_xe
)
1584 for (vp
= cv
->c_vplist
; vp
; vp
= vp
->v_next
)
1585 if (y
>= vp
->v_ys
&& y
<= vp
->v_ye
&& xe
>= vp
->v_xs
&& xs
<= vp
->v_xe
)
1593 flayer
= cv
->c_layer
;
1594 cvlist
= flayer
->l_cvlist
;
1595 cvlnext
= cv
->c_lnext
;
1596 flayer
->l_cvlist
= cv
;
1598 LayRewrite(y
- vp
->v_yoff
, xs
- vp
->v_xoff
, xe
- vp
->v_xoff
, &D_rend
, 1);
1599 flayer
->l_cvlist
= cvlist
;
1600 cv
->c_lnext
= cvlnext
;
1604 if (cv
== 0 || cv
->c_layer
== 0)
1605 return EXPENSIVE
; /* not found or nothing on it */
1606 if (xs
< vp
->v_xs
|| xe
> vp
->v_xe
)
1607 return EXPENSIVE
; /* crosses viewport boundaries */
1608 if (y
- vp
->v_yoff
< 0 || y
- vp
->v_yoff
>= cv
->c_layer
->l_height
)
1609 return EXPENSIVE
; /* line not on layer */
1610 if (xs
- vp
->v_xoff
< 0 || xe
- vp
->v_xoff
>= cv
->c_layer
->l_width
)
1611 return EXPENSIVE
; /* line not on layer */
1613 if (D_encoding
== UTF8
)
1617 flayer
= cv
->c_layer
;
1618 debug3("Calling Rewrite %d %d %d\n", y
- vp
->v_yoff
, xs
- vp
->v_xoff
, xe
- vp
->v_xoff
);
1619 cost
= LayRewrite(y
- vp
->v_yoff
, xs
- vp
->v_xoff
, xe
- vp
->v_xoff
, &D_rend
, 0);
1622 cost
+= D_EIcost
+ D_IMcost
;
1631 register int dy
, dx
, x1
, y1
;
1632 register int costx
, costy
;
1636 enum move_t xm
= M_NONE
, ym
= M_NONE
;
1647 x1
= -1; /* don't know how the terminal treats this */
1655 if (dy
== 0 && dx
== 0)
1657 debug2("GotoPos (%d,%d)", x1
, y1
);
1658 debug2(" -> (%d,%d)\n", x2
, y2
);
1659 if (!D_MS
) /* Safe to move ? */
1660 SetRendition(&mchar_null
);
1661 if (y1
< 0 /* don't know the y position */
1662 || (y2
> D_bot
&& y1
<= D_bot
) /* have to cross border */
1663 || (y2
< D_top
&& y1
>= D_top
)) /* of scrollregion ? */
1666 if (D_HO
&& !x2
&& !y2
)
1669 AddCStr(tgoto(D_CM
, x2
, y2
));
1675 /* some scrollregion implementations don't allow movements
1676 * away from the region. sigh.
1678 if ((y1
> D_bot
&& y2
> y1
) || (y1
< D_top
&& y2
< y1
))
1681 /* Calculate CMcost */
1682 if (D_HO
&& !x2
&& !y2
)
1685 s
= tgoto(D_CM
, x2
, y2
);
1686 CMcost
= CalcCost(s
);
1688 /* Calculate the cost to move the cursor to the right x position */
1690 if (x1
>= 0) /* relativ x positioning only if we know where we are */
1694 if (D_CRI
&& (dx
> 1 || !D_ND
))
1696 costx
= CalcCost(tgoto(D_CRI
, 0, dx
));
1699 if ((m
= D_NDcost
* dx
) < costx
)
1704 /* Speedup: dx <= LayRewrite() */
1705 if (dx
< costx
&& (m
= CallRewrite(y1
, x1
, x2
- 1, 0)) < costx
)
1713 if (D_CLE
&& (dx
< -1 || !D_BC
))
1715 costx
= CalcCost(tgoto(D_CLE
, 0, -dx
));
1718 if ((m
= -dx
* D_LEcost
) < costx
)
1727 /* Speedup: LayRewrite() >= x2 */
1728 if (x2
+ D_CRcost
< costx
&& (m
= (x2
? CallRewrite(y1
, 0, x2
- 1, 0) : 0) + D_CRcost
) < costx
)
1734 /* Check if it is already cheaper to do CM */
1735 if (costx
>= CMcost
)
1738 /* Calculate the cost to move the cursor to the right y position */
1742 if (D_CDO
&& dy
> 1) /* DO & NL are always != 0 */
1744 costy
= CalcCost(tgoto(D_CDO
, 0, dy
));
1747 if ((m
= dy
* ((x2
== 0) ? D_NLcost
: D_DOcost
)) < costy
)
1755 if (D_CUP
&& (dy
< -1 || !D_UP
))
1757 costy
= CalcCost(tgoto(D_CUP
, 0, -dy
));
1760 if ((m
= -dy
* D_UPcost
) < costy
)
1769 /* Finally check if it is cheaper to do CM */
1770 if (costx
+ costy
>= CMcost
)
1780 AddCStr2(D_CLE
, -dx
);
1787 AddCStr2(D_CRI
, dx
);
1796 (void) CallRewrite(y1
, x1
, x2
- 1, 1);
1809 AddCStr2(D_CUP
, -dy
);
1812 s
= (x2
== 0) ? D_NL
: D_DO
;
1817 AddCStr2(D_CDO
, dy
);
1830 ClearArea(0, 0, 0, D_width
- 1, D_width
- 1, D_height
- 1, 0, 0);
1834 ClearArea(x1
, y1
, xs
, xe
, x2
, y2
, bce
, uselayfn
)
1835 int x1
, y1
, xs
, xe
, x2
, y2
, bce
, uselayfn
;
1839 struct viewport
*vp
;
1841 debug2("Clear %d,%d", x1
, y1
);
1842 debug2(" %d-%d", xs
, xe
);
1843 debug2(" %d,%d", x2
, y2
);
1844 debug2(" uselayfn=%d bce=%d\n", uselayfn
, bce
);
1854 if (D_UT
) /* Safe to erase ? */
1855 SetRendition(&mchar_null
);
1860 if (D_lp_missing
&& y1
<= D_bot
&& xe
>= D_width
- 1)
1862 if (y2
> D_bot
|| (y2
== D_bot
&& x2
>= D_width
- 1))
1865 if (x2
== D_width
- 1 && (xs
== 0 || y1
== y2
) && xe
== D_width
- 1 && y2
== D_height
- 1 && (!bce
|| D_BE
))
1868 if (x1
== 0 && y1
== 0 && D_auto_nuke
)
1871 if (x1
== 0 && y1
== 0 && D_CL
)
1878 * Workaround a hp700/22 terminal bug. Do not use CD where CE
1879 * is also appropriate.
1881 if (D_CD
&& (y1
< y2
|| !D_CE
))
1888 if (x1
== 0 && xs
== 0 && (xe
== D_width
- 1 || y1
== y2
) && y1
== 0 && D_CCD
&& (!bce
|| D_BE
))
1895 for (y
= y1
; y
<= y2
; y
++, x1
= xs
)
1899 if (x1
== 0 && D_CB
&& (xxe
!= D_width
- 1 || (D_x
== xxe
&& D_y
== y
)) && (!bce
|| D_BE
))
1905 if (xxe
== D_width
- 1 && D_CE
&& (!bce
|| D_BE
))
1914 for (cv
= D_cvlist
; cv
; cv
= cv
->c_next
)
1916 if (y
< cv
->c_ys
|| y
> cv
->c_ye
|| xxe
< cv
->c_xs
|| x1
> cv
->c_xe
)
1918 for (vp
= cv
->c_vplist
; vp
; vp
= vp
->v_next
)
1919 if (y
>= vp
->v_ys
&& y
<= vp
->v_ye
&& xxe
>= vp
->v_xs
&& x1
<= vp
->v_xe
)
1924 if (cv
&& cv
->c_layer
&& x1
>= vp
->v_xs
&& xxe
<= vp
->v_xe
&&
1925 y
- vp
->v_yoff
>= 0 && y
- vp
->v_yoff
< cv
->c_layer
->l_height
&&
1926 xxe
- vp
->v_xoff
>= 0 && x1
- vp
->v_xoff
< cv
->c_layer
->l_width
)
1928 struct layer
*oldflayer
= flayer
;
1929 struct canvas
*cvlist
, *cvlnext
;
1930 flayer
= cv
->c_layer
;
1931 cvlist
= flayer
->l_cvlist
;
1932 cvlnext
= cv
->c_lnext
;
1933 flayer
->l_cvlist
= cv
;
1935 LayClearLine(y
- vp
->v_yoff
, x1
- vp
->v_xoff
, xxe
- vp
->v_xoff
, bce
);
1936 flayer
->l_cvlist
= cvlist
;
1937 cv
->c_lnext
= cvlnext
;
1942 ClearLine((struct mline
*)0, y
, x1
, xxe
, bce
);
1948 * if cur_only > 0, we only redisplay current line, as a full refresh is
1949 * too expensive over a low baud line.
1957 /* XXX do em all? */
1959 ChangeScrollRegion(0, D_height
- 1);
1962 CursorVisibility(0);
1964 SetRendition(&mchar_null
);
1971 if (cur_only
> 0 && D_fore
)
1972 RefreshArea(0, D_fore
->w_y
, D_width
- 1, D_fore
->w_y
, 1);
1976 CV_CALL(D_forecv
, LayRestore();LaySetCursor());
1980 RedisplayDisplays(cur_only
)
1983 struct display
*olddisplay
= display
;
1984 for (display
= displays
; display
; display
= display
->d_next
)
1985 Redisplay(cur_only
);
1986 display
= olddisplay
;
1992 ScrollH(y
, xs
, xe
, n
, bce
, oml
)
1993 int y
, xs
, xe
, n
, bce
;
2000 if (xe
!= D_width
- 1)
2002 RefreshLine(y
, xs
, xe
, 0);
2003 /* UpdateLine(oml, y, xs, xe); */
2008 SetRendition(&mchar_null
);
2015 if (n
>= xe
- xs
+ 1)
2017 if (D_CDC
&& !(n
== 1 && D_DC
))
2026 RefreshLine(y
, xs
, xe
, 0);
2027 /* UpdateLine(oml, y, xs, xe); */
2033 if (-n
>= xe
- xs
+ 1)
2037 if (D_CIC
&& !(n
== -1 && D_IC
))
2038 AddCStr2(D_CIC
, -n
);
2047 SetRendition(&mchar_null
);
2053 bce
= 0; /* all done */
2057 /* UpdateLine(oml, y, xs, xe); */
2058 RefreshLine(y
, xs
, xe
, 0);
2064 SetRendition(&mchar_null
);
2070 bce
= 0; /* all done */
2076 ClearLine((struct mline
*)0, y
, xe
- n
+ 1, xe
, bce
);
2078 ClearLine((struct mline
*)0, y
, xs
, xs
- n
- 1, bce
);
2080 if (D_lp_missing
&& y
== D_bot
)
2083 WriteLP(D_width
- 1 - n
, y
);
2089 ScrollV(xs
, ys
, xe
, ye
, n
, bce
)
2090 int xs
, ys
, xe
, ye
, n
, bce
;
2095 int alok
, dlok
, aldlfaster
;
2101 if (n
>= ye
- ys
+ 1 || -n
>= ye
- ys
+ 1)
2103 ClearArea(xs
, ys
, xs
, xe
, xe
, ye
, bce
, 0);
2106 if (xs
> D_vpxmin
|| xe
< D_vpxmax
)
2108 RefreshArea(xs
, ys
, xe
, ye
, 0);
2114 if (D_bot
> ye
|| D_bot
< ys
)
2119 if (missy
> ye
|| missy
< ys
)
2130 if (n
>= ye
- ys
+ 1)
2135 if (ys
< D_top
|| D_bot
!= ye
)
2136 ChangeScrollRegion(ys
, ye
);
2137 alok
= (D_AL
|| D_CAL
|| (ys
>= D_top
&& ye
== D_bot
&& up
));
2138 dlok
= (D_DL
|| D_CDL
|| (ys
>= D_top
&& ye
== D_bot
&& !up
));
2139 if (D_top
!= ys
&& !(alok
&& dlok
))
2140 ChangeScrollRegion(ys
, ye
);
2144 (oldbot
== D_bot
&& up
&& D_top
== ys
&& D_bot
== ye
)))
2146 WriteLP(D_width
- 1, oldbot
);
2147 if (oldbot
== D_bot
) /* have scrolled */
2152 ChangeScrollRegion(oldtop, oldbot);
2155 ClearLine((struct mline
*)0, ye
, xs
, xe
, bce
);
2162 SetRendition(&mchar_null
);
2168 aldlfaster
= (n
> 1 && ys
>= D_top
&& ye
== D_bot
&& ((up
&& D_CDL
) || (!up
&& D_CAL
)));
2170 if ((up
|| D_SR
) && D_top
== ys
&& D_bot
== ye
&& !aldlfaster
)
2175 for(i
= n
; i
-- > 0; )
2176 AddCStr(D_NL
); /* was SF, I think NL is faster */
2181 for(i
= n
; i
-- > 0; )
2185 else if (alok
&& dlok
)
2187 if (up
|| ye
!= D_bot
)
2189 GotoPos(0, up
? ys
: ye
+1-n
);
2190 if (D_CDL
&& !(n
== 1 && D_DL
))
2196 if (!up
|| ye
!= D_bot
)
2198 GotoPos(0, up
? ye
+1-n
: ys
);
2199 if (D_CAL
&& !(n
== 1 && D_AL
))
2208 RefreshArea(xs
, ys
, xe
, ye
, 0);
2214 ClearArea(xs
, ye
- n
+ 1, xs
, xe
, xe
, ye
, bce
, 0);
2216 ClearArea(xs
, ys
, xs
, xe
, xe
, ys
+ n
- 1, bce
, 0);
2218 if (D_lp_missing
&& missy
!= D_bot
)
2219 WriteLP(D_width
- 1, missy
);
2221 ChangeScrollRegion(oldtop, oldbot);
2222 if (D_lp_missing && missy != D_bot)
2223 WriteLP(D_width - 1, missy);
2231 register int i
, j
, old
, typ
;
2233 if (!display
|| (old
= D_rend
.attr
) == new)
2236 D_col16change
= (old
^ new) & (A_BFG
| A_BBG
);
2237 new ^= D_col16change
;
2241 #if defined(TERMINFO) && defined(USE_SGR)
2247 tputs(tparm(D_SA
, new & A_SO
, new & A_US
, new & A_RV
, new & A_BL
,
2248 new & A_DI
, new & A_BD
, 0 , 0 ,
2254 rend_setdefault(&D_rend
);
2261 if ((new & old
) != old
)
2271 /* ansi attrib handling: \E[m resets color, too */
2273 rend_setdefault(&D_rend
);
2278 /* D_ME may also reset the alternate charset */
2290 for (i
= 0, j
= 1; old
&& i
< NATTR
; i
++, j
<<= 1)
2297 AddCStr(D_attrtab
[i
]);
2298 typ
|= D_attrtyp
[i
];
2309 int old
= D_rend
.font
;
2310 if (!display
|| old
== new)
2314 if (D_encoding
&& CanEncodeFont(D_encoding
, new))
2316 if (new == D_realfont
)
2320 if (D_xtable
&& D_xtable
[(int)(unsigned char)new] &&
2321 D_xtable
[(int)(unsigned char)new][256])
2323 AddCStr(D_xtable
[(int)(unsigned char)new][256]);
2327 if (!D_CG0
&& new != '0')
2346 AddCStr2(D_CS0
, new);
2361 jj
= (jj
- 232) / 6;
2362 jj
= (jj
& 1) << 3 | (jj
& 2 ? 7 : 0);
2370 min
= r
< g
? (r
< b
? r
: b
) : (g
< b
? g
: b
);
2371 max
= r
> g
? (r
> b
? r
: b
) : (g
> b
? g
: b
);
2373 jj
= ((max
+ 1) & 2) << 2 | ((max
+ 1) & 4 ? 7 : 0);
2375 jj
= (b
- min
) / (max
- min
) << 2 | (g
- min
) / (max
- min
) << 1 | (r
-
2376 min
) / (max
- min
) | (max
> 3 ? 8 : 0);
2389 return (jj
- 232) / 3 + 80;
2396 return ((r
+ 1) / 2) * 16 + ((g
+ 1) / 2) * 4 + ((b
+ 1) / 2) + 16;
2407 static unsigned char sftrans
[8] = {0,4,2,6,1,5,3,7};
2412 of
= rend_getfg(&D_rend
);
2413 ob
= rend_getbg(&D_rend
);
2416 /* intense default not invented yet */
2422 debug2("SetColor %d %d", coli2e(of
), coli2e(ob
));
2423 debug2(" -> %d %d\n", coli2e(f
), coli2e(b
));
2424 debug2("(%d %d", of
, ob
);
2425 debug2(" -> %d %d)\n", f
, b
);
2427 if (!D_CAX
&& D_hascolor
&& ((f
== 0 && f
!= of
) || (b
== 0 && b
!= ob
)))
2434 oattr
= D_rend
.attr
;
2435 AddCStr(D_ME
? D_ME
: "\033[m");
2439 /* D_ME may also reset the alternate charset */
2452 rend_setfg(&D_rend
, f
);
2453 rend_setbg(&D_rend
, b
);
2459 f
= f
? coli2e(f
) : -1;
2460 b
= b
? coli2e(b
) : -1;
2461 of
= of
? coli2e(of
) : -1;
2462 ob
= ob
? coli2e(ob
) : -1;
2464 if (f
!= of
&& f
> 15 && D_CCO
!= 256)
2465 f
= D_CCO
== 88 && D_CAF
? color256to88(f
) : color256to16(f
);
2466 if (f
!= of
&& f
> 15 && D_CAF
)
2471 if (b
!= ob
&& b
> 15 && D_CCO
!= 256)
2472 b
= D_CCO
== 88 && D_CAB
? color256to88(b
) : color256to16(b
);
2473 if (b
!= ob
&& b
> 15 && D_CAB
)
2479 if (f
!= of
&& f
!= (of
| 8))
2482 AddCStr("\033[39m"); /* works because AX is set */
2484 AddCStr2(D_CAF
, f
& 7);
2486 AddCStr2(D_CSF
, sftrans
[f
& 7]);
2488 if (b
!= ob
&& b
!= (ob
| 8))
2491 AddCStr("\033[49m"); /* works because AX is set */
2493 AddCStr2(D_CAB
, b
& 7);
2495 AddCStr2(D_CSB
, sftrans
[b
& 7]);
2498 if (f
!= of
&& D_CXT
&& (f
& 8) != 0 && f
!= -1)
2501 AddCStr2("\033[9%p1%dm", f
& 7);
2503 AddCStr2("\033[9%dm", f
& 7);
2506 if (b
!= ob
&& D_CXT
&& (b
& 8) != 0 && b
!= -1)
2509 AddCStr2("\033[10%p1%dm", b
& 7);
2511 AddCStr2("\033[10%dm", b
& 7);
2523 SetColor(rend_getfg(&D_rend
), new);
2533 if (nattr2color
&& D_hascolor
&& (mc
->attr
& nattr2color
) != 0)
2535 static struct mchar mmc
;
2538 for (i
= 0; i
< 8; i
++)
2539 if (attr2color
[i
] && (mc
->attr
& (1 << i
)) != 0)
2541 if (mc
->color
== 0 && attr2color
[i
][3])
2542 ApplyAttrColor(attr2color
[i
][3], &mmc
);
2543 else if ((mc
->color
& 0x0f) == 0 && attr2color
[i
][2])
2544 ApplyAttrColor(attr2color
[i
][2], &mmc
);
2545 else if ((mc
->color
& 0xf0) == 0 && attr2color
[i
][1])
2546 ApplyAttrColor(attr2color
[i
][1], &mmc
);
2548 ApplyAttrColor(attr2color
[i
][0], &mmc
);
2551 debug2("SetRendition: mapped to %02x %02x\n", (unsigned char)mc
->attr
, 0x99 - (unsigned char)mc
->color
);
2553 if (D_hascolor
&& D_CC8
&& (mc
->attr
& (A_BFG
|A_BBG
)))
2556 if ((mc
->attr
& A_BFG
) && D_MD
)
2558 if ((mc
->attr
& A_BBG
) && D_MB
)
2560 if (D_rend
.attr
!= a
)
2563 else if (D_rend
.attr
!= mc
->attr
)
2566 if (D_rend
.color
!= mc
->color
2568 || D_rend
.colorx
!= mc
->colorx
2574 SetColor(rend_getfg(mc
), rend_getbg(mc
));
2577 if (D_rend
.font
!= mc
->font
)
2583 SetRenditionMline(ml
, x
)
2589 if (nattr2color
&& D_hascolor
&& (ml
->attr
[x
] & nattr2color
) != 0)
2592 copy_mline2mchar(&mc
, ml
, x
);
2596 if (D_hascolor
&& D_CC8
&& (ml
->attr
[x
] & (A_BFG
|A_BBG
)))
2598 int a
= ml
->attr
[x
];
2599 if ((ml
->attr
[x
] & A_BFG
) && D_MD
)
2601 if ((ml
->attr
[x
] & A_BBG
) && D_MB
)
2603 if (D_rend
.attr
!= a
)
2606 else if (D_rend
.attr
!= ml
->attr
[x
])
2607 SetAttr(ml
->attr
[x
]);
2609 if (D_rend
.color
!= ml
->color
[x
]
2611 || D_rend
.colorx
!= ml
->colorx
[x
]
2619 copy_mline2mchar(&mc
, ml
, x
);
2620 SetColor(rend_getfg(&mc
), rend_getbg(&mc
));
2624 if (D_rend
.font
!= ml
->font
[x
])
2625 SetFont(ml
->font
[x
]);
2633 register char *s
, *t
;
2643 debug("tc not inited, just writing msg\n");
2644 if (D_processinputdata
)
2645 return; /* XXX: better */
2651 if (!use_hardstatus
|| !D_HS
)
2658 max
= D_WS
> 0 ? D_WS
: (D_width
- !D_CLP
);
2662 if (strcmp(msg
, D_status_lastmsg
) == 0)
2664 debug("same message - increase timeout");
2665 SetTimeout(&D_statusev
, MsgWait
);
2672 gettimeofday(&now
, NULL
);
2673 ti
= (now
.tv_sec
- D_status_time
.tv_sec
) * 1000 + (now
.tv_usec
- D_status_time
.tv_usec
) / 1000;
2674 if (ti
< MsgMinWait
)
2675 DisplaySleep1000(MsgMinWait
- ti
, 0);
2679 for (s
= t
= msg
; *s
&& t
- msg
< max
; ++s
)
2682 else if ((unsigned char)*s
>= ' ' && *s
!= 0177)
2687 if (t
- msg
>= D_status_buflen
)
2690 if (D_status_lastmsg
)
2691 buf
= realloc(D_status_lastmsg
, t
- msg
+ 1);
2693 buf
= malloc(t
- msg
+ 1);
2696 D_status_lastmsg
= buf
;
2697 D_status_buflen
= t
- msg
+ 1;
2700 if (t
- msg
< D_status_buflen
)
2701 strcpy(D_status_lastmsg
, msg
);
2702 D_status_len
= t
- msg
;
2703 D_status_lastx
= D_x
;
2704 D_status_lasty
= D_y
;
2705 if (!use_hardstatus
|| D_has_hstatus
== HSTATUS_IGNORE
|| D_has_hstatus
== HSTATUS_MESSAGE
)
2707 D_status
= STATUS_ON_WIN
;
2708 debug1("using STATLINE %d\n", STATLINE
);
2709 GotoPos(0, STATLINE
);
2710 SetRendition(&mchar_so
);
2713 if (D_status_len
< max
)
2715 /* Wayne Davison: add extra space for readability */
2717 SetRendition(&mchar_null
);
2719 if (D_status_len
< max
)
2731 D_status
= STATUS_ON_HS
;
2737 if (D_status
== STATUS_ON_WIN
)
2739 struct display
*olddisplay
= display
;
2740 struct layer
*oldflayer
= flayer
;
2742 ASSERT(D_obuffree
== D_obuflen
);
2743 /* this is copied over from RemoveStatus() */
2745 GotoPos(0, STATLINE
);
2746 RefreshLine(STATLINE
, 0, D_status_len
- 1, 0);
2747 GotoPos(D_status_lastx
, D_status_lasty
);
2748 flayer
= D_forecv
? D_forecv
->c_layer
: 0;
2751 display
= olddisplay
;
2753 D_status_obuflen
= D_obuflen
;
2754 D_status_obuffree
= D_obuffree
;
2755 D_obuffree
= D_obuflen
= 0;
2756 D_status
= STATUS_ON_WIN
;
2758 gettimeofday(&D_status_time
, NULL
);
2759 SetTimeout(&D_statusev
, MsgWait
);
2762 RefreshBraille(); /* let user see multiple Msg()s */
2769 struct display
*olddisplay
;
2770 struct layer
*oldflayer
;
2775 if (!(where
= D_status
))
2778 debug("RemoveStatus\n");
2779 if (D_status_obuffree
>= 0)
2781 D_obuflen
= D_status_obuflen
;
2782 D_obuffree
= D_status_obuffree
;
2783 D_status_obuffree
= -1;
2788 olddisplay
= display
;
2790 if (where
== STATUS_ON_WIN
)
2792 GotoPos(0, STATLINE
);
2793 RefreshLine(STATLINE
, 0, D_status_len
- 1, 0);
2794 GotoPos(D_status_lastx
, D_status_lasty
);
2798 flayer
= D_forecv
? D_forecv
->c_layer
: 0;
2801 display
= olddisplay
;
2805 /* refresh the display's hstatus line */
2812 if (D_status
== STATUS_ON_WIN
&& D_has_hstatus
== HSTATUS_LASTLINE
&& STATLINE
== D_height
-1)
2813 return; /* sorry, in use */
2817 if (D_HS
&& D_has_hstatus
== HSTATUS_HS
)
2819 if (!D_hstatus
&& (str
== 0 || *str
== 0))
2821 debug("ShowHStatus: using HS\n");
2822 SetRendition(&mchar_null
);
2827 if (str
== 0 || *str
== 0)
2830 max
= D_WS
> 0 ? D_WS
: (D_width
- !D_CLP
);
2831 if ((int)strlen(str
) > max
)
2838 else if (D_has_hstatus
== HSTATUS_LASTLINE
)
2840 debug("ShowHStatus: using last line\n");
2843 str
= str
? str
: "";
2847 GotoPos(0, D_height
- 1);
2848 SetRendition(captionalways
|| D_cvlist
== 0 || D_cvlist
->c_next
? &mchar_null
: &mchar_so
);
2849 PutWinMsg(str
, 0, l
);
2850 if (!captionalways
&& D_cvlist
&& !D_cvlist
->c_next
)
2851 while (l
++ < D_width
)
2854 ClearArea(l
, D_height
- 1, l
, D_width
- 1, D_width
- 1, D_height
- 1, 0, 0);
2855 if (ox
!= -1 && oy
!= -1)
2857 D_hstatus
= *str
? 1 : 0;
2858 SetRendition(&mchar_null
);
2860 else if (str
&& *str
&& D_has_hstatus
== HSTATUS_MESSAGE
)
2862 debug("ShowHStatus: using message\n");
2869 * Refreshes the harstatus of the fore window. Shouldn't be here...
2876 evdeq(&D_hstatusev
);
2877 if (D_status
== STATUS_ON_HS
)
2879 buf
= MakeWinMsgEv(hstatusstring
, D_fore
, '%', (D_HS
&& D_has_hstatus
== HSTATUS_HS
&& D_WS
> 0) ? D_WS
: D_width
- !D_CLP
, &D_hstatusev
, 0);
2883 if (D_has_hstatus
!= HSTATUS_IGNORE
&& D_hstatusev
.timeout
.tv_sec
)
2884 evenq(&D_hstatusev
);
2887 ShowHStatus((char *)0);
2890 /*********************************************************************/
2892 * Here come the routines that refresh an arbitrary part of the screen.
2902 debug("Signalling full refresh!\n");
2903 for (cv
= D_cvlist
; cv
; cv
= cv
->c_next
)
2905 CV_CALL(cv
, LayRedisplayLine(-1, -1, -1, isblank
));
2906 display
= cv
->c_display
; /* just in case! */
2908 RefreshArea(0, 0, D_width
- 1, D_height
- 1, isblank
);
2912 RefreshArea(xs
, ys
, xe
, ye
, isblank
)
2913 int xs
, ys
, xe
, ye
, isblank
;
2917 debug2("Refresh Area: %d,%d", xs
, ys
);
2918 debug3(" - %d,%d (isblank=%d)\n", xe
, ye
, isblank
);
2919 if (!isblank
&& xs
== 0 && xe
== D_width
- 1 && ye
== D_height
- 1 && (ys
== 0 || D_CD
))
2921 ClearArea(xs
, ys
, xs
, xe
, xe
, ye
, 0, 0);
2924 for (y
= ys
; y
<= ye
; y
++)
2925 RefreshLine(y
, xs
, xe
, isblank
);
2929 RefreshLine(y
, from
, to
, isblank
)
2930 int y
, from
, to
, isblank
;
2932 struct viewport
*vp
, *lvp
;
2933 struct canvas
*cv
, *lcv
, *cvlist
, *cvlnext
;
2934 struct layer
*oldflayer
;
2941 debug2("RefreshLine %d %d", y
, from
);
2942 debug2(" %d %d\n", to
, isblank
);
2944 if (D_status
== STATUS_ON_WIN
&& y
== STATLINE
)
2946 if (to
>= D_status_len
)
2947 D_status_len
= to
+ 1;
2948 return; /* can't refresh status */
2951 /* The following check makes plenty of sense. Unfortunately,
2952 vte-based terminals (notably gnome-terminal) experience a quirk
2953 that causes the final line not to update properly when it falls outside
2954 the scroll region; clearing the line with D_CE avoids the glitch,
2955 so we'll disable this perfectly sensible shortcut until such a time
2956 as widespread vte installations lack the glitch.
2958 See http://bugzilla.gnome.org/show_bug.cgi?id=542087 for current
2959 status of the VTE bug report, and
2960 https://savannah.gnu.org/bugs/index.php?23699 for the history from
2961 the Savannah BTS. */
2963 if (y
== D_height
- 1 && D_has_hstatus
== HSTATUS_LASTLINE
)
2970 if (isblank
== 0 && D_CE
&& to
== D_width
- 1 && from
< to
)
2974 SetRendition(&mchar_null
);
2982 for (cv
= display
->d_cvlist
; cv
; cv
= cv
->c_next
)
2984 if (y
== cv
->c_ye
+ 1 && from
>= cv
->c_xs
&& from
<= cv
->c_xe
)
2986 p
= Layer2Window(cv
->c_layer
);
2987 buf
= MakeWinMsgEv(captionstring
, p
, '%', cv
->c_xe
- cv
->c_xs
+ (cv
->c_xe
+ 1 < D_width
|| D_CLP
), &cv
->c_captev
, 0);
2988 if (cv
->c_captev
.timeout
.tv_sec
)
2989 evenq(&cv
->c_captev
);
2990 xx
= to
> cv
->c_xe
? cv
->c_xe
: to
;
2993 SetRendition(&mchar_so
);
2994 if (l
> xx
- cv
->c_xs
+ 1)
2995 l
= xx
- cv
->c_xs
+ 1;
2996 PutWinMsg(buf
, from
- cv
->c_xs
, l
);
2997 from
= cv
->c_xs
+ l
;
2998 for (; from
<= xx
; from
++)
3002 if (from
== cv
->c_xe
+ 1 && y
>= cv
->c_ys
&& y
<= cv
->c_ye
+ 1)
3005 SetRendition(&mchar_so
);
3010 if (y
< cv
->c_ys
|| y
> cv
->c_ye
|| to
< cv
->c_xs
|| from
> cv
->c_xe
)
3012 debug2("- canvas hit: %d %d", cv
->c_xs
, cv
->c_ys
);
3013 debug2(" %d %d\n", cv
->c_xe
, cv
->c_ye
);
3014 for (vp
= cv
->c_vplist
; vp
; vp
= vp
->v_next
)
3016 debug2(" - vp: %d %d", vp
->v_xs
, vp
->v_ys
);
3017 debug2(" %d %d\n", vp
->v_xe
, vp
->v_ye
);
3018 /* find leftmost overlapping vp */
3019 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
))
3027 continue; /* we advanced from */
3030 if (from
< lvp
->v_xs
)
3033 DisplayLine(&mline_null
, &mline_blank
, y
, from
, lvp
->v_xs
- 1);
3037 /* call LayRedisplayLine on canvas lcv viewport lvp */
3038 yy
= y
- lvp
->v_yoff
;
3039 xx
= to
< lvp
->v_xe
? to
: lvp
->v_xe
;
3041 if (lcv
->c_layer
&& yy
== lcv
->c_layer
->l_height
)
3044 SetRendition(&mchar_blank
);
3045 while (from
<= lvp
->v_xe
&& from
- lvp
->v_xoff
< lcv
->c_layer
->l_width
)
3050 if (from
>= lvp
->v_xe
+ 1)
3053 if (lcv
->c_layer
== 0 || yy
>= lcv
->c_layer
->l_height
|| from
- lvp
->v_xoff
>= lcv
->c_layer
->l_width
)
3056 DisplayLine(&mline_null
, &mline_blank
, y
, from
, lvp
->v_xe
);
3057 from
= lvp
->v_xe
+ 1;
3061 if (xx
- lvp
->v_xoff
>= lcv
->c_layer
->l_width
)
3062 xx
= lcv
->c_layer
->l_width
+ lvp
->v_xoff
- 1;
3064 flayer
= lcv
->c_layer
;
3065 cvlist
= flayer
->l_cvlist
;
3066 cvlnext
= lcv
->c_lnext
;
3067 flayer
->l_cvlist
= lcv
;
3069 LayRedisplayLine(yy
, from
- lvp
->v_xoff
, xx
- lvp
->v_xoff
, isblank
);
3070 flayer
->l_cvlist
= cvlist
;
3071 lcv
->c_lnext
= cvlnext
;
3076 if (!isblank
&& from
<= to
)
3077 DisplayLine(&mline_null
, &mline_blank
, y
, from
, to
);
3080 /*********************************************************************/
3082 /* clear lp_missing by writing the char on the screen. The
3083 * position must be safe.
3089 struct mchar oldrend
;
3092 ASSERT(D_lp_missing
);
3094 debug2("WriteLP(%d,%d)\n", x2
, y2
);
3101 D_lpchar
= mchar_blank
;
3104 /* Can't use PutChar */
3106 SetRendition(&D_lpchar
);
3107 PUTCHAR(D_lpchar
.image
);
3110 PUTCHAR(D_lpchar
.mbcs
);
3113 SetRendition(&oldrend
);
3117 ClearLine(oml
, y
, from
, to
, bce
)
3119 int from
, to
, y
, bce
;
3123 struct mchar bcechar
;
3126 debug3("ClearLine %d,%d-%d\n", y
, from
, to
);
3127 if (D_UT
) /* Safe to erase ? */
3128 SetRendition(&mchar_null
);
3133 if (from
== 0 && D_CB
&& (to
!= D_width
- 1 || (D_x
== to
&& D_y
== y
)) && (!bce
|| D_BE
))
3139 if (to
== D_width
- 1 && D_CE
&& (!bce
|| D_BE
))
3150 DisplayLine(oml
, &mline_blank
, y
, from
, to
);
3153 bcechar
= mchar_blank
;
3154 rend_setbg(&bcechar
, bce
);
3155 for (x
= from
; x
<= to
; x
++)
3156 copy_mchar2mline(&bcechar
, &mline_old
, x
);
3157 DisplayLine(oml
, &mline_old
, y
, from
, to
);
3159 DisplayLine(oml
, &mline_blank
, y
, from
, to
);
3164 DisplayLine(oml
, ml
, y
, from
, to
)
3165 struct mline
*oml
, *ml
;
3169 int last2flag
= 0, delete_lp
= 0;
3172 ASSERT(y
>= 0 && y
< D_height
);
3173 ASSERT(from
>= 0 && from
< D_width
);
3174 ASSERT(to
>= 0 && to
< D_width
);
3175 if (!D_CLP
&& y
== D_bot
&& to
== D_width
- 1)
3177 if (D_lp_missing
|| !cmp_mline(oml
, ml
, to
))
3180 if ((D_IC
|| D_IM
) && from
< to
&& !dw_left(ml
, to
, D_encoding
))
3182 if ((D_IC
|| D_IM
) && from
< to
)
3191 delete_lp
= !cmp_mchar_mline(&mchar_blank
, oml
, to
) && (D_CE
|| D_DC
|| D_CDC
);
3192 D_lp_missing
= !cmp_mchar_mline(&mchar_blank
, ml
, to
);
3193 copy_mline2mchar(&D_lpchar
, ml
, to
);
3201 /* finish dw-char (can happen after a wrap) */
3202 debug("DisplayLine finishing kanji\n");
3203 SetRenditionMline(ml
, from
);
3204 PUTCHAR(ml
->image
[from
]);
3208 for (x
= from
; x
<= to
; x
++)
3210 #if 0 /* no longer needed */
3211 if (x
|| D_x
!= D_width
|| D_y
!= y
- 1)
3214 if (x
< to
|| x
!= D_width
- 1 || ml
->image
[x
+ 1])
3215 if (cmp_mline(oml
, ml
, x
))
3220 if (dw_right(ml
, x
, D_encoding
))
3223 debug1("DisplayLine on right side of dw char- x now %d\n", x
);
3226 if (x
== to
&& dw_left(ml
, x
, D_encoding
))
3227 break; /* don't start new kanji */
3229 SetRenditionMline(ml
, x
);
3230 PUTCHAR(ml
->image
[x
]);
3232 if (dw_left(ml
, x
, D_encoding
))
3233 PUTCHAR(ml
->image
[++x
]);
3236 #if 0 /* not needed any longer */
3237 /* compare != 0 because ' ' can happen when clipping occures */
3238 if (to
== D_width
- 1 && y
< D_height
- 1 && D_x
== D_width
&& ml
->image
[to
+ 1])
3244 SetRenditionMline(ml
, x
+ 1);
3245 PUTCHAR(ml
->image
[x
+ 1]);
3247 SetRenditionMline(ml
, x
);
3248 INSERTCHAR(ml
->image
[x
]);
3253 SetRendition(&mchar_null
);
3270 PUTCHARLP(c
->image
);
3275 if (D_encoding
== UTF8
)
3284 InsChar(c
, x
, xe
, y
, oml
)
3290 if (y
== D_bot
&& !D_CLP
)
3292 if (x
== D_width
- 1)
3298 if (xe
== D_width
- 1)
3304 PUTCHARLP(c
->image
);
3307 if (!(D_IC
|| D_CIC
|| D_IM
) || xe
!= D_width
- 1)
3309 RefreshLine(y
, x
, xe
, 0);
3311 /* UpdateLine(oml, y, x, xe); */
3318 if (c
->mbcs
&& D_IC
)
3323 AddCStr2(D_CIC
, c
->mbcs
? 2 : 1);
3332 RAW_PUTCHAR(c
->image
);
3337 if (D_encoding
== UTF8
)
3340 if (D_x
== D_width
- 1)
3343 RAW_PUTCHAR(c
->mbcs
);
3349 WrapChar(c
, x
, y
, xs
, ys
, xe
, ye
, ins
)
3358 bce
= rend_getbg(c
);
3363 debug2(" x %d y %d", x
, y
);
3364 debug2(" Dx %d Dy %d", D_x
, D_y
);
3365 debug2(" xs %d ys %d", xs
, ys
);
3366 debug3(" xe %d ye %d ins %d\n", xe
, ye
, ins
);
3367 if (xs
!= 0 || x
!= D_width
|| !D_AM
)
3370 ScrollV(xs
, ys
, xe
, ye
, 1, bce
);
3371 else if (y
< D_height
- 1)
3374 InsChar(c
, xs
, xe
, y
, 0);
3379 if (y
== ye
) /* we have to scroll */
3381 debug("- scrolling\n");
3382 ChangeScrollRegion(ys
, ye
);
3383 if (D_bot
!= y
|| D_x
!= D_width
|| (!bce
&& !D_BE
))
3385 debug("- have to call ScrollV\n");
3386 ScrollV(xs
, ys
, xe
, ye
, 1, bce
);
3390 else if (y
== D_bot
) /* remove unusable region? */
3391 ChangeScrollRegion(0, D_height
- 1);
3392 if (D_x
!= D_width
|| D_y
!= y
)
3394 if (D_CLP
&& y
>= 0) /* don't even try if !LP */
3395 RefreshLine(y
, D_width
- 1, D_width
- 1, 0);
3396 debug2("- refresh last char -> x,y now %d,%d\n", D_x
, D_y
);
3397 if (D_x
!= D_width
|| D_y
!= y
) /* sorry, no bonus */
3400 ScrollV(xs
, ys
, xe
, ye
, 1, bce
);
3401 GotoPos(xs
, y
== ye
|| y
== D_height
- 1 ? y
: y
+ 1);
3404 debug("- writeing new char");
3405 if (y
!= ye
&& y
< D_height
- 1)
3407 if (ins
!= D_insert
)
3409 if (ins
&& !D_insert
)
3411 InsChar(c
, 0, xe
, y
, 0);
3412 debug2(" -> done with insert (%d,%d)\n", D_x
, D_y
);
3418 RAW_PUTCHAR(c
->image
);
3423 if (D_encoding
== UTF8
)
3426 RAW_PUTCHAR(c
->mbcs
);
3429 debug2(" -> done (%d,%d)\n", D_x
, D_y
);
3433 ResizeDisplay(wi
, he
)
3437 debug2("ResizeDisplay: to (%d,%d).\n", wi
, he
);
3438 if (D_width
== wi
&& D_height
== he
)
3440 debug("ResizeDisplay: No change\n");
3443 if (D_width
!= wi
&& (D_height
== he
|| !D_CWS
) && D_CZ0
&& (wi
== Z0width
|| wi
== Z1width
))
3445 debug("ResizeDisplay: using Z0/Z1\n");
3446 AddCStr(wi
== Z0width
? D_CZ0
: D_CZ1
);
3447 ChangeScreenSize(wi
, D_height
, 0);
3448 return (he
== D_height
) ? 0 : -1;
3452 debug("ResizeDisplay: using WS\n");
3453 AddCStr(tgoto(D_CWS
, wi
, he
));
3454 ChangeScreenSize(wi
, he
, 0);
3461 ChangeScrollRegion(newtop
, newbot
)
3466 if (newtop
== newbot
)
3467 return; /* xterm etc can't do it */
3471 newbot
= D_height
- 1;
3475 D_bot
= D_height
- 1;
3478 if (D_top
== newtop
&& D_bot
== newbot
)
3480 debug2("ChangeScrollRegion: (%d - %d)\n", newtop
, newbot
);
3481 AddCStr(tgoto(D_CS
, newbot
, newtop
));
3484 D_y
= D_x
= -1; /* Just in case... */
3493 static char oscs
[] = "1;\000\00020;\00039;\00049;\000";
3500 if (!D_xtermosc
[i
] && !*s
)
3503 s
= "screen"; /* always set icon name */
3505 s
= ""; /* no background */
3507 s
= "black"; /* black text */
3509 s
= "white"; /* on white background */
3512 AddStr(oscs
+ i
* 4);
3521 for (i
= 3; i
>= 0; i
--)
3527 * Output buffering routines
3539 if (D_encoding
== UTF8
)
3541 while ((c
= *str
++))
3542 AddUtf8((unsigned char)c
);
3546 while ((c
= *str
++))
3559 if (D_encoding
== UTF8
)
3561 while ((c
= *str
++) && n
-- > 0)
3562 AddUtf8((unsigned char)c
);
3566 while ((c
= *str
++) && n
-- > 0)
3579 l
= D_obufp
- D_obuf
;
3580 debug1("Flush(): %d\n", l
);
3583 ASSERT(l
+ D_obuffree
== D_obuflen
);
3591 if (fcntl(D_userfd
, F_SETFL
, 0))
3592 debug1("Warning: BLOCK fcntl failed: %d\n", errno
);
3596 wr
= write(D_userfd
, p
, l
);
3601 debug1("Writing to display: %d\n", errno
);
3612 if (fcntl(D_userfd
, F_SETFL
, FNBLOCK
))
3613 debug1("Warning: NBLOCK fcntl failed: %d\n", errno
);
3624 debug1("did freetty %d\n", D_userfd
);
3632 D_obuflenmax
= -D_obufmax
;
3638 * Asynchronous output routines by
3639 * Tim MacKenzie (tym@dibbler.cs.monash.edu.au)
3648 if (D_status_obuffree
>= 0)
3650 ASSERT(D_obuffree
== -1);
3655 gettimeofday(&now
, NULL
);
3656 ti
= (now
.tv_sec
- D_status_time
.tv_sec
) * 1000 + (now
.tv_usec
- D_status_time
.tv_usec
) / 1000;
3657 if (ti
< MsgMinWait
)
3658 DisplaySleep1000(MsgMinWait
- ti
, 0);
3661 if (--D_obuffree
> 0) /* redo AddChar decrement */
3664 if (D_obuflen
&& D_obuf
)
3666 ind
= D_obufp
- D_obuf
;
3668 D_obuffree
+= GRAIN
;
3669 D_obuf
= realloc(D_obuf
, D_obuflen
);
3676 D_obuf
= malloc(D_obuflen
);
3679 Panic(0, "Out of memory");
3680 D_obufp
= D_obuf
+ ind
;
3681 D_obuflenmax
= D_obuflen
- D_obufmax
;
3682 debug1("ResizeObuf: resized to %d\n", D_obuflen
);
3686 DisplaySleep1000(n
, eat
)
3698 debug("DisplaySleep has no display sigh\n");
3702 t
.tv_usec
= (n
% 1000) * 1000;
3703 t
.tv_sec
= n
/ 1000;
3705 FD_SET(D_userfd
, &r
);
3706 if (select(FD_SETSIZE
, &r
, (fd_set
*)0, (fd_set
*)0, &t
) > 0)
3708 debug("display activity stopped sleep\n");
3710 read(D_userfd
, &buf
, 1);
3712 debug2("DisplaySleep(%d) ending, eat was %d\n", n
, eat
);
3718 {/* Nuke pending output in current display, clear screen */
3720 int oldtop
= D_top
, oldbot
= D_bot
;
3721 struct mchar oldrend
;
3722 int oldkeypad
= D_keypad
, oldcursorkeys
= D_cursorkeys
;
3723 int oldcurvis
= D_curvis
;
3724 int oldmouse
= D_mouse
;
3727 len
= D_obufp
- D_obuf
;
3728 debug1("NukePending: nuking %d chars\n", len
);
3730 /* Throw away any output that we can... */
3732 tcflush(D_userfd
, TCOFLUSH
);
3735 (void) ioctl(D_userfd
, TCFLSH
, (char *) 1);
3744 /* Turn off all attributes. (Tim MacKenzie) */
3751 AddStr("\033[m"); /* why is D_ME not set? */
3756 /* Check for toggle */
3757 if (D_IM
&& strcmp(D_IM
, D_EI
))
3760 /* Check for toggle */
3762 if (D_KS
&& strcmp(D_KS
, D_KE
))
3764 if (D_CCS
&& strcmp(D_CCS
, D_CCE
))
3767 if (D_KS
&& strcmp(D_KS
, D_KE
))
3770 if (D_CCS
&& strcmp(D_CCS
, D_CCE
))
3775 D_rend
= mchar_null
;
3781 ChangeScrollRegion(oldtop
, oldbot
);
3782 SetRendition(&oldrend
);
3783 KeypadMode(oldkeypad
);
3784 CursorkeysMode(oldcursorkeys
);
3785 CursorVisibility(oldcurvis
);
3786 MouseMode(oldmouse
);
3789 debug("ResizeDisplay: using WS\n");
3790 AddCStr(tgoto(D_CWS
, D_width
, D_height
));
3792 else if (D_CZ0
&& (D_width
== Z0width
|| D_width
== Z1width
))
3794 debug("ResizeDisplay: using Z0/Z1\n");
3795 AddCStr(D_width
== Z0width
? D_CZ0
: D_CZ1
);
3798 #endif /* AUTO_NUKE */
3801 /* linux' select can't handle flow control, so wait 100ms if
3805 disp_writeev_eagain(ev
, data
)
3809 display
= (struct display
*)data
;
3811 D_writeev
.type
= EV_WRITE
;
3812 D_writeev
.handler
= disp_writeev_fn
;
3818 disp_writeev_fn(ev
, data
)
3822 int len
, size
= OUTPUT_BLOCK_SIZE
;
3824 display
= (struct display
*)data
;
3825 len
= D_obufp
- D_obuf
;
3829 size
= write(D_userfd
, D_obuf
, size
);
3835 bcopy(D_obuf
+ size
, D_obuf
, len
);
3836 debug2("ASYNC: wrote %d - remaining %d\n", size
, len
);
3842 D_blocked_fuzz
-= size
;
3843 if (D_blocked_fuzz
< 0)
3846 if (D_blockedev
.queued
)
3848 if (D_obufp
- D_obuf
> D_obufmax
/ 2)
3850 debug2("%s: resetting timeout to %g secs\n", D_usertty
, D_nonblock
/1000.);
3851 SetTimeout(&D_blockedev
, D_nonblock
);
3855 debug1("%s: deleting blocked timeout\n", D_usertty
);
3856 evdeq(&D_blockedev
);
3859 if (D_blocked
== 1 && D_obuf
== D_obufp
)
3861 /* empty again, restart output */
3862 debug1("%s: buffer empty, unblocking\n", D_usertty
);
3864 Activate(D_fore
? D_fore
->w_norefresh
: 0);
3865 D_blocked_fuzz
= D_obufp
- D_obuf
;
3871 /* linux flow control is badly broken */
3872 if (errno
== EAGAIN
)
3875 D_writeev
.type
= EV_TIMEOUT
;
3876 D_writeev
.handler
= disp_writeev_eagain
;
3877 SetTimeout(&D_writeev
, 100);
3881 if (errno
!= EINTR
&& errno
!= EAGAIN
)
3882 #if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
3883 if (errno
!= EWOULDBLOCK
)
3885 Msg(errno
, "Error writing output to display");
3890 disp_readev_fn(ev
, data
)
3898 display
= (struct display
*)data
;
3900 /* Hmmmm... a bit ugly... */
3902 for (cv
= D_forecv
->c_layer
->l_cvlist
; cv
; cv
= cv
->c_lnext
)
3904 display
= cv
->c_display
;
3905 if (D_status
== STATUS_ON_WIN
)
3909 display
= (struct display
*)data
;
3916 size
= sizeof(D_fore
->w_pwin
->p_inbuf
) - D_fore
->w_pwin
->p_inlen
;
3919 size
= sizeof(D_fore
->w_inbuf
) - D_fore
->w_inlen
;
3925 size
= 1; /* Always allow one char for command keys */
3927 size
= read(D_userfd
, buf
, size
);
3930 if (errno
== EINTR
|| errno
== EAGAIN
)
3932 #if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
3933 if (errno
== EWOULDBLOCK
)
3936 debug1("Read error: %d - hangup!\n", errno
);
3943 debug("Found EOF - hangup!\n");
3954 Activate(D_fore
? D_fore
->w_norefresh
: 0);
3959 if (D_blocked
> 1) /* 2, 3 */
3965 for (p
= windows
; p
; p
= p
->w_next
)
3966 if (p
->w_zdisplay
== display
)
3968 flayer
= &p
->w_layer
;
3971 LayProcess(&bufp
, &size
);
3974 debug("zmodem window gone, deblocking display");
3975 zmodem_abort(0, display
);
3981 D_fore
->w_lastdisp
= display
;
3982 if (D_mouse
&& D_forecv
)
3984 unsigned char *bp
= (unsigned char *)buf
;
3987 /* XXX this assumes that the string is read in as a whole... */
3988 for (i
= size
; i
> 0; i
--, bp
++)
3990 if (i
> 5 && bp
[0] == 033 && bp
[1] == '[' && bp
[2] == 'M')
3995 else if (i
< 5 || bp
[0] != 0233 || bp
[1] != 'M')
3999 if (x
>= D_forecv
->c_xs
&& x
<= D_forecv
->c_xe
&& y
>= D_forecv
->c_ys
&& y
<= D_forecv
->c_ye
)
4001 x
-= D_forecv
->c_xoff
;
4002 y
-= D_forecv
->c_yoff
;
4003 if (x
>= 0 && x
< D_forecv
->c_layer
->l_width
&& y
>= 0 && y
< D_forecv
->c_layer
->l_height
)
4014 bcopy((char *)bp
+ 1, (char *)bp
, i
);
4019 bcopy((char *)bp
+ 5, (char *)bp
, i
- 5);
4026 if (D_encoding
!= (D_forecv
? D_forecv
->c_layer
->l_encoding
: 0))
4029 char buf2
[IOSIZE
* 2 + 10];
4030 enc
= D_forecv
? D_forecv
->c_layer
->l_encoding
: 0;
4031 for (i
= j
= 0; i
< size
; i
++)
4033 c
= ((unsigned char *)buf
)[i
];
4034 c
= DecodeChar(c
, D_encoding
, &D_decodestate
);
4036 i
--; /* try char again */
4042 j
+= EncodeChar(buf2
+ j
, c
, enc
, &font
);
4043 j
+= EncodeChar(buf2
+ j
, -1, enc
, &font
);
4046 j
+= EncodeChar(buf2
+ j
, c
, enc
, 0);
4047 if (j
> (int)sizeof(buf2
) - 10) /* just in case... */
4050 (*D_processinput
)(buf2
, j
);
4054 (*D_processinput
)(buf
, size
);
4058 disp_status_fn(ev
, data
)
4062 display
= (struct display
*)data
;
4063 debug1("disp_status_fn for display %x\n", (int)display
);
4069 disp_hstatus_fn(ev
, data
)
4073 display
= (struct display
*)data
;
4074 if (D_status
== STATUS_ON_HS
)
4084 disp_blocked_fn(ev
, data
)
4090 display
= (struct display
*)data
;
4091 debug1("blocked timeout %s\n", D_usertty
);
4092 if (D_obufp
- D_obuf
> D_obufmax
+ D_blocked_fuzz
)
4094 debug("stopping output to display\n");
4096 /* re-enable all windows */
4097 for (p
= windows
; p
; p
= p
->w_next
)
4098 if (p
->w_readev
.condneg
== &D_obuflenmax
)
4100 debug1("freeing window #%d\n", p
->w_number
);
4101 p
->w_readev
.condpos
= p
->w_readev
.condneg
= 0;
4107 cv_winid_fn(ev
, data
)
4112 struct canvas
*cv
= (struct canvas
*)data
;
4114 display
= cv
->c_display
;
4115 if (D_status
== STATUS_ON_WIN
)
4123 if (cv
->c_ye
+ 1 < D_height
)
4124 RefreshLine(cv
->c_ye
+ 1, 0, D_width
- 1, 0);
4125 if (ox
!= -1 && oy
!= -1)
4131 disp_map_fn(ev
, data
)
4138 display
= (struct display
*)data
;
4139 debug("Flushing map sequence\n");
4142 p
= (char *)D_seqp
- l
;
4143 D_seqp
= D_kmaps
+ 3;
4145 if ((q
= D_seqh
) != 0)
4148 i
= q
[0] << 8 | q
[1];
4149 i
&= ~KMAP_NOTIMEOUT
;
4150 debug1("Mapping former hit #%d - ", i
);
4151 debug2("%d(%s) - ", q
[2], q
+ 3);
4153 ProcessInput2((char *)q
+ 3, q
[2]);
4166 disp_idle_fn(ev
, data
)
4170 struct display
*olddisplay
;
4171 display
= (struct display
*)data
;
4172 debug("idle timeout\n");
4173 if (idletimo
<= 0 || idleaction
.nr
== RC_ILLEGAL
)
4175 olddisplay
= display
;
4176 flayer
= D_forecv
->c_layer
;
4178 DoAction(&idleaction
, -1);
4179 if (idleaction
.nr
== RC_BLANKER
)
4181 for (display
= displays
; display
; display
= display
->d_next
)
4182 if (olddisplay
== display
)
4193 SetTimeout(&D_idleev
, idletimo
);
4194 if (!D_idleev
.queued
)
4205 disp_blanker_fn(ev
, data
)
4209 char buf
[IOSIZE
], *b
;
4212 display
= (struct display
*)data
;
4213 size
= read(D_blankerev
.fd
, buf
, IOSIZE
);
4216 evdeq(&D_blankerev
);
4217 close(D_blankerev
.fd
);
4218 D_blankerev
.fd
= -1;
4221 for (b
= buf
; size
; size
--)
4228 int oldtop
= D_top
, oldbot
= D_bot
;
4229 struct mchar oldrend
;
4231 if (D_blankerev
.fd
== -1)
4235 evdeq(&D_blankerev
);
4236 close(D_blankerev
.fd
);
4237 D_blankerev
.fd
= -1;
4238 Kill(D_blankerpid
, SIGHUP
);
4250 AddStr("\033[m\033[m"); /* why is D_ME not set? */
4257 D_rend
= mchar_null
;
4261 ChangeScrollRegion(oldtop
, oldbot
);
4262 SetRendition(&oldrend
);
4275 char libuf
[20], cobuf
[20];
4279 strcpy(termname
, "TERM=");
4280 strncpy(termname
+ 5, D_termname
, sizeof(termname
) - 6);
4281 termname
[sizeof(termname
) - 1] = 0;
4284 if ((D_blankerev
.fd
= OpenPTY(&m
)) == -1)
4286 Msg(0, "OpenPty failed");
4292 if ((slave
= open(m
, O_RDWR
|O_NOCTTY
)) == -1)
4294 Msg(errno
, "%s", m
);
4295 close(D_blankerev
.fd
);
4296 D_blankerev
.fd
= -1;
4301 switch (pid
= (int)fork())
4305 close(D_blankerev
.fd
);
4306 D_blankerev
.fd
= -1;
4311 if (dfp
&& dfp
!= stderr
)
4314 if (setgid(real_gid
) || setuid(real_uid
))
4315 Panic(errno
, "setuid/setgid");
4321 closeallfiles(slave
);
4322 if (open(m
, O_RDWR
))
4323 Panic(errno
, "Cannot open %s", m
);
4330 SetTTY(0, &D_OldMode
);
4335 glwz
.ws_col
= D_width
;
4336 glwz
.ws_row
= D_height
;
4337 (void)ioctl(0, TIOCSWINSZ
, (char *)&glwz
);
4339 sprintf(libuf
, "LINES=%d", D_height
);
4340 sprintf(cobuf
, "COLUMNS=%d", D_width
);
4345 signal(SIGPIPE
, SIG_DFL
);
4348 execvpe(*cmdv
, cmdv
, NewEnv
+ 3);
4349 Panic(errno
, *cmdv
);
4354 evenq(&D_blankerev
);
4361 struct layout
*layouts
;
4362 struct layout
*laytab
[MAXLAY
];
4363 struct layout
*layout_last
, layout_last_marker
;
4364 struct layout
*layout_attach
= &layout_last_marker
;
4370 struct canvas
*cnext
, *c
= cv
;
4371 for (; cv
; cv
= cnext
)
4375 FreeLayoutCv(cv
->c_slperp
);
4379 cnext
= cv
->c_slnext
;
4387 DupLayoutCv(cvf
, cvt
, save
)
4388 struct canvas
*cvf
, *cvt
;
4393 cvt
->c_slorient
= cvf
->c_slorient
;
4394 cvt
->c_slweight
= cvf
->c_slweight
;
4395 if (cvf
== D_forecv
)
4399 cvt
->c_display
= display
;
4402 cvt
->c_captev
.type
= EV_TIMEOUT
;
4403 cvt
->c_captev
.data
= (char *)cvt
;
4404 cvt
->c_captev
.handler
= cv_winid_fn
;
4405 cvt
->c_blank
.l_cvlist
= 0;
4406 cvt
->c_blank
.l_layfn
= &BlankLf
;
4407 cvt
->c_blank
.l_bottom
= &cvt
->c_blank
;
4409 cvt
->c_layer
= cvf
->c_layer
;
4413 struct win
*p
= cvf
->c_layer
? Layer2Window(cvf
->c_layer
) : 0;
4414 cvt
->c_layer
= p
? &p
->w_layer
: 0;
4418 cvt
->c_slperp
= (struct canvas
*)calloc(1, sizeof(struct canvas
));
4419 cvt
->c_slperp
->c_slback
= cvt
;
4420 DupLayoutCv(cvf
->c_slperp
, cvt
->c_slperp
, save
);
4424 cvt
->c_slnext
= (struct canvas
*)calloc(1, sizeof(struct canvas
));
4425 cvt
->c_slnext
->c_slprev
= cvt
;
4426 cvt
->c_slnext
->c_slback
= cvt
->c_slback
;
4428 cvf
= cvf
->c_slnext
;
4429 cvt
= cvt
->c_slnext
;
4438 for (; cv
; cv
= cv
->c_slnext
)
4442 PutWindowCv(cv
->c_slperp
);
4445 p
= cv
->c_layer
? (struct win
*)cv
->c_layer
->l_data
: 0;
4447 SetCanvasWindow(cv
, p
);
4452 CreateLayout(title
, startat
)
4459 if (startat
>= MAXLAY
|| startat
< 0)
4461 for (i
= startat
; ;)
4469 Msg(0, "No more layouts\n");
4473 lay
= (struct layout
*)calloc(1, sizeof(*lay
));
4474 lay
->lay_title
= SaveStr(title
);
4475 lay
->lay_autosave
= 1;
4476 lay
->lay_number
= i
;
4478 lay
->lay_next
= layouts
;
4484 SaveLayout(name
, cv
)
4490 for (lay
= layouts
; lay
; lay
= lay
->lay_next
)
4491 if (!strcmp(lay
->lay_title
, name
))
4494 FreeLayoutCv(&lay
->lay_canvas
);
4496 lay
= CreateLayout(name
, 0);
4500 DupLayoutCv(cv
, &lay
->lay_canvas
, 1);
4501 lay
->lay_forecv
= D_forecv
;
4511 if (!lay
|| !lay
->lay_autosave
)
4513 FreeLayoutCv(&lay
->lay_canvas
);
4515 DupLayoutCv(&D_canvas
, &lay
->lay_canvas
, 1);
4516 lay
->lay_forecv
= D_forecv
;
4527 for (i
= 0, s
= name
; *s
>= '0' && *s
<= '9'; s
++)
4528 i
= i
* 10 + (*s
- '0');
4529 if (!*s
&& s
!= name
&& i
>= 0 && i
< MAXLAY
)
4531 for (lay
= layouts
; lay
; lay
= lay
->lay_next
)
4532 if (!strcmp(lay
->lay_title
, name
))
4542 AutosaveLayout(D_layout
);
4545 while (D_canvas
.c_slperp
)
4546 FreeCanvas(D_canvas
.c_slperp
);
4547 MakeDefaultCanvas();
4548 SetCanvasWindow(D_forecv
, 0);
4552 while (D_canvas
.c_slperp
)
4553 FreeCanvas(D_canvas
.c_slperp
);
4555 D_forecv
= lay
->lay_forecv
;
4556 DupLayoutCv(&lay
->lay_canvas
, &D_canvas
, 0);
4557 D_canvas
.c_ye
= D_height
- 1 - ((D_canvas
.c_slperp
&& D_canvas
.c_slperp
->c_slnext
) || captionalways
) - (D_has_hstatus
== HSTATUS_LASTLINE
);
4558 ResizeCanvas(&D_canvas
);
4559 RecreateCanvasChain();
4560 RethinkDisplayViewports();
4561 PutWindowCv(&D_canvas
);
4562 ResizeLayersToCanvases();
4567 NewLayout(title
, startat
)
4574 lay
= CreateLayout(title
, startat
);
4577 LoadLayout(0, &D_canvas
);
4579 DupLayoutCv(&D_canvas
, &lay
->lay_canvas
, 1);
4580 lay
->lay_forecv
= D_forecv
;
4583 lay
->lay_autosave
= 1;
4587 AddLayoutsInfo(buf
, len
, where
)
4593 struct layout
*p
, **pp
;
4597 for (pp
= laytab
; pp
< laytab
+ MAXLAY
; pp
++)
4599 if (pp
- laytab
== where
&& ss
== buf
)
4607 if (s
- buf
+ l
> len
- 24)
4614 sprintf(s
, "%d", p
->lay_number
);
4615 if (p
->lay_number
== where
)
4618 if (display
&& p
== D_layout
)
4639 Msg(0, "No layouts defined\n");
4642 if (where
== -1 && D_layout
)
4643 where
= D_layout
->lay_number
;
4644 ss
= AddLayoutsInfo(buf
, sizeof(buf
), where
);
4645 s
= buf
+ strlen(buf
);
4646 if (ss
- buf
> D_width
/ 2)
4649 if (s
- ss
< D_width
)