Bringing apdf from vendor into main branch.
[AROS-Contrib.git] / apdf / apdf / notifier.c
blob4ab805e47d66c833bdc2e5bddf91a8298bdc981c
1 /*************************************************************************\
2 * *
3 * notifier.c *
4 * *
5 * Copyright 2001 Emmanuel Lesueur *
6 * *
7 \*************************************************************************/
9 #include "config.h"
10 #include <stdlib.h>
11 #include <dos/dos.h>
12 #include <dos/notify.h>
13 #include <libraries/mui.h>
14 #include <proto/exec.h>
15 #include <proto/muimaster.h>
16 #include <proto/dos.h>
17 #include "notifier.h"
19 #define DB(x) //x
20 #define Static static
23 struct NotifierData {
24 struct NotifyRequest nr;
26 typedef struct NotifierData NotifierData;
28 struct NotifierBaseData {
29 struct MUI_InputHandlerNode ihn;
31 typedef struct NotifierBaseData NotifierBaseData;
33 static struct MsgPort *port;
34 static Object *app;
36 Static ULONG ntfNew(struct IClass *cl,Object *obj,struct opSet *msg) {
37 STRPTR name=(STRPTR)GetTagData(MYATTR_Notifier_Name,NULL,msg->ops_AttrList);
38 if(name) {
39 if(obj=(Object*)DoSuperMethodA(cl,obj,(Msg)msg)) {
40 NotifierData *dat=INST_DATA(cl,obj);
41 dat->nr.nr_Name=(STRPTR)GetTagData(MYATTR_Notifier_Name,NULL,msg->ops_AttrList);
42 dat->nr.nr_Flags=NRF_SEND_MESSAGE;
43 dat->nr.nr_UserData=(ULONG)obj;
44 dat->nr.nr_stuff.nr_Msg.nr_Port=port;
45 StartNotify(&dat->nr);
47 } else
48 obj=NULL;
49 return (ULONG)obj;
52 Static ULONG ntfDispose(struct IClass *cl,Object *obj,Msg msg) {
53 NotifierData *dat=INST_DATA(cl,obj);
54 EndNotify(&dat->nr);
55 return DoSuperMethodA(cl,obj,msg);
58 Static ULONG ntfGet(struct IClass *cl,Object *obj,struct opGet *msg) {
59 NotifierData *dat=INST_DATA(cl,obj);
60 switch(msg->opg_AttrID) {
61 case MYATTR_Notifier_Changed:
62 *msg->opg_Storage=FALSE;
63 return TRUE;
65 return DoSuperMethodA(cl,obj,(APTR)msg);
68 BEGIN_DISPATCHER(ntfDispatcher,cl,obj,msg) {
69 switch(msg->MethodID) {
70 case OM_NEW: return ntfNew (cl,obj,(APTR)msg);
71 case OM_DISPOSE: return ntfDispose (cl,obj,msg);
72 //case OM_SET: return ntfSet (cl,obj,(APTR)msg);
73 case OM_GET: return ntfGet (cl,obj,(APTR)msg);
75 return DoSuperMethodA(cl,obj,msg);
77 END_DISPATCHER(ntfDispatcher)
80 Static ULONG ntbNew(struct IClass *cl,Object *obj,struct opSet *msg) {
81 /*struct MsgPort **/port=CreateMsgPort();
82 if(port && (obj=(Object*)DoSuperMethodA(cl,obj,(Msg)msg))) {
83 NotifierBaseData *dat=INST_DATA(cl,obj);
84 //dat->port=port;
85 dat->ihn.ihn_Object=obj;
86 dat->ihn.ihn_Signals=1<<port->mp_SigBit;
87 dat->ihn.ihn_Flags=0;
88 dat->ihn.ihn_Method=MYM_NotifierBase_Handle;
89 DoMethod3(app,MUIM_Application_AddInputHandler,&dat->ihn);
91 else
92 obj=NULL;
93 return (ULONG)obj;
96 Static ULONG ntbDispose(struct IClass *cl,Object *obj,Msg msg) {
97 NotifierBaseData *dat=INST_DATA(cl,obj);
98 DoMethod3(app,MUIM_Application_RemInputHandler,&dat->ihn);
99 DeleteMsgPort(/*dat->*/port);
100 return DoSuperMethodA(cl,obj,msg);
103 Static ULONG ntbHandle(struct IClass *cl,Object *obj) {
104 struct NotifyMessage *nm;
105 while(nm=(APTR)GetMsg(port)) {
106 Object *o=(APTR)nm->nm_NReq->nr_UserData;
107 ReplyMsg(&nm->nm_ExecMessage);
108 set(o,MYATTR_Notifier_Changed,TRUE);
112 BEGIN_DISPATCHER(ntbDispatcher,cl,obj,msg) {
113 switch(msg->MethodID) {
114 case OM_NEW: return ntbNew (cl,obj,(APTR)msg);
115 case OM_DISPOSE: return ntbDispose (cl,obj,msg);
116 case MYM_NotifierBase_Handle: return ntbHandle (cl,obj);
118 return DoSuperMethodA(cl,obj,msg);
120 END_DISPATCHER(ntbDispatcher)
123 struct MUI_CustomClass *notifier_mcc;
124 static struct MUI_CustomClass *notifierbase_mcc;
125 static int count;
126 static Object *notifier_base;
128 BOOL initNotifier(Object *app1) {
129 app=app1;
130 if(count++==0) {
131 notifierbase_mcc=MUI_CreateCustomClass(NULL,MUIC_Notify,NULL,sizeof(NotifierBaseData),(APTR)&ntbDispatcher);
132 if(notifierbase_mcc) {
133 notifier_base=NewObject(notifierbase_mcc->mcc_Class,NULL,TAG_END);
134 if(notifier_base) {
135 notifier_mcc=MUI_CreateCustomClass(NULL,MUIC_Notify,NULL,sizeof(NotifierData),(APTR)&ntfDispatcher);
136 if (notifier_mcc) {
137 return TRUE;
139 DisposeObject(notifier_base);
141 MUI_DeleteCustomClass(notifierbase_mcc);
143 notifier_base=NULL;
144 count=0;
146 return FALSE;
149 void cleanupNotifier(void) {
150 if(count && !--count) {
151 MUI_DeleteCustomClass(notifier_mcc);
152 DisposeObject(notifier_base);
153 MUI_DeleteCustomClass(notifierbase_mcc);