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.

420 lines
8.8 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 1994-1995 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: dci.c
  6. * Content: 16-bit DCI code
  7. * This was cut from DCIMAN to provide basic DCI services when
  8. * we don't have DirectDraw drivers
  9. * History:
  10. * Date By Reason
  11. * ==== == ======
  12. * 19-jun-95 craige split out of DCIMAN.C, tweaked
  13. * 31-jul-95 craige added DCIIsBanked
  14. * 13-may-96 colinmc Bug 21192: DCI HDC being freed erroneously due to
  15. * process termination
  16. *
  17. ***************************************************************************/
  18. #define _INC_DCIDDI
  19. #include "ddraw16.h"
  20. #undef _INC_DCIDDI
  21. UINT wFlatSel;
  22. LPVOID pWin16Lock;
  23. #undef WINAPI
  24. #define WINAPI FAR PASCAL _loadds
  25. #include "dciman.h"
  26. static char szDISPLAY[] = "display";
  27. /*
  28. * define some types so we dont go insane, these structures are exactly the
  29. * same (dont need repacked) but it is nice to have different types
  30. * so we can read the code
  31. */
  32. typedef LPDCISURFACEINFO LPDCISURFACEINFO16;
  33. typedef LPDCISURFACEINFO LPDCISURFACEINFO32;
  34. typedef LPDCIOFFSCREEN LPDCIOFFSCREEN16;
  35. typedef LPDCIOFFSCREEN LPDCIOFFSCREEN32;
  36. typedef LPDCIOVERLAY LPDCIOVERLAY16;
  37. typedef LPDCIOVERLAY LPDCIOVERLAY32;
  38. #define PDCI16(pdci32) (LPDCISURFACEINFO16)(((LPDCISURFACEINFO32)(pdci32))->dwReserved2)
  39. extern HINSTANCE hInstApp;
  40. /*
  41. * DCIOpenProvider
  42. *
  43. * only open the DISPLAY driver.
  44. */
  45. HDC WINAPI DCIOpenProvider(void)
  46. {
  47. HDC hdc;
  48. UINT u;
  49. u = SetErrorMode(SEM_NOOPENFILEERRORBOX);
  50. hdc = CreateDC( szDISPLAY, NULL, NULL, NULL );
  51. SetErrorMode(u);
  52. /*
  53. * now check for the Escape
  54. */
  55. if (hdc)
  56. {
  57. u = DCICOMMAND;
  58. if( Escape(hdc, QUERYESCSUPPORT,sizeof(u),(LPCSTR)&u,NULL) == 0 )
  59. {
  60. /*
  61. * driver does not do escape, punt it.
  62. */
  63. DeleteDC(hdc);
  64. hdc = NULL;
  65. }
  66. }
  67. if (hdc)
  68. {
  69. /*
  70. * Reparent it to prevent it going away when the app. dies.
  71. */
  72. SetObjectOwner(hdc, hInstApp);
  73. }
  74. return hdc;
  75. } /* DCIOpenProvider */
  76. /*
  77. * DCICloseProvider
  78. */
  79. void WINAPI DCICloseProvider(HDC hdc)
  80. {
  81. if( hdc )
  82. {
  83. DeleteDC(hdc);
  84. }
  85. } /* DCICloseProvider */
  86. /*
  87. * dciSendCommand
  88. */
  89. static int dciSendCommand(
  90. HDC hdc,
  91. VOID FAR *pcmd,
  92. int nSize,
  93. VOID FAR * FAR * lplpOut )
  94. {
  95. if( lplpOut )
  96. {
  97. *lplpOut = NULL;
  98. }
  99. return Escape( hdc, DCICOMMAND, nSize, (LPCSTR)pcmd, lplpOut );
  100. } /* dciSendCommand */
  101. /*
  102. * DCICreatePrimary
  103. */
  104. int WINAPI DCICreatePrimary(HDC hdc, LPDCISURFACEINFO FAR *lplpSurface)
  105. {
  106. DCICREATEINPUT ci;
  107. DCIRVAL err;
  108. HDC hdcScreen;
  109. ci.cmd.dwCommand = (DWORD)DCICREATEPRIMARYSURFACE;
  110. ci.cmd.dwParam1 = 0;
  111. ci.cmd.dwParam2 = 0;
  112. ci.cmd.dwVersion = (DWORD)DCI_VERSION;
  113. ci.cmd.dwReserved = 0;
  114. ci.dwDCICaps = DCI_PRIMARY | DCI_VISIBLE;
  115. DPF( 4, "DCICreatePrimary" );
  116. /*
  117. * for the primary surface we always use the display driver over
  118. * a external provider.
  119. */
  120. hdcScreen = GetDC( NULL );
  121. err = dciSendCommand(hdcScreen, &ci, sizeof(DCICREATEINPUT), lplpSurface);
  122. ReleaseDC( NULL, hdcScreen );
  123. if( err != DCI_OK || *lplpSurface == NULL )
  124. {
  125. err = dciSendCommand(hdc, &ci, sizeof(DCICREATEINPUT), lplpSurface);
  126. }
  127. return err;
  128. } /* DCICreatePrimary */
  129. /*
  130. * DCIDestroy
  131. */
  132. void WINAPI DCIDestroy(LPDCISURFACEINFO pdci)
  133. {
  134. if( (DWORD)pdci->DestroySurface == 0xFFFFFFFF )
  135. {
  136. pdci = PDCI16(pdci);
  137. }
  138. if( pdci->DestroySurface != NULL )
  139. {
  140. pdci->DestroySurface(pdci);
  141. }
  142. } /* DCIDestroy */
  143. /*
  144. * DCIEndAccess
  145. */
  146. void WINAPI DCIEndAccess( LPDCISURFACEINFO pdci )
  147. {
  148. if( (DWORD)pdci->DestroySurface == 0xFFFFFFFF)
  149. {
  150. pdci = PDCI16( pdci );
  151. }
  152. if( pdci->EndAccess != NULL )
  153. {
  154. pdci->EndAccess( pdci );
  155. }
  156. LeaveSysLevel( pWin16Lock );
  157. } /* DCIEndAccess */
  158. /*
  159. * dciSurface16to32
  160. *
  161. * convert a DCI16 bit structure to a 32bit structure
  162. */
  163. static int dciSurface16to32(
  164. LPDCISURFACEINFO16 pdci16,
  165. LPDCISURFACEINFO32 pdci32 )
  166. {
  167. DPF( 4, "dciSurface16to32" );
  168. if( pdci16 == NULL )
  169. {
  170. DPF( 1, "pdci16=NULL" );
  171. return DCI_FAIL_GENERIC;
  172. }
  173. if( pdci32 == NULL )
  174. {
  175. DPF( 1, "pdci32=NULL" );
  176. return DCI_FAIL_GENERIC;
  177. }
  178. if (pdci16->dwSize < sizeof(DCISURFACEINFO))
  179. {
  180. //
  181. // invalid DCISURCACEINFO.
  182. //
  183. pdci16->dwSize = sizeof(DCISURFACEINFO);
  184. }
  185. if (pdci16->dwSize > sizeof(DCIOFFSCREEN))
  186. {
  187. //
  188. // invalid DCISURCACEINFO.
  189. //
  190. return DCI_FAIL_GENERIC;
  191. }
  192. _fmemcpy(pdci32, pdci16, (UINT) pdci32->dwSize); // copy the info.
  193. pdci32->dwReserved2 = (DWORD)(LPVOID)pdci16;
  194. if (pdci16->BeginAccess != NULL)
  195. {
  196. (DWORD)pdci32->BeginAccess = 0xFFFFFFFF; // you cant call these, but they
  197. (DWORD)pdci32->EndAccess = 0xFFFFFFFF; // must be non-zero
  198. }
  199. (DWORD)pdci32->DestroySurface = 0xFFFFFFFF; // must be non-zero
  200. /*
  201. * now we need to convert the pointer to a 0:32 (ie flat pointer)
  202. * we can only do this if the provider has *not* set the 1632_ACCESS bit.
  203. *
  204. * if the 1632_ACCESS bit is set, call VFlatD to see if we can
  205. * enable linear access mode.
  206. */
  207. if( pdci16->wSelSurface != 0 )
  208. {
  209. if( pdci16->dwDCICaps & DCI_1632_ACCESS )
  210. {
  211. if( pdci16->wSelSurface == VFDQuerySel())
  212. {
  213. if( (wFlatSel == 0) && VFDBeginLinearAccess() )
  214. {
  215. wFlatSel = AllocSelector(SELECTOROF((LPVOID)&pdci16));
  216. SetSelectorBase(wFlatSel, 0);
  217. SetSelLimit(wFlatSel, 0xFFFFFFFF);
  218. }
  219. if (wFlatSel != 0)
  220. {
  221. pdci32->dwOffSurface += VFDQueryBase();
  222. pdci32->wSelSurface = wFlatSel; // 0;
  223. pdci32->dwDCICaps &= ~DCI_1632_ACCESS;
  224. }
  225. }
  226. }
  227. else
  228. {
  229. /*
  230. * convert the 16:32 pointer to a flat pointer.
  231. */
  232. pdci32->dwOffSurface += GetSelectorBase(pdci16->wSelSurface);
  233. pdci32->wSelSurface = 0;
  234. pdci32->wReserved = 0;
  235. }
  236. }
  237. else
  238. {
  239. // selector is zero so the address is flat already
  240. // clear DCI_1632_ACCESS for broken providers?
  241. pdci32->dwDCICaps &= ~DCI_1632_ACCESS; // !!!needed?
  242. }
  243. return DCI_OK;
  244. } /* dciSurface16to32 */
  245. /*
  246. * DCIBeginAccess
  247. */
  248. DCIRVAL WINAPI DCIBeginAccess(
  249. LPDCISURFACEINFO pdci,
  250. int x,
  251. int y,
  252. int dx,
  253. int dy )
  254. {
  255. int err;
  256. RECT rc;
  257. rc.left = x;
  258. rc.top = y;
  259. rc.right = x+dx;
  260. rc.bottom = y+dy;
  261. if( (DWORD)pdci->DestroySurface == 0xFFFFFFFF )
  262. {
  263. LPDCISURFACEINFO16 pdci16 = PDCI16(pdci);
  264. if( pdci16->BeginAccess != NULL )
  265. {
  266. err = pdci16->BeginAccess( pdci16, &rc );
  267. }
  268. else
  269. {
  270. err = DCI_OK;
  271. }
  272. if( err > 0 )
  273. {
  274. err = dciSurface16to32(pdci16, pdci);
  275. }
  276. }
  277. else
  278. {
  279. if( pdci->BeginAccess != NULL )
  280. {
  281. err = pdci->BeginAccess(pdci, &rc);
  282. }
  283. else
  284. {
  285. err = DCI_OK;
  286. }
  287. }
  288. if( err >= 0 )
  289. {
  290. EnterSysLevel( pWin16Lock );
  291. }
  292. return err;
  293. } /* DCIBeginAccess */
  294. /*
  295. * DCICreatePrimary
  296. */
  297. int WINAPI DCICreatePrimary32(HDC hdc, LPDCISURFACEINFO32 pdci32)
  298. {
  299. LPDCISURFACEINFO pdci16;
  300. int rc;
  301. DPF( 4, "DCICreatePrimary32" );
  302. rc = DCICreatePrimary(hdc, &pdci16);
  303. if( rc == DCI_OK )
  304. {
  305. rc = dciSurface16to32( pdci16, pdci32 );
  306. }
  307. return rc;
  308. } /* DCICreatePrimary */
  309. /*
  310. * DCIIsBanked
  311. */
  312. BOOL DDAPI DCIIsBanked( HDC hdc )
  313. {
  314. LPDCISURFACEINFO pdci16;
  315. int rc;
  316. rc = DCICreatePrimary(hdc, &pdci16);
  317. if( rc == DCI_OK )
  318. {
  319. if( !IsBadReadPtr( pdci16, sizeof( *pdci16 ) ) )
  320. {
  321. if( pdci16->dwDCICaps & DCI_1632_ACCESS )
  322. {
  323. rc = TRUE;
  324. }
  325. else
  326. {
  327. rc = FALSE;
  328. }
  329. DCIDestroy( pdci16 );
  330. }
  331. else
  332. {
  333. rc = FALSE;
  334. }
  335. return rc;
  336. }
  337. return FALSE;
  338. } /* DCIIsBanked */
  339. #pragma optimize("", off)
  340. void SetSelLimit(UINT sel, DWORD limit)
  341. {
  342. if( limit >= 1024*1024l )
  343. {
  344. limit = ((limit+4096) & ~4095) - 1;
  345. }
  346. _asm
  347. {
  348. mov ax,0008h ; DPMI set limit
  349. mov bx,sel
  350. mov dx,word ptr limit[0]
  351. mov cx,word ptr limit[2]
  352. int 31h
  353. }
  354. } /* SetSelLimit */
  355. #pragma optimize("", on)