File: screen.c

package info (click to toggle)
pdmenu 1.2.96
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 840 kB
  • ctags: 250
  • sloc: sh: 3,048; ansic: 2,037; makefile: 131; perl: 58
file content (185 lines) | stat: -rw-r--r-- 4,320 bytes parent folder | download | duplicates (2)
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
/*
 * Screen functions for use with slang. 
 * Draw dialogs, init and reset terminal, etc 
 */

/*
 * Copyright (c) 1995-2003 Joey Hess (joey@kitenet.net)
 * All rights reserved. See COPYING for full copyright information (GPL).
 */

#include "global.h"
#include "screen.h"
#include "menu.h"
#include "error.h"
#include <signal.h>
#include "slang.h"
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <libintl.h>
#define _(String) gettext (String)

/* equals 1 if the screen is set up in slang mode */
int screen_is_setup=0;

/* Draw the desktop; the background of the screen */
void DrawDesktop () {
  int x;

  SLsmg_set_color(DESKTOP);
  for (x=1;x<SLtt_Screen_Rows-1;x++) {
    SLsmg_gotorc(x,0);
    SLsmg_erase_eol();	
  }
}

/* Draw the title at the top of the screen (centered) */
void DrawTitle (char *title) {
  SLsmg_gotorc(0,0);
  SLsmg_set_color(TITLE);
  SLsmg_erase_eol();
  SLsmg_gotorc(0,(SLtt_Screen_Cols-strlen(title))/2);
  SLsmg_write_string(title);
}

/* Draw the message at the base of the screen */
void DrawBase (char *base) {
  SLsmg_gotorc(SLtt_Screen_Rows-1,0);
  SLsmg_set_color(BASE);
  SLsmg_write_nstring(base,SLtt_Screen_Cols);
  SLsmg_gotorc(SLtt_Screen_Rows-1,SLtt_Screen_Cols-1);
}

/* Convert the character at the given screen position to a shadow */
void _shadow_char (int x, int y) {
	SLsmg_Char_Type ch;
	
	SLsmg_set_color_in_region(SHADOW, y, x, 1, 1);
}

/*
 * Draw a shadow "under" a given rectangle.
 * Shadows are omitted in B&W mode.
 */
void DrawShadow (int x,int y,int dx,int dy) {
  int c;
  
  if (SLtt_Use_Ansi_Colors) {
    for (c=0;c<dy-1;c++) {
      _shadow_char(x+dx, c+1+y);
      _shadow_char(x+dx+1, c+1+y);
    }
    for (c=0;c<dx;c++) {
      _shadow_char(x+2+c, y+dy);
    }
  }
}

/*
 * Draw a dialog box on the screen. Doesn't save whatever's under it; sorry.
 * Pass 1 as shadow to enable a shadow
 * Set the color beforehand 
 */
void DrawDialog (char *title,int x,int y,int dx,int dy,int shadow) {
  int c;
  char *empty="";

  SLsmg_draw_box(y,x,dy,dx);
  for(c=y+1;c<y+dy-1;c++) {
    SLsmg_gotorc(c,x+1);
    SLsmg_write_nstring(empty,dx-2);
  }	
  SLsmg_gotorc(y,x+(dx-strlen(title))/2);
  SLsmg_write_string(title);

  if (shadow) 
    DrawShadow(x,y,dx,dy);
}

/* 
 * SIGWINCH=resize window.
 * This just sets a global variable because the SL* functions that
 * are used to redraw the screen arn't reentrant.
 */
RETSIGTYPE Sigwinch_Handler (int sig) {
  Want_Screen_Resize = 1;
  /* 
   * Ugly hack: push 0 into keyboard buffer, becuase pdmenu is often
   * waiting on keyboard input when the window is resized. Note that
   * this means that all the functions that do keyboard input need to check
   * for this and do a screen resize. There has to be a better way to do
   * this!
   */
  SLang_ungetkey(0);
  signal(SIGWINCH,Sigwinch_Handler);
}

/* init terminal
 * turn on color if the global Use_Color is = 1
 */
void Screen_Init () {
  static int SLkp_init_ran;
  
  if (screen_is_setup == 0) {
    SLsig_block_signals(); /* Block signals while initializing. */

    SLtt_get_terminfo();

    /* note this has to run immediatly after SLtt_get_terminfo to work */
    if (Lowbit)
      SLtt_Has_Alt_Charset = 0;

    if (SLkp_init_ran != 1) {
      if (SLkp_init() == -1) {
	SLsig_unblock_signals();
	Error(_("Unable to initialize key mappings."));
      }
      SLkp_init_ran=1;
    }

    SLang_init_tty(0, 0, 1);
    SLsmg_init_smg();
    SLtt_Use_Ansi_Colors=Use_Color;

    SLsignal(SIGWINCH,Sigwinch_Handler);
    
    SLsig_unblock_signals();

    screen_is_setup=1;
  }
}

/*
 * set object colors
 * This requres that the FG[] and BG[] arrays be filled with the strings
 * that denote the colors of the different objects.
 */
void Screen_Setcolors () {
  int c;
	
  for (c=0;c<NUMSCREENPARTS;c++) {
    SLtt_set_color(c+1,"",FG[c],BG[c]);
  }
}

/* reset terminal */
void Screen_Reset () {	
  if (screen_is_setup) {
    SLsmg_gotorc(SLtt_Screen_Rows - 1, 0);
    SLsmg_refresh();
    SLang_reset_tty ();
    SLsmg_reset_smg ();
    screen_is_setup=0;
  }
}	

/* figure out the current screen size */
void SetScreensize () {
  SLtt_get_screen_size();

  if ((SLtt_Screen_Rows < MIN_ROWS) || (SLtt_Screen_Cols < MIN_COLS))
    Error(_("Error: The screen is too small."));
}