Release 960811
[wine] / misc / user.c
1 /*
2  * Misc. USER functions
3  *
4  * Copyright 1993 Robert J. Amstadt
5  *           1996 Alex Korobka 
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include "windows.h"
11 #include "gdi.h"
12 #include "user.h"
13 #include "task.h"
14 #include "queue.h"
15 #include "win.h"
16 #include "hook.h"
17 #include "debug.h"
18 #include "toolhelp.h"
19
20 WORD USER_HeapSel = 0;
21
22
23 extern HTASK    TASK_GetNextTask(HTASK);
24
25 /***********************************************************************
26  *           GetFreeSystemResources   (USER.284)
27  */
28 WORD GetFreeSystemResources( WORD resType )
29 {
30     int userPercent, gdiPercent;
31
32     switch(resType)
33     {
34     case GFSR_USERRESOURCES:
35         userPercent = (int)LOCAL_CountFree( USER_HeapSel ) * 100 /
36                                LOCAL_HeapSize( USER_HeapSel );
37         gdiPercent  = 100;
38         break;
39
40     case GFSR_GDIRESOURCES:
41         gdiPercent  = (int)LOCAL_CountFree( GDI_HeapSel ) * 100 /
42                                LOCAL_HeapSize( GDI_HeapSel );
43         userPercent = 100;
44         break;
45
46     case GFSR_SYSTEMRESOURCES:
47         userPercent = (int)LOCAL_CountFree( USER_HeapSel ) * 100 /
48                                LOCAL_HeapSize( USER_HeapSel );
49         gdiPercent  = (int)LOCAL_CountFree( GDI_HeapSel ) * 100 /
50                                LOCAL_HeapSize( GDI_HeapSel );
51         break;
52
53     default:
54         return 0;
55     }
56     return (WORD)MIN( userPercent, gdiPercent );
57 }
58
59
60 /***********************************************************************
61  *           SystemHeapInfo   (TOOLHELP.71)
62  */
63 BOOL16 SystemHeapInfo( SYSHEAPINFO *pHeapInfo )
64 {
65     pHeapInfo->wUserFreePercent = GetFreeSystemResources( GFSR_USERRESOURCES );
66     pHeapInfo->wGDIFreePercent  = GetFreeSystemResources( GFSR_GDIRESOURCES );
67     pHeapInfo->hUserSegment = USER_HeapSel;
68     pHeapInfo->hGDISegment  = GDI_HeapSel;
69     return TRUE;
70 }
71
72
73 /***********************************************************************
74  *           TimerCount   (TOOLHELP.80)
75  */
76 BOOL16 TimerCount( TIMERINFO *pTimerInfo )
77 {
78     /* FIXME
79      * In standard mode, dwmsSinceStart = dwmsThisVM 
80      *
81      * I tested this, under Windows in enhanced mode, and
82      * if you never switch VM (ie start/stop DOS) these
83      * values should be the same as well. 
84      *
85      * Also, Wine should adjust for the hardware timer
86      * to reduce the amount of error to ~1ms. 
87      * I can't be bothered, can you?
88      */
89     pTimerInfo->dwmsSinceStart = pTimerInfo->dwmsThisVM = GetTickCount();
90     return TRUE;
91 }
92
93
94 /**********************************************************************
95  *                                      USER_InitApp
96  */
97 int USER_InitApp(HINSTANCE hInstance)
98 {
99     int queueSize;
100
101       /* Create task message queue */
102     queueSize = GetProfileInt( "windows", "DefaultQueueSize", 8 );
103     if (!SetMessageQueue( queueSize )) return 0;
104
105     return 1;
106 }
107
108 /**********************************************************************
109  *                                      USER_AppExit
110  */
111 void USER_AppExit( HTASK16 hTask, HINSTANCE16 hInstance, HQUEUE16 hQueue )
112 {
113     /* FIXME: flush send messages (which are not implemented yet),
114      *        empty clipboard if needed, maybe destroy menus (Windows
115      *        only complains about them but does nothing);
116      */
117
118     WND* desktop = WIN_GetDesktop();
119
120     /* Patch desktop window queue */
121     if( desktop->hmemTaskQ == hQueue )
122         desktop->hmemTaskQ = GetTaskQueue(TASK_GetNextTask(hTask));
123
124     /* Nuke timers */
125
126     TIMER_RemoveQueueTimers( hQueue );
127
128     HOOK_FreeQueueHooks( hQueue );
129
130     QUEUE_SetDoomedQueue( hQueue );
131
132     /* Nuke orphaned windows */
133
134     WIN_DestroyQueueWindows( desktop->child, hQueue );
135
136     QUEUE_SetDoomedQueue( 0 );
137
138     /* Free the message queue */
139
140     QUEUE_DeleteMsgQueue( hQueue );
141 }
142