Subversion Repositories HelenOS-historic

Rev

Rev 1487 | Rev 1525 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1487 Rev 1497
1
/*
1
/*
2
 * Copyright (C) 2006 Josef Cejka
2
 * Copyright (C) 2006 Josef Cejka
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
#include <screenbuffer.h>
29
#include <screenbuffer.h>
30
#include <malloc.h>
30
#include <malloc.h>
31
#include <unistd.h>
31
#include <unistd.h>
32
 
32
 
33
/** Get field from buffer that corresponds to character at position x,y at screen
33
/** Get field from buffer that corresponds to character at position x,y at screen
34
 *
34
 *
35
 */
35
 */
36
 
36
 
37
int screenbuffer_putchar(screenbuffer_t *scr, char c)
37
int screenbuffer_putchar(screenbuffer_t *scr, char c)
38
{
38
{
39
    keyfield_t *field;
39
    keyfield_t *field;
40
   
40
   
41
    field = get_field_at(scr, scr->position_x, scr->position_y);
41
    field = get_field_at(scr, scr->position_x, scr->position_y);
42
 
42
 
43
    field->character = c;
43
    field->character = c;
44
    field->style = scr->style;
44
    field->style = scr->style;
45
   
45
   
46
    scr->position_x++;
-
 
47
    if (scr->position_x == scr->size_x) {
-
 
48
        scr->position_x = 0;
-
 
49
        scr->position_y++;
-
 
50
        if (scr->position_y == scr->size_y) {
-
 
51
            /* scroll */
-
 
52
            scr->position_y--;
-
 
53
            screenbuffer_clear_line(scr, scr->top_line++);
-
 
54
        }
-
 
55
    }
-
 
56
   
-
 
57
    return 1;
46
    return 1;
58
}
47
}
59
 
48
 
60
screenbuffer_t *screenbuffer_init(screenbuffer_t *scr, int size_x, int size_y)
49
screenbuffer_t *screenbuffer_init(screenbuffer_t *scr, int size_x, int size_y)
61
{
50
{
62
    if ((scr->buffer = (keyfield_t *)malloc(sizeof(keyfield_t) * size_x * size_y)) == NULL) {
51
    if ((scr->buffer = (keyfield_t *)malloc(sizeof(keyfield_t) * size_x * size_y)) == NULL) {
63
        return NULL;
52
        return NULL;
64
    }
53
    }
65
   
54
   
66
    scr->size_x = size_x;
55
    scr->size_x = size_x;
67
    scr->size_y = size_y;
56
    scr->size_y = size_y;
68
    scr->position_y = 0;
-
 
69
    scr->position_x = 0;
-
 
70
    scr->style.fg_color = DEFAULT_FOREGROUND_COLOR;
57
    scr->style.fg_color = DEFAULT_FOREGROUND_COLOR;
71
    scr->style.bg_color = DEFAULT_BACKGROUND_COLOR;
58
    scr->style.bg_color = DEFAULT_BACKGROUND_COLOR;
-
 
59
   
72
    scr->top_line = 0;
60
    screenbuffer_clear(scr);
-
 
61
   
73
    return scr;
62
    return scr;
74
}
63
}
75
 
64
 
76
void screenbuffer_clear(screenbuffer_t *scr)
65
void screenbuffer_clear(screenbuffer_t *scr)
77
{
66
{
78
    unsigned int i;
67
    unsigned int i;
79
   
68
   
80
    for (i = 0; i < scr->size_x * scr->size_y; i++) {
69
    for (i = 0; i < (scr->size_x * scr->size_y); i++) {
81
        scr->buffer[i].character = ' ';
70
        scr->buffer[i].character = ' ';
82
        scr->buffer[i].style = scr->style;
71
        scr->buffer[i].style = scr->style;
83
    }
72
    }
84
 
73
 
85
    scr->top_line = 0;
74
    scr->top_line = 0;
86
    scr->position_y = 0;
75
    scr->position_y = 0;
87
    scr->position_x = 0;
76
    scr->position_x = 0;
88
}
77
}
89
 
78
 
90
/** Clear one buffer line
79
/** Clear one buffer line
91
 * @param scr
80
 * @param scr
92
 * @param line One buffer line (not a screen line!)
81
 * @param line One buffer line (not a screen line!)
93
 */
82
 */
94
void screenbuffer_clear_line(screenbuffer_t *scr, unsigned int line)
83
void screenbuffer_clear_line(screenbuffer_t *scr, unsigned int line)
95
{
84
{
96
    unsigned int i;
85
    unsigned int i;
97
   
86
   
98
    for (i = 0; i < scr->size_x; i++) {
87
    for (i = 0; i < scr->size_x; i++) {
99
        scr->buffer[i + line*scr->size_x].character = ' ';
88
        scr->buffer[i + line*scr->size_x].character = ' ';
100
        scr->buffer[i + line*scr->size_x].style = scr->style;
89
        scr->buffer[i + line*scr->size_x].style = scr->style;
101
    }
90
    }
102
}
91
}
103
 
92
 
104
void screenbuffer_copy_buffer(screenbuffer_t *scr, keyfield_t *dest)
93
void screenbuffer_copy_buffer(screenbuffer_t *scr, keyfield_t *dest)
105
{
94
{
106
    unsigned int i;
95
    unsigned int i;
107
   
96
   
108
    for (i = 0; i < scr->size_x * scr->size_y; i++) {
97
    for (i = 0; i < scr->size_x * scr->size_y; i++) {
109
        dest[i] = scr->buffer[i];
98
        dest[i] = scr->buffer[i];
110
    }
99
    }
111
}
100
}
112
 
101
 
113
void screenbuffer_goto(screenbuffer_t *scr, unsigned int x, unsigned int y)
102
void screenbuffer_goto(screenbuffer_t *scr, unsigned int x, unsigned int y)
114
{
103
{
115
    scr->position_x = x % scr->size_x;
104
    scr->position_x = x % scr->size_x;
116
    scr->position_y = (y + scr->top_line) % scr->size_y;
105
    scr->position_y = y  % scr->size_y;
117
}
106
}
118
 
107
 
119
 
108