aboutsummaryrefslogtreecommitdiffstats
path: root/Text.c
blob: 983af51e663ef9d8013dad8985e1bfc613b8c7ae (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
221
222
223
224
225
226
227
228
229
230
/* $Id: Text.c,v 1.4 2001/03/16 16:40:17 ltoetsch Exp $
 *
 * pure ncurses based text driver
 *
 * Copyright 2001 by Leopold T�tsch (lt@toetsch.at)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 * $Log: Text.c,v $
 * Revision 1.4  2001/03/16 16:40:17  ltoetsch
 * implemented time bar
 *
 * Revision 1.3  2001/03/16 09:28:08  ltoetsch
 * bugfixes
 *
 * Revision 1.2  2001/03/09 15:04:53  reinelt
 *
 * rename 'raster' to 'Text in Text.c
 * added TOTO item
 *
 * Revision 1.1  2001/03/09 13:08:11  ltoetsch
 * Added Text driver
 *
 *
 */

/* 
 *
 * exported fuctions:
 *
 * struct LCD Text[]
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <curses.h>

#define min(x,y) (x)<(y)?x:y

#ifdef STANDALONE

int main(int argc, char *argv[]) 
{
  WINDOW *w;
  int x,y;
  
  w = initscr();
  
  for (x=0; x < 255; x++)
    addch(acs_map[x]);    
  refresh();
  sleep(5);
  endwin();
  return 0;
  
}

#else

#include <string.h>
#include "debug.h"
#include "cfg.h"
#include "display.h"
extern int foreground;

static LCD Lcd;
static WINDOW *w;
WINDOW *err_win;
#define BARS ( BAR_R | BAR_H2 | BAR_T )
int err_rows;

int Text_clear (void)
{
  werase(w);
  box(w, 0, 0);
  return 0;
}

int Text_quit(void) {
  endwin();
  return 0;
}

int Text_init (LCD *Self)
{
  int cols=-1, rows=-1;
  int scr_cols, scr_rows;
  char *s;
  if (!foreground) {
    error("Text: you want me to display on /dev/null: sorry, I can't");
    return -1;
  }
  
  if (sscanf(s=cfg_get("size")?:"20x4", "%dx%d", &cols, &rows)!=2 || rows<1 || cols<1) {
    error ("Text: bad size '%s'", s);
    return -1;
  }
  Self->rows=rows;
  Self->cols=cols;
  Self->xres=1;
  Self->yres=1;
  Lcd=*Self;
  
  w = initscr();
  scr_cols=COLS;
  scr_rows=LINES;
  delwin(w);
  w = newwin(rows+2,cols+2,0,0);
  err_rows = scr_rows-rows-3;
  err_rows = min(99, err_rows);
  if (err_rows >= 4) {
    err_win = newwin(err_rows, scr_cols, rows+3, 0);
    err_rows -= 3;
    box(err_win, 0, 0);
    mvwprintw(err_win,0,3, "Stderr:");
    wmove(err_win, 1 , 0);
    wrefresh(err_win);
  }
  Text_clear();
  return w ? 0 : -1;
}

/* ncures scroll SIGSEGVs on my system, so this is a workaroud */

int curs_err(char *buffer) 
{
  static int lines;
  static char *lb[100];
  int start, i;
  char *p;
  
  if (err_win) {
    /* replace \r, \n with underscores */
    while ((p = strpbrk(buffer, "\r\n")) != NULL)
      *p='_';
    if (lines >= err_rows) {
      free(lb[0]);
      for (i=1; i<=err_rows; i++) 
	lb[i-1] = lb[i];
      start = 0;
    }
    else
      start = lines;
    lb[lines] = strdup(buffer);
    for (i=start; i<=lines; i++) {
      mvwprintw(err_win,i+1,1, "%s", lb[i]);
      wclrtoeol(err_win);
    }
    box(err_win, 0, 0);
    mvwprintw(err_win,0,3, "Stderr:");
    wrefresh(err_win);
    if (lines < err_rows)
      lines++;
    return 1;
  }
  return 0;
}

int Text_put (int row, int col, char *text)
{
  char *p;
  
  if ((p = strpbrk(text, "\r\n")) != NULL)
    *p='\0';
  if (col < Lcd.cols)
    mvwprintw(w, row+1 , col+1, "%.*s", Lcd.cols-col, text);
  return 0;
}

int Text_bar (int type, int row, int col, int max, int len1, int len2)
{
  int len, i;
  if (cfg_get("TextBar")) 
    mvwprintw(w, row+1 , col+1, "%d %d %d", max, len1, len2);
  else {
    len = min(len1, len2);
    len = min(len, Lcd.cols-col-1);
    if (len) {
      wmove(w, row+1 , col+1);
      for (i=0; i<len;i++)
        waddch(w,ACS_BLOCK);
    }
    col += len;
    len1 -= len;
    len2 -= len;
    len1 = min(len1, Lcd.cols-col-1);
    for (i=0; i<len1;i++)
      waddch(w,ACS_S1);
    len2 = min(len2, Lcd.cols-col-1);
    for (i=0; i<len2;i++)
      waddch(w,ACS_S9);
  }
  
  return 0;
}

int Text_gpo (int num, int val)
{
  return 0;
}

int Text_flush (void)
{
  box(w, 0, 0);
  wrefresh(w);  
  return 0;
}


LCD Text[] = {
  { "Text",4,20,1,1,BARS,0,
      Text_init,Text_clear,Text_put,Text_bar,Text_gpo,Text_flush,Text_quit },
  { NULL }
};

#endif