Moved some messaging and input functions to message.c and input.c
[wine] / windows / queue.c
1 /*
2  * Message queues related functions
3  *
4  * Copyright 1993, 1994 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdarg.h>
25 #include <string.h>
26 #include <signal.h>
27 #include <assert.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winerror.h"
32 #include "wine/winbase16.h"
33 #include "wine/winuser16.h"
34 #include "message.h"
35 #include "win.h"
36 #include "user_private.h"
37 #include "thread.h"
38 #include "wine/debug.h"
39 #include "wine/server.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(msg);
42
43
44 /***********************************************************************
45  *           QUEUE_CreateMsgQueue
46  *
47  * Creates a message queue. Doesn't link it into queue list!
48  */
49 static HQUEUE16 QUEUE_CreateMsgQueue(void)
50 {
51     HQUEUE16 hQueue;
52     HANDLE handle;
53     MESSAGEQUEUE * msgQueue;
54
55     TRACE_(msg)("(): Creating message queue...\n");
56
57     if (!(hQueue = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT,
58                                   sizeof(MESSAGEQUEUE) )))
59         return 0;
60
61     msgQueue = (MESSAGEQUEUE *) GlobalLock16( hQueue );
62     if ( !msgQueue )
63         return 0;
64
65     SERVER_START_REQ( get_msg_queue )
66     {
67         wine_server_call_err( req );
68         handle = reply->handle;
69     }
70     SERVER_END_REQ;
71     if (!handle)
72     {
73         ERR_(msg)("Cannot get thread queue\n");
74         GlobalFree16( hQueue );
75         return 0;
76     }
77     msgQueue->server_queue = handle;
78     msgQueue->self = hQueue;
79     return hQueue;
80 }
81
82
83 /***********************************************************************
84  *           QUEUE_Current
85  *
86  * Get the current thread queue, creating it if required.
87  * QUEUE_Unlock is not needed since the queue can only be deleted by
88  * the current thread anyway.
89  */
90 MESSAGEQUEUE *QUEUE_Current(void)
91 {
92     HQUEUE16 hQueue = NtCurrentTeb()->queue;
93
94     if (!hQueue)
95     {
96         if (!(hQueue = QUEUE_CreateMsgQueue())) return NULL;
97         SetThreadQueue16( 0, hQueue );
98     }
99
100     return GlobalLock16( hQueue );
101 }
102
103
104
105 /***********************************************************************
106  *           QUEUE_DeleteMsgQueue
107  *
108  * Delete a message queue.
109  */
110 void QUEUE_DeleteMsgQueue(void)
111 {
112     HQUEUE16 hQueue = NtCurrentTeb()->queue;
113     MESSAGEQUEUE * msgQueue;
114
115     if (!hQueue) return;  /* thread doesn't have a queue */
116
117     TRACE("(): Deleting message queue %04x\n", hQueue);
118
119     if (!(msgQueue = GlobalLock16( hQueue )))
120     {
121         ERR("invalid thread queue\n");
122         return;
123     }
124
125     SetThreadQueue16( 0, 0 );
126     CloseHandle( msgQueue->server_queue );
127     GlobalFree16( hQueue );
128 }
129
130
131 /***********************************************************************
132  *              InitThreadInput   (USER.409)
133  */
134 HQUEUE16 WINAPI InitThreadInput16( WORD unknown, WORD flags )
135 {
136     MESSAGEQUEUE *queue = QUEUE_Current();
137     return queue ? queue->self : 0;
138 }