summaryrefslogtreecommitdiff
path: root/payloads/libpayload/curses/PDCurses-3.4/pdcurses/bkgd.c
blob: d511efe8b3abf61ca2f180fb7675bdacb39b3f04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* Public Domain Curses */

#include <curspriv.h>

RCSID("$Id: bkgd.c,v 1.39 2008/07/13 16:08:18 wmcbrine Exp $")

/*man-start**************************************************************

  Name:                                                         bkgd

  Synopsis:
        int bkgd(chtype ch);
        void bkgdset(chtype ch);
        chtype getbkgd(WINDOW *win);
        int wbkgd(WINDOW *win, chtype ch);
        void wbkgdset(WINDOW *win, chtype ch);

        int bkgrnd(const cchar_t *wch);
        void bkgrndset(const cchar_t *wch);
        int getbkgrnd(cchar_t *wch);
        int wbkgrnd(WINDOW *win, const cchar_t *wch);
        void wbkgrndset(WINDOW *win, const cchar_t *wch);
        int wgetbkgrnd(WINDOW *win, cchar_t *wch);

  Description:
        bkgdset() and wbkgdset() manipulate the background of a window.
        The background is a chtype consisting of any combination of
        attributes and a character; it is combined with each chtype
        added or inserted to the window by waddch() or winsch(). Only
        the attribute part is used to set the background of non-blank
        characters, while both character and attributes are used for
        blank positions.

        bkgd() and wbkgd() not only change the background, but apply it
        immediately to every cell in the window.

        The attributes that are defined with the attrset()/attron() set
        of functions take precedence over the background attributes if
        there is a conflict (e.g., different color pairs).

  Return Value:
        bkgd() and wbkgd() return OK, unless the window is NULL, in
        which case they return ERR.

  Portability                                X/Open    BSD    SYS V
        bkgd                                    Y       -      4.0
        bkgdset                                 Y       -      4.0
        getbkgd                                 Y
        wbkgd                                   Y       -      4.0
        wbkgdset                                Y       -      4.0
        bkgrnd                                  Y
        bkgrndset                               Y
        getbkgrnd                               Y
        wbkgrnd                                 Y
        wbkgrndset                              Y
        wgetbkgrnd                              Y

**man-end****************************************************************/

int wbkgd(WINDOW *win, chtype ch)
{
    int x, y;
    chtype oldcolr, oldch, newcolr, newch, colr, attr;
    chtype oldattr = 0, newattr = 0;
    chtype *winptr;

    PDC_LOG(("wbkgd() - called\n"));

    if (!win)
        return ERR;

    if (win->_bkgd == ch)
        return OK;

    oldcolr = win->_bkgd & A_COLOR;
    if (oldcolr)
        oldattr = (win->_bkgd & A_ATTRIBUTES) ^ oldcolr;

    oldch = win->_bkgd & A_CHARTEXT;

    wbkgdset(win, ch);

    newcolr = win->_bkgd & A_COLOR;
    if (newcolr)
        newattr = (win->_bkgd & A_ATTRIBUTES) ^ newcolr;

    newch = win->_bkgd & A_CHARTEXT;

    /* what follows is what seems to occur in the System V
       implementation of this routine */

    for (y = 0; y < win->_maxy; y++)
    {
        for (x = 0; x < win->_maxx; x++)
        {
            winptr = win->_y[y] + x;

            ch = *winptr;

            /* determine the colors and attributes of the character read
               from the window */

            colr = ch & A_COLOR;
            attr = ch & (A_ATTRIBUTES ^ A_COLOR);

            /* if the color is the same as the old background color,
               then make it the new background color, otherwise leave it */

            if (colr == oldcolr)
                colr = newcolr;

            /* remove any attributes (non color) from the character that
               were part of the old background, then combine the
               remaining ones with the new background */

            attr ^= oldattr;
            attr |= newattr;

            /* change character if it is there because it was the old
               background character */

            ch &= A_CHARTEXT;
            if (ch == oldch)
                ch = newch;

            ch |= (attr | colr);

            *winptr = ch;

        }
    }

    touchwin(win);
    PDC_sync(win);
    return OK;
}

int bkgd(chtype ch)
{
    PDC_LOG(("bkgd() - called\n"));

    return wbkgd(stdscr, ch);
}

void wbkgdset(WINDOW *win, chtype ch)
{
    PDC_LOG(("wbkgdset() - called\n"));

    if (win)
    {
        if (!(ch & A_CHARTEXT))
            ch |= ' ';

        win->_bkgd = ch;
    }
}

void bkgdset(chtype ch)
{
    PDC_LOG(("bkgdset() - called\n"));

    wbkgdset(stdscr, ch);
}

chtype getbkgd(WINDOW *win)
{
    PDC_LOG(("getbkgd() - called\n"));

    return win ? win->_bkgd : (chtype)ERR;
}

#ifdef PDC_WIDE
int wbkgrnd(WINDOW *win, const cchar_t *wch)
{
    PDC_LOG(("wbkgrnd() - called\n"));

    return wch ? wbkgd(win, *wch) : ERR;
}

int bkgrnd(const cchar_t *wch)
{
    PDC_LOG(("bkgrnd() - called\n"));

    return wbkgrnd(stdscr, wch);
}

void wbkgrndset(WINDOW *win, const cchar_t *wch)
{
    PDC_LOG(("wbkgdset() - called\n"));

    if (wch)
        wbkgdset(win, *wch);
}

void bkgrndset(const cchar_t *wch)
{
    PDC_LOG(("bkgrndset() - called\n"));

    wbkgrndset(stdscr, wch);
}

int wgetbkgrnd(WINDOW *win, cchar_t *wch)
{
    PDC_LOG(("wgetbkgrnd() - called\n"));

    if (!win || !wch)
        return ERR;

    *wch = win->_bkgd;

    return OK;
}

int getbkgrnd(cchar_t *wch)
{
    PDC_LOG(("getbkgrnd() - called\n"));

    return wgetbkgrnd(stdscr, wch);
}
#endif