Handle the '2' code for OSC.
[screen-lua.git] / src / resize.c
blob9c7b70f1e2da0cdbc97ff1e4ec804acdffd8af1e
1 /* Copyright (c) 2008, 2009
2 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
3 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
4 * Micah Cowan (micah@cowan.name)
5 * Sadrul Habib Chowdhury (sadrul@users.sourceforge.net)
6 * Copyright (c) 1993-2002, 2003, 2005, 2006, 2007
7 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
8 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
9 * Copyright (c) 1987 Oliver Laumann
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3, or (at your option)
14 * any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program (see the file COPYING); if not, see
23 * http://www.gnu.org/licenses/, or contact Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 ****************************************************************
29 #include <sys/types.h>
30 #include <signal.h>
31 #ifndef sun
32 #include <sys/ioctl.h>
33 #endif
35 #ifdef ISC
36 # include <sys/tty.h>
37 # include <sys/sioctl.h>
38 # include <sys/pty.h>
39 #endif
41 #include "config.h"
42 #include "screen.h"
43 #include "extern.h"
45 static void CheckMaxSize __P((int));
46 static void FreeMline __P((struct mline *));
47 static int AllocMline __P((struct mline *ml, int));
48 static void MakeBlankLine __P((unsigned char *, int));
49 static void kaablamm __P((void));
50 static int BcopyMline __P((struct mline *, int, struct mline *, int, int, int));
51 static void SwapAltScreen __P((struct win *));
53 extern struct layer *flayer;
54 extern struct display *display, *displays;
55 extern unsigned char *blank, *null;
56 extern struct mline mline_blank, mline_null, mline_old;
57 extern struct win *windows;
58 extern int Z0width, Z1width;
59 extern int captionalways;
61 #if defined(TIOCGWINSZ) || defined(TIOCSWINSZ)
62 struct winsize glwz;
63 #endif
65 static struct mline mline_zero = {
66 (unsigned char *)0,
67 (unsigned char *)0
68 #ifdef FONT
69 ,(unsigned char *)0
70 #endif
71 #ifdef COLOR
72 ,(unsigned char *)0
73 # ifdef COLORS256
74 ,(unsigned char *)0
75 # endif
76 #endif
80 * ChangeFlag: 0: try to modify no window
81 * 1: modify fore (and try to modify no other) + redisplay
82 * 2: modify all windows
84 * Note: Activate() is only called if change_flag == 1
85 * i.e. on a WINCH event
88 void
89 CheckScreenSize(change_flag)
90 int change_flag;
92 int wi, he;
94 if (display == 0)
96 debug("CheckScreenSize: No display -> no check.\n");
97 return;
99 #ifdef TIOCGWINSZ
100 if (ioctl(D_userfd, TIOCGWINSZ, (char *)&glwz) != 0)
102 debug2("CheckScreenSize: ioctl(%d, TIOCGWINSZ) errno %d\n", D_userfd, errno);
103 wi = D_CO;
104 he = D_LI;
106 else
108 wi = glwz.ws_col;
109 he = glwz.ws_row;
110 if (wi == 0)
111 wi = D_CO;
112 if (he == 0)
113 he = D_LI;
115 #else
116 wi = D_CO;
117 he = D_LI;
118 #endif
120 debug2("CheckScreenSize: screen is (%d,%d)\n", wi, he);
122 #if 0 /* XXX: Fixme */
123 if (change_flag == 2)
125 debug("Trying to adapt all windows (-A)\n");
126 for (p = windows; p; p = p->w_next)
127 if (p->w_display == 0 || p->w_display == display)
128 ChangeWindowSize(p, wi, he, p->w_histheight);
130 #endif
131 if (D_width == wi && D_height == he)
133 debug("CheckScreenSize: No change -> return.\n");
134 return;
136 #ifdef BLANKER_PRG
137 KillBlanker();
138 #endif
139 ResetIdle();
140 ChangeScreenSize(wi, he, change_flag);
141 /* XXX Redisplay logic */
142 #if 0
143 if (change_flag == 1)
144 Redisplay(D_fore ? D_fore->w_norefresh : 0);
145 #endif
148 void
149 ChangeScreenSize(wi, he, change_fore)
150 int wi, he;
151 int change_fore;
153 struct win *p;
154 struct canvas *cv;
155 int wwi;
157 debug2("ChangeScreenSize from (%d,%d) ", D_width, D_height);
158 debug3("to (%d,%d) (change_fore: %d)\n",wi, he, change_fore);
160 cv = &D_canvas;
161 cv->c_xe = wi - 1;
162 cv->c_ye = he - 1 - ((cv->c_slperp && cv->c_slperp->c_slnext) || captionalways) - (D_has_hstatus == HSTATUS_LASTLINE);
163 cv->c_blank.l_height = cv->c_ye - cv->c_ys + 1;
164 if (cv->c_slperp)
166 ResizeCanvas(cv);
167 RecreateCanvasChain();
168 RethinkDisplayViewports();
170 if (D_forecv == 0)
171 D_forecv = D_cvlist;
172 if (D_forecv)
173 D_fore = Layer2Window(D_forecv->c_layer);
175 D_width = wi;
176 D_height = he;
178 CheckMaxSize(wi);
179 if (D_CWS)
181 D_defwidth = D_CO;
182 D_defheight = D_LI;
184 else
186 if (D_CZ0 && (wi == Z0width || wi == Z1width) &&
187 (D_CO == Z0width || D_CO == Z1width))
188 D_defwidth = D_CO;
189 else
190 D_defwidth = wi;
191 D_defheight = he;
193 debug2("Default size: (%d,%d)\n", D_defwidth, D_defheight);
194 if (change_fore)
195 ResizeLayersToCanvases();
196 if (change_fore == 2 && D_CWS == NULL && displays->d_next == 0)
198 /* adapt all windows - to be removed ? */
199 for (p = windows; p; p = p->w_next)
201 debug1("Trying to change window %d.\n", p->w_number);
202 wwi = wi;
203 #if 0
204 if (D_CZ0 && p->w_width != wi && (wi == Z0width || wi == Z1width))
206 if (p->w_width > (Z0width + Z1width) / 2)
207 wwi = Z0width;
208 else
209 wwi = Z1width;
211 #endif
212 if (p->w_savelayer && p->w_savelayer->l_cvlist == 0)
213 ResizeLayer(p->w_savelayer, wwi, he, 0);
214 #if 0
215 ChangeWindowSize(p, wwi, he, p->w_histheight);
216 #endif
221 void
222 ResizeLayersToCanvases()
224 struct canvas *cv;
225 struct layer *l;
226 int lx, ly;
228 debug("ResizeLayersToCanvases\n");
229 D_kaablamm = 0;
230 for (cv = D_cvlist; cv; cv = cv->c_next)
232 l = cv->c_layer;
233 if (l == 0)
234 continue;
235 debug("Doing canvas: ");
236 if (l->l_width == cv->c_xe - cv->c_xs + 1 &&
237 l->l_height == cv->c_ye - cv->c_ys + 1)
239 debug("already fitting.\n");
240 continue;
242 if (!MayResizeLayer(l))
244 debug("may not resize.\n");
246 else
248 debug("doing resize.\n");
249 ResizeLayer(l, cv->c_xe - cv->c_xs + 1, cv->c_ye - cv->c_ys + 1, display);
252 /* normalize window, see screen.c */
253 lx = cv->c_layer->l_x;
254 ly = cv->c_layer->l_y;
255 if (ly + cv->c_yoff < cv->c_ys)
257 cv->c_yoff = cv->c_ys - ly;
258 RethinkViewportOffsets(cv);
260 else if (ly + cv->c_yoff > cv->c_ye)
262 cv->c_yoff = cv->c_ye - ly;
263 RethinkViewportOffsets(cv);
265 if (lx + cv->c_xoff < cv->c_xs)
267 int n = cv->c_xs - (lx + cv->c_xoff);
268 if (n < (cv->c_xe - cv->c_xs + 1) / 2)
269 n = (cv->c_xe - cv->c_xs + 1) / 2;
270 if (cv->c_xoff + n > cv->c_xs)
271 n = cv->c_xs - cv->c_xoff;
272 cv->c_xoff += n;
273 RethinkViewportOffsets(cv);
275 else if (lx + cv->c_xoff > cv->c_xe)
277 int n = lx + cv->c_xoff - cv->c_xe;
278 if (n < (cv->c_xe - cv->c_xs + 1) / 2)
279 n = (cv->c_xe - cv->c_xs + 1) / 2;
280 if (cv->c_xoff - n + cv->c_layer->l_width - 1 < cv->c_xe)
281 n = cv->c_xoff + cv->c_layer->l_width - 1 - cv->c_xe;
282 cv->c_xoff -= n;
283 RethinkViewportOffsets(cv);
286 Redisplay(0);
287 if (D_kaablamm)
289 kaablamm();
290 D_kaablamm = 0;
295 MayResizeLayer(l)
296 struct layer *l;
298 int cvs = 0;
299 debug("MayResizeLayer:\n");
300 for (; l; l = l->l_next)
302 if (l->l_cvlist)
303 if (++cvs > 1 || l->l_cvlist->c_lnext)
305 debug1("may not - cvs %d\n", cvs);
306 return 0;
309 debug("may resize\n");
310 return 1;
314 * Easy implementation: rely on the fact that the only layers
315 * supporting resize are Win and Blank. So just kill all overlays.
317 * This is a lot harder if done the right way...
320 static void
321 kaablamm()
323 Msg(0, "Aborted because of window size change.");
326 void
327 ResizeLayer(l, wi, he, norefdisp)
328 struct layer *l;
329 int wi, he;
330 struct display *norefdisp;
332 struct win *p;
333 struct canvas *cv;
334 struct layer *oldflayer = flayer;
335 struct display *d, *olddisplay = display;
337 if (l->l_width == wi && l->l_height == he)
338 return;
339 p = Layer2Window(l);
341 if (oldflayer && (l == oldflayer || Layer2Window(oldflayer) == p))
342 while (oldflayer->l_next)
343 oldflayer = oldflayer->l_next;
345 if (p)
347 for (d = displays; d; d = d->d_next)
348 for (cv = d->d_cvlist; cv; cv = cv->c_next)
350 if (p == Layer2Window(cv->c_layer))
352 flayer = cv->c_layer;
353 if (flayer->l_next)
354 d->d_kaablamm = 1;
355 while (flayer->l_next)
356 ExitOverlayPage();
359 l = p->w_savelayer;
361 flayer = l;
362 if (p == 0 && flayer->l_next && flayer->l_next->l_next == 0 && LayResize(wi, he) == 0)
364 flayer = flayer->l_next;
365 LayResize(wi, he);
366 flayer = l;
368 else
370 if (flayer->l_next)
371 for (cv = flayer->l_cvlist; cv; cv = cv->c_lnext)
372 cv->c_display->d_kaablamm = 1;
373 while (flayer->l_next)
374 ExitOverlayPage();
376 if (p)
377 flayer = &p->w_layer;
378 LayResize(wi, he);
379 /* now everybody is on flayer, redisplay */
380 l = flayer;
381 for (display = displays; display; display = display->d_next)
383 if (display == norefdisp)
384 continue;
385 for (cv = D_cvlist; cv; cv = cv->c_next)
386 if (cv->c_layer == l)
388 CV_CALL(cv, LayRedisplayLine(-1, -1, -1, 0));
389 RefreshArea(cv->c_xs, cv->c_ys, cv->c_xe, cv->c_ye, 0);
391 if (D_kaablamm)
393 kaablamm();
394 D_kaablamm = 0;
397 flayer = oldflayer;
398 display = olddisplay;
402 static void
403 FreeMline(ml)
404 struct mline *ml;
406 if (ml->image)
407 free(ml->image);
408 if (ml->attr && ml->attr != null)
409 free(ml->attr);
410 #ifdef FONT
411 if (ml->font && ml->font != null)
412 free(ml->font);
413 #endif
414 #ifdef COLOR
415 if (ml->color && ml->color != null)
416 free(ml->color);
417 # ifdef COLORS256
418 if (ml->colorx && ml->colorx != null)
419 free(ml->colorx);
420 # endif
421 #endif
422 *ml = mline_zero;
425 static int
426 AllocMline(ml, w)
427 struct mline *ml;
428 int w;
430 ml->image = malloc(w);
431 ml->attr = null;
432 #ifdef FONT
433 ml->font = null;
434 #endif
435 #ifdef COLOR
436 ml->color = null;
437 # ifdef COLORS256
438 ml->colorx = null;
439 # endif
440 #endif
441 if (ml->image == 0)
442 return -1;
443 return 0;
447 static int
448 BcopyMline(mlf, xf, mlt, xt, l, w)
449 struct mline *mlf, *mlt;
450 int xf, xt, l, w;
452 int r = 0;
454 bcopy((char *)mlf->image + xf, (char *)mlt->image + xt, l);
455 if (mlf->attr != null && mlt->attr == null)
457 if ((mlt->attr = (unsigned char *)calloc(w, 1)) == 0)
458 mlt->attr = null, r = -1;
460 if (mlt->attr != null)
461 bcopy((char *)mlf->attr + xf, (char *)mlt->attr + xt, l);
462 #ifdef FONT
463 if (mlf->font != null && mlt->font == null)
465 if ((mlt->font = (unsigned char *)calloc(w, 1)) == 0)
466 mlt->font = null, r = -1;
468 if (mlt->font != null)
469 bcopy((char *)mlf->font + xf, (char *)mlt->font + xt, l);
470 #endif
471 #ifdef COLOR
472 if (mlf->color != null && mlt->color == null)
474 if ((mlt->color = (unsigned char *)calloc(w, 1)) == 0)
475 mlt->color = null, r = -1;
477 if (mlt->color != null)
478 bcopy((char *)mlf->color + xf, (char *)mlt->color + xt, l);
479 # ifdef COLORS256
480 if (mlf->colorx != null && mlt->colorx == null)
482 if ((mlt->colorx = (unsigned char *)calloc(w, 1)) == 0)
483 mlt->colorx = null, r = -1;
485 if (mlt->colorx != null)
486 bcopy((char *)mlf->colorx + xf, (char *)mlt->colorx + xt, l);
487 # endif
488 #endif
489 return r;
493 static int maxwidth;
495 static void
496 CheckMaxSize(wi)
497 int wi;
499 unsigned char *oldnull = null;
500 unsigned char *oldblank = blank;
501 struct win *p;
502 int i;
503 struct mline *ml;
505 wi = ((wi + 1) + 255) & ~255;
506 if (wi <= maxwidth)
507 return;
508 maxwidth = wi;
509 debug1("New maxwidth: %d\n", maxwidth);
510 blank = (unsigned char *)xrealloc((char *)blank, maxwidth);
511 null = (unsigned char *)xrealloc((char *)null, maxwidth);
512 mline_old.image = (unsigned char *)xrealloc((char *)mline_old.image, maxwidth);
513 mline_old.attr = (unsigned char *)xrealloc((char *)mline_old.attr, maxwidth);
514 #ifdef FONT
515 mline_old.font = (unsigned char *)xrealloc((char *)mline_old.font, maxwidth);
516 #endif
517 #ifdef COLOR
518 mline_old.color = (unsigned char *)xrealloc((char *)mline_old.color, maxwidth);
519 # ifdef COLORS256
520 mline_old.colorx = (unsigned char *)xrealloc((char *)mline_old.colorx, maxwidth);
521 # endif
522 #endif
523 if (!(blank && null && mline_old.image && mline_old.attr IFFONT(&& mline_old.font) IFCOLOR(&& mline_old.color) IFCOLORX(&& mline_old.colorx)))
524 Panic(0, strnomem);
526 MakeBlankLine(blank, maxwidth);
527 bzero((char *)null, maxwidth);
529 mline_blank.image = blank;
530 mline_blank.attr = null;
531 mline_null.image = null;
532 mline_null.attr = null;
533 #ifdef FONT
534 mline_blank.font = null;
535 mline_null.font = null;
536 #endif
537 #ifdef COLOR
538 mline_blank.color = null;
539 mline_null.color = null;
540 # ifdef COLORS256
541 mline_blank.colorx = null;
542 mline_null.colorx = null;
543 # endif
544 #endif
546 #define RESET_AFC(x, bl) do { if (x == old##bl) x = bl; } while (0)
548 #define RESET_LINES(lines, count) \
549 do { \
550 ml = lines; \
551 for (i = 0; i < count; i++, ml++) \
553 RESET_AFC(ml->image, blank); \
554 RESET_AFC(ml->attr, null); \
555 IFFONT(RESET_AFC(ml->font, null)); \
556 IFCOLOR(RESET_AFC(ml->color, null)); \
557 IFCOLORX(RESET_AFC(ml->colorx, null)); \
559 } while (0)
561 /* We have to run through all windows to substitute
562 * the null and blank references.
564 for (p = windows; p; p = p->w_next)
566 RESET_LINES(p->w_mlines, p->w_height);
568 #ifdef COPY_PASTE
569 RESET_LINES(p->w_hlines, p->w_histheight);
570 RESET_LINES(p->w_alt_hlines, p->w_alt_histheight);
571 #endif
573 RESET_LINES(p->w_alt_mlines, p->w_alt_height);
578 char *
579 xrealloc(mem, len)
580 char *mem;
581 int len;
583 register char *nmem;
585 if (mem == 0)
586 return malloc(len);
587 if ((nmem = realloc(mem, len)))
588 return nmem;
589 free(mem);
590 return (char *)0;
593 static void
594 MakeBlankLine(p, n)
595 register unsigned char *p;
596 register int n;
598 while (n--)
599 *p++ = ' ';
605 #ifdef COPY_PASTE
607 #define OLDWIN(y) ((y < p->w_histheight) \
608 ? &p->w_hlines[(p->w_histidx + y) % p->w_histheight] \
609 : &p->w_mlines[y - p->w_histheight])
611 #define NEWWIN(y) ((y < hi) ? &nhlines[y] : &nmlines[y - hi])
613 #else
615 #define OLDWIN(y) (&p->w_mlines[y])
616 #define NEWWIN(y) (&nmlines[y])
618 #endif
622 ChangeWindowSize(p, wi, he, hi)
623 struct win *p;
624 int wi, he, hi;
626 struct mline *mlf = 0, *mlt = 0, *ml, *nmlines, *nhlines;
627 int fy, ty, l, lx, lf, lt, yy, oty, addone;
628 int ncx, ncy, naka, t;
629 int y, shift;
631 if (wi == 0)
632 he = hi = 0;
634 if (p->w_type == W_TYPE_GROUP)
635 return 0;
636 if (p->w_width == wi && p->w_height == he && p->w_histheight == hi)
638 debug("ChangeWindowSize: No change.\n");
639 return 0;
642 CheckMaxSize(wi);
644 /* XXX */
645 #if 0
646 /* just in case ... */
647 if (wi && (p->w_width != wi || p->w_height != he) && p->w_lay != &p->w_winlay)
649 debug("ChangeWindowSize: No resize because of overlay?\n");
650 return -1;
652 #endif
654 debug("ChangeWindowSize");
655 debug3(" from (%d,%d)+%d", p->w_width, p->w_height, p->w_histheight);
656 debug3(" to(%d,%d)+%d\n", wi, he, hi);
658 fy = p->w_histheight + p->w_height - 1;
659 ty = hi + he - 1;
661 nmlines = nhlines = 0;
662 ncx = 0;
663 ncy = 0;
664 naka = 0;
666 if (wi)
668 if (wi != p->w_width || he != p->w_height)
670 if ((nmlines = (struct mline *)calloc(he, sizeof(struct mline))) == 0)
672 KillWindow(p);
673 Msg(0, strnomem);
674 return -1;
677 else
679 debug1("image stays the same: %d lines\n", he);
680 nmlines = p->w_mlines;
681 fy -= he;
682 ty -= he;
683 ncx = p->w_x;
684 ncy = p->w_y;
685 naka = p->w_autoaka;
688 #ifdef COPY_PASTE
689 if (hi)
691 if ((nhlines = (struct mline *)calloc(hi, sizeof(struct mline))) == 0)
693 Msg(0, "No memory for history buffer - turned off");
694 hi = 0;
695 ty = he - 1;
698 #endif
700 /* special case: cursor is at magic margin position */
701 addone = 0;
702 if (p->w_width && p->w_x == p->w_width)
704 debug2("Special addone case: %d %d\n", p->w_x, p->w_y);
705 addone = 1;
706 p->w_x--;
709 /* handle the cursor and autoaka lines now if the widths are equal */
710 if (p->w_width == wi)
712 ncx = p->w_x + addone;
713 ncy = p->w_y + he - p->w_height;
714 /* never lose sight of the line with the cursor on it */
715 shift = -ncy;
716 for (yy = p->w_y + p->w_histheight - 1; yy >= 0 && ncy + shift < he; yy--)
718 ml = OLDWIN(yy);
719 if (ml->image[p->w_width] == ' ')
720 break;
721 shift++;
723 if (shift < 0)
724 shift = 0;
725 else
726 debug1("resize: cursor out of bounds, shifting %d\n", shift);
727 ncy += shift;
728 if (p->w_autoaka > 0)
730 naka = p->w_autoaka + he - p->w_height + shift;
731 if (naka < 1 || naka > he)
732 naka = 0;
734 while (shift-- > 0)
736 ml = OLDWIN(fy);
737 FreeMline(ml);
738 fy--;
741 debug2("fy %d ty %d\n", fy, ty);
742 if (fy >= 0)
743 mlf = OLDWIN(fy);
744 if (ty >= 0)
745 mlt = NEWWIN(ty);
747 while (fy >= 0 && ty >= 0)
749 if (p->w_width == wi)
751 /* here is a simple shortcut: just copy over */
752 *mlt = *mlf;
753 *mlf = mline_zero;
754 if (--fy >= 0)
755 mlf = OLDWIN(fy);
756 if (--ty >= 0)
757 mlt = NEWWIN(ty);
758 continue;
761 /* calculate lenght */
762 for (l = p->w_width - 1; l > 0; l--)
763 if (mlf->image[l] != ' ' || mlf->attr[l])
764 break;
765 if (fy == p->w_y + p->w_histheight && l < p->w_x)
766 l = p->w_x; /* cursor is non blank */
767 l++;
768 lf = l;
770 /* add wrapped lines to length */
771 for (yy = fy - 1; yy >= 0; yy--)
773 ml = OLDWIN(yy);
774 if (ml->image[p->w_width] == ' ')
775 break;
776 l += p->w_width;
779 /* rewrap lines */
780 lt = (l - 1) % wi + 1; /* lf is set above */
781 oty = ty;
782 while (l > 0 && fy >= 0 && ty >= 0)
784 lx = lt > lf ? lf : lt;
785 if (mlt->image == 0)
787 if (AllocMline(mlt, wi + 1))
788 goto nomem;
789 MakeBlankLine(mlt->image + lt, wi - lt);
790 mlt->image[wi] = ((oty == ty) ? ' ' : 0);
792 if (BcopyMline(mlf, lf - lx, mlt, lt - lx, lx, wi + 1))
793 goto nomem;
795 /* did we copy the cursor ? */
796 if (fy == p->w_y + p->w_histheight && lf - lx <= p->w_x && lf > p->w_x)
798 ncx = p->w_x + lt - lf + addone;
799 ncy = ty - hi;
800 shift = wi ? -ncy + (l - lx) / wi : 0;
801 if (ty + shift > hi + he - 1)
802 shift = hi + he - 1 - ty;
803 if (shift > 0)
805 debug3("resize: cursor out of bounds, shifting %d [%d/%d]\n", shift, lt - lx, wi);
806 for (y = hi + he - 1; y >= ty; y--)
808 mlt = NEWWIN(y);
809 FreeMline(mlt);
810 if (y - shift < ty)
811 continue;
812 ml = NEWWIN(y - shift);
813 *mlt = *ml;
814 *ml = mline_zero;
816 ncy += shift;
817 ty += shift;
818 mlt = NEWWIN(ty);
819 if (naka > 0)
820 naka = naka + shift > he ? 0 : naka + shift;
822 ASSERT(ncy >= 0);
824 /* did we copy autoaka line ? */
825 if (p->w_autoaka > 0 && fy == p->w_autoaka - 1 + p->w_histheight && lf - lx <= 0)
826 naka = ty - hi >= 0 ? 1 + ty - hi : 0;
828 lf -= lx;
829 lt -= lx;
830 l -= lx;
831 if (lf == 0)
833 FreeMline(mlf);
834 lf = p->w_width;
835 if (--fy >= 0)
836 mlf = OLDWIN(fy);
838 if (lt == 0)
840 lt = wi;
841 if (--ty >= 0)
842 mlt = NEWWIN(ty);
845 ASSERT(l != 0 || fy == yy);
847 while (fy >= 0)
849 FreeMline(mlf);
850 if (--fy >= 0)
851 mlf = OLDWIN(fy);
853 while (ty >= 0)
855 if (AllocMline(mlt, wi + 1))
856 goto nomem;
857 MakeBlankLine(mlt->image, wi + 1);
858 if (--ty >= 0)
859 mlt = NEWWIN(ty);
862 #ifdef DEBUG
863 if (nmlines != p->w_mlines)
864 for (fy = 0; fy < p->w_height + p->w_histheight; fy++)
866 ml = OLDWIN(fy);
867 ASSERT(ml->image == 0);
869 #endif
871 if (p->w_mlines && p->w_mlines != nmlines)
872 free((char *)p->w_mlines);
873 p->w_mlines = nmlines;
874 #ifdef COPY_PASTE
875 if (p->w_hlines && p->w_hlines != nhlines)
876 free((char *)p->w_hlines);
877 p->w_hlines = nhlines;
878 #endif
879 nmlines = nhlines = 0;
881 /* change tabs */
882 if (p->w_width != wi)
884 if (wi)
886 t = p->w_tabs ? p->w_width : 0;
887 p->w_tabs = xrealloc(p->w_tabs, wi + 1);
888 if (p->w_tabs == 0)
890 nomem:
891 if (nmlines)
893 for (ty = he + hi - 1; ty >= 0; ty--)
895 mlt = NEWWIN(ty);
896 FreeMline(mlt);
898 if (nmlines && p->w_mlines != nmlines)
899 free((char *)nmlines);
900 #ifdef COPY_PASTE
901 if (nhlines && p->w_hlines != nhlines)
902 free((char *)nhlines);
903 #endif
905 KillWindow(p);
906 Msg(0, strnomem);
907 return -1;
909 for (; t < wi; t++)
910 p->w_tabs[t] = t && !(t & 7) ? 1 : 0;
911 p->w_tabs[wi] = 0;
913 else
915 if (p->w_tabs)
916 free(p->w_tabs);
917 p->w_tabs = 0;
921 /* Change w_Saved_y - this is only an estimate... */
922 p->w_Saved_y += ncy - p->w_y;
924 p->w_x = ncx;
925 p->w_y = ncy;
926 if (p->w_autoaka > 0)
927 p->w_autoaka = naka;
929 /* do sanity checks */
930 if (p->w_x > wi)
931 p->w_x = wi;
932 if (p->w_y >= he)
933 p->w_y = he - 1;
934 if (p->w_Saved_x > wi)
935 p->w_Saved_x = wi;
936 if (p->w_Saved_y < 0)
937 p->w_Saved_y = 0;
938 if (p->w_Saved_y >= he)
939 p->w_Saved_y = he - 1;
941 /* reset scrolling region */
942 p->w_top = 0;
943 p->w_bot = he - 1;
945 /* signal new size to window */
946 #ifdef TIOCSWINSZ
947 if (wi && (p->w_width != wi || p->w_height != he)
948 && p->w_width != 0 && p->w_height != 0 && p->w_ptyfd >= 0 && p->w_pid)
950 glwz.ws_col = wi;
951 glwz.ws_row = he;
952 debug("Setting pty winsize.\n");
953 if (ioctl(p->w_ptyfd, TIOCSWINSZ, (char *)&glwz))
954 debug2("SetPtySize: errno %d (fd:%d)\n", errno, p->w_ptyfd);
956 #endif /* TIOCSWINSZ */
958 /* store new size */
959 p->w_width = wi;
960 p->w_height = he;
961 #ifdef COPY_PASTE
962 p->w_histidx = 0;
963 p->w_histheight = hi;
964 #endif
966 #ifdef BUILTIN_TELNET
967 if (p->w_type == W_TYPE_TELNET)
968 TelWindowSize(p);
969 #endif
971 #ifdef DEBUG
972 /* Test if everything was ok */
973 for (fy = 0; fy < p->w_height + p->w_histheight; fy++)
975 ml = OLDWIN(fy);
976 ASSERT(ml->image);
977 # ifdef UTF8
978 if (p->w_encoding == UTF8)
980 for (l = 0; l < p->w_width; l++)
981 ASSERT(ml->image[l] >= ' ' || ml->font[l]);
983 else
984 #endif
985 for (l = 0; l < p->w_width; l++)
986 ASSERT(ml->image[l] >= ' ');
988 #endif
989 return 0;
992 void
993 FreeAltScreen(p)
994 struct win *p;
996 int i;
998 if (p->w_alt_mlines)
1000 for (i = 0; i < p->w_alt_height; i++)
1001 FreeMline(p->w_alt_mlines + i);
1002 free(p->w_alt_mlines);
1004 p->w_alt_mlines = 0;
1005 p->w_alt_width = 0;
1006 p->w_alt_height = 0;
1007 p->w_alt_x = 0;
1008 p->w_alt_y = 0;
1009 #ifdef COPY_PASTE
1010 if (p->w_alt_hlines)
1012 for (i = 0; i < p->w_alt_histheight; i++)
1013 FreeMline(p->w_alt_hlines + i);
1014 free(p->w_alt_hlines);
1016 p->w_alt_hlines = 0;
1017 p->w_alt_histidx = 0;
1018 #endif
1019 p->w_alt_histheight = 0;
1022 static void
1023 SwapAltScreen(p)
1024 struct win *p;
1026 struct mline *ml;
1027 int t;
1029 ml = p->w_alt_mlines; p->w_alt_mlines = p->w_mlines; p->w_mlines = ml;
1030 t = p->w_alt_width; p->w_alt_width = p->w_width; p->w_width = t;
1031 t = p->w_alt_height; p->w_alt_height = p->w_height; p->w_height = t;
1032 t = p->w_alt_histheight; p->w_alt_histheight = p->w_histheight; p->w_histheight = t;
1033 t = p->w_alt_x; p->w_alt_x = p->w_x; p->w_x = t;
1034 t = p->w_alt_y; p->w_alt_y = p->w_y; p->w_y = t;
1035 #ifdef COPY_PASTE
1036 ml = p->w_alt_hlines; p->w_alt_hlines = p->w_hlines; p->w_hlines = ml;
1037 t = p->w_alt_histidx; p->w_alt_histidx = p->w_histidx; p->w_histidx = t;
1038 #endif
1041 void
1042 EnterAltScreen(p)
1043 struct win *p;
1045 int ox = p->w_x, oy = p->w_y;
1046 FreeAltScreen(p);
1047 SwapAltScreen(p);
1048 ChangeWindowSize(p, p->w_alt_width, p->w_alt_height, p->w_alt_histheight);
1049 p->w_x = ox;
1050 p->w_y = oy;
1053 void
1054 LeaveAltScreen(p)
1055 struct win *p;
1057 if (!p->w_alt_mlines)
1058 return;
1059 SwapAltScreen(p);
1060 ChangeWindowSize(p, p->w_alt_width, p->w_alt_height, p->w_alt_histheight);
1061 FreeAltScreen(p);