graph.c 12 KB
Newer Older
1 2 3 4 5 6
/*
 *  ReactOS Task Manager
 *
 *  graph.c
 *
 *  Copyright (C) 1999 - 2001  Brian Palmer  <brianp@reactos.org>
7
 *  Copyright (C) 2008  Vladimir Pankratov
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 23 24 25 26 27 28 29 30 31 32
 */
    
#define WIN32_LEAN_AND_MEAN    /* Exclude rarely-used stuff from Windows headers */
#include <windows.h>
#include <commctrl.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <stdio.h>
#include <winnt.h>
    
33
#include "wine/unicode.h"
34 35 36 37 38 39 40 41
#include "taskmgr.h"
#include "perfdata.h"

#define BRIGHT_GREEN	RGB(0, 255, 0)
#define DARK_GREEN	RGB(0, 130, 0)
#define RED		RGB(255, 0, 0)


42
WNDPROC             OldGraphWndProc;
43

44
static void Graph_DrawCpuUsageGraph(HDC hDC, HWND hWnd)
45 46 47 48
{
    RECT            rcClient;
    RECT            rcBarLeft;
    RECT            rcBarRight;
49
    WCHAR            Text[256];
50 51 52 53 54 55 56 57 58 59
    ULONG            CpuUsage;
    ULONG            CpuKernelUsage;
    int                nBars;
    int                nBarsUsed; 
/* Bottom bars that are "used", i.e. are bright green, representing used cpu time */
    int                nBarsUsedKernel; 
/* Bottom bars that are "used", i.e. are bright green, representing used cpu kernel time */
    int                nBarsFree; 
/* Top bars that are "unused", i.e. are dark green, representing free cpu time */
    int                i;
60 61 62 63

    static const WCHAR    wszFormatI[] = {'%','d','%','%',0};
    static const WCHAR    wszFormatII[] = {' ',' ','%','d','%','%',0};
    static const WCHAR    wszFormatIII[] = {' ','%','d','%','%',0};
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    
    /*
     * Get the client area rectangle
     */
    GetClientRect(hWnd, &rcClient);
    
    /*
     * Fill it with blackness
     */
    FillSolidRect(hDC, &rcClient, RGB(0, 0, 0));
    
    /*
     * Get the CPU usage
     */
    CpuUsage = PerfDataGetProcessorUsage();
    CpuKernelUsage = PerfDataGetProcessorSystemUsage();

    /*
     * Check and see how many digits it will take
     * so we get the indentation right every time.
     */
    if (CpuUsage == 100)
    {
87
        sprintfW(Text, wszFormatI, (int)CpuUsage);
88 89 90
    }
    else if (CpuUsage < 10)
    {
91
        sprintfW(Text, wszFormatII, (int)CpuUsage);
92 93 94
    }
    else
    {
95
        sprintfW(Text, wszFormatIII, (int)CpuUsage);
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
    }
    
    /*
     * Draw the font text onto the graph
     * The bottom 20 pixels are reserved for the text
     */
    Font_DrawText(hDC, Text, ((rcClient.right - rcClient.left) - 32) / 2, rcClient.bottom - 11 - 5);

    /*
     * Now we have to draw the graph
     * So first find out how many bars we can fit
     */
    nBars = ((rcClient.bottom - rcClient.top) - 25) / 3;
    nBarsUsed = (nBars * CpuUsage) / 100;
    if ((CpuUsage) && (nBarsUsed == 0))
    {
        nBarsUsed = 1;
    }
    nBarsFree = nBars - nBarsUsed;
    if (TaskManagerSettings.ShowKernelTimes)
    {
        nBarsUsedKernel = ((nBars * 2) * CpuKernelUsage) / 100;
        nBarsUsed -= (nBarsUsedKernel / 2);
    }
    else
    {
        nBarsUsedKernel = 0;
    }

    /*
     * Now draw the bar graph
     */
    rcBarLeft.left =  ((rcClient.right - rcClient.left) - 33) / 2;
    rcBarLeft.right =  rcBarLeft.left + 16;
    rcBarRight.left = rcBarLeft.left + 17;
    rcBarRight.right = rcBarLeft.right + 17;
    rcBarLeft.top = rcBarRight.top = 5;
    rcBarLeft.bottom = rcBarRight.bottom = 7;

    if (nBarsUsed < 0)     nBarsUsed = 0;
    if (nBarsUsed > nBars) nBarsUsed = nBars;

    if (nBarsFree < 0)     nBarsFree = 0;
    if (nBarsFree > nBars) nBarsFree = nBars;

    if (nBarsUsedKernel < 0)     nBarsUsedKernel = 0;
    if (nBarsUsedKernel > nBars) nBarsUsedKernel = nBars;

    /*
     * Draw the "free" bars
     */
    for (i=0; i<nBarsFree; i++)
    {
        FillSolidRect(hDC, &rcBarLeft, DARK_GREEN);
        FillSolidRect(hDC, &rcBarRight, DARK_GREEN);
        
        rcBarLeft.top += 3;
        rcBarLeft.bottom += 3;
        
        rcBarRight.top += 3;
        rcBarRight.bottom += 3;
    }
    
    /*
     * Draw the "used" bars
     */
    for (i=0; i<nBarsUsed; i++)
    {
        if (nBarsUsed > 5000) nBarsUsed = 5000;

        FillSolidRect(hDC, &rcBarLeft, BRIGHT_GREEN);
        FillSolidRect(hDC, &rcBarRight, BRIGHT_GREEN);
        
        rcBarLeft.top += 3;
        rcBarLeft.bottom += 3;
        
        rcBarRight.top += 3;
        rcBarRight.bottom += 3;
    }
    
    /*
     * Draw the "used" kernel bars
     */
    rcBarLeft.bottom--;
    rcBarRight.bottom--;
    if (nBarsUsedKernel && nBarsUsedKernel % 2)
    {
        rcBarLeft.top -= 2;
        rcBarLeft.bottom -= 2;
        
        rcBarRight.top -= 2;
        rcBarRight.bottom -= 2;

        FillSolidRect(hDC, &rcBarLeft, RED);
        FillSolidRect(hDC, &rcBarRight, RED);
        
        rcBarLeft.top += 2;
        rcBarLeft.bottom += 2;
        
        rcBarRight.top += 2;
        rcBarRight.bottom += 2;

        nBarsUsedKernel--;
    }
    for (i=0; i<nBarsUsedKernel; i++)
    {
        if (nBarsUsedKernel > 5000) nBarsUsedKernel = 5000;

        FillSolidRect(hDC, &rcBarLeft, RED);
        FillSolidRect(hDC, &rcBarRight, RED);
        
        rcBarLeft.top++;
        rcBarLeft.bottom++;
        
        rcBarRight.top++;
        rcBarRight.bottom++;

        if (i % 2)
        {
            rcBarLeft.top++;
            rcBarLeft.bottom++;
            
            rcBarRight.top++;
            rcBarRight.bottom++;
        }
    }
}

224
static void Graph_DrawMemUsageGraph(HDC hDC, HWND hWnd)
225 226 227 228
{
    RECT            rcClient;
    RECT            rcBarLeft;
    RECT            rcBarRight;
229
    WCHAR            Text[256];
230 231 232 233 234 235 236 237
    ULONGLONG        CommitChargeTotal;
    ULONGLONG        CommitChargeLimit;
    int                nBars;
    int                nBarsUsed = 0; 
/* Bottom bars that are "used", i.e. are bright green, representing used memory */
    int                nBarsFree; 
/* Top bars that are "unused", i.e. are dark green, representing free memory */
    int                i;
238 239

    static const WCHAR    wszFormat[] = {'%','d','K',0};
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    
    /*
     * Get the client area rectangle
     */
    GetClientRect(hWnd, &rcClient);
    
    /*
     * Fill it with blackness
     */
    FillSolidRect(hDC, &rcClient, RGB(0, 0, 0));
    
    /*
     * Get the memory usage
     */
    CommitChargeTotal = (ULONGLONG)PerfDataGetCommitChargeTotalK();
    CommitChargeLimit = (ULONGLONG)PerfDataGetCommitChargeLimitK();

257
    sprintfW(Text, wszFormat, (int)CommitChargeTotal);
258 259 260 261 262
    
    /*
     * Draw the font text onto the graph
     * The bottom 20 pixels are reserved for the text
     */
263
    Font_DrawText(hDC, Text, ((rcClient.right - rcClient.left) - (strlenW(Text) * 8)) / 2, rcClient.bottom - 11 - 5);
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

    /*
     * Now we have to draw the graph
     * So first find out how many bars we can fit
     */
    nBars = ((rcClient.bottom - rcClient.top) - 25) / 3;
        if (CommitChargeLimit)
    nBarsUsed = (nBars * (int)((CommitChargeTotal * 100) / CommitChargeLimit)) / 100;
    nBarsFree = nBars - nBarsUsed;

    if (nBarsUsed < 0)     nBarsUsed = 0;
    if (nBarsUsed > nBars) nBarsUsed = nBars;

    if (nBarsFree < 0)     nBarsFree = 0;
    if (nBarsFree > nBars) nBarsFree = nBars;

    /*
     * Now draw the bar graph
     */
    rcBarLeft.left =  ((rcClient.right - rcClient.left) - 33) / 2;
    rcBarLeft.right =  rcBarLeft.left + 16;
    rcBarRight.left = rcBarLeft.left + 17;
    rcBarRight.right = rcBarLeft.right + 17;
    rcBarLeft.top = rcBarRight.top = 5;
    rcBarLeft.bottom = rcBarRight.bottom = 7;
    
    /*
     * Draw the "free" bars
     */
    for (i=0; i<nBarsFree; i++)
    {
        FillSolidRect(hDC, &rcBarLeft, DARK_GREEN);
        FillSolidRect(hDC, &rcBarRight, DARK_GREEN);
        
        rcBarLeft.top += 3;
        rcBarLeft.bottom += 3;
        
        rcBarRight.top += 3;
        rcBarRight.bottom += 3;
    }
    
    /*
     * Draw the "used" bars
     */
    for (i=0; i<nBarsUsed; i++)
    {
        FillSolidRect(hDC, &rcBarLeft, BRIGHT_GREEN);
        FillSolidRect(hDC, &rcBarRight, BRIGHT_GREEN);
        
        rcBarLeft.top += 3;
        rcBarLeft.bottom += 3;
        
        rcBarRight.top += 3;
        rcBarRight.bottom += 3;
    }
}

321
static void Graph_DrawMemUsageHistoryGraph(HDC hDC, HWND hWnd)
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
{
    RECT            rcClient;
    ULONGLONG        CommitChargeLimit;
    int                i;
    static int        offset = 0;
    
    if (offset++ >= 10)
        offset = 0;
    
    /*
     * Get the client area rectangle
     */
    GetClientRect(hWnd, &rcClient);
    
    /*
     * Fill it with blackness
     */
    FillSolidRect(hDC, &rcClient, RGB(0, 0, 0));

    /*
     * Get the memory usage
     */
    CommitChargeLimit = (ULONGLONG)PerfDataGetCommitChargeLimitK();

    /*
     * Draw the graph background
     *
     * Draw the horizontal bars
     */
    for (i=0; i<rcClient.bottom; i++)
    {
        if ((i % 11) == 0)
        {
            /* FillSolidRect2(hDC, 0, i, rcClient.right, 1, DARK_GREEN);  */
        }
    }
    /*
     * Draw the vertical bars
     */
    for (i=11; i<rcClient.right + offset; i++)
    {
        if ((i % 11) == 0)
        {
            /* FillSolidRect2(hDC, i - offset, 0, 1, rcClient.bottom, DARK_GREEN);  */
        }
    }

    /*
     * Draw the memory usage
     */
    for (i=rcClient.right; i>=0; i--)
    {
    }
}
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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474

INT_PTR CALLBACK
Graph_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC                hdc;
    PAINTSTRUCT        ps;
    LONG            WindowId;
    
    switch (message)
    {
    case WM_ERASEBKGND:
        return TRUE;

    /*
     * Filter out mouse  & keyboard messages
     */
    /* case WM_APPCOMMAND: */
    case WM_CAPTURECHANGED:
    case WM_LBUTTONDBLCLK:
    case WM_LBUTTONDOWN:
    case WM_LBUTTONUP:
    case WM_MBUTTONDBLCLK:
    case WM_MBUTTONDOWN:
    case WM_MBUTTONUP:
    case WM_MOUSEACTIVATE:
    case WM_MOUSEHOVER:
    case WM_MOUSELEAVE:
    case WM_MOUSEMOVE:
    /* case WM_MOUSEWHEEL: */
    case WM_NCHITTEST:
    case WM_NCLBUTTONDBLCLK:
    case WM_NCLBUTTONDOWN:
    case WM_NCLBUTTONUP:
    case WM_NCMBUTTONDBLCLK:
    case WM_NCMBUTTONDOWN:
    case WM_NCMBUTTONUP:
    /* case WM_NCMOUSEHOVER: */
    /* case WM_NCMOUSELEAVE: */
    case WM_NCMOUSEMOVE:
    case WM_NCRBUTTONDBLCLK:
    case WM_NCRBUTTONDOWN:
    case WM_NCRBUTTONUP:
    /* case WM_NCXBUTTONDBLCLK: */
    /* case WM_NCXBUTTONDOWN: */
    /* case WM_NCXBUTTONUP: */
    case WM_RBUTTONDBLCLK:
    case WM_RBUTTONDOWN:
    case WM_RBUTTONUP:
    /* case WM_XBUTTONDBLCLK: */
    /* case WM_XBUTTONDOWN: */
    /* case WM_XBUTTONUP: */
    case WM_ACTIVATE:
    case WM_CHAR:
    case WM_DEADCHAR:
    case WM_GETHOTKEY:
    case WM_HOTKEY:
    case WM_KEYDOWN:
    case WM_KEYUP:
    case WM_KILLFOCUS:
    case WM_SETFOCUS:
    case WM_SETHOTKEY:
    case WM_SYSCHAR:
    case WM_SYSDEADCHAR:
    case WM_SYSKEYDOWN:
    case WM_SYSKEYUP:
            
    case WM_NCCALCSIZE:
        return 0;

    case WM_PAINT:
        
        hdc = BeginPaint(hWnd, &ps);

        WindowId = GetWindowLongPtr(hWnd, GWLP_ID);

        switch (WindowId)
        {
        case IDC_CPU_USAGE_GRAPH:
            Graph_DrawCpuUsageGraph(hdc, hWnd);
            break;
        case IDC_MEM_USAGE_GRAPH:
            Graph_DrawMemUsageGraph(hdc, hWnd);
            break;
        case IDC_MEM_USAGE_HISTORY_GRAPH:
            Graph_DrawMemUsageHistoryGraph(hdc, hWnd);
            break;
        }
        
        EndPaint(hWnd, &ps);
        
        return 0;
        
    }
    
    /*
     * We pass on all non-handled messages
     */
    return CallWindowProc((WNDPROC)OldGraphWndProc, hWnd, message, wParam, lParam);
}