Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1206 lines
28 KiB

  1. //
  2. // Utilities
  3. //
  4. #ifndef _H_UT
  5. #define _H_UT
  6. #define SIZEOF_ARRAY(ar) (sizeof(ar)/sizeof((ar)[0]))
  7. //
  8. // Data types stored in the profile information.
  9. //
  10. #define COM_PROFTYPE_STRING 1L
  11. #define COM_PROFTYPE_INT 2L
  12. #define COM_PROFTYPE_BOOL 3L
  13. #define COM_PROFTYPE_UNKNOWN 4L
  14. #define COM_MAX_SUBKEY 256
  15. #define COM_MAX_BOOL_STRING 5
  16. //
  17. //
  18. // TYPEDEFS
  19. //
  20. //
  21. //
  22. // Priorities for UT_RegisterEventProc()
  23. //
  24. // Event procedures are registered with a priority that affects the order
  25. // that the event procedures are called in.
  26. //
  27. // All event procedures of a given priority are called before event
  28. // procedures of a numerically lower priority.
  29. //
  30. // The priority can be any number between 0 and UT_MAX_PRIORITY
  31. //
  32. // The following values have been defined for specific uses:
  33. // UT_PRIORITY_OBMAN : Used by OBMAN so its client event procedures
  34. // are called before those of the client
  35. // UT_PRIORITY_APPSHARE : Used by the DCShare Core to ensure it sees
  36. // events before 'Normal' event procs.
  37. // UT_PRIORITY_NORMAL : For all cases where the order of callling is
  38. // not important.
  39. // UT_PRIORITY_NETWORK : Used by the Network Layer to free any
  40. // unprocessed network buffers.
  41. // UT_PRIORITY_LAST : Used by the Utility Services to get the
  42. // default event procedure called last
  43. //
  44. //
  45. typedef enum
  46. {
  47. UT_PRIORITY_LAST = 0,
  48. UT_PRIORITY_NETWORK,
  49. UT_PRIORITY_NORMAL,
  50. UT_PRIORITY_APPSHARING,
  51. UT_PRIORITY_OBMAN,
  52. UT_PRIORITY_MAX
  53. } UT_PRIORITY;
  54. typedef UT_PRIORITY * PUT_PRIORITY;
  55. //
  56. // SYSTEM LIMITS
  57. //
  58. //
  59. // Maximum number of event handlers for each task
  60. //
  61. #define UTEVENT_HANDLERS_MAX 4
  62. //
  63. // Maximum number of exit procedures
  64. //
  65. #define UTEXIT_PROCS_MAX 4
  66. //
  67. // The groupware critsects, identified by constant
  68. //
  69. #define UTLOCK_FIRST 0
  70. typedef enum
  71. {
  72. UTLOCK_UT = UTLOCK_FIRST,
  73. UTLOCK_OM, // obman
  74. UTLOCK_AL, // app loader
  75. UTLOCK_T120, // gcc/mcs
  76. UTLOCK_AS, // app sharing
  77. UTLOCK_MAX
  78. }
  79. UTLOCK;
  80. // Event message
  81. #define WM_UTTRIGGER_MSG (WM_APP)
  82. //
  83. // BASEDLIST
  84. //
  85. // This is a list structure with based offsets
  86. //
  87. // next : the next item in the list
  88. // prev : the previous item in the list
  89. //
  90. //
  91. typedef struct tagBASEDLIST
  92. {
  93. DWORD next;
  94. DWORD prev;
  95. }
  96. BASEDLIST;
  97. typedef BASEDLIST FAR * PBASEDLIST;
  98. typedef struct
  99. {
  100. BASEDLIST chain;
  101. void FAR *pData;
  102. }
  103. SIMPLE_LIST, FAR * PSIMPLE_LIST;
  104. //
  105. //
  106. // MACROS
  107. //
  108. //
  109. //
  110. // List handling
  111. // =============
  112. // The common functions support the concept of a doubly linked list of
  113. // objects. Objects can be inserted and removed from specified locations
  114. // in the list.
  115. //
  116. // At start of day the calling application must call COM_BasedListInit with a
  117. // pointer to a private piece of memory for a BASEDLIST structure. The list
  118. // handling will initialise this structure. The application must not
  119. // release this memory while the list is active. (Nor must it release any
  120. // object while it is in a list!)
  121. //
  122. // The list functions can only manage a single list, however the app
  123. // can load objects with multiple lists. Each call to the common list
  124. // functions takes a BASEDLIST pointer as the object handle and if the
  125. // application defines multiple BASEDLIST structures within an object then it
  126. // may manage them through the list functions.
  127. //
  128. //
  129. // List chaining
  130. // =============
  131. // For normal list chaining, we have something like
  132. //
  133. // while (pointer != NULL)
  134. // {
  135. // do something;
  136. // pointer = pointer->next;
  137. // }
  138. //
  139. // When using lists whose elements contain offsets (in this case, relative
  140. // offsets) to the next element, we have to cast to a 32-bit integer before
  141. // we can add the offset. This macro encapsulates this, and the example
  142. // above would be modified as follows to use it:
  143. //
  144. // while (pointer != NULL)
  145. // {
  146. // do something;
  147. // pointer = (TYPE) COM_BasedNextListField(pointer);
  148. // }
  149. //
  150. // Note also that the value returned by the macro is a pointer to a generic
  151. // list object i.e. a PBASEDLIST, and so must be cast back to the
  152. // appropriate type.
  153. //
  154. //
  155. //
  156. // List traversing macros
  157. // ======================
  158. // These macros make use of DC_NEXT and DC_PREV, but also take the type of
  159. // list being traversed in order to return the start pointer of the chained
  160. // structure.
  161. //
  162. // The LIST_FIND macro supports the searching of a list, matching a key
  163. // value to a selected structure element.
  164. //
  165. // The parameters to the macros are as follows:
  166. //
  167. // pHead (type: PBASEDLIST)
  168. // -----
  169. // a pointer the root of the list
  170. //
  171. // pEntry (type: STRUCT FAR * FAR *)
  172. // ------
  173. // a pointer to pointer to structure to chain from
  174. //
  175. // STRUCT (a type name)
  176. // ------
  177. // the type of **pEntry
  178. //
  179. // chain (a field name)
  180. // -----
  181. // the text name of the field in STRUCT which is the link along which
  182. // you wish to traverse
  183. //
  184. // field (a field name)
  185. // -----
  186. // when FINDing, the text name of the field in STRUCT against which
  187. // you wish to match
  188. //
  189. // key (a value, of the same type as STRUCT.field)
  190. // ---
  191. // when FINDing, the value to match against STRUCT.field against
  192. //
  193. //
  194. //
  195. // Offset arithmetic
  196. // =================
  197. // Using offsets within memory blocks, rather than pointers, to refer to
  198. // objects in shared memory (as necessitated by the DC-Groupware shared
  199. // memory architecture) presents certain difficulties. Pointer arithmetic
  200. // in C assumes that addition/subtraction operations involve objects of the
  201. // same type and the offsets are presented as number of units of that
  202. // particular type, rather than number of bytes.
  203. //
  204. // Therefore, pointers must be cast to integers before performing
  205. // arithmetic on them (note that casting the pointers to byte pointers is
  206. // not enough since on segmented architectures C performs bounds checking
  207. // when doing pointer arithmetic which we don't want).
  208. //
  209. // Since this would make for cumbersome code if repeated everywhere, we
  210. // define some useful macros to convert
  211. //
  212. // - an (offset, base) pair to a pointer (OFFSETBASE_TO_PTR)
  213. //
  214. // - a (pointer, base) pair to an offset (PTRBASE_TO_OFFSET)
  215. //
  216. // - a NULL pointer value to an offset(NULLBASE_TO_OFFSET)
  217. //
  218. // The offset calculated is the offset of the first parameter from the
  219. // second. As described above, the pointers passed in must be cast to
  220. // 32-bit unsigned integers first, subtracted to get the offset, and then
  221. // cast to 32-bit signed.
  222. //
  223. // The NULLBASE_TO_OFFSET value gives an offset that after translation back
  224. // to a pointer gives a NULL. This is NOT the same as a NULL offset, since
  225. // this translates back to the base pointer (which is a perfectly valid
  226. // address).
  227. //
  228. //
  229. #define PTRBASE_TO_OFFSET(pObject, pBase) \
  230. (LONG)(((DWORD_PTR)(pObject)) - ((DWORD_PTR)(pBase)))
  231. #define OFFSETBASE_TO_PTR(offset, pBase) \
  232. ((void FAR *) ((DWORD_PTR)(pBase) + (LONG)(offset)))
  233. #define NULLBASE_TO_OFFSET(pBase) \
  234. ((DWORD_PTR) (0L - (LONG_PTR)(pBase)))
  235. __inline BOOL COM_BasedListIsEmpty ( PBASEDLIST pHead )
  236. {
  237. ASSERT((pHead->next == 0 && pHead->prev == 0) ||
  238. (pHead->next != 0 && pHead->prev != 0));
  239. return (pHead->next == 0);
  240. }
  241. __inline void FAR * COM_BasedFieldToStruct ( PBASEDLIST pField, UINT nOffset )
  242. {
  243. return (void FAR *) ((DWORD_PTR)pField - nOffset);
  244. }
  245. __inline PBASEDLIST COM_BasedStructToField ( void FAR * pStruct, UINT nOffset )
  246. {
  247. return (PBASEDLIST) ((DWORD_PTR) pStruct + nOffset);
  248. }
  249. __inline PBASEDLIST COM_BasedNextListField ( PBASEDLIST p )
  250. {
  251. return (PBASEDLIST) OFFSETBASE_TO_PTR(p->next, p);
  252. }
  253. __inline PBASEDLIST COM_BasedPrevListField ( PBASEDLIST p )
  254. {
  255. return (PBASEDLIST) OFFSETBASE_TO_PTR(p->prev, p);
  256. }
  257. void FAR * COM_BasedListNext ( PBASEDLIST pHead, void FAR * pEntry, UINT nOffset );
  258. void FAR * COM_BasedListPrev ( PBASEDLIST pHead, void FAR * pEntry, UINT nOffset );
  259. void FAR * COM_BasedListFirst ( PBASEDLIST pHead, UINT nOffset );
  260. void FAR * COM_BasedListLast ( PBASEDLIST pHead, UINT nOffset );
  261. typedef enum
  262. {
  263. LIST_FIND_FROM_FIRST,
  264. LIST_FIND_FROM_NEXT
  265. }
  266. LIST_FIND_TYPE;
  267. void COM_BasedListFind ( LIST_FIND_TYPE eType,
  268. PBASEDLIST pHead,
  269. void FAR * FAR* ppEntry,
  270. UINT nOffset,
  271. int nOffsetKey,
  272. DWORD_PTR Key,
  273. int cbKeySize );
  274. PSIMPLE_LIST COM_SimpleListAppend ( PBASEDLIST pHead, void FAR * pData );
  275. void FAR * COM_SimpleListRemoveHead ( PBASEDLIST pHead );
  276. //
  277. //
  278. // FUNCTION PROTOTYPES
  279. //
  280. //
  281. //
  282. // API FUNCTION: COM_Rect16sIntersect(...)
  283. //
  284. // DESCRIPTION:
  285. // ============
  286. // Checks whether two TSHR_RECT16s rectangles intersect. Rectangles are
  287. // defined to be inclusive of all edges.
  288. //
  289. // PARAMETERS:
  290. // ===========
  291. // pRect1 : pointer to a TSHR_RECT16 rectangle.
  292. // pRect2 : pointer to a TSHR_RECT16 rectangle.
  293. //
  294. // RETURNS:
  295. // ========
  296. // TRUE - if the rectangles intersect
  297. // FALSE - otherwise.
  298. //
  299. //
  300. __inline BOOL COM_Rect16sIntersect(LPTSHR_RECT16 pRect1, LPTSHR_RECT16 pRect2)
  301. {
  302. if ((pRect1->left > pRect2->right) ||
  303. (pRect1->right < pRect2->left) ||
  304. (pRect1->top > pRect2->bottom) ||
  305. (pRect1->bottom < pRect2->top))
  306. {
  307. return(FALSE);
  308. }
  309. else
  310. {
  311. return(TRUE);
  312. }
  313. }
  314. //
  315. // API FUNCTION: COM_BasedListInit(...)
  316. //
  317. // DESCRIPTION:
  318. // ============
  319. // Initialise a list root.
  320. //
  321. // PARAMETERS:
  322. // ===========
  323. // pListRoot : pointer to the list root.
  324. //
  325. // RETURNS:
  326. // ========
  327. // Nothing.
  328. //
  329. //
  330. __inline void COM_BasedListInit(PBASEDLIST pListRoot)
  331. {
  332. //
  333. // The <next> and <prev> items in a list are the offsets, from the list
  334. // item, of the next and previous list items.
  335. //
  336. // In an empty list, the next item after the root is the root itself,
  337. // so the <next> offset is zero. Likewise for <prev>.
  338. //
  339. pListRoot->next = 0;
  340. pListRoot->prev = 0;
  341. }
  342. //
  343. // API FUNCTION: COM_BasedListInsertBefore(...)
  344. // Inserts an item into a list. To insert an item at the start of a list,
  345. // specify the list root as the <pListLink> parameter.
  346. //
  347. void COM_BasedListInsertBefore(PBASEDLIST pListLink, PBASEDLIST pNewLink);
  348. //
  349. // API FUNCTION: COM_BasedListInsertAfter(...)
  350. // Inserts an item into a list. To insert an item at the start of a list,
  351. // specify the list root as the <pListLink> parameter.
  352. //
  353. //
  354. void COM_BasedListInsertAfter(PBASEDLIST pListLink, PBASEDLIST pNewLink);
  355. //
  356. // API FUNCTION: COM_BasedListRemove(...)
  357. //
  358. // DESCRIPTION:
  359. // ============
  360. // This function removes an item from a list. The item to be removed is
  361. // specified by a pointer to the BASEDLIST structure within the item.
  362. //
  363. // PARAMETERS:
  364. // ===========
  365. // pListLink : pointer to link of the item to be removed.
  366. //
  367. // RETURNS:
  368. // ========
  369. // Nothing.
  370. //
  371. //
  372. void COM_BasedListRemove(PBASEDLIST pListLink);
  373. //
  374. // API FUNCTION: COM_ReadProfInt(...)
  375. //
  376. // DESCRIPTION:
  377. // ============
  378. // This reads a private profile integer from the registry.
  379. //
  380. // PARAMETERS:
  381. // ===========
  382. // pSection : section containing the entry to read.
  383. // pEntry : entry name of integer to retrieve.
  384. // defaultValue : default value to return
  385. // pValue : buffer to return the entry in.
  386. //
  387. // RETURNS:
  388. // ========
  389. // Nothing.
  390. //
  391. //
  392. void COM_ReadProfInt(LPSTR pSection, LPSTR pEntry, int defValue, int * pValue);
  393. //
  394. // API FUNCTION: COM_GetSiteName(...)
  395. //
  396. // DESCRIPTION:
  397. // ============
  398. // Reads the site name out of the system registry.
  399. //
  400. // PARAMETERS:
  401. // ===========
  402. // siteName : pointer to string to fill in with the site name.
  403. // siteNameLen : length of this string.
  404. //
  405. // RETURNS:
  406. // ========
  407. // None
  408. //
  409. //
  410. void COM_GetSiteName(LPSTR siteName, UINT siteNameLen);
  411. #ifndef DLL_DISP
  412. //
  413. // API FUNCTION: DCS_StartThread(...)
  414. //
  415. // DESCRIPTION:
  416. // ============
  417. // Start a new thread of execution
  418. //
  419. // PARAMETERS:
  420. // ===========
  421. // entryFunction : A pointer to the thread entry point.
  422. //
  423. //
  424. BOOL DCS_StartThread(LPTHREAD_START_ROUTINE entryFunction);
  425. #endif // DLL_DISP
  426. #ifndef DLL_DISP
  427. BOOL COMReadEntry(HKEY topLevelKey,
  428. LPSTR pSection,
  429. LPSTR pEntry,
  430. LPSTR pBuffer,
  431. int bufferSize,
  432. ULONG expectedDataType);
  433. #endif // DLL_DISP
  434. #define MAKE_SUBALLOC_PTR(pPool, chunkOffset) OFFSETBASE_TO_PTR(chunkOffset, pPool)
  435. #define MAKE_SUBALLOC_OFFSET(pPool, pChunk) PTRBASE_TO_OFFSET(pChunk, pPool)
  436. //
  437. //
  438. // Return codes - all offset from UT_BASE_RC
  439. //
  440. //
  441. enum
  442. {
  443. UT_RC_OK = UT_BASE_RC,
  444. UT_RC_NO_MEM
  445. };
  446. //
  447. // The maximum number of UT events which we try to process without yielding
  448. //
  449. #define MAX_EVENTS_TO_PROCESS 10
  450. //
  451. //
  452. // Types
  453. //
  454. //
  455. //
  456. // Utility Functions Interface handle
  457. //
  458. typedef struct tagUT_CLIENT * PUT_CLIENT;
  459. #define UTTASK_FIRST 0
  460. typedef enum
  461. {
  462. UTTASK_UI = UTTASK_FIRST,
  463. UTTASK_CMG,
  464. UTTASK_OM,
  465. UTTASK_AL,
  466. UTTASK_DCS,
  467. UTTASK_WB,
  468. UTTASK_MAX
  469. }
  470. UT_TASK;
  471. //
  472. // Event procedure registered by UT_RegisterEvent().
  473. //
  474. // Takes event handler registered data, event number and 2 parameters
  475. // Returns TRUE if event processed
  476. // Returns FALSE if not and event should be passed on to next handler
  477. //
  478. //
  479. typedef BOOL (CALLBACK * UTEVENT_PROC)(LPVOID, UINT, UINT_PTR, UINT_PTR);
  480. //
  481. // Exit procedure
  482. //
  483. typedef void (CALLBACK * UTEXIT_PROC)( LPVOID exitData );
  484. //
  485. // The name of the class used to create UT windows
  486. //
  487. #define UT_WINDOW_CLASS "DCUTWindowClass"
  488. //
  489. // The ID of the timer to use for trigger events.
  490. //
  491. #define UT_DELAYED_TIMER_ID 0x10101010
  492. //
  493. //
  494. // Prototypes
  495. //
  496. //
  497. //
  498. //
  499. // Task routines
  500. //
  501. // UT_WndProc() Subclassing window procedure
  502. // UT_InitTask() Initialise a task
  503. // UT_TermTask() Terminate a task
  504. // UT_RegisterEvent() Register an event handler
  505. // UT_DeregisterEvent() Deregisters an event handler
  506. // UT_RegisterExit() Register an exit routine
  507. // UT_DeregisterExit() Deregister an exit routine
  508. // UT_PostEvent() Send an event to a task
  509. //
  510. //
  511. LRESULT CALLBACK UT_WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  512. BOOL UT_InitTask(UT_TASK task, PUT_CLIENT * pputTask);
  513. //
  514. //
  515. // Overview:
  516. // This registers a task and assigns it a handle.
  517. // All other Utility Functions require this handle to be passed to them.
  518. //
  519. // If a task has already been registered with the same process ID, the
  520. // utilities handle that has already been allocated is returned.
  521. // This is to allows the Utility Functions to be used in the context of
  522. // tasks that DC-SHARE has intercepted the graphics calls for.
  523. //
  524. // Each task is identified by a name.
  525. //
  526. // Parameters:
  527. //
  528. // task
  529. // Unique it for identifying task
  530. //
  531. // pUtHandle (returned)
  532. // Utility Services handle to be used for all calls to the Utility
  533. // Services by this task
  534. //
  535. //
  536. void UT_TermTask(PUT_CLIENT * pputTask);
  537. //
  538. //
  539. // Overview:
  540. // This de-registers a task
  541. // All task resources are freed and the utHandle is released
  542. //
  543. // Parameters:
  544. //
  545. // utHandle
  546. // Utility Functions Handle
  547. //
  548. void UT_RegisterEvent(PUT_CLIENT putTask,
  549. UTEVENT_PROC eventProc,
  550. LPVOID eventData,
  551. UT_PRIORITY priority);
  552. void UT_DeregisterEvent(PUT_CLIENT putTask,
  553. UTEVENT_PROC eventProc,
  554. LPVOID eventData);
  555. void UT_PostEvent(PUT_CLIENT putTaskFrom,
  556. PUT_CLIENT putTaskTo,
  557. UINT delay,
  558. UINT eventNo,
  559. UINT_PTR param1,
  560. UINT_PTR param2);
  561. #define NO_DELAY 0
  562. //
  563. //
  564. // Overview:
  565. // This posts an event to another task.
  566. //
  567. // Parameters:
  568. //
  569. // utHandle
  570. // Utility Functions handle of invoking task
  571. //
  572. // toHandle
  573. // Utility Functions TASK handle of task to post event to
  574. //
  575. // delay
  576. // Delay (in milliseconds) before event is posted
  577. //
  578. // eventNo
  579. // event to be posted (see autevt.h for details of events)
  580. //
  581. // param1
  582. // parameter 1 for event (meaning depends on event)
  583. //
  584. // param2
  585. // parameter 2 for event (meaning depends on event)
  586. //
  587. //
  588. // NOTES:
  589. //
  590. // 1) The delay time is in milliseconds. This may not be supported by
  591. // underlying OS but the setting and checking of the pop time value
  592. // is OS specific.
  593. //
  594. // 2) The posting of events is asynchronous, the delay is simply
  595. // the time before the event is posted. The task the event is
  596. // posted to will receive the event NOT BEFRE this time is up.
  597. //
  598. // 3) If an event is posted with a delay specified, the sending task
  599. // must continue to process messages for the event to be posted
  600. //
  601. void UT_RegisterExit(PUT_CLIENT putTask, UTEXIT_PROC exitProc, LPVOID exitData);
  602. void UT_DeregisterExit(PUT_CLIENT putTask, UTEXIT_PROC exitProc, LPVOID exitData);
  603. //
  604. // Memory routines
  605. // UT_MallocRefCount
  606. // UT_BumpUpRefCount
  607. // UT_FreeRefCount
  608. //
  609. void * UT_MallocRefCount(UINT cbSizeMem, BOOL fZeroMem);
  610. void UT_BumpUpRefCount(void * pMemory);
  611. void UT_FreeRefCount(void ** ppMemory, BOOL fNullOnlyWhenFreed);
  612. // Ref count allocs
  613. typedef struct tagUTREFCOUNTHEADER
  614. {
  615. STRUCTURE_STAMP
  616. UINT refCount;
  617. }
  618. UTREFCOUNTHEADER;
  619. typedef UTREFCOUNTHEADER * PUTREFCOUNTHEADER;
  620. //
  621. // UT_MoveMemory()
  622. // Replacement for CRT memmove(); handles overlapping
  623. //
  624. void * UT_MoveMemory(void * dst, const void * src, size_t count);
  625. //
  626. // Locks
  627. // - UT_Lock() - Locks a lock
  628. // - UT_Unlock() - Unlocks a lock
  629. //
  630. #ifndef DLL_DISP
  631. extern CRITICAL_SECTION g_utLocks[UTLOCK_MAX];
  632. __inline void UT_Lock(UTLOCK lock)
  633. {
  634. ASSERT(lock >= UTLOCK_FIRST);
  635. ASSERT(lock < UTLOCK_MAX);
  636. EnterCriticalSection(&g_utLocks[lock]);
  637. }
  638. __inline void UT_Unlock(UTLOCK lock)
  639. {
  640. ASSERT(lock >= UTLOCK_FIRST);
  641. ASSERT(lock < UTLOCK_MAX);
  642. LeaveCriticalSection(&g_utLocks[lock]);
  643. }
  644. #endif // DLL_DISP
  645. //
  646. // Tasks
  647. // UT_HandleProcessStart()
  648. // UT_HandleProcessEnd()
  649. // UT_HandleThreadEnd()
  650. //
  651. BOOL UT_HandleProcessStart(HINSTANCE hInstance);
  652. void UT_HandleProcessEnd(void);
  653. void UT_HandleThreadEnd(void);
  654. //
  655. // Structure for holding an event. The first two fields allow the event to
  656. // be held on the delayed event Q to be scheduled later.
  657. //
  658. typedef struct tagUTEVENT_INFO
  659. {
  660. STRUCTURE_STAMP
  661. BASEDLIST chain;
  662. // Params
  663. UINT event;
  664. UINT_PTR param1;
  665. UINT_PTR param2;
  666. PUT_CLIENT putTo;
  667. UINT popTime;
  668. }
  669. UTEVENT_INFO;
  670. typedef UTEVENT_INFO * PUTEVENT_INFO;
  671. #ifndef DLL_DISP
  672. void __inline ValidateEventInfo(PUTEVENT_INFO pEventInfo)
  673. {
  674. ASSERT(!IsBadWritePtr(pEventInfo, sizeof(UTEVENT_INFO)));
  675. }
  676. #endif // DLL_DISP
  677. //
  678. // Information held about each exit procedure
  679. //
  680. typedef struct tagUTEXIT_PROC_INFO
  681. {
  682. UTEXIT_PROC exitProc;
  683. LPVOID exitData;
  684. } UTEXIT_PROC_INFO;
  685. typedef UTEXIT_PROC_INFO * PUTEXIT_PROC_INFO;
  686. //
  687. // Information held about each event procedure
  688. //
  689. typedef struct tagUTEVENT_PROC_INFO
  690. {
  691. UTEVENT_PROC eventProc;
  692. LPVOID eventData;
  693. UT_PRIORITY priority;
  694. }
  695. UTEVENT_PROC_INFO;
  696. typedef UTEVENT_PROC_INFO * PUTEVENT_PROC_INFO;
  697. //
  698. //
  699. // UT_CLIENT
  700. //
  701. // Information stored about each Utilities registered task. A pointer to
  702. // this structure is returned as the UT Handle from UT_InitTask(), and is
  703. // passed in as a parameter to subsequent calls to UT.
  704. //
  705. // This structure is allocated in the shared memory bank.
  706. //
  707. // This should be a multiple of 4 bytes to ensure DWORD alignment of the
  708. // allocated memory
  709. //
  710. //
  711. typedef struct tagUT_CLIENT
  712. {
  713. DWORD dwThreadId;
  714. HWND utHwnd; // Window to post UT events to
  715. UTEXIT_PROC_INFO exitProcs[UTEXIT_PROCS_MAX];
  716. // Exit procedures registered for
  717. // this task.
  718. UTEVENT_PROC_INFO eventHandlers[UTEVENT_HANDLERS_MAX];
  719. // Event procedures registered for
  720. // this task.
  721. BASEDLIST pendingEvents; // List of events for this task
  722. // which are ready to be
  723. // processed.
  724. BASEDLIST delayedEvents; // List of delayed events destined
  725. // for this task.
  726. }
  727. UT_CLIENT;
  728. #ifndef DLL_DISP
  729. void __inline ValidateUTClient(PUT_CLIENT putTask)
  730. {
  731. extern UT_CLIENT g_autTasks[UTTASK_MAX];
  732. ASSERT(putTask >= &(g_autTasks[UTTASK_FIRST]));
  733. ASSERT(putTask < &(g_autTasks[UTTASK_MAX]));
  734. ASSERT(putTask->dwThreadId);
  735. }
  736. #endif // DLL_DISP
  737. //
  738. //
  739. // UTTaskEnd(...)
  740. //
  741. // This routine frees all resources associated with the task and
  742. // releases the handle
  743. //
  744. // Parameters:
  745. //
  746. // pTaskData - The Utility Functions handle for the task that is ending
  747. //
  748. //
  749. void UTTaskEnd(PUT_CLIENT putTask);
  750. //
  751. //
  752. // Overview:
  753. // This routine is called to check the status of delayed events and to post
  754. // them to the target process if required.
  755. //
  756. // Parameters:
  757. //
  758. // utHandle
  759. // Utility Functions handle of invoking task
  760. //
  761. // NOTES:
  762. //
  763. // 1) This routine is called periodically or whenever the application
  764. // believes a delayed event has popped.
  765. //
  766. // Return codes: None
  767. //
  768. //
  769. void UTCheckEvents(PUT_CLIENT putTask);
  770. void UTCheckDelayedEvents(PUT_CLIENT putTask);
  771. //
  772. //
  773. // UTProcessEvent(...)
  774. //
  775. // Overview:
  776. // This process an event for the current task
  777. //
  778. //
  779. // Parameters:
  780. //
  781. // utHandle
  782. // Utility Functions Handle
  783. //
  784. // event
  785. // The event to process
  786. //
  787. // param1
  788. // The 1st parameter for the event
  789. //
  790. // param2
  791. // The 2nd parameter for the event
  792. //
  793. //
  794. void UTProcessEvent(PUT_CLIENT putTask, UINT event, UINT_PTR param1, UINT_PTR param2);
  795. //
  796. //
  797. // UTProcessDelayedEvent(...)
  798. //
  799. // A delayed event destined for the current task is ready to be processed.
  800. //
  801. // pTaskData - The current tasks data.
  802. // eventOffset - Offset into the shared memory bank at which the event
  803. // is stored.
  804. //
  805. //
  806. void UTProcessDelayedEvent(PUT_CLIENT putTask, DWORD eventOffset);
  807. //
  808. //
  809. // UTPostImmediateEvt(...)
  810. //
  811. // This function adds an event to a task's pending event queue, and posts
  812. // a trigger event if required.
  813. //
  814. // pSrcTaskData - originating tasks data
  815. // pDestTaskData - destination tasks data
  816. // event - event data
  817. // param1 - parm1
  818. // param2 - parm2
  819. //
  820. //
  821. void UTPostImmediateEvt(PUT_CLIENT putTaskFrom,
  822. PUT_CLIENT putTaskTo,
  823. UINT event,
  824. UINT_PTR param1,
  825. UINT_PTR param2);
  826. //
  827. //
  828. // UTPostDelayedEvt(...)
  829. //
  830. // This function adds an event to a task's delayed event queue, and starts
  831. // a timer (on the destination's task) to get that task to process the
  832. // event when the timer ticks.
  833. //
  834. // pSrcTaskData - originating tasks data
  835. // pDestTaskData - destination tasks data
  836. // delay - the delay (in milliseconds)
  837. // event - event data
  838. // param1 - parm1
  839. // param2 - parm2
  840. //
  841. //
  842. void UTPostDelayedEvt(PUT_CLIENT putTaskFrom,
  843. PUT_CLIENT putTaskTo,
  844. UINT delay,
  845. UINT event,
  846. UINT_PTR param1,
  847. UINT_PTR param2);
  848. //
  849. //
  850. // Overview:
  851. // This posts a event to another task
  852. //
  853. // Parameters:
  854. //
  855. // pSrcTaskInfo - task data for the source task
  856. // pDestTaskInfo - task data for the dest task
  857. //
  858. void UTTriggerEvt(PUT_CLIENT putTaskFrom, PUT_CLIENT putTaskTo);
  859. //
  860. //
  861. // Overview:
  862. // This starts a delayed-event timer for a task.
  863. //
  864. // Parameters:
  865. // pTaskData
  866. // The task data for the task
  867. //
  868. // popTime
  869. // The target time for the timer to pop - this is an OS specific value
  870. // in the same format as that returned by UTPopTime().
  871. //
  872. //
  873. void UTStartDelayedEventTimer(PUT_CLIENT putTask, UINT popTime);
  874. #ifdef __cplusplus
  875. #include <mappedfile.h>
  876. // --------------------------------------------------------------------------
  877. //
  878. // Shared Variables are in the GLOBALDATA structure
  879. //
  880. // --------------------------------------------------------------------------
  881. typedef struct tagGLOBALDATA {
  882. HWND g_asMainWindow;
  883. ATOM g_asHostProp;
  884. HHOOK g_imMouseHook;
  885. char g_osiDriverName[CCHDEVICENAME];
  886. }GLOBALDATA;
  887. // pointer to shared global data
  888. extern GLOBALDATA *g_pGlobalData;
  889. // pointer to mem mapped file handle
  890. extern CMemMappedFile *g_CMappedFile;
  891. // size of global data memory mapped file
  892. const int c_cbGlobalData = sizeof(GLOBALDATA);
  893. // name of memory mapped file
  894. const TCHAR c_szMappedFileName[] = TEXT("AppshareHookShared");
  895. // mutex to access mem mapped file and wait time
  896. const TCHAR c_szMutex[] = TEXT("AppshareHookMutex");
  897. const int c_nMutexWait = 5000;
  898. __inline BOOL CreateMappedFile()
  899. {
  900. g_CMappedFile = new CMemMappedFile;
  901. if (g_CMappedFile)
  902. {
  903. if (g_CMappedFile->Open(c_szMappedFileName, c_cbGlobalData))
  904. {
  905. CScopeMutex csMutex;
  906. if (csMutex.Create(c_szMutex, c_nMutexWait))
  907. {
  908. g_CMappedFile->AccessMem((void **)&g_pGlobalData);
  909. if (g_CMappedFile->FirstOpen())
  910. {
  911. memset(g_pGlobalData, 0, c_cbGlobalData);
  912. }
  913. return TRUE;
  914. }
  915. }
  916. }
  917. return FALSE;
  918. }
  919. __inline void CloseMappedFile()
  920. {
  921. if (g_CMappedFile)
  922. {
  923. g_CMappedFile->Close();
  924. delete g_CMappedFile;
  925. g_CMappedFile = 0;
  926. }
  927. }
  928. __inline HWND GetAsMainWindow()
  929. {
  930. if(g_pGlobalData)
  931. {
  932. return g_pGlobalData->g_asMainWindow;
  933. }
  934. else
  935. {
  936. return NULL;
  937. }
  938. }
  939. __inline BOOL SetAsMainWindow(HWND hwnd)
  940. {
  941. if(g_pGlobalData)
  942. {
  943. g_pGlobalData->g_asMainWindow = hwnd;
  944. return TRUE;
  945. }
  946. else
  947. {
  948. return FALSE;
  949. }
  950. }
  951. __inline ATOM GetAsHostProp()
  952. {
  953. if(g_pGlobalData)
  954. {
  955. return g_pGlobalData->g_asHostProp;
  956. }
  957. else
  958. {
  959. return NULL;
  960. }
  961. }
  962. __inline BOOL SetAsHostProp(ATOM atom)
  963. {
  964. if(g_pGlobalData)
  965. {
  966. g_pGlobalData->g_asHostProp = atom;
  967. return TRUE;
  968. }
  969. else
  970. {
  971. return FALSE;
  972. }
  973. }
  974. __inline const char * GetOsiDriverName()
  975. {
  976. if(g_pGlobalData)
  977. {
  978. return g_pGlobalData->g_osiDriverName;
  979. }
  980. else
  981. {
  982. return NULL;
  983. }
  984. }
  985. __inline BOOL SetOsiDriverName(LPCSTR szDriverName)
  986. {
  987. if(g_pGlobalData &&
  988. szDriverName &&
  989. (lstrlen(szDriverName) <= SIZEOF_ARRAY(g_pGlobalData->g_osiDriverName)))
  990. {
  991. lstrcpy(g_pGlobalData->g_osiDriverName,szDriverName);
  992. return TRUE;
  993. }
  994. else
  995. {
  996. return FALSE;
  997. }
  998. }
  999. __inline HHOOK GetImMouseHook()
  1000. {
  1001. if(g_pGlobalData)
  1002. {
  1003. return g_pGlobalData->g_imMouseHook;
  1004. }
  1005. else
  1006. {
  1007. return NULL;
  1008. }
  1009. }
  1010. __inline BOOL SetImMouseHook(HHOOK hook)
  1011. {
  1012. if(g_pGlobalData)
  1013. {
  1014. g_pGlobalData->g_imMouseHook = hook;
  1015. return TRUE;
  1016. }
  1017. else
  1018. {
  1019. return FALSE;
  1020. }
  1021. }
  1022. #endif // __cplusplus
  1023. #endif // _H_UT