Listing 9-2

aus dem 9. Teil in die RISC OS C-Programmierung: Symbole und Ereignisbehandlung: Pollspeed.

Alle Listings können heruntergeladen werden.

Listing 9-2: Pollspeed


/* Programm misst die Pollgeschwindigkeit
   program measures the pool speed
   Alexander Ausserstorfer, 08.01.2017 */

#include "oslib/wimp.h"
#include "oslib/hourglass.h"
#include "oslib/os.h"         /* read_monotonic_timer */
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int main()
{

 /* melde Aufgabe beim Betriebssystem an
    report our task to WIMP */
 wimp_version_no version_out;
 wimp_t task_handle;
 task_handle=wimp_initialise(310, "Pollspeed", NULL, &version_out);

 /* Lege Datenblock für Fenster + Symbol an
    install data block for window and icon */
 wimp_window *window;
 window = malloc(sizeof(wimp_window) + 2*sizeof(wimp_icon));
 window->visible.x0 = 500;
 window->visible.y0 = 200;
 window->visible.x1 = 805;
 window->visible.y1 = 255;
 window->xscroll = 0;
 window->yscroll = 0;
 window->next = wimp_TOP;
 window->flags = wimp_WINDOW_MOVEABLE | wimp_WINDOW_AUTO_REDRAW | wimp_WINDOW_BACK_ICON | wimp_WINDOW_CLOSE_ICON | wimp_WINDOW_TITLE_ICON | wimp_WINDOW_NEW_FORMAT;
 window->title_fg = wimp_COLOUR_BLACK;
 window->title_bg = wimp_COLOUR_LIGHT_GREY;
 window->work_fg =  wimp_COLOUR_BLACK;
 window->work_bg = wimp_COLOUR_VERY_LIGHT_GREY;
 window->scroll_outer = wimp_COLOUR_MID_LIGHT_GREY;
 window->scroll_inner = wimp_COLOUR_VERY_LIGHT_GREY;
 window->highlight_bg = wimp_COLOUR_CREAM;
 window->extent.x0 = 0;
 window->extent.y0 = -200;
 window->extent.x1 = 500;
 window->extent.y1 = 0;
 window->extra_flags = 0;
 window->title_flags = wimp_ICON_TEXT | wimp_ICON_HCENTRED | wimp_ICON_VCENTRED;
 window->work_flags = wimp_BUTTON_CLICK << wimp_ICON_BUTTON_TYPE_SHIFT;
 window->sprite_area = 0;
 strncpy (window->title_data.text, "Pollspeed", 12);
 window->icon_count = 2;
 window->xmin = 0;
 window->ymin = 0;

 window->icons[0].extent.x0 =  5;
 window->icons[0].extent.y0 = -57;
 window->icons[0].extent.x1 =  205;
 window->icons[0].extent.y1 =  -5;
 window->icons[0].flags = wimp_ICON_TEXT | wimp_ICON_INDIRECTED | wimp_ICON_HCENTRED | wimp_COLOUR_BLACK << wimp_ICON_FG_COLOUR_SHIFT | wimp_ICON_BORDER;
 char pollspeed[12] = "0";
 window->icons[0].data.indirected_text.text = pollspeed;
 window->icons[0].data.indirected_text.validation = "R2";
 window->icons[0].data.indirected_text.size = 12;

 window->icons[1].extent.x0 =  210;
 window->icons[1].extent.y0 = -57;
 window->icons[1].extent.x1 =  305;
 window->icons[1].extent.y1 =  -5;
 window->icons[1].flags = wimp_ICON_TEXT | wimp_ICON_VCENTRED |  wimp_COLOUR_BLACK << wimp_ICON_FG_COLOUR_SHIFT;
 strcpy (window->icons[1].data.text, "1/sec");

 /* Erzeuge Fenster
    create window */
 wimp_block block;
 block.open.w = wimp_create_window (window);
 block.open.visible.x0 = window->visible.x0;
 block.open.visible.y0 = window->visible.y0;
 block.open.visible.x1 = window->visible.x1;
 block.open.visible.y1 = window->visible.y1;
 block.open.xscroll = window->xscroll;
 block.open.yscroll = window->yscroll;
 block.open.next = window->next;

 /* mache Fenster sichtbar
    opens window */
 wimp_open_window (&(block.open));

 /* Variablen für's Pollen
    variables for polling */
 wimp_event_no event;
 wimp_poll_flags mask = 0;
 osbool quit_pending = FALSE;
 wimp_block block2;

 /* Variable für Laufzeituhr
    variable for timer */
 os_t oldtime = os_read_monotonic_time();
 os_t newtime = 0;

 int speed = 0;

 /* Hauptschleife
    main loop */
 while (!quit_pending)
    {

       /* Erhöhe Zähler
          increment counter */
       speed++;

       /* Laufzeituhr neu auslesen
          read timer */
       newtime = os_read_monotonic_time();

       /* Sichere Zeitstempel
         save time */
       if (newtime >= (oldtime +100) )

           {

                 /* Wandle gezählte Ansprünge in Text und schreibe diese in die Symbole
                    convert counted calls to text and write this into the icons */
                 snprintf(window->icons[0].data.indirected_text.text, (size_t)255, "%d", speed);
                 wimp_set_icon_state(block.open.w, 0, 0, 0);

                 /* Setze Zähler zurück
                    reset counter */
                 speed = 0;

                 /* sichere Zeitstempel
                    save time */
                 oldtime = newtime;

           }

      /* Ereignisbehandler
         event handler */
      event = wimp_poll(mask, &block2, NULL);
      switch (event)
      {

       case wimp_OPEN_WINDOW_REQUEST:
                wimp_open_window(&(block2.open));
             break;

       case wimp_CLOSE_WINDOW_REQUEST:
                wimp_close_window(block2.close.w);
                quit_pending = true;
             break;

        case wimp_USER_MESSAGE_RECORDED:
             if (block2.message.action == message_QUIT)
                {
                    quit_pending = true;
                }
             break;
       }

    }
 wimp_close_down(task_handle);
 return 0;
}