ROVLib2
RISCOSC++applicationdevelopmentlibrary
wimp.c
1 #include <kernel.h>
2 #include <string.h>
3 #include "wimp.h"
4 #include "wimp_messages.h"
5 #include "wimp_window.h"
6 #include "wimp_menu.h"
7 #include "wimp_font.h"
8 
9 #define XOS_Byte (0x020006)
10 
11 #define XWimp_Initialise (0x0600c0)
12 #define XWimp_CreateWindow (0x0600c1)
13 #define XWimp_CreateIcon (0x0600c2)
14 #define XWimp_DeleteWindow (0x0600c3)
15 #define XWimp_DeleteIcon (0x0600c4)
16 #define XWimp_OpenWindow (0x0600c5)
17 #define XWimp_CloseWindow (0x0600c6)
18 #define XWimp_Poll (0x0600c7)
19 #define XWimp_RedrawWindow (0x0600c8)
20 #define XWimp_UpdateWindow (0x0600c9)
21 #define XWimp_GetRectangle (0x0600ca)
22 #define XWimp_GetWindowState (0x0600cb)
23 #define XWimp_GetWindowInfo (0x0600cc)
24 #define XWimp_SetIconState (0x0600cd)
25 #define XWimp_GetIconState (0x0600ce)
26 #define XWimp_GetPointerInfo (0x0600cf)
27 #define XWimp_DragBox (0x0600d0)
28 #define XWimp_ForceRedraw (0x0600d1)
29 #define XWimp_SetCaretPosition (0x0600d2)
30 #define XWimp_GetCaretPosition (0x0600d3)
31 #define XWimp_CreateMenu (0x0600d4)
32 #define XWimp_DecodeMenu (0x0600d5)
33 #define XWimp_WhichIcon (0x0600d6)
34 #define XWimp_SetExtent (0x0600d7)
35 #define XWimp_SetPointerShape (0x0600d8)
36 #define XWimp_OpenTemplate (0x0600d9)
37 #define XWimp_CloseTemplate (0x0600da)
38 #define XWimp_LoadTemplate (0x0600db)
39 #define XWimp_ProcessKey (0x0600dc)
40 #define XWimp_CloseDown (0x0600dd)
41 #define XWimp_StartTask (0x0600de)
42 #define XWimp_ReportError (0x0600df)
43 #define XWimp_GetWindowOutline (0x0600e0)
44 #define XWimp_PollIdle (0x0600e1)
45 #define XWimp_PlotIcon (0x0600e2)
46 #define XWimp_SetMode (0x0600e3)
47 #define XWimp_SetPalette (0x0600e4)
48 #define XWimp_ReadPalette (0x0600e5)
49 #define XWimp_SetColour (0x0600e6)
50 #define XWimp_SendMessage (0x0600e7)
51 #define XWimp_CreateSubMenu (0x0600e8)
52 #define XWimp_SpriteOp (0x0600e9)
53 #define XWimp_BaseOfSprites (0x0600ea)
54 #define XWimp_BlockCopy (0x0600eb)
55 #define XWimp_SlotSize (0x0600ec)
56 #define XWimp_ReadPixTrans (0x0600ed)
57 #define XWimp_ClaimFreeMemory (0x0600ee)
58 #define XWimp_CommandWindow (0x0600ef)
59 #define XWimp_TextColour (0x0600f0)
60 #define XWimp_TransferBlock (0x0600f1)
61 #define XWimp_ReadSysInfo (0x0600f2)
62 #define XWimp_SetFontColours (0x0600f3)
63 #define XWimp_GetMenuState (0x0600f4)
64 #define XWimp_RegisterFilter (0x0600f5)
65 #define XWimp_AddMessages (0x0600f6)
66 #define XWimp_RemoveMessages (0x0600f7)
67 #define XWimp_SetColourMapping (0x0600f8)
68 #define XWimp_TextOp (0x0600f9)
69 #define XWimp_SetWatchdogState (0x0600fa)
70 #define XWimp_ResizeIcon (0x0600fc)
71 
72 #define CallSwi(swino) pebLastError = _kernel_swi(swino, &swiRegs, &swiRegs)
73 
74 static char caTaskName[32];
75 static unsigned int iLastEvent;
76 static _kernel_swi_regs swiRegs;
77 
78 static int wimp_version;
79 
80 _kernel_oserror* pebLastError;
81 tWimpBlock twbWimpPollBlock;
82 
83 static int iaScratch[64];
84 
85 static inline int returnZeroOnError()
86 {
87  if (pebLastError != NULL)
88  {
89  return 0;
90  }
91 
92  return 1;
93 }
94 
95 static inline int returnMinusOneOnError(int ret)
96 {
97  if (pebLastError != NULL)
98  {
99  return -1;
100  }
101 
102  return ret;
103 }
104 
105 /* From wimp.h */
106 
107 int wimp_init(const char* taskName)
108 {
109  strcpy(caTaskName, taskName);
110  swiRegs.r[0] = 200;
111  swiRegs.r[1] = 0x4b534154;
112  swiRegs.r[2] = (int)caTaskName;
113 
114  CallSwi(XWimp_Initialise);
115  if (pebLastError == NULL)
116  {
117  wimp_version = swiRegs.r[0];
118  }
119 
120  return returnZeroOnError();
121 }
122 
123 void wimp_exit(void)
124 {
125  CallSwi(XWimp_CloseDown);
126 }
127 
128 int wimp_3dstatus(void)
129 {
130  if (wimp_version < 300)
131  {
132  return 0;
133  }
134 
135  swiRegs.r[0] = 161;
136  swiRegs.r[1] = 140;
137  CallSwi(XOS_Byte);
138  return swiRegs.r[2] & 1;
139 }
140 
141 /* From wimp_messages.h */
142 
143 eWimpEvent wimp_poll(void)
144 {
145  swiRegs.r[0] = 1;
146  swiRegs.r[1] = (int)&twbWimpPollBlock;
147  CallSwi(XWimp_Poll);
148  if (pebLastError == NULL)
149  {
150  iLastEvent = swiRegs.r[0];
151  }
152 
153  return (eWimpEvent)returnMinusOneOnError(swiRegs.r[0]);
154 }
155 
156 int wimp_sendmessage(eWimpEvent weEventCode, const tUserMessageBlock* kumbpBlock)
157 {
158  swiRegs.r[0] = (int)weEventCode;
159  swiRegs.r[1] = (int)kumbpBlock;
160  CallSwi(XWimp_SendMessage);
161  return returnZeroOnError();
162 }
163 
164 int wimp_getpointerinfo(tMouseClickBlock* pmcbPointerInfo)
165 {
166  swiRegs.r[1] = (int)pmcbPointerInfo;
167  CallSwi(XWimp_GetPointerInfo);
168  return returnZeroOnError();
169 }
170 
171 /* From wimp_window.h */
172 tWindowHandle wimp_createwindow(const tWindowCreateBlock* kpwcbWindow)
173 {
174  swiRegs.r[1] = (int)kpwcbWindow;
175  CallSwi(XWimp_CreateWindow);
176 
177  return (tWindowHandle)returnMinusOneOnError(swiRegs.r[0]);
178 }
179 
180 int wimp_deletewindow(const tWindowHandle kwhHandle)
181 {
182  iaScratch[0] = (int)kwhHandle;
183  swiRegs.r[1] = (int)&iaScratch;
184  CallSwi(XWimp_DeleteWindow);
185 
186  return returnZeroOnError();
187 }
188 
189 int wimp_openwindow(const tWindowOpenBlock* kpwobOpenBlock)
190 {
191  swiRegs.r[1] = (int)kpwobOpenBlock;
192  CallSwi(XWimp_OpenWindow);
193  return returnZeroOnError();
194 }
195 
196 int wimp_closewindow(const tWindowHandle kwhHandle)
197 {
198  iaScratch[0] = (int)kwhHandle;
199  swiRegs.r[1] = (int)&iaScratch;
200  CallSwi(XWimp_CloseWindow);
201  return returnZeroOnError();
202 }
203 
204 /* From wimp_menu.h */
205 
206 int wimp_createmenu(const tMenu* kpmMenu, tCoordinate cX, tCoordinate cY)
207 {
208  swiRegs.r[1] = (int)kpmMenu;
209  swiRegs.r[2] = (int)cX;
210  swiRegs.r[3] = (int)cY;
211  CallSwi(XWimp_CreateMenu);
212  return returnZeroOnError();
213 }
214 
215 int wimp_closemenu(void)
216 {
217  swiRegs.r[1] = -1;
218  CallSwi(XWimp_CreateMenu);
219  return returnZeroOnError();
220 }
221 
222 /* From wimp_font.h */
223 
224 tCoordinate wimp_strwidth(const char* kpcaText)
225 {
226  swiRegs.r[0] = 1;
227  swiRegs.r[1] = (int)kpcaText;
228  swiRegs.r[2] = 0;
229  if (_kernel_swi(XWimp_TextOp, &swiRegs, &swiRegs) == NULL)
230  {
231  return swiRegs.r[0];
232  }
233 
234  return strlen(kpcaText) * 16;
235 }
236 
237 /* From wimp_icons.h */
238 
239 const tIconHandle wimp_createicon(const tIconCreateBlock* kpicbIcon)
240 {
241  swiRegs.r[1] = (int)kpicbIcon;
242  CallSwi(XWimp_CreateIcon);
243  return returnMinusOneOnError(swiRegs.r[0]);
244 }
245 
246 void wimp_deleteicon(const tWindowHandle kwhWindow, const tIconHandle kihIcon)
247 {
248  iaScratch[0] = (int)kwhWindow;
249  iaScratch[1] = (int)kihIcon;
250  swiRegs.r[1] = (int)iaScratch;
251  CallSwi(XWimp_DeleteIcon);
252  //return returnZeroOnError();
253 }
254 
255 /* From wimp_error.h */
256 int wimp_error(const char* error_string, unsigned int flags)
257 {
258  iaScratch[0] = 0;
259  strcpy((char*)&iaScratch[1], error_string);
260  swiRegs.r[0] = (int)iaScratch;
261  swiRegs.r[1] = flags;
262  swiRegs.r[2] = (int)caTaskName;
263  CallSwi(XWimp_ReportError);
264  return swiRegs.r[0];
265 }