Listing 9-3

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

Alle Listings können heruntergeladen werden.

Listing 9-2: Systemuhr


/* Programm öffnet Fenster und zeigt Datum und Zeit
   program opens window and shows time and date
   Alexander Ausserstorfer, 06.01.2017 */

#include "oslib/wimp.h"
#include "oslib/osword.h"
#include "oslib/hourglass.h"
#include "oslib/os.h"
#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, "Systemuhr", NULL, &version_out);

 /* Lege Struktur für Systemuhr an und lese diese aus
    install structure for clock and read clock */
 oswordreadclock_local_string_block *systemclock;
 osword_block oswblk;
 systemclock = &oswblk.string;
 systemclock->op = oswordreadclock_OP_LOCAL_STRING;
 oswordreadclock_local_string (systemclock);

 /* Lege Datenblock für Fenster + Symbol an
    install data block for window and icon */
 wimp_window *window;
 window = malloc(sizeof(wimp_window) + 1*sizeof(wimp_icon));
 window->visible.x0 = 0;
 window->visible.y0 = 200;
 window->visible.x1 = 550;
 window->visible.y1 = 350;
 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 = -60;
 window->extent.x1 = 410;
 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, "Systemuhr", 12);
 window->icon_count = 1;
 window->xmin = 0;
 window->ymin = 0;
 window->icons[0].extent.x0 =  5;
 window->icons[0].extent.y0 = -55;
 window->icons[0].extent.x1 =  405;
 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;
 window->icons[0].data.indirected_text.text = systemclock->string.c;
 window->icons[0].data.indirected_text.validation = "R2";
 window->icons[0].data.indirected_text.size = strlen(systemclock->string.c);

/* 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 = wimp_MASK_LEAVING | wimp_MASK_ENTERING | wimp_QUEUE_MOUSE | wimp_QUEUE_KEY | wimp_MASK_LOSE | wimp_MASK_GAIN;
 osbool quit_pending = FALSE;
 wimp_block block2;

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

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

     /* Ereignisbehandler
        event handler */
      event = wimp_poll_idle(mask, &block2, newtime+100, NULL);
      switch (event)
      {

       case wimp_NULL_REASON_CODE:

                /* Datum und Uhrzeit auslesen
                   read date ant time */
                systemclock->op = oswordreadclock_OP_LOCAL_STRING;
                oswordreadclock_local_string (systemclock);

                 /* aktualisiere Symbol
                   update icon */
                 wimp_set_icon_state(block.open.w, 0, 0, 0);

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

                 break;

       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;
       }

    }

 /* beende unsere Aufgabe
    stops our task */
 wimp_close_down(task_handle);
 return 0;
}