File: slw_console.cpp

package info (click to toggle)
freej 0.10git20100110-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 32,080 kB
  • ctags: 22,705
  • sloc: cpp: 156,254; ansic: 25,531; sh: 13,538; perl: 4,624; makefile: 3,278; python: 2,889; objc: 1,284; asm: 1,125; ruby: 126
file content (286 lines) | stat: -rw-r--r-- 7,275 bytes parent folder | download
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*  S-Lang console functions in C++
 *
 *  (C) Copyright 2004-2006 Denis Rojo <jaromil@dyne.org>
 *
 * This source code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Public License as published 
 * by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 *
 * This source code 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.
 * Please refer to the GNU Public License for more details.
 *
 * You should have received a copy of the GNU Public License along with
 * this source code; if not, write to:
 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <stdlib.h>
#include <string.h>
#include <signal.h>

#include <slang.h>

#include <slw_console.h>


///////////////////////////////////
// dirty signals

static bool screen_size_changed;
static void sigwinch_handler (int sig) {
  screen_size_changed = true;
  SLsignal (SIGWINCH, sigwinch_handler);
}

static bool keyboard_quit;
static void sigint_handler (int sig) {
  SLsignal_intr (SIGINT, sigint_handler);
  keyboard_quit = true;
#if SLANG_VERSION < 20000
  if (SLang_Ignore_User_Abort == 0) 
	  SLang_Error = USER_BREAK;
#endif
}

/* non blocking getkey */
static int getkey_handler() {
  unsigned int ch = 0;
  if(SLang_input_pending(1))
  //    return SLang_getkey();
    ch = SLang_getkey();
  if(ch) func("getkey_handler detected char %u",ch);
  return ch;
}

// end of dirty signals
/////////////////////////////////////


SLangConsole::SLangConsole() {

  w = h = 0;
  focused = NULL;

}

SLangConsole::~SLangConsole() { close(); }

bool SLangConsole::init() {

  setenv("TERM","xterm-color",0); 
  SLtt_get_terminfo();

  if( -1 == SLkp_init() ) { // keyboard interface
	fprintf(stderr, "failure in SLkp_init()");
  	return false;
  }

  SLang_init_tty(-1, 0, 0);

  SLsmg_init_smg(); // screen manager

  //  SLutf8_enable(1); // enable UTF8 character set
//  this is a mess ...
  screen_size_changed = false;

  // register signals
  SLsignal (SIGWINCH, sigwinch_handler);
  SLang_set_abort_signal(sigint_handler);

  SLkp_set_getkey_function(getkey_handler);
  

  // SLsmg_Tab_Width = 8;
  // SLsmg_Display_Eight_Bit = 1;
  // SLtt_Use_Ansi_Colors = 0;
  // SLtt_Term_Cannot_Scroll = 0;

  // set sizes of the whole console
  w = SLtt_Screen_Cols;
  h = SLtt_Screen_Rows;

  /* setup colors with the palette scheme:
     n = normal;
     n+10 = highlight;
     n+20 = reverse normal;
     n+30 = reverse highlight; */

  // crazy casting for crazy slang
  SLtt_set_color(1,NULL,(char *)"lightgray",(char *)"black");
  SLtt_set_color(11,NULL,(char *)"white",(char *)"black");
  SLtt_set_color(21,NULL,(char *)"black",(char *)"lightgray");
  SLtt_set_color(31,NULL,(char *)"black",(char *)"white");
  
  SLtt_set_color(2,NULL,(char *)"red",(char *)"black");
  SLtt_set_color(12,NULL,(char *)"brightred",(char *)"black");
  SLtt_set_color(22,NULL,(char *)"black",(char *)"red");
  SLtt_set_color(32,NULL,(char *)"black",(char *)"brightred");
  
  SLtt_set_color(3,NULL,(char *)"green",(char *)"black");
  SLtt_set_color(13,NULL,(char *)"brightgreen",(char *)"black");
  SLtt_set_color(23,NULL,(char *)"black",(char *)"green");
  SLtt_set_color(33,NULL,(char *)"black",(char *)"brightgreen");
  
  SLtt_set_color(4,NULL,(char *)"brown",(char *)"black");
  SLtt_set_color(14,NULL,(char *)"yellow",(char *)"black");
  SLtt_set_color(24,NULL,(char *)"black",(char *)"brown");
  SLtt_set_color(34,NULL,(char *)"black",(char *)"yellow");
  
  SLtt_set_color(5,NULL,(char *)"blue",(char *)"black");
  SLtt_set_color(15,NULL,(char *)"brightblue",(char *)"black");
  SLtt_set_color(25,NULL,(char *)"black",(char *)"blue");
  SLtt_set_color(35,NULL,(char *)"black",(char *)"brightblue");
  
  SLtt_set_color(6,NULL,(char *)"magenta",(char *)"black");
  SLtt_set_color(16,NULL,(char *)"brightmagenta",(char *)"black");
  SLtt_set_color(26,NULL,(char *)"black",(char *)"magenta");
  SLtt_set_color(36,NULL,(char *)"black",(char *)"brightmagenta");
  
  SLtt_set_color(7,NULL,(char *)"cyan",(char *)"black");
  SLtt_set_color(17,NULL,(char *)"brightcyan",(char *)"black");
  SLtt_set_color(27,NULL,(char *)"black",(char *)"cyan");
  SLtt_set_color(37,NULL,(char *)"black",(char *)"brightcyan");
  

  
  refresh();

  return true;
}

void SLangConsole::close() {
  func("%s",__PRETTY_FUNCTION__);
  SLsmg_reset_smg();
  SLang_reset_tty();
}

int SLangConsole::getkey() {
  // low level
  return SLkp_getkey();
}

void SLangConsole::feed(int key) {

///	func("feeding '%c' to widget %s", key, focused->name);

	if(focused)
		focused->feed(key);
}


bool SLangConsole::place(SLangWidget *wid, int hx, int hy, int lx, int ly) {
 
	wid->orig_x = hx;
	wid->orig_y = hy;

	// boundaries where lower right coord
	// get converted into WxH
	if( lx > this->w )
		wid->w = this->w - hx;
	else
		wid->w = lx - hx;

	if( ly > this->h )
		wid->h = this->h - hy;
	else
		wid->h = ly - hy;


	// save a reference of the console in the widget
	wid->console = this;


	// append the new widget in our linklist
	widgets.append( wid );

	func("s-lang widget %s sized %ux%u placed at %u,%u",
			wid->name, wid->w, wid->h,
			wid->orig_x, wid->orig_y);

	// draw border if requested
	if(wid->border)
		/* S-Lang has a coordinate system which is very
		 * different from the typical approach in computer graphics;
		 * the draw_box function works as follows:
		   SLsmg_draw_box (int r, int c, int dr, int dc);
		   Draw a box whose right corner is at row r and column c.
		   The box spans dr rows and dc columns.
		   The current position will be left at row r and column c.
		   */
		SLsmg_draw_box(wid->orig_y, wid->orig_x,
			       wid->h,    wid->w);

	// wid->refresh();	

	// focus the first widget
	if(!focused) {
	  func("setting focus on widget %s",wid->name);
	  focused = wid;
	}

	return true;
}


bool SLangConsole::refresh() {

  /* S-Lang says: 
   * All well behaved applications should block signals that
   * may affect the display while performing screen update. */
  SLsig_block_signals ();
  
  if(screen_size_changed && !keyboard_quit) {

    SLtt_get_screen_size ();
    SLsmg_reinit_smg ();

    this->w = SLtt_Screen_Cols;
    this->h = SLtt_Screen_Rows;

    screen_size_changed = false;
  
    // refresh all widgets
    SLangWidget *wid;
    wid = (SLangWidget*) widgets.begin();
    while(wid) {

      wid->refresh();
      wid = (SLangWidget*) wid->next;

    }
  }

//   if(focused && !keyboard_quit)
// 	  if(focused->cursor) {
// 	    SLtt_set_cursor_visibility(1);
// 	    focused->gotoxy( focused->cur_x, focused->cur_y);
//           } else
// 	    SLtt_set_cursor_visibility(0);

 
  SLsmg_refresh();
  
  SLsig_unblock_signals();
 
  if(keyboard_quit) {
	  func("keyboard requested forced quit");
	  return false;
  }

  return true;
}

////////////////////////
/// messaging interface (TODO)
// void SLangConsole::notice (char *str) { }
// void SLangConsole::act    (char *str) { }
// void SLangConsole::warning(char *str) { }
// void SLangConsole::error  (char *str) { }
// void SLangConsole::func   (char *str) { }
////////////////////////