Add info about MakeWinMsgEv to HACKING.
[screen-lua.git] / src / resize.c
blob0bf5f3d5185c9aae52e75450c11ad3bdd9fe3df8
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 struct win *p;
501 int i;
502 struct mline *ml;
504 wi = ((wi + 1) + 255) & ~255;
505 if (wi <= maxwidth)
506 return;
507 maxwidth = wi;
508 debug1("New maxwidth: %d\n", maxwidth);
509 blank = (unsigned char *)xrealloc((char *)blank, maxwidth);
510 null = (unsigned char *)xrealloc((char *)null, maxwidth);
511 mline_old.image = (unsigned char *)xrealloc((char *)mline_old.image, maxwidth);
512 mline_old.attr = (unsigned char *)xrealloc((char *)mline_old.attr, maxwidth);
513 #ifdef FONT
514 mline_old.font = (unsigned char *)xrealloc((char *)mline_old.font, maxwidth);
515 #endif
516 #ifdef COLOR
517 mline_old.color = (unsigned char *)xrealloc((char *)mline_old.color, maxwidth);
518 # ifdef COLORS256
519 mline_old.colorx = (unsigned char *)xrealloc((char *)mline_old.color, maxwidth);
520 # endif
521 #endif
522 if (!(blank && null && mline_old.image && mline_old.attr IFFONT(&& mline_old.font) IFCOLOR(&& mline_old.color) IFCOLORX(&& mline_old.colorx)))
523 Panic(0, strnomem);
525 MakeBlankLine(blank, maxwidth);
526 bzero((char *)null, maxwidth);
528 mline_blank.image = blank;
529 mline_blank.attr = null;
530 mline_null.image = null;
531 mline_null.attr = null;
532 #ifdef FONT
533 mline_blank.font = null;
534 mline_null.font = null;
535 #endif
536 #ifdef COLOR
537 mline_blank.color = null;
538 mline_null.color = null;
539 # ifdef COLORS256
540 mline_blank.colorx = null;
541 mline_null.colorx = null;
542 # endif
543 #endif
545 /* We have to run through all windows to substitute
546 * the null references.
548 for (p = windows; p; p = p->w_next)
550 ml = p->w_mlines;
551 for (i = 0; i < p->w_height; i++, ml++)
553 if (ml->attr == oldnull)
554 ml->attr = null;
555 #ifdef FONT
556 if (ml->font == oldnull)
557 ml->font = null;
558 #endif
559 #ifdef COLOR
560 if (ml->color == oldnull)
561 ml->color= null;
562 #ifdef COLORS256
563 if (ml->colorx == oldnull)
564 ml->colorx = null;
565 #endif
566 #endif
568 #ifdef COPY_PASTE
569 ml = p->w_hlines;
570 for (i = 0; i < p->w_histheight; i++, ml++)
572 if (ml->attr == oldnull)
573 ml->attr = null;
574 # ifdef FONT
575 if (ml->font == oldnull)
576 ml->font = null;
577 # endif
578 # ifdef COLOR
579 if (ml->color == oldnull)
580 ml->color= null;
581 # ifdef COLORS256
582 if (ml->colorx == oldnull)
583 ml->colorx = null;
584 # endif
585 # endif
587 #endif
592 char *
593 xrealloc(mem, len)
594 char *mem;
595 int len;
597 register char *nmem;
599 if (mem == 0)
600 return malloc(len);
601 if ((nmem = realloc(mem, len)))
602 return nmem;
603 free(mem);
604 return (char *)0;
607 static void
608 MakeBlankLine(p, n)
609 register unsigned char *p;
610 register int n;
612 while (n--)
613 *p++ = ' ';
619 #ifdef COPY_PASTE
621 #define OLDWIN(y) ((y < p->w_histheight) \
622 ? &p->w_hlines[(p->w_histidx + y) % p->w_histheight] \
623 : &p->w_mlines[y - p->w_histheight])
625 #define NEWWIN(y) ((y < hi) ? &nhlines[y] : &nmlines[y - hi])
627 #else
629 #define OLDWIN(y) (&p->w_mlines[y])
630 #define NEWWIN(y) (&nmlines[y])
632 #endif
636 ChangeWindowSize(p, wi, he, hi)
637 struct win *p;
638 int wi, he, hi;
640 struct mline *mlf = 0, *mlt = 0, *ml, *nmlines, *nhlines;
641 int fy, ty, l, lx, lf, lt, yy, oty, addone;
642 int ncx, ncy, naka, t;
643 int y, shift;
645 if (wi == 0)
646 he = hi = 0;
648 if (p->w_type == W_TYPE_GROUP)
649 return 0;
650 if (p->w_width == wi && p->w_height == he && p->w_histheight == hi)
652 debug("ChangeWindowSize: No change.\n");
653 return 0;
656 CheckMaxSize(wi);
658 /* XXX */
659 #if 0
660 /* just in case ... */
661 if (wi && (p->w_width != wi || p->w_height != he) && p->w_lay != &p->w_winlay)
663 debug("ChangeWindowSize: No resize because of overlay?\n");
664 return -1;
666 #endif
668 debug("ChangeWindowSize");
669 debug3(" from (%d,%d)+%d", p->w_width, p->w_height, p->w_histheight);
670 debug3(" to(%d,%d)+%d\n", wi, he, hi);
672 fy = p->w_histheight + p->w_height - 1;
673 ty = hi + he - 1;
675 nmlines = nhlines = 0;
676 ncx = 0;
677 ncy = 0;
678 naka = 0;
680 if (wi)
682 if (wi != p->w_width || he != p->w_height)
684 if ((nmlines = (struct mline *)calloc(he, sizeof(struct mline))) == 0)
686 KillWindow(p);
687 Msg(0, strnomem);
688 return -1;
691 else
693 debug1("image stays the same: %d lines\n", he);
694 nmlines = p->w_mlines;
695 fy -= he;
696 ty -= he;
697 ncx = p->w_x;
698 ncy = p->w_y;
699 naka = p->w_autoaka;
702 #ifdef COPY_PASTE
703 if (hi)
705 if ((nhlines = (struct mline *)calloc(hi, sizeof(struct mline))) == 0)
707 Msg(0, "No memory for history buffer - turned off");
708 hi = 0;
709 ty = he - 1;
712 #endif
714 /* special case: cursor is at magic margin position */
715 addone = 0;
716 if (p->w_width && p->w_x == p->w_width)
718 debug2("Special addone case: %d %d\n", p->w_x, p->w_y);
719 addone = 1;
720 p->w_x--;
723 /* handle the cursor and autoaka lines now if the widths are equal */
724 if (p->w_width == wi)
726 ncx = p->w_x + addone;
727 ncy = p->w_y + he - p->w_height;
728 /* never lose sight of the line with the cursor on it */
729 shift = -ncy;
730 for (yy = p->w_y + p->w_histheight - 1; yy >= 0 && ncy + shift < he; yy--)
732 ml = OLDWIN(yy);
733 if (ml->image[p->w_width] == ' ')
734 break;
735 shift++;
737 if (shift < 0)
738 shift = 0;
739 else
740 debug1("resize: cursor out of bounds, shifting %d\n", shift);
741 ncy += shift;
742 if (p->w_autoaka > 0)
744 naka = p->w_autoaka + he - p->w_height + shift;
745 if (naka < 1 || naka > he)
746 naka = 0;
748 while (shift-- > 0)
750 ml = OLDWIN(fy);
751 FreeMline(ml);
752 fy--;
755 debug2("fy %d ty %d\n", fy, ty);
756 if (fy >= 0)
757 mlf = OLDWIN(fy);
758 if (ty >= 0)
759 mlt = NEWWIN(ty);
761 while (fy >= 0 && ty >= 0)
763 if (p->w_width == wi)
765 /* here is a simple shortcut: just copy over */
766 *mlt = *mlf;
767 *mlf = mline_zero;
768 if (--fy >= 0)
769 mlf = OLDWIN(fy);
770 if (--ty >= 0)
771 mlt = NEWWIN(ty);
772 continue;
775 /* calculate lenght */
776 for (l = p->w_width - 1; l > 0; l--)
777 if (mlf->image[l] != ' ' || mlf->attr[l])
778 break;
779 if (fy == p->w_y + p->w_histheight && l < p->w_x)
780 l = p->w_x; /* cursor is non blank */
781 l++;
782 lf = l;
784 /* add wrapped lines to length */
785 for (yy = fy - 1; yy >= 0; yy--)
787 ml = OLDWIN(yy);
788 if (ml->image[p->w_width] == ' ')
789 break;
790 l += p->w_width;
793 /* rewrap lines */
794 lt = (l - 1) % wi + 1; /* lf is set above */
795 oty = ty;
796 while (l > 0 && fy >= 0 && ty >= 0)
798 lx = lt > lf ? lf : lt;
799 if (mlt->image == 0)
801 if (AllocMline(mlt, wi + 1))
802 goto nomem;
803 MakeBlankLine(mlt->image + lt, wi - lt);
804 mlt->image[wi] = ((oty == ty) ? ' ' : 0);
806 if (BcopyMline(mlf, lf - lx, mlt, lt - lx, lx, wi + 1))
807 goto nomem;
809 /* did we copy the cursor ? */
810 if (fy == p->w_y + p->w_histheight && lf - lx <= p->w_x && lf > p->w_x)
812 ncx = p->w_x + lt - lf + addone;
813 ncy = ty - hi;
814 shift = wi ? -ncy + (l - lx) / wi : 0;
815 if (ty + shift > hi + he - 1)
816 shift = hi + he - 1 - ty;
817 if (shift > 0)
819 debug3("resize: cursor out of bounds, shifting %d [%d/%d]\n", shift, lt - lx, wi);
820 for (y = hi + he - 1; y >= ty; y--)
822 mlt = NEWWIN(y);
823 FreeMline(mlt);
824 if (y - shift < ty)
825 continue;
826 ml = NEWWIN(y - shift);
827 *mlt = *ml;
828 *ml = mline_zero;
830 ncy += shift;
831 ty += shift;
832 mlt = NEWWIN(ty);
833 if (naka > 0)
834 naka = naka + shift > he ? 0 : naka + shift;
836 ASSERT(ncy >= 0);
838 /* did we copy autoaka line ? */
839 if (p->w_autoaka > 0 && fy == p->w_autoaka - 1 + p->w_histheight && lf - lx <= 0)
840 naka = ty - hi >= 0 ? 1 + ty - hi : 0;
842 lf -= lx;
843 lt -= lx;
844 l -= lx;
845 if (lf == 0)
847 FreeMline(mlf);
848 lf = p->w_width;
849 if (--fy >= 0)
850 mlf = OLDWIN(fy);
852 if (lt == 0)
854 lt = wi;
855 if (--ty >= 0)
856 mlt = NEWWIN(ty);
859 ASSERT(l != 0 || fy == yy);
861 while (fy >= 0)
863 FreeMline(mlf);
864 if (--fy >= 0)
865 mlf = OLDWIN(fy);
867 while (ty >= 0)
869 if (AllocMline(mlt, wi + 1))
870 goto nomem;
871 MakeBlankLine(mlt->image, wi + 1);
872 if (--ty >= 0)
873 mlt = NEWWIN(ty);
876 #ifdef DEBUG
877 if (nmlines != p->w_mlines)
878 for (fy = 0; fy < p->w_height + p->w_histheight; fy++)
880 ml = OLDWIN(fy);
881 ASSERT(ml->image == 0);
883 #endif
885 if (p->w_mlines && p->w_mlines != nmlines)
886 free((char *)p->w_mlines);
887 p->w_mlines = nmlines;
888 #ifdef COPY_PASTE
889 if (p->w_hlines && p->w_hlines != nhlines)
890 free((char *)p->w_hlines);
891 p->w_hlines = nhlines;
892 #endif
893 nmlines = nhlines = 0;
895 /* change tabs */
896 if (p->w_width != wi)
898 if (wi)
900 t = p->w_tabs ? p->w_width : 0;
901 p->w_tabs = xrealloc(p->w_tabs, wi + 1);
902 if (p->w_tabs == 0)
904 nomem:
905 if (nmlines)
907 for (ty = he + hi - 1; ty >= 0; ty--)
909 mlt = NEWWIN(ty);
910 FreeMline(mlt);
912 if (nmlines && p->w_mlines != nmlines)
913 free((char *)nmlines);
914 #ifdef COPY_PASTE
915 if (nhlines && p->w_hlines != nhlines)
916 free((char *)nhlines);
917 #endif
919 KillWindow(p);
920 Msg(0, strnomem);
921 return -1;
923 for (; t < wi; t++)
924 p->w_tabs[t] = t && !(t & 7) ? 1 : 0;
925 p->w_tabs[wi] = 0;
927 else
929 if (p->w_tabs)
930 free(p->w_tabs);
931 p->w_tabs = 0;
935 /* Change w_Saved_y - this is only an estimate... */
936 p->w_Saved_y += ncy - p->w_y;
938 p->w_x = ncx;
939 p->w_y = ncy;
940 if (p->w_autoaka > 0)
941 p->w_autoaka = naka;
943 /* do sanity checks */
944 if (p->w_x > wi)
945 p->w_x = wi;
946 if (p->w_y >= he)
947 p->w_y = he - 1;
948 if (p->w_Saved_x > wi)
949 p->w_Saved_x = wi;
950 if (p->w_Saved_y < 0)
951 p->w_Saved_y = 0;
952 if (p->w_Saved_y >= he)
953 p->w_Saved_y = he - 1;
955 /* reset scrolling region */
956 p->w_top = 0;
957 p->w_bot = he - 1;
959 /* signal new size to window */
960 #ifdef TIOCSWINSZ
961 if (wi && (p->w_width != wi || p->w_height != he)
962 && p->w_width != 0 && p->w_height != 0 && p->w_ptyfd >= 0 && p->w_pid)
964 glwz.ws_col = wi;
965 glwz.ws_row = he;
966 debug("Setting pty winsize.\n");
967 if (ioctl(p->w_ptyfd, TIOCSWINSZ, (char *)&glwz))
968 debug2("SetPtySize: errno %d (fd:%d)\n", errno, p->w_ptyfd);
970 #endif /* TIOCSWINSZ */
972 /* store new size */
973 p->w_width = wi;
974 p->w_height = he;
975 #ifdef COPY_PASTE
976 p->w_histidx = 0;
977 p->w_histheight = hi;
978 #endif
980 #ifdef BUILTIN_TELNET
981 if (p->w_type == W_TYPE_TELNET)
982 TelWindowSize(p);
983 #endif
985 #ifdef DEBUG
986 /* Test if everything was ok */
987 for (fy = 0; fy < p->w_height + p->w_histheight; fy++)
989 ml = OLDWIN(fy);
990 ASSERT(ml->image);
991 # ifdef UTF8
992 if (p->w_encoding == UTF8)
994 for (l = 0; l < p->w_width; l++)
995 ASSERT(ml->image[l] >= ' ' || ml->font[l]);
997 else
998 #endif
999 for (l = 0; l < p->w_width; l++)
1000 ASSERT(ml->image[l] >= ' ');
1002 #endif
1003 return 0;
1006 void
1007 FreeAltScreen(p)
1008 struct win *p;
1010 int i;
1012 if (p->w_alt_mlines)
1014 for (i = 0; i < p->w_alt_height; i++)
1015 FreeMline(p->w_alt_mlines + i);
1016 free(p->w_alt_mlines);
1018 p->w_alt_mlines = 0;
1019 p->w_alt_width = 0;
1020 p->w_alt_height = 0;
1021 p->w_alt_x = 0;
1022 p->w_alt_y = 0;
1023 #ifdef COPY_PASTE
1024 if (p->w_alt_hlines)
1026 for (i = 0; i < p->w_alt_histheight; i++)
1027 FreeMline(p->w_alt_hlines + i);
1028 free(p->w_alt_hlines);
1030 p->w_alt_hlines = 0;
1031 p->w_alt_histidx = 0;
1032 #endif
1033 p->w_alt_histheight = 0;
1036 static void
1037 SwapAltScreen(p)
1038 struct win *p;
1040 struct mline *ml;
1041 int t;
1043 ml = p->w_alt_mlines; p->w_alt_mlines = p->w_mlines; p->w_mlines = ml;
1044 t = p->w_alt_width; p->w_alt_width = p->w_width; p->w_width = t;
1045 t = p->w_alt_height; p->w_alt_height = p->w_height; p->w_height = t;
1046 t = p->w_alt_histheight; p->w_alt_histheight = p->w_histheight; p->w_histheight = t;
1047 t = p->w_alt_x; p->w_alt_x = p->w_x; p->w_x = t;
1048 t = p->w_alt_y; p->w_alt_y = p->w_y; p->w_y = t;
1049 #ifdef COPY_PASTE
1050 ml = p->w_alt_hlines; p->w_alt_hlines = p->w_hlines; p->w_hlines = ml;
1051 t = p->w_alt_histidx; p->w_alt_histidx = p->w_histidx; p->w_histidx = t;
1052 #endif
1055 void
1056 EnterAltScreen(p)
1057 struct win *p;
1059 int ox = p->w_x, oy = p->w_y;
1060 FreeAltScreen(p);
1061 SwapAltScreen(p);
1062 ChangeWindowSize(p, p->w_alt_width, p->w_alt_height, p->w_alt_histheight);
1063 p->w_x = ox;
1064 p->w_y = oy;
1067 void
1068 LeaveAltScreen(p)
1069 struct win *p;
1071 if (!p->w_alt_mlines)
1072 return;
1073 SwapAltScreen(p);
1074 ChangeWindowSize(p, p->w_alt_width, p->w_alt_height, p->w_alt_histheight);
1075 FreeAltScreen(p);