File: display.c

package info (click to toggle)
defrag 0.73-1
  • links: PTS
  • area: main
  • in suites: hamm, potato, slink
  • size: 384 kB
  • ctags: 599
  • sloc: ansic: 4,463; makefile: 137; sh: 37
file content (416 lines) | stat: -rw-r--r-- 10,516 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
 * display.c - all screen handling functions for the picture option of the
 * file system defragmenter.
 *
 * Copyright (C) 1997 Stephen Tweedie (sct@dcs.ed.ac.uk)
 * Copyright (C) 1997 Ulrich E. Habel (espero@b31.hadiko.de)
 * Copyleft (C) 1994 Alexey Vovenko (vovenko@ixwin.ihep.su)
 */

#include <sys/types.h>
#include <strings.h>
#include <stdlib.h>
#include <ncurses.h>
#include <stdarg.h>
#include <unistd.h>
#include <signal.h>

#include "display.h"

#ifdef EXTFS       /* The use of EXT fs is deprecated */
   int voyer_mode = 0;
#else
   int voyer_mode = 1;
#endif

static WINDOW *map_w=NULL;
static WINDOW *legend=NULL,*stats=NULL, *st_line=NULL;

static int map_w_width;
static int map_w_height;
static ulong screen_cells,granularity;
static ushort *screen_map;
       
void _die(char *last_words) {
   fprintf(stderr,last_words);
   exit(1);
}

/* Job control handler is broken in ncurses, so we have to set our
 * own.
 */
void tstp_signal(int dummy) {
   endwin();
   signal(SIGTSTP,SIG_DFL);
   sigsetmask(0);
   /* Put us to stop */
   kill(getpid(),SIGTSTP);     
   
   /* Something has wakened us */
   signal(SIGTSTP,tstp_signal);
   wrefresh(curscr); 
}

void init_screen(ulong blocks) {
   initscr();

   if (has_colors ()) {
     start_color ();

     curs_set (0);
     init_pair (1, COLOR_BLUE, COLOR_CYAN);
     attrset (CELL_ATTR (1));
     mvaddstr ( 5, 30, "                    ");
     mvaddstr ( 6, 30, " starting in Colour ");
     mvaddstr ( 7, 30, "                    ");
     refresh ();

     init_pair (ATR_SB,     COL_SB_FG,     COL_SB_BG);
     init_pair (ATR_GRP,    COL_GRP_FG,    COL_GRP_BG);
     init_pair (ATR_BITMAP, COL_BITMAP_FG, COL_BITMAP_BG);
     init_pair (ATR_INODE,  COL_INODE_FG,  COL_INODE_BG);
     init_pair (ATR_KERNEL, COL_KERNEL_FG, COL_KERNEL_BG);
     init_pair (ATR_DIR,    COL_DIR_FG,    COL_DIR_BG);
     init_pair (ATR_FILE,   COL_FILE_FG,   COL_FILE_BG);
     init_pair (ATR_BAD,    COL_BAD_FG,    COL_BAD_BG);
     init_pair (ATR_DATA,   COL_DATA_FG,   COL_DATA_BG);
     init_pair (ATR_FREE,   COL_FREE_FG,   COL_FREE_BG);
     init_pair (ATR_RD,     COL_RD_FG,     COL_RD_BG);
     init_pair (ATR_WR,     COL_WR_FG,     COL_WR_BG);
     init_pair (ATR_FRAG,   COL_FRAG_FG,   COL_FRAG_BG);

     napms (1000);
     erase ();
  }
   else { 
     curs_set (0);
     attrset (A_NORMAL);
     mvaddstr ( 6, 29, "starting in Monochrome");
      
     refresh ();
     napms (1000);
     erase ();
   }

   cbreak();
   noecho();

   keypad(stdscr,TRUE);
   if (LINES < 10 || COLS < 20) 
      _die("Unable to determine screen size, set LINES and COLUMNS in environment\n");
      
   if (COLS < 80)
      _die("Need at least 80-columns display to work\n");
   if (LINES < 15)
      _die("Need at least 15-lines display to work\n");
    
   map_w_width  = COLS;
   map_w_height = LINES - 7;
   map_w        = newwin (map_w_height,map_w_width,0,0);
   legend       = newwin (6,              0, LINES - 7, (COLS / 2) - 2);
   stats        = newwin (6, (COLS / 2) - 2, LINES - 7,              0);
   st_line      = newwin (0,              0, LINES - 1,              0);
   if (!map_w || !legend || !stats || !st_line) 
          _die("allocating screen map");
   map_w_width  -= 2;         /* border */
   map_w_height -= 2;         /* border */

   screen_cells = map_w_width*map_w_height;
   granularity = blocks / screen_cells;
   if (blocks % screen_cells) 
      granularity++;
   if (granularity == 0)
      granularity = 1;
   
   screen_cells = blocks / granularity;

   screen_map = malloc(screen_cells*sizeof(*screen_map));
   
   if (screen_map == NULL) {
      done_screen(FALSE);
      _die("Out of memory\n");
   }
   memset(screen_map, 0, screen_cells);
   signal(SIGINT,SIG_IGN);      /* ignore keyboard interrupt for safety */
   signal(SIGTSTP,tstp_signal);

   init_pair (1, COLOR_YELLOW, COLOR_BLUE);
   attrset (CELL_ATTR (1) | WA_BLINK);
   mvaddstr ( 5, 30, "                   ");
   mvaddstr ( 6, 30, "  Please  wait...  ");
   mvaddstr ( 7, 30, "                   ");
   refresh ();
}

void done_screen(int wait_key) {
   if (!map_w)
        return;

   if (wait_key) {     
       stat_line("Press any key to quit"); 
      
       /* Deletion of window if getch() is used  ->  getchar() */
       getchar();
   }

   stat_line("");
   
   delwin(map_w);
   delwin(legend);        
   delwin(stats);        
   delwin(st_line);
   map_w = stats = st_line = NULL;

   curs_set (1);
   endwin ();
}

/* -------------------------- Legend window ------------------------------ */
static struct { 
  ushort attr;
  int    abbr;
  char  *name;
} legend_names[] = {
   { AT_SUPER,  'S', " Superblock" },
   { AT_GROUP,  'G', " Group dscr. " },
   { AT_BITMAP, 'M', " Bitmap " },
   { AT_INODE,  'I', " Inode block " },
   { AT_KERNEL, 'K', " Kernel " },
   { AT_DIR,    'D', " Directory" },
   { AT_REG,    'F', " File   " },
   { AT_BAD,    'B', " Bad block" },
   { AT_DATA, EMPTY, " Data block" },
   { 0,         '.', " Free block" },
   { AT_FRAG,   'F', " Fragment" },
   { 0,          0 , NULL          }
};

void display_legend(ushort attr) {
   int  y = 1,
        x = 2,
        i = 0;

   while (legend_names[i++].abbr != EMPTY)
     ;
   legend_names[--i].abbr = ACS_DIAMOND;

   if (!voyer_mode) 
      return;

   box (legend, ACS_VLINE, ACS_HLINE);
   mvwaddstr (legend, 0, ((COLS + (2 * 2) + 1) / 4) - 4, " Legend ");
   
   for (i = 0; legend_names[i].name != NULL; i++) {
      if ((attr & legend_names[i].attr) || (legend_names[i].attr == 0)) {
	  wattrset (legend, CELL_ATTR ((i + 2)));
          mvwaddch (legend, y++, x, legend_names[i].abbr);
	  wattrset (legend, A_NORMAL);
	  waddstr  (legend,         legend_names[i].name);
	  
          if (y > 4) {
	      y = 1;
		
	      if (x < 10) 
		  x += 15;
	      else if (x < 22) {
		  x += 13;
	      } else      
		  break;
	  }
      }
   }     

   wrefresh (legend);
   wattrset (legend, A_NORMAL);
}

/* --------------------- Statistic window ------------------------------ */
#define MAX_COMMENTS 4
static int comments_count=0;
void add_comment(char *comment) {
   if (stats==NULL)
       puts(comment);
   else    
       if (comments_count < MAX_COMMENTS) 
          mvwaddstr(stats,++comments_count,2,comment);
}
       
void display_comments (char *title) {
   int i;
   if (stats == NULL) return;
   box (stats, ACS_VLINE, ACS_HLINE);
   i = strlen (title) / 2;
   if (i > 0)
      mvwaddstr(stats, 0, ((COLS - (2 * 2) + 1) / 4) - i, title);
   if (comments_count < MAX_COMMENTS)
      mvwprintw(stats,++comments_count,2,"%6d block%s in each screen cell",
                granularity,granularity==1 ? "" : "s");
   wrefresh(stats);
}

void clear_comments (void) {
   if (stats == NULL) return;
   comments_count = 0;
   wclear(stats);
}

/* -------------------------- Status line -------------------------------- */
void stat_line(const char *fmt, ...) {
   char s[256];
   va_list args;
   va_start(args,fmt);
   vsprintf(s,fmt,args);
   va_end(args);
   if (st_line != NULL) {
        wmove(st_line,0,0);
        waddstr(st_line,s);
        wclrtoeol(st_line);
        wrefresh(st_line);
   }
   else
        puts(s);
}

/* ------------------------- Disk map window ----------------------------- */
void set_attr(ulong block, ushort attr) {
   if (!voyer_mode) 
      return;
   block /= granularity;
   if (block > screen_cells) {
      fprintf(stderr,"set_map: block %lu out of range\n",block);
      return;
   }
   screen_map[block] |= attr; 
}

void clear_attr(ushort attr) {
   int i;
   if (!voyer_mode)
       return;
   if (attr == AT_READ) {
                     /* Hack: If we have read the block than its place 
                      * becomes empty. This is not always correct,
                      * because each cell represents more than 1 disk block.
                      */
      for (i = 0; i < screen_cells; i++) 
          if (screen_map[i] & AT_READ) 
              screen_map[i] &= ~(AT_READ | AT_DATA);
      return;       
   }
   if (attr == AT_WRITE) {
      for (i = 0; i < screen_cells; i++) 
          if (screen_map[i] & AT_WRITE) {
              screen_map[i] &= ~(AT_WRITE);
              screen_map[i] |= AT_DATA; 
          }    
      return;       
   }
   for (i = 0; i < screen_cells; i++) 
       screen_map[i] &= ~attr;
   update_display();    
}

void show_cell(int i) {
  int mask = 0;
  int attr;
  int glyph;
  
  if (screen_map[i] & AT_SELECTED && 
     !(screen_map[i] & (AT_SUPER | AT_BITMAP | AT_INODE))) 
         mask |= A_REVERSE;

  /* A screen cell typically contains data for more than one block.
   * If the blocks are of different types we have to choose the
   * most important one to show it on the screen
   */
  if (screen_map[i] & AT_READ) {
      attr = CELL_ATTR (ATR_RD);
      glyph = 'R';
  }

  else if (screen_map[i] & AT_WRITE) {
      attr = CELL_ATTR (ATR_WR);
      glyph = 'W';
  }

  /* Normal status */
  else if (screen_map[i] & AT_GROUP) {
      attr = CELL_ATTR (ATR_GRP);
      glyph = 'G';
  }
  
  else if (screen_map[i] & AT_SUPER) {
      attr = CELL_ATTR (ATR_SB);
      glyph = 'S';
  }
  
  else if (screen_map[i] & AT_BITMAP) {
      attr = CELL_ATTR (ATR_BITMAP);
      glyph = 'M';
  }
  
  else if (screen_map[i] & AT_INODE) {
      attr = CELL_ATTR (ATR_INODE);
      glyph = 'I';
  }
   
  else if (screen_map[i] & AT_BAD) {
      attr = CELL_ATTR (ATR_BAD);
      glyph = 'B';
  }
   
  else if (screen_map[i] & AT_KERNEL) {
      attr = CELL_ATTR (ATR_KERNEL);
      glyph = 'K';
  }
   
  else if (screen_map[i] & AT_DATA) {
      attr = CELL_ATTR (ATR_DATA);
      glyph = ACS_DIAMOND;
  }

  else if (screen_map[i] & AT_DIR) {
      attr = CELL_ATTR (ATR_DIR);
      glyph = 'D';
  }
   
  else if (screen_map[i] & AT_REG) {
      attr = CELL_ATTR (ATR_FILE);
      glyph = 'F';
  }

  else {
      attr = CELL_ATTR (ATR_FREE);
      glyph = '.';
  }

  if ((screen_map[i] & AT_FRAG) &&
     !(screen_map[i] & (AT_SUPER | AT_BITMAP | AT_INODE))) 
         attr = CELL_ATTR (ATR_FRAG);

  wattrset (map_w, attr);
  waddch (map_w, glyph);
}
       
void update_display(void) {
   int i, row = 0;
   if (!voyer_mode)
        return;
   for (i = 0; i < screen_cells; i++) {
        if ((i % map_w_width) == 0)  {
             wmove(map_w,++row,1);
        }
        show_cell(i);             
   }      
   wrefresh(map_w);
}

void display_map(void) {
   if (!voyer_mode)
        return;
   box(map_w,ACS_VLINE,ACS_HLINE);
   update_display();
}