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