blob: eed4dbcbdf8369414379dc3e46b92838a39cbdc6 [file] [log] [blame]
micky3879b9f5e72025-07-08 18:04:53 -04001/****************************************************************************
2 * Copyright 2018-2020,2023 Thomas E. Dickey *
3 * *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
11 * *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
14 * *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
22 * *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
26 * authorization. *
27 ****************************************************************************/
28/*
29 * $Id: dump_window.c,v 1.5 2023/11/11 00:30:50 tom Exp $
30 */
31#include <dump_window.h>
32
33static FILE *dumpfp;
34
35int
36open_dump(const char *fn)
37{
38 int result = 0;
39 close_dump();
40 if ((dumpfp = fopen(fn, "a")) != 0)
41 result = 1;
42 return result;
43}
44
45void
46close_dump(void)
47{
48 if (dumpfp != 0) {
49 fclose(dumpfp);
50 dumpfp = 0;
51 }
52}
53
54void
55dump_window(WINDOW *w)
56{
57 wgetch(w);
58 if (dumpfp != 0) {
59 int y, x;
60 int oldy, oldx;
61 int maxy, maxx;
62 int pass;
63 char *cvec = 0;
64 char *avec = 0;
65 char *pvec = 0;
66 int ccnt = 0;
67 int acnt = 0;
68 int pcnt = 0;
69 int endy = -1;
70 int endx = -1;
71
72 fprintf(dumpfp, "Window %p\n", (void *) w);
73
74 getyx(w, oldy, oldx);
75 getmaxyx(w, maxy, maxx);
76 fprintf(dumpfp, "size (%dx%d)\n", maxy, maxx);
77 getbegyx(w, y, x);
78 fprintf(dumpfp, "begin (%dx%d)\n", maxy, maxx);
79 getyx(w, y, x);
80 fprintf(dumpfp, "position (%d,%d)\n", y, x);
81
82 if (maxy > 0 && maxx > 0) {
83 for (pass = 0; pass < 2; ++pass) {
84 for (y = 0; y < maxy; ++y) {
85
86 if (cvec)
87 memset(cvec, 0, (size_t) maxx + 1);
88 if (avec)
89 memset(avec, 0, (size_t) maxx + 1);
90 if (pvec)
91 memset(pvec, 0, (size_t) maxx + 1);
92
93 for (x = 0; x < maxx; ++x) {
94 chtype data = mvwinch(w, y, x);
95 chtype temp;
96 char cc = (char) ((data & 0xff) ? (data & 0xff) : ' ');
97 char aa;
98 char pp;
99
100 temp = ((data & A_ATTRIBUTES) & (chtype) (~A_COLOR));
101 if (temp) {
102 if (temp & A_ALTCHARSET) {
103 aa = (temp & A_BOLD) ? 'A' : 'a';
104 } else if (temp & A_STANDOUT) {
105 aa = (temp & A_BOLD) ? 'S' : 's';
106 } else if (temp & A_REVERSE) {
107 aa = (temp & A_BOLD) ? 'R' : 'r';
108 } else if (temp & A_UNDERLINE) {
109 aa = (temp & A_BOLD) ? 'U' : 'u';
110 } else {
111 aa = (temp & A_BOLD) ? 'b' : '?';
112 }
113 } else {
114 aa = ' ';
115 }
116 if (data & A_COLOR) {
117 if (PAIR_NUMBER((int) data) < 8) {
118 pp = (char) ('0' + PAIR_NUMBER((int) data));
119 } else {
120 pp = '*';
121 }
122 } else {
123 pp = ' ';
124 }
125
126 if (pass) {
127 if (cvec)
128 cvec[x] = cc;
129 if (avec)
130 avec[x] = aa;
131 if (pvec)
132 pvec[x] = pp;
133 } else {
134 if (cc != ' ' || aa != ' ' || pp != ' ') {
135 if (endx < x)
136 endx = x;
137 if (endy < y)
138 endy = y;
139 }
140 ccnt += (cc != ' ');
141 acnt += (aa != ' ');
142 pcnt += (pp != ' ');
143 }
144 }
145 if (pass) {
146 fprintf(dumpfp, "%3d", y + 1);
147 if (cvec)
148 fprintf(dumpfp, "\tc|%.*s|\n", maxx, cvec);
149 if (avec)
150 fprintf(dumpfp, "\ta|%.*s|\n", maxx, avec);
151 if (pvec)
152 fprintf(dumpfp, "\tp|%.*s|\n", maxx, pvec);
153 }
154 }
155 if (pass) {
156 free(cvec);
157 free(avec);
158 free(pvec);
159 } else {
160 fprintf(dumpfp, "%d cells with characters\n", ccnt);
161 fprintf(dumpfp, "%d cells with video-attributes\n", acnt);
162 fprintf(dumpfp, "%d cells with color-attributes\n", pcnt);
163 if (endy < 0 || endx < 0)
164 break;
165 /* reduce the dump a little, ignore really blank cells */
166 maxx = endx + 1;
167 maxy = endy + 1;
168 if (ccnt)
169 cvec = malloc((size_t) maxx + 1);
170 if (acnt)
171 avec = malloc((size_t) maxx + 1);
172 if (pcnt)
173 pvec = malloc((size_t) maxx + 1);
174 }
175 }
176 }
177 wmove(w, oldy, oldx);
178 }
179}