Diary of an Amiga fanatic

Tag Archives: amiga os4 programming

Since I took office upstairs in one of the rooms my A1SE was cut off from any network access. Since the network was the only way to transfer files from, or to, my A1SE I had a bit of a problem. I left it at first since my new office was only going to be temporary before moving to a different location. But since I really like it in my new office and my wife could really use what was going to be my office for her hobby I decided to make the temporary location permanent.  Because of this and because I wanted to start working on some new material for my OS4.x programming section I needed a solution. I was thinking about using a repeater with a LAN port that I could use the connect my A1SE to. I wrote about this in my column (When the dust settles – Week 11) to which JetSetSkippy replied. He suggested I should use HomePlugs so I could provide network (and internet) acces through the power line to my A1SE upstairs. I completely forgot about this option. I gave it some thought before when I was still planning to create my new office in my garage but I thought it might now work because of the distance. I could not stop thinking about this solution so I decided yesterday (Saturday 17-3-2012) that I wanted to go for this solution. After checking online for some prices I came to the conclusion that the ICIDU HomePlug Starter Kit was the cheapest.

ICIDU HomePlug Starter Kit Box

ICIDU HomePlugs

Next up was to get my wife to go shopping (not much of a challange, eh?) and when we would be near Dixons (the place I wanted to purchase it) I would play my part. I would talk about my A1Se not having any network access and since I would be staying in my new office for good because I gave my new office location to my wife I would really love to have a solution for this. Maybe I could have a look in Dixons to see if I could get some ideas for this? Hook, line and sinker. Of course my wife is not from yesterday and after I purchase the HomePlug Starter Kit we had to stop at a shop and she asked if I would not mind waiting in the car and it could take some time but I had my HomePlug Starter Kit already so I would not really mind. It did take rather long but that was a small price to pay to get what I wanted.

I was really hoping it would work. When I got home I plugged one adapter in the extension cord next to the router and the other one upstairs in the wall socket. I decided to test it with my laptop first and it worked just great. Even with the downstairs adapter plugged in the extenstion cord (I know they can give you some problems) it worked without any problems. Next up was the A1SE and also with this it worked really great. It is really out of the box and plug and play. I am really impressed by it and with a price of around 55 Euro you get really great value for your money. I actually wrote most of this article on my A1SE using OWB. Now I can start working on some more articles for my OS4.x programming section.


Working with screens using the C programming language on the Amiga is a first for me. Remember how easy it was in Amos? I had seen some C code using screens before and frankly it scared me. Actually it is not really difficult to open a screen on the Amiga using C. As with all my articles I will try to take all the difficult information that is out there on a particular subject and make it understandable. The same goes for the example source code that is out there. I come across so many examples that even make me scratch my head. Especially when you can write in a much better and understandable way.

What steps do we need to take in order to open a screen?

Step 1 – Declare a pointer that will hold our screen structure (Line 10)
Step 2 – Setting up our screen’s parameters (Line 12 till 27)
Step 3 – Open the Intuition Library and Interface (Line 33 till 44)
Step 4 – Opening our screen and check if we managed to open it (Line 45 till 48)
Step 5 – Cleaning up by closing our screen, Intuition Library and it’s Interface (Line 50 till 79)

00 #include <stdio.h>
01 #include <exec/types.h>
02 #include <intuition/intuition.h>
03
04 #include <proto/exec.h>
05 #include <proto/intuition.h>
06 
07 struct Library *IntuitionBase = NULL;
08 struct IntuitionIFace *IIntuition = NULL;
09 
10 struct Screen *my_screen = NULL;
11
12 /* Declare and initialize our screen structure */
13 struct NewScreen my_new_screen=
14 {
15 0, /* LeftEdge */
16 0, /* TopEdge */
17 320, /* Width */
18 200, /* Height */
19 3, /* Depth */
20 0, /* DetailPen */
21 1, /* BlockPen */ 
22 CUSTOMSCREEN, /* Type of screen */
23 SPRITES, /* ViewModes option */
24 NULL, /* Font option */
25 NULL, /* Title of the screen */
26 NULL /* Gadgets option */
27 };
28
29 int main (void)
30 {
31
32 /* Opening the Intuition Library */
33 IntuitionBase = IExec->OpenLibrary("intuition.library", 50L);
34
35 /* Could we open the Intuition Library? */
36 if (IntuitionBase != NULL)
37 {
38
39 /* Open the Intuition Interface */
40 IIntuition = (struct IntuitionIFace *) IExec->GetInterface (IntuitionBase, "main", 1, NULL);
41
42 /* Did we get the Intuition Interface? */
43 if (IIntuition != NULL)
44 {
45 my_screen = IIntuition->OpenScreen(&my_new_screen);
46
47 if (my_screen !=NULL)
48 {
49
50 /* If the screen is still open we need to close it */
51 IIntuition->CloseScreen(my_screen);
52 }
53
54 /* We could not open our screen */
56 else
57 {
58 printf ("Unable to open our screen!\n");
59 }
60
61 /* If the Intuition Library Interface is open, close it */
62 IExec->DropInterface((struct Interface *)IIntuition);
63 }
64 
65 /* We could not open the Intuition Library Interface */
66 else
67 {
68 printf ("Unable to open the Intuition Library Interface!\n");
69 }
70
71 /* If the Intuition Library is open, close it */
72 IExec->CloseLibrary(IntuitionBase);
73 }
74
75 /* We could not open the Intuition Library */
76 else
77 {
78 printf ("Unable to open the Intuition Library!\n");
79 }
80
81 return 0;
82 }

When you compile the code and run it you will see it flashes a black screen for just a second. This is because we have not instruct our little example to do anything with our screen. We will do a lot more in the coming articles regarding screens.

Let’s analyse the source code.

10 struct Screen *my_screen = NULL;

Line 10 will set up a pointer called “my_screen” which will hold our screen structure. In other words if we open our screen successfully we will store the screens info in the “my_screen” pointer. We are going to use this to make our screen do things. To put it simple; every time we want to do things with our screen we need to refer to which screen we would like to use which means we will refer to “my_screen”. If my wife asks me to get a certain pair of shoes she will always refer to one of the 200 boxes she has. Without this info I would not be able to know what to do and shout some error codes.

12 struct NewScreen my_new_screen =
14 {
15 0, /* LeftEdge */
16 0, /* TopEdge */
17 320, /* Width */
18 200, /* Height */
19 3, /* Depth */
20 0, /* DetailPen */
21 1, /* BlockPen */
22 CUSTOMSCREEN, /* Type of screen */
23 SPRITES, /* ViewModes option */
24 NULL, /* Font option */
25 NULL, /* Title of the screen */
26 NULL /* Gadgets option */
27 };

Line 12 till 27 will define our screens parameters. Here we will define for example the width and height of our screen. For the moment we will not pay much attention to this. Going in to depth on this might just scare you away. Our screen will be of the type CUSTOMSCREEN and the width is 320 and the height is 200 is all you need to know at this stage. The width and height speaks for itself and a CUSTOMSCREEN means that we control how it looks, talks and breaths. In order words we decide the width, hight, colours etc.

45 my_screen = IIntuition->OpenScreen(&my_new_screen);

Line 45 will open our screen and store the info in our “my_screen” pointer. The part “(&my_new_screen)” tells the system how our screen should look like.

47 if (my_screen !=NULL)
48 {

Line 47-48 will check if we managed to open the screen. If not the program will jump to line 55-58 and display the message that the screen could not be opened.

51 IIntuition->CloseScreen(my_screen);

Line 51 will close our screen once we are done. Remember I was talking about the fact that we would use our “my_screen” pointer every time we would do something with our screen? Here we are closing our screen and referring to which screen we would like to close. In other words my wife asked me to put her pair of shoes back in box number 145. Next time we will actually keep our screen open until we decide it should be closed. Happy coding!


For about a week now I am stuck with finding a certain bug in my code. Every time I run it OS4 crashes. What I have done far:

– Opening all the libraries and classes

– Setting up my window parameters

– Opening the window

If I run the code it displays the window but instantly brings up the Grim Reaper saying it crashed. Being the smart ass as I am I believe my code is clean as a whistle. 🙂 The problem is how to you find this bug? My first thought was that it goes wrong at the “looping” part. This is the part that keeps the window displayed until someone closes it. But after checking and comparing my code  I really code not spot a wrong line of code. To narrow it down I started printing text on the screen after each line of code so I know what part works. The benefit of this is that I soon discovered that it does not even get to the looping part.

A good practice sometimes is that if you really can’t find the problem to take some example code that does work and strip it of all it’s extra’s till it does only what you failed to achieve with your approach. Once you have that you just continue from there. Since I am running the Pre-release version of OS4 I also have to consider that sometimes the code is just fine but it is the Pre-release version that is causing it. Time to squash that bug.


The first thing that fascinated me when I tried programming for the Amiga were the libraries. The first language I used to open and close libraries and use it’s functions was Assembler. At that time it took me some time to understand how libraries worked. This was because of the available information I had. Even today when I look at some of that information I can see that the way it was explained was just too difficult. Would it have been done easier it would have been quicker for me to get to grips with it. The only source of information you had was the magazines and the odd programming book you might be able to purchase. Not like today when you have the internet which gives you access to unlimited resources.

But what are those libraries you keep talking about? The ROM Kernel Manual describes it as follow:

The Amiga has two kinds of libraries: run-time libraries and link libraries. Run-time libraries make up most of the Amiga’s operating system. There is another type of library known as a link library. Even though a link library is a collection of functions just like a run-time library, there are some major differences in the two types.

Run-time libraries
A run-time, or shared library is a group of functions managed by Exec that resides either in ROM or on disk (in the LIBS: directory). A run-time library must be opened before it can be used. The functions in a run-time library are accessed dynamically at run-time and can be used by many programs at once even though only one copy of the library is in memory. A disk based run-time library is loaded into memory only if requested by a program and can be automatically flushed from memory when no longer needed.

Link libraries
A link library is a group of functions on disk that are managed by the compiler at link time. Link libraries do not have to be opened before they are used, instead you must link your code with the library when you compile a program. The functions in a link library are actually copied into every program that uses them.

To sum it up in a simple way: Run-time libraries stored on the hard drive and are opened and closed by the program whenever needed. Link libraries are (actually it’s functions) copied into your program when you are compiling it.

I am discussing the run-time libraries here. I assume you have some understanding of the basics of C programming. If something is not clear (and in future articles) just let me know and I will explain it. Don”t worry if at first it, or something, makes no sense. Sooner or later the penny will drop and it suddenly makes sense.

So what are we going to do for a first example? We are going to open the Intuition library and check if it is opened successfully or not and in both cases display a message. 

01 #include <stdio.h>
02 #include <intuition/intuition.h>
03
04 #include <proto/intuition.h>
05 #include <proto/exec.h>
06
07 struct Library *IntuitionBase = NULL;
08
09 struct IntuitionIFace *IIntuition = NULL;
10
11  int main (void)
12
13 {
14
15  /* Opening the Intuition Library */
16  IntuitionBase = IExec->OpenLibrary(“intuition.library”, 50L);
17
18   /* Did we manage to open the Intuition Library? */
19   if (IntuitionBase != NULL)
20   {
21
22     printf (“We opened the Intuition Library!!\n”);
23
24     /* Open the Intuition Interface */
25     IIntuition = (struct IntuitionIFace *) IExec->GetInterface (IntuitionBase, “main”, 1, NULL);
26
27     /* Did we get the Intuition Interface? */
28      if (IIntuition != NULL)
29      {
30
31        printf (“We did get the Intuition Interface!!\n”);
32
33       /* If the Intuition Library Interface is open, close it */
34       IExec->DropInterface((struct Interface *)IIntuition);
35       }
36
37     /* We could not open the Intuition Library Interface */
38     else
39    {
40
41    printf (“Unable to open the Intuition Library Interface!\n”);
42    }
43
44   /* If the Intuition Library is open, close it */
45   IExec->CloseLibrary(IntuitionBase);
46   }
47
48     /* We could not open the Intuition Library */
49    else
50   {
51
52  printf (“Unable to open the Intuition Library!\n”);
53  }
54
55 return 0;
56 }

Type the above code (without the line numbers) in the text editor of your choice or IDE you might use. As you can see in the below screenshot I saved it in the “RAM Disk” with the name “openlibraryos4.c”. Assuming you have installed the OS4 SDK you can compile the code by typing “gcc openlibraryos4.c” in the AmigaShell as shown in the below sceenshot. This will produce the executable with the name “a.out”. If things go as planned you will get the correct out put saying that both the Intuition library and interface could be opened when executing “a.out”.

Compiling our code

Congratulations! You just compiled your first working OS4 code. Cool! But of course you like to know what all the code is about. Time to have a look at the source code.

The first 4 lines of code are includes.

01 #include <stdio.h>
02 #include <intuition/intuition.h>
04 #include <proto/intuition.h>
05 #include <proto/exec.h>

“#include” is a preprocessor directive which sounds rather difficult but it is not. It inserts another file into our code. Take for example the <stdio.h> file which includes information regarding the printf statement we are using. The intuition includes for example contain information if you want to display a window. For a simple example as this we don’t need so much includes to get our work done but if you create a larger program you will have more includes to (pardon the pun) include. I usually have a standard set of includes I use even if not all of them are being used. You might get errors during compiling because your code uses a function for which you did not use a include. This might sound rather complicated but after some trial and error from your side it will suddenly make sense.

Before we go to our next two lines a bit of further explanation about libraries is needed. I won’t get to technical but address just enough for you to understand the concept. With the classic Amiga OS you just opened the library you wanted and that was it. With OS4 we also need to use something called a “interface”. With the classic way you open the library and you can use any function from that library. With OS4 we have the interface which sits between the library and the functions it has. Let’s say we want to use a function from the intuition library we use the interface to call/use that function. You can compare it with talking to your wife. The old way would be to directly ask her a question. With the new way we use another person to ask our wife that question. This seems like a great idea (the wife example) but is this not adding extra work (and not to forget make it more difficult) when writing our code? Not really. It is just adding the name of the interface in front of the function. But what about the extra code for getting the interface and check we got it and also close it again? Well (getting ahead now) this is something I will address in part 2 in which you will see we can skip all that opening, checking and closing. Curious? Good!

But why use that interface stuff? Well that has to do with compatibility with the classic software. There is a lot more to it but without scaring you too much we will leave it at this. Let’s have a look at the next two lines.

07 struct Library *IntuitionBase = NULL;
09 struct IntuitionIFace *IIntuition = NULL;

This is usually the part you loose it all. Includes I can follow but this? The two lines declare pointers to structures we need to open the intuition library and also the intuition interface. These structures are actually defined in the <intuition.h> file. When we open the intuition library and get the interface we need to store the value we get back from trying to open them. We can use those values to test if we succeeded in opening both library and interface. We need to set both pointers to zero (NULL) to make sure they don’t point to any random address in memory which might cause the whole system to crash. *IntuitionBase is going to hold the value we get back from trying to open the intuition library and *IIntuition is going to hold the value we get back from trying to open the Intuition interface.

The next line is the start of our program. All programs start with main. To get a better understanding of how C works (it can’t hurt to mention this again) I suggest you check out the Amiga C Programming Tutorial from Michael Ness.

11  int main (void)

The next line is going to (try) open the Intuition Library.

16  IntuitionBase = IExec->OpenLibrary(“intuition.library”, 50L);

Some of you might have spotted the “IExec” part in front of OpenLibrary. This is the interface of the Exec Library. Wait! Stop! Are we not suppose to open this first, including the Exec Library, like we are doing with the Intuition Library? We don’t have to. The Exec Library is always open when OS4 (also with the classic OS) is running. In this case we want to open version 50 or later of the Intuition Library.

Line 19 is going to check if we managed to open the Intuition Library. Since we set the IntuitionBase in the beginning of our program to NULL we know we succeeded if the value of IntuitionBase is not NULL.

19   if (IntuitionBase != NULL)

If we managed to open the Intuition Library our program is going to jump to line 22 and print on our screen that we succeeded in opening the library.

22     printf (“We opened the Intuition Library!!\n”);

If we failed to open the intuition Library the value would still be NULL. If this is the case our program is going to jump to lines 49 – 53.

49    else
50   {
51
52  printf (“Unable to open the Intuition Library!\n”);
53  }

Line 52 will print on our screen that we did not manage to open the intuition Library.

If we successfully opened the Intuition Library and also printed on the screen we did we need to get the interface of the Intuition Library.

25     IIntuition = (struct IntuitionIFace *) IExec->GetInterface (IntuitionBase, “main”, 1, NULL);

Line 25 is trying to get Intuition’s Interface. The value that is being returned after we are trying to get the interface will be stored in IIntuition. IExec->GetInterface  is the function that will try and get the interface of the Intuition Library. Looking at the part (IntuitionBase, “main”, 1, NULL); we can see that we are trying to get the Interface of the (IntuitionBase) Intuition Library. Looking further at (IntuitionBase, “main”, 1, NULL); we can see that we are trying to get the main interface and the version number is 1. The last parameter (NULL) is a pointer to a tag list which we won’t discuss here. 

What about the (struct IntuitionIFace *) part? Without getting to technical (and loose you all) I can tell you this is called a cast. It will make sure the value returned by IExec->GetInterface is of the type struct IntuitionIFace *.

After we tried to get the Interface we need to check if we are successful.

28      if (IIntuition != NULL)

Line 28 (just like line 19 when we checked if we managed to open the Intuition Library) will check if we got the Interface by checking if the value if IIntuition is not NULL.

If we did manage to get the Interface we will print this as we can see in line 31.

31        printf (“We did get the Intuition Interface!!\n”);

If we failed to get the Interface the program will jump to lines 38-42.

38     else
39    {
40
41    printf (“Unable to open the Intuition Library Interface!\n”);
42    }

Line 42 will print that we did not manage to get the Intuition Library Interface.

If we did manage to open the Intuition Library and get it’s Interface (and of course print it on our screen) we need to end our program which means we need to clean up. The golden rule is to close everything that you open. First part of the clean up starts at line 34.

34       IExec->DropInterface((struct Interface *)IIntuition);

First up we are going to drop the Interface of the Intuition Library. The function DropInterface speaks for itself.

Last part of the clean up starts at line 45. We need to close the Intuition Library.

45   IExec->CloseLibrary(IntuitionBase);

Our program ends at line 55.

55 return 0;

Return can be used to return error codes but we give it the value zero to keep everyone happy. Surely I don’t want to scare (if anyone is still left) what is left of our readers at the very end. That’s it! As I already mentioned earlier in part 2 I will show you how you can skip all that opening and checking of libraries and interfaces.

Happy programming!