revert between 56095 -> 55830 in arch
[AROS.git] / rom / usb / classes / hid / hidctrl.gui.c
blobf4ac06cf9f3ba5ae8e810b053cbdf0ceb5d28950
1 /*
2 * GUI
3 */
5 #include "debug.h"
6 #include "numtostr.h"
8 #include "hid.class.h"
10 extern const STRPTR GM_UNIQUENAME(libname);
12 /* /// "nHIDCtrlGUITask()" */
13 AROS_UFH0(void, GM_UNIQUENAME(nHIDCtrlGUITask))
15 AROS_USERFUNC_INIT
17 struct Task *thistask;
18 struct NepHidBase *nh;
19 struct NepClassHid *nch;
21 thistask = FindTask(NULL);
23 #undef ps
24 #define ps nch->nch_HCPsdBase
25 #undef IntuitionBase
26 #define IntuitionBase nch->nch_HCIntBase
27 #undef MUIMasterBase
28 #define MUIMasterBase nch->nch_HCMUIBase
30 nch = thistask->tc_UserData;
31 nch->nch_HCGUITask = thistask;
32 nh = nch->nch_ClsBase;
34 ++nh->nh_Library.lib_OpenCnt;
35 NewList(&nch->nch_HCGUIItems);
36 if(!(MUIMasterBase = OpenLibrary(MUIMASTER_NAME, MUIMASTER_VMIN)))
38 KPRINTF(10, ("Couldn't open muimaster.library.\n"));
39 GM_UNIQUENAME(nHIDCtrlGUITaskCleanup)(nch);
40 return;
43 if(!(IntuitionBase = OpenLibrary("intuition.library", 39)))
45 KPRINTF(10, ("Couldn't open intuition.library.\n"));
46 GM_UNIQUENAME(nHIDCtrlGUITaskCleanup)(nch);
47 return;
50 if(!(ps = OpenLibrary("poseidon.library", 4)))
52 KPRINTF(10, ("Couldn't open poseidon.library.\n"));
53 GM_UNIQUENAME(nHIDCtrlGUITaskCleanup)(nch);
54 return;
57 nch->nch_HCActionClass = MUI_CreateCustomClass(NULL, MUIC_Area , NULL, sizeof(struct ActionData), GM_UNIQUENAME(HCActionDispatcher));
59 if(!nch->nch_HCActionClass)
61 KPRINTF(10, ("Couldn't create ActionClass.\n"));
62 GM_UNIQUENAME(nHIDCtrlGUITaskCleanup)(nch);
63 return;
65 nch->nch_HCApp = ApplicationObject,
66 MUIA_Application_Title , (IPTR)nch->nch_CDC->cdc_HIDCtrlTitle,
67 MUIA_Application_Version , (IPTR)VERSION_STRING,
68 MUIA_Application_Copyright , (IPTR)"©2002-2009 Chris Hodges",
69 MUIA_Application_Author , (IPTR)"Chris Hodges <chrisly@platon42.de>",
70 MUIA_Application_Description, (IPTR)"HID Device Output Control",
71 MUIA_Application_Base , (IPTR)nch->nch_CDC->cdc_HIDCtrlRexx,
72 MUIA_Application_HelpFile , (IPTR)"HELP:Poseidon.guide",
73 MUIA_Application_Menustrip , (IPTR)MenustripObject,
74 Child, (IPTR)MenuObjectT((IPTR)"Project"),
75 Child, (IPTR)(nch->nch_HCAboutMI = MenuitemObject,
76 MUIA_Menuitem_Title, (IPTR)"About...",
77 MUIA_Menuitem_Shortcut, (IPTR)"?",
78 End),
79 Child, (IPTR)MenuitemObject,
80 MUIA_Menuitem_Title, (IPTR)NM_BARLABEL,
81 End,
82 Child, (IPTR)(nch->nch_HCCloseMI = MenuitemObject,
83 MUIA_Menuitem_Title, (IPTR)"Hide",
84 MUIA_Menuitem_Shortcut, (IPTR)"H",
85 End),
86 End,
87 Child, (IPTR)MenuObjectT((IPTR)"Settings"),
88 Child, (IPTR)(nch->nch_HCMUIPrefsMI = MenuitemObject,
89 MUIA_Menuitem_Title, (IPTR)"MUI Settings",
90 MUIA_Menuitem_Shortcut, (IPTR)"M",
91 End),
92 End,
93 End,
95 SubWindow, (IPTR)(nch->nch_HCMainWindow = WindowObject,
96 MUIA_Window_ID , MAKE_ID('H','C','T','L'),
97 MUIA_Window_Title, (IPTR)nch->nch_CDC->cdc_HIDCtrlTitle,
98 MUIA_HelpNode, (IPTR)GM_UNIQUENAME(libname),
100 WindowContents, (IPTR)VGroup,
101 Child, (IPTR)(nch->nch_HCActionObj = NewObject(nch->nch_HCActionClass->mcc_Class, 0, MUIA_ShowMe, FALSE, TAG_END)),
102 Child, (IPTR)(nch->nch_HCGroupObj = ColGroup(4),
103 End),
104 Child, (IPTR)(nch->nch_HCCloseObj = TextObject, ButtonFrame,
105 MUIA_Background, MUII_ButtonBack,
106 MUIA_CycleChain, 1,
107 MUIA_InputMode, MUIV_InputMode_RelVerify,
108 MUIA_Text_Contents, (IPTR)"\33c Hide ",
109 End),
110 End,
111 End),
112 End;
114 if(!nch->nch_HCApp)
116 KPRINTF(10, ("Couldn't create application\n"));
117 GM_UNIQUENAME(nHIDCtrlGUITaskCleanup)(nch);
118 return;
122 struct ActionData *ad = INST_DATA(nch->nch_HCActionClass->mcc_Class, nch->nch_HCActionObj);
123 ad->ad_NCH = nch;
125 /* add items */
127 struct NepHidReport *nhr;
128 struct NepHidItem **nhiptr;
129 struct NepHidItem *nhi;
130 struct NepHidGItem *nhgi;
131 UWORD count;
132 Object *obj;
133 UWORD numobj = 0;
135 nhr = (struct NepHidReport *) nch->nch_HidReports.lh_Head;
136 while(nhr->nhr_Node.ln_Succ)
138 if((count = nhr->nhr_OutItemCount))
140 nhiptr = nhr->nhr_OutItemMap;
141 if(nhiptr)
145 nhi = *nhiptr++;
146 obj = NULL;
147 nhgi = NULL;
148 if(nhi->nhi_Flags & RPF_MAIN_VARIABLE)
150 if(nhi->nhi_Flags & RPF_MAIN_RELATIVE)
152 if((nhi->nhi_LogicalMin == 0) && (nhi->nhi_LogicalMax == 1))
154 if((nhgi = nAllocGHCItem(nch, nhi, NULL, nhi->nhi_Usage)))
156 /* one shot */
157 obj = VGroup,
158 Child, (IPTR)VSpace(0),
159 Child, (IPTR)(nhgi->nhgi_GUIObj = TextObject, ButtonFrame,
160 MUIA_Background, MUII_ButtonBack,
161 MUIA_CycleChain, 1,
162 MUIA_InputMode, MUIV_InputMode_RelVerify,
163 MUIA_Text_PreParse, (IPTR)"\33c",
164 MUIA_Text_Contents, (IPTR)nhgi->nhgi_Name,
165 End),
166 Child, (IPTR)VSpace(0),
167 End;
168 nhgi->nhgi_ObjType = NHGIOT_SHOTBUTTON;
171 else if(nhi->nhi_LogicalMin < 0)
173 if((nhgi = nAllocGHCItem(nch, nhi, NULL, nhi->nhi_Usage)))
175 /* Horizontal slider */
176 obj = VGroup,
177 Child, (IPTR)VSpace(0),
178 Child, (IPTR)HGroup,
179 Child, (IPTR)Label((IPTR)nhgi->nhgi_Name),
180 Child, (IPTR)(nhgi->nhgi_GUIObj = SliderObject, SliderFrame,
181 MUIA_Slider_Horiz, TRUE,
182 MUIA_CycleChain, 1,
183 MUIA_InputMode, MUIV_InputMode_Immediate,
184 MUIA_Numeric_Min, nhi->nhi_LogicalMin,
185 MUIA_Numeric_Max, nhi->nhi_LogicalMax,
186 MUIA_Numeric_Value, nhi->nhi_OldValue,
187 End),
188 End,
189 Child, (IPTR)VSpace(0),
190 End;
191 nhgi->nhgi_ObjType = NHGIOT_SLIDERIMM;
193 } else {
194 if((nhgi = nAllocGHCItem(nch, nhi, NULL, nhi->nhi_Usage)))
196 /* Vertical slider */
197 obj = HGroup,
198 Child, (IPTR)HSpace(0),
199 Child, (IPTR)VGroup,
200 Child, (IPTR)(nhgi->nhgi_GUIObj = SliderObject, SliderFrame,
201 MUIA_Slider_Horiz, FALSE,
202 MUIA_CycleChain, 1,
203 MUIA_InputMode, MUIV_InputMode_Immediate,
204 MUIA_Numeric_Min, nhi->nhi_LogicalMin,
205 MUIA_Numeric_Max, nhi->nhi_LogicalMax,
206 MUIA_Numeric_Value, nhi->nhi_OldValue,
207 End),
208 Child, (IPTR)Label((IPTR)nhgi->nhgi_Name),
209 End,
210 Child, (IPTR)HSpace(0),
211 End;
212 nhgi->nhgi_ObjType = NHGIOT_SLIDERIMM;
215 } else {
216 if((nhi->nhi_LogicalMin == 0) && (nhi->nhi_LogicalMax == 1))
218 if((nhgi = nAllocGHCItem(nch, nhi, NULL, nhi->nhi_Usage)))
220 /* toggle button */
221 obj = VGroup,
222 Child, (IPTR)VSpace(0),
223 Child, (IPTR)(nhgi->nhgi_GUIObj = TextObject, ButtonFrame,
224 MUIA_Background, MUII_ButtonBack,
225 MUIA_CycleChain, 1,
226 MUIA_InputMode, MUIV_InputMode_Toggle,
227 MUIA_Text_PreParse, (IPTR)"\33c",
228 MUIA_Text_Contents, (IPTR)nhgi->nhgi_Name,
229 End),
230 Child, (IPTR)VSpace(0),
231 End;
232 nhgi->nhgi_ObjType = NHGIOT_TOGGLEBUTTON;
235 else if(nhi->nhi_LogicalMin < 0)
237 if((nhgi = nAllocGHCItem(nch, nhi, NULL, nhi->nhi_Usage)))
239 /* Horizontal slider */
240 obj = VGroup,
241 Child, (IPTR)VSpace(0),
242 Child, (IPTR)HGroup,
243 Child, (IPTR)Label((IPTR)nhgi->nhgi_Name),
244 Child, (IPTR)(nhgi->nhgi_GUIObj = SliderObject, SliderFrame,
245 MUIA_Slider_Horiz, TRUE,
246 MUIA_CycleChain, 1,
247 MUIA_Numeric_Min, nhi->nhi_LogicalMin,
248 MUIA_Numeric_Max, nhi->nhi_LogicalMax,
249 MUIA_Numeric_Value, nhi->nhi_OldValue,
250 End),
251 End,
252 Child, (IPTR)VSpace(0),
253 End;
254 nhgi->nhgi_ObjType = NHGIOT_SLIDER;
256 } else {
257 if((nhgi = nAllocGHCItem(nch, nhi, NULL, nhi->nhi_Usage)))
259 /* Vertical slider */
260 obj = HGroup,
261 Child, (IPTR)HSpace(0),
262 Child, (IPTR)VGroup,
263 Child, (IPTR)(nhgi->nhgi_GUIObj = SliderObject, SliderFrame,
264 MUIA_Slider_Horiz, FALSE,
265 MUIA_CycleChain, 1,
266 MUIA_Numeric_Min, nhi->nhi_LogicalMin,
267 MUIA_Numeric_Max, nhi->nhi_LogicalMax,
268 MUIA_Numeric_Value, nhi->nhi_OldValue,
269 End),
270 Child, (IPTR)Label(nhgi->nhgi_Name),
271 End,
272 Child, (IPTR)HSpace(0),
273 End;
274 nhgi->nhgi_ObjType = NHGIOT_SLIDER;
279 if(obj)
281 DoMethod(nch->nch_HCGroupObj, OM_ADDMEMBER, obj);
282 switch(nhgi->nhgi_ObjType)
284 case NHGIOT_SHOTBUTTON:
285 DoMethod(nhgi->nhgi_GUIObj, MUIM_Notify, MUIA_Pressed, FALSE,
286 nch->nch_HCActionObj, 2, MUIM_Action_UpdateHIDCtrl, nhgi);
287 break;
289 case NHGIOT_TOGGLEBUTTON:
290 DoMethod(nhgi->nhgi_GUIObj, MUIM_Notify, MUIA_Selected, MUIV_EveryTime,
291 nch->nch_HCActionObj, 2, MUIM_Action_UpdateHIDCtrl, nhgi);
292 break;
294 case NHGIOT_SLIDERIMM:
295 DoMethod(nhgi->nhgi_GUIObj, MUIM_Notify, MUIA_Numeric_Value, MUIV_EveryTime,
296 nch->nch_HCActionObj, 2, MUIM_Action_UpdateHIDCtrl, nhgi);
297 break;
299 case NHGIOT_SLIDER:
300 DoMethod(nhgi->nhgi_GUIObj, MUIM_Notify, MUIA_Numeric_Value, MUIV_EveryTime,
301 nch->nch_HCActionObj, 2, MUIM_Action_UpdateHIDCtrl, nhgi);
302 break;
304 numobj++;
306 } while(--count);
309 nhr = (struct NepHidReport *) nhr->nhr_Node.ln_Succ;
311 if(!numobj)
313 DoMethod(nch->nch_HCGroupObj, OM_ADDMEMBER, Label("No output items in this interface!"));
317 DoMethod(nch->nch_HCMainWindow, MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
318 nch->nch_HCActionObj, 1, MUIM_Action_HideHIDControl);
319 DoMethod(nch->nch_HCCloseObj, MUIM_Notify, MUIA_Pressed, FALSE,
320 nch->nch_HCActionObj, 1, MUIM_Action_HideHIDControl);
322 DoMethod(nch->nch_HCAboutMI, MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
323 nch->nch_HCActionObj, 1, MUIM_Action_About);
324 DoMethod(nch->nch_HCCloseMI, MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
325 nch->nch_HCActionObj, 1, MUIM_Action_HideHIDControl);
326 DoMethod(nch->nch_HCMUIPrefsMI, MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
327 nch->nch_HCApp, 2, MUIM_Application_OpenConfigWindow, 0);
330 IPTR isopen = 0;
331 IPTR iconify = 0;
332 ULONG sigs;
333 ULONG sigmask;
334 LONG retid;
336 if(nch->nch_CDC->cdc_HIDCtrlOpen)
338 get(nch->nch_HCApp, MUIA_Application_Iconified, &iconify);
339 set(nch->nch_HCMainWindow, MUIA_Window_Open, TRUE);
340 get(nch->nch_HCMainWindow, MUIA_Window_Open, &isopen);
341 if(!(isopen || iconify))
343 GM_UNIQUENAME(nHIDCtrlGUITaskCleanup)(nch);
344 return;
347 sigmask = 0;
350 retid = DoMethod(nch->nch_HCApp, MUIM_Application_NewInput, &sigs);
351 if(sigs)
353 sigs = Wait(sigs | sigmask | SIGBREAKF_CTRL_C);
354 if(sigs & SIGBREAKF_CTRL_C)
356 break;
359 } while(retid != MUIV_Application_ReturnID_Quit);
360 set(nch->nch_HCMainWindow, MUIA_Window_Open, FALSE);
362 GM_UNIQUENAME(nHIDCtrlGUITaskCleanup)(nch);
364 AROS_USERFUNC_EXIT
366 /* \\\ */
368 /* /// "nHIDCtrlGUITaskCleanup()" */
369 void GM_UNIQUENAME(nHIDCtrlGUITaskCleanup)(struct NepClassHid *nch)
371 struct NepHidGItem *nhgi;
372 if(nch->nch_HCApp)
374 MUI_DisposeObject(nch->nch_HCApp);
375 nch->nch_HCApp = NULL;
376 nch->nch_HCActionObj = NULL;
379 nhgi = (struct NepHidGItem *) nch->nch_HCGUIItems.lh_Head;
380 while(nhgi->nhgi_Node.ln_Succ)
382 Remove(&nhgi->nhgi_Node);
383 psdFreeVec(nhgi->nhgi_Name);
384 psdFreeVec(nhgi);
385 nhgi = (struct NepHidGItem *) nch->nch_HCGUIItems.lh_Head;
387 if(nch->nch_HCActionClass)
389 MUI_DeleteCustomClass(nch->nch_HCActionClass);
390 nch->nch_HCActionClass = NULL;
393 if(MUIMasterBase)
395 CloseLibrary(MUIMasterBase);
396 MUIMasterBase = NULL;
398 if(IntuitionBase)
400 CloseLibrary(IntuitionBase);
401 IntuitionBase = NULL;
403 if(ps)
405 CloseLibrary(ps);
406 ps = NULL;
408 Forbid();
409 nch->nch_HCGUITask = NULL;
410 if(nch->nch_ReadySigTask)
412 Signal(nch->nch_ReadySigTask, 1L<<nch->nch_ReadySignal);
414 --nch->nch_ClsBase->nh_Library.lib_OpenCnt;
416 /* \\\ */
418 /* /// "nGetGHCUsageName()" */
419 STRPTR nGetGHCUsageName(struct NepClassHid *nch, ULONG uid)
421 STRPTR uname;
423 uname = nNumToStr(nch, NTS_USAGEID, uid, NULL);
424 if(uname)
426 return(psdCopyStr(uname));
428 uname = nNumToStr(nch, NTS_USAGEPAGE, uid>>16, "unknown");
429 return(psdCopyStrFmt("%s (0x%lx)", uname, uid));
431 /* \\\ */
433 /* /// "nAllocGHCItem()" */
434 struct NepHidGItem * nAllocGHCItem(struct NepClassHid *nch, struct NepHidItem *nhi, struct List *actionlist, ULONG usageid)
436 struct NepHidGItem *nhgi;
438 if(!(nhgi = psdAllocVec(sizeof(struct NepHidGItem))))
440 return(NULL);
442 nhgi->nhgi_Item = nhi;
443 nhgi->nhgi_ActionList = actionlist;
444 if(usageid)
446 nhgi->nhgi_Name = nGetGHCUsageName(nch, usageid);
448 AddTail(&nch->nch_HCGUIItems, &nhgi->nhgi_Node);
449 return(nhgi);
451 /* \\\ */
453 /* /// "HCActionDispatcher()" */
454 AROS_UFH3(IPTR, GM_UNIQUENAME(HCActionDispatcher),
455 AROS_UFHA(struct IClass *, cl, A0),
456 AROS_UFHA(Object *, obj, A2),
457 AROS_UFHA(Msg, msg, A1))
459 AROS_USERFUNC_INIT
461 struct ActionData *ad = (struct ActionData *) 0xABADCAFE;
462 struct NepClassHid *nch = NULL;
463 if(msg->MethodID != OM_NEW)
465 ad = INST_DATA(cl, obj);
466 nch = ad->ad_NCH;
468 switch(msg->MethodID)
470 case OM_NEW:
471 if(!(obj = (Object *) DoSuperMethodA(cl,obj,msg)))
473 return(0);
475 return (IPTR)obj;
477 case MUIM_Action_UpdateHIDCtrl:
479 struct NepHidGItem *nhgi = (struct NepHidGItem *) ((struct opSet *) msg)->ops_AttrList;
480 switch(nhgi->nhgi_ObjType)
482 case NHGIOT_SHOTBUTTON:
483 nhgi->nhgi_Item->nhi_OldValue = 1;
484 nhgi->nhgi_Item->nhi_Collection->nhc_Report->nhr_OutTouched = TRUE;
485 Signal(nch->nch_Task, (1L<<nch->nch_TaskMsgPort->mp_SigBit));
486 while(nhgi->nhgi_Item->nhi_Collection->nhc_Report->nhr_OutTouched)
488 psdDelayMS(10);
490 nhgi->nhgi_Item->nhi_OldValue = 0;
491 nhgi->nhgi_Item->nhi_Collection->nhc_Report->nhr_OutTouched = TRUE;
492 nch->nch_OutFeatTouched = TRUE;
493 Signal(nch->nch_Task, (1L<<nch->nch_TaskMsgPort->mp_SigBit));
494 break;
496 case NHGIOT_TOGGLEBUTTON:
497 get(nhgi->nhgi_GUIObj, MUIA_Selected, &nhgi->nhgi_Item->nhi_OldValue);
498 nhgi->nhgi_Item->nhi_Collection->nhc_Report->nhr_OutTouched = TRUE;
499 nch->nch_OutFeatTouched = TRUE;
500 Signal(nch->nch_Task, (1L<<nch->nch_TaskMsgPort->mp_SigBit));
501 break;
503 case NHGIOT_SLIDER:
504 case NHGIOT_SLIDERIMM:
505 get(nhgi->nhgi_GUIObj, MUIA_Numeric_Value, &nhgi->nhgi_Item->nhi_OldValue);
506 nhgi->nhgi_Item->nhi_Collection->nhc_Report->nhr_OutTouched = TRUE;
507 nch->nch_OutFeatTouched = TRUE;
508 Signal(nch->nch_Task, (1L<<nch->nch_TaskMsgPort->mp_SigBit));
509 break;
511 return(TRUE);
514 case MUIM_Action_About:
515 MUI_RequestA(nch->nch_HCApp, nch->nch_HCMainWindow, 0, NULL, "Blimey!", "HID Output Control Window", NULL);
516 return(TRUE);
518 case MUIM_Action_ShowHIDControl:
519 set(nch->nch_HCMainWindow, MUIA_Window_Open, TRUE);
520 return(TRUE);
522 case MUIM_Action_HideHIDControl:
523 set(nch->nch_HCMainWindow, MUIA_Window_Open, FALSE);
524 return(TRUE);
526 return(DoSuperMethodA(cl,obj,msg));
528 AROS_USERFUNC_EXIT
530 /* \\\ */