⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 notifier.3

📁 linux系统下的音频通信
💻 3
📖 第 1 页 / 共 2 页
字号:
force the external event loop to call Tcl.  In this case,\fBTcl_SetMaxBlockTime\fR invokes \fBTcl_SetTimer\fR with the shortestinterval seen since the last call to \fBTcl_DoOneEvent\fR or\fBTcl_ServiceAll\fR..PPIn addition to the generic procedure \fBTcl_SetMaxBlockTime\fR, otherplatform-specific procedures may also be available for\fIsetupProc\fR, if there is additional information needed by\fBTcl_WaitForEvent\fR on that platform.  For example, on Unix systemsthe \fBTcl_CreateFileHandler\fR interface can be used to wait for file events..VE.PPThe second procedure provided by each event source is its checkprocedure, indicated by the \fIcheckProc\fR argument to\fBTcl_CreateEventSource\fR.  \fICheckProc\fR must match thefollowing prototype:.CStypedef void Tcl_EventCheckProc(	ClientData \fIclientData\fR,	int \fIflags\fR);.CEThe arguments to this procedure are the same as those for \fIsetupProc\fR.\fBCheckProc\fR is invoked by \fBTcl_DoOneEvent\fR after it has waitedfor events.  Presumably at least one event source is now prepared toqueue an event.  \fBTcl_DoOneEvent\fR calls each of the event sourcesin turn, so they all have a chance to queue any events that are ready.The check procedure does two things.  First, it must see if any eventshave triggered.  Different event sources do this in different ways..PPIf an event source's check procedure detects an interesting event, itmust add the event to Tcl's event queue.  To do this, the event sourcecalls \fBTcl_QueueEvent\fR.  The \fIevPtr\fR argument is a pointer toa dynamically allocated structure containing the event (see below formore information on memory management issues).  Each event source candefine its own event structure with whatever information is relevantto that event source.  However, the first element of the structuremust be a structure of type \fBTcl_Event\fR, and the address of thisstructure is used when communicating between the event source and therest of the notifier.  A \fBTcl_Event\fR has the following definition:.CStypedef struct Tcl_Event {    Tcl_EventProc *\fIproc\fR;    struct Tcl_Event *\fInextPtr\fR;};.CEThe event source must fill in the \fIproc\fR field ofthe event before calling \fBTcl_QueueEvent\fR.The \fInextPtr\fR is used to link together the events in the queueand should not be modified by the event source..PPAn event may be added to the queue at any of three positions, dependingon the \fIposition\fR argument to \fBTcl_QueueEvent\fR:.IP \fBTCL_QUEUE_TAIL\fR 24Add the event at the back of the queue, so that all other pendingevents will be serviced first.  This is almost always the rightplace for new events..IP \fBTCL_QUEUE_HEAD\fR 24Add the event at the front of the queue, so that it will be servicedbefore all other queued events..IP \fBTCL_QUEUE_MARK\fR 24Add the event at the front of the queue, unless there are otherevents at the front whose position is \fBTCL_QUEUE_MARK\fR;  if so,add the new event just after all other \fBTCL_QUEUE_MARK\fR events.This value of \fIposition\fR is used to insert an ordered sequence ofevents at the front of the queue, such as a series ofEnter and Leave events synthesized during a grab or ungrab operationin Tk..PP.VSWhen it is time to handle an event from the queue (steps 1 and 4above) \fBTcl_ServiceEvent\fR will invoke the \fIproc\fR specified.VEin the first queued \fBTcl_Event\fR structure.\fIProc\fR must match the following prototype:.CStypedef int Tcl_EventProc(	Tcl_Event *\fIevPtr\fR,	int \fIflags\fR);.CEThe first argument to \fIproc\fR is a pointer to the event, which willbe the same as the first argument to the \fBTcl_QueueEvent\fR call thatadded the event to the queue.The second argument to \fIproc\fR is the \fIflags\fR argument for the.VScurrent call to \fBTcl_ServiceEvent\fR;  this is used by the event source.VEto return immediately if its events are not relevant..PPIt is up to \fIproc\fR to handle the event, typically by invokingone or more Tcl commands or C-level callbacks.Once the event source has finished handling the event it returns 1to indicate that the event can be removed from the queue.If for some reason the event source decides that the event cannotbe handled at this time, it may return 0 to indicate that the event.VSshould be deferred for processing later;  in this case \fBTcl_ServiceEvent\fR.VEwill go on to the next event in the queue and attempt to service it.There are several reasons why an event source might defer an event.One possibility is that events of this type are excluded by the\fIflags\fR argument.For example, the file event source will always return 0 if the\fBTCL_FILE_EVENTS\fR bit isn't set in \fIflags\fR.Another example of deferring events happens in Tk if\fBTk_RestrictEvents\fR has been invoked to defer certain kindsof window events..PP.VSWhen \fIproc\fR returns 1, \fBTcl_ServiceEvent\fR will remove theevent from the event queue and free its storage.Note that the storage for an event must be allocated bythe event source (using \fBTcl_Alloc\fR or the Tcl macro \fBckalloc\fR)before calling \fBTcl_QueueEvent\fR, but itwill be freed by \fBTcl_ServiceEvent\fR, not by the event source..PP\fBTcl_DeleteEvents\fR can be used to explicitly remove one or moreevents from the event queue.  \fBTcl_DeleteEvents\fR calls \fIproc\fRfor each event in the queue, deleting those for with the procedurereturns 1.  Events for which the procedure returns 0 are left in thequeue.  \fIProc\fR should match the following prototype:.CStypedef int Tcl_EventDeleteProc(	Tcl_Event *\fIevPtr\fR,	ClientData \fIclientData\fR);.CEThe \fIclientData\fR argument will be the same as the \fIclientData\fRargument to \fBTcl_DeleteEvents\fR; it is typically used to point toprivate information managed by the event source.  The \fIevPtr\fR willpoint to the next event in the queue..VE.SH "CREATING A NEW NOTIFIER".PPThe notifier consists of all the procedures described in this manualentry, plus \fBTcl_DoOneEvent\fR and \fBTcl_Sleep\fR, which are.VSavailable on all platforms, and \fBTcl_CreateFileHandler\fR and\fBTcl_DeleteFileHandler\fR, which are Unix-specific.  Most of theseprocedures are generic, in that they are the same for all notifiers.However, five of the procedures are notifier-dependent:\fBTcl_SetTimer\fR, \fBTcl_Sleep\fR, \fBTcl_WaitForEvent\fR,\fBTcl_CreateFileHandler\fR and \fBTcl_DeleteFileHandler\fR.  Tosupport a new platform or to integrate Tcl with anapplication-specific event loop, you must write new versions of theseprocedures..PP\fBTcl_WaitForEvent\fR is the lowest-level procedure in the notifier;it is responsible for waiting for an ``interesting'' event to occur orfor a given time to elapse.  Before \fBTcl_WaitForEvent\fR is invoked,each of the event sources' setup procedure will have been invoked.The \fItimePtr\fR argument to\fBTcl_WaitForEvent\fR gives the maximum time to block for an event,based on calls to \fBTcl_SetMaxBlockTime\fR made by setup proceduresand on other information (such as the \fBTCL_DONT_WAIT\fR bit in\fIflags\fR)..PPIdeally, \fBTcl_WaitForEvent\fR should only wait for an eventto occur; it should not actually process the event in any way.Later on, theevent sources will process the raw events and create Tcl_Events onthe event queue in their \fIcheckProc\fR procedures.However, on some platforms (such as Windows) this isn't possible;events may be processed in \fBTcl_WaitForEvent\fR, including queuingTcl_Events and more (for example, callbacks for native widgets may beinvoked).  The return value from \fBTcl_WaitForEvent\fR must be either0, 1, or \-1.  On platforms such as Windows where events get processed in\fBTcl_WaitForEvent\fR, a return value of 1 means that there may be moreevents still pending that haven't been processed.  This is a sign to thecaller that it must call \fBTcl_WaitForEvent\fR again if it wants allpending events to be processed. A 0 return value means that calling\fBTcl_WaitForEvent\fR again will not have any effect: either this is aplatform where \fBTcl_WaitForEvent\fR only waits without doing any eventprocessing, or \fBTcl_WaitForEvent\fR knows for sure that there are noadditional events to process (e.g. it returned because the timeelapsed).  Finally, a return value of \-1 means that the event loop isno longer operational and the application should probably unwind andterminate.  Under Windows this happens when a WM_QUIT message is received;under Unix it happens when \fBTcl_WaitForEvent\fR would have waitedforever because there were no active event sources and the timeout wasinfinite..PPIf the notifier will be used with an external event loop, then it mustalso support the \fBTcl_SetTimer\fR interface.  \fBTcl_SetTimer\fR isinvoked by \fBTcl_SetMaxBlockTime\fR whenever the maximum blockingtime has been reduced.  \fBTcl_SetTimer\fR should arrange for theexternal event loop to invoke \fBTcl_ServiceAll\fR after the specifiedinterval even if no events have occurred.  This interface is neededbecause \fBTcl_WaitForEvent\fR isn't invoked when there is an externalevent loop.  If thenotifier will only be used from \fBTcl_DoOneEvent\fR, then\fBTcl_SetTimer\fR need not do anything..PPOn Unix systems, the file event source also needs support from thenotifier.  The file event source consists of the\fBTcl_CreateFileHandler\fR and \fBTcl_DeleteFileHandler\fRprocedures, which are described elsewhere..PPThe \fBTcl_Sleep\fR and \fBTcl_DoOneEvent\fR interfaces are describedelsewhere..PPThe easiest way to create a new notifier is to look at the codefor an existing notifier, such as the files \fBunix/tclUnixNotfy.c\fRor \fBwin/tclWinNotify.c\fR in the Tcl source distribution..SH "EXTERNAL EVENT LOOPS".PPThe notifier interfaces are designed so that Tcl can be embedded intoapplications that have their own private event loops.  In this case,the application does not call \fBTcl_DoOneEvent\fR except in the caseof recursive event loops such as calls to the Tcl commands \fBupdate\fRor \fBvwait\fR.  Most of the time is spent in the external event loopof the application.  In this case the notifier must arrange for theexternal event loop to call back into Tcl when somethinghappens on the various Tcl event sources.  These callbacks shouldarrange for appropriate Tcl events to be placed on the Tcl event queue..PPBecause the external event loop is not calling \fBTcl_DoOneEvent\fR ona regular basis, it is up to the notifier to arrange for\fBTcl_ServiceEvent\fR to be called whenever events are pending on theTcl event queue.  The easiest way to do this is to invoke\fBTcl_ServiceAll\fR at the end of each callback from the externalevent loop.  This will ensure that all of the event sources arepolled, any queued events are serviced, and any pending idle handlersare processed before returning control to the application.  Inaddition, event sources that need to poll for events can call\fBTcl_SetMaxBlockTime\fR to force the external event loop to callTcl even if no events are available on the system event queue..PPAs a side effect of processing events detected in the main externalevent loop, Tcl may invoke \fBTcl_DoOneEvent\fR to start a recursive eventloop in commands like \fBvwait\fR.  \fBTcl_DoOneEvent\fR will invokethe external event loop, which will result in callbacks as describedin the preceding paragraph, which will result in calls to\fBTcl_ServiceAll\fR.  However, in these cases it is undesirable toservice events in \fBTcl_ServiceAll\fR.  Servicing events there isunnecessary because control will immediately return to theexternal event loop and hence to \fBTcl_DoOneEvent\fR, which canservice the events itself.  Furthermore, \fBTcl_DoOneEvent\fR issupposed to service only a single event, whereas \fBTcl_ServiceAll\fRnormally services all pending events.  To handle this situation,\fBTcl_DoOneEvent\fR sets a flag for \fBTcl_ServiceAll\fRthat causes it to return without servicing any events.This flag is called the \fIservice mode\fR;\fBTcl_DoOneEvent\fR restores it to its previous value before it returns..PPIn some cases, however, it may be necessary for \fBTcl_ServiceAll\fRto service eventseven when it has been invoked from \fBTcl_DoOneEvent\fR.  This happenswhen there is yet another recursive event loop invoked via anevent handler called by \fBTcl_DoOneEvent\fR (such as one that ispart of a native widget).  In this case, \fBTcl_DoOneEvent\fR may nothave a chance to service events so \fBTcl_ServiceAll\fR must servicethem all.  Any recursive event loop that calls an external eventloop rather than \fBTcl_DoOneEvent\fR must reset the service mode sothat all events get processed in \fBTcl_ServiceAll\fR.  This is doneby invoking the \fBTcl_SetServiceMode\fR procedure.  If\fBTcl_SetServiceMode\fR is passed \fBTCL_SERVICE_NONE\fR, then callsto \fBTcl_ServiceAll\fR will return immediately without processing anyevents.  If \fBTcl_SetServiceMode\fR is passed \fBTCL_SERVICE_ALL\fR,then calls to \fBTcl_ServiceAll\fR will behave normally.\fBTcl_SetServiceMode\fR returns the previous value of the servicemode, which should be restored when the recursive loop exits.\fBTcl_GetServiceMode\fR returns the current value of the servicemode..VE.SH KEYWORDSevent, notifier, event queue, event sources, file events, timer, idle, service mode

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -