summaryrefslogtreecommitdiff
path: root/tvision/examples/demo/gadgets.cc
blob: 1af7f6fea856e08e42a8f21b7f8c3d7405f5e6db (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
/*-------------------------------------------------------------------*/
/*                                                                   */
/*   Turbo Vision Demo                                               */
/*                                                                   */
/*   Gadgets.cpp:  Gadgets for the Turbo Vision Demo.  Includes a    */
/*        heap view and a clock view which display the clock at the  */
/*        right end of the menu bar and the current heap space at    */
/*        the right end of the status line.                          */
/*                                                                   */
/*-------------------------------------------------------------------*/
/*
 *      Turbo Vision - Version 2.0
 *
 *      Copyright (c) 1994 by Borland International
 *      All Rights Reserved.
 *
 */
/*
 * Modified by Sergio Sigala <ssigala@globalnet.it>
 * Modified to compile with gcc v3.x by Salvador E. Tropea, with the help of
 * Andris Pavenis.
 * Modified by Mike Gorchak <mike@malva.ua> to report free memory on QNX.
 */
#include <tv/configtv.h>

// SET: moved the standard headers before tv.h
#define Uses_string
#define Uses_stdlib
#define Uses_ctype
#define Uses_time
#define Uses_iomanip
#if defined(TVOSf_QNXRtP) || defined(TVOSf_QNX4)
 #define Uses_stdio
 #define Uses_sys_stat
#endif // TVOSf_QNXRtP

#define Uses_TRect
#define Uses_TView
#define Uses_TDrawBuffer
#include <tv.h>

#ifdef TVOSf_QNX4
 #include <sys/osinfo.h>
#endif // TVOSf_QNX4

#include "gadgets.h"

//extern "C" unsigned long farcoreleft( void );

//
// ------------- Heap Viewer functions
//

THeapView::THeapView(TRect& r) : TView( r )
{
    oldMem = 0;
    newMem = heapSize();

	/* SS: now resizing under X works well */
	growMode = gfGrowLoX | gfGrowLoY | gfGrowHiX | gfGrowHiY;
}


void THeapView::draw()
{
    TDrawBuffer buf;
    char c = getColor(2);

    buf.moveChar(0, ' ', c, (short)size.x);
    buf.moveStr(0, heapStr, c);
    writeLine(0, 0, (short)size.x, 1, buf);
}


void THeapView::update()
{
    if( (newMem = heapSize()) != oldMem )
        {
        oldMem = newMem;
        drawView();
        }
}


long THeapView::heapSize()
{
 #if defined(TVOSf_QNXRtP)
   struct stat st;
   long rval=0;

   if (stat("/proc", &st)==0)
   {
      rval=st.st_size;
   }

   if (rval>1024UL*512UL) // one half megabyte !
   {
      if (rval>1024UL*1024UL*32UL) // if above 32Mb free
      {
         sprintf(heapStr, "%10dMb", rval/1024UL/1024UL);
      }
      else
      {
         sprintf(heapStr, "%10dKb", rval/1024UL);
      }
   }
   else
   {
      sprintf(heapStr, "%12d", rval);
   }

   return rval;
 #elif defined(TVOSf_QNX4)

   _osinfo CurrInfo;
   unsigned long rval;
   
   qnx_osinfo(0, &CurrInfo);
   rval=CurrInfo.freepmem;

   if (rval>1024UL*512UL) // one half megabyte !
   {
      if (rval>1024UL*1024UL*32UL) // if above 32Mb free
      {
         sprintf(heapStr, "%10dMb", rval/1024UL/1024UL);
      }
      else
      {
         sprintf(heapStr, "%10dKb", rval/1024UL);
      }
   }
   else
   {
      sprintf(heapStr, "%12d", rval);
   }

   return rval;

 #else
	/* SS: changed */
	strcpy(heapStr, "Hello world!");
	return -1;
 #endif // TVOSf_QNXRtP
}


//
// -------------- Clock Viewer functions
//

TClockView::TClockView( TRect& r ) : TView( r )
{
    strcpy(lastTime, "        ");
    strcpy(curTime, "        ");

	/* SS: now resizing under X works well */
	growMode = gfGrowLoX | gfGrowHiX;
}


void TClockView::draw()
{
    TDrawBuffer buf;
    char c = getColor(2);

    buf.moveChar(0, ' ', c, (short)size.x);
    buf.moveStr(0, curTime, c);
    writeLine(0, 0, (short)size.x, 1, buf);
}


void TClockView::update()
{
    time_t t = time(0);
    char *date = ctime(&t);

    date[19] = '\0';
    strcpy(curTime, &date[11]);        /* Extract time. */

    if( strcmp(lastTime, curTime) )
        {
        drawView();
        strcpy(lastTime, curTime);
        }
}