winmsgwindow.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (C) Jon TURNEY 2011
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. */
  24. #ifdef HAVE_XWIN_CONFIG_H
  25. #include <xwin-config.h>
  26. #endif
  27. #include "win.h"
  28. /*
  29. * This is the messaging window, a hidden top-level window. We never do anything
  30. * with it, but other programs may send messages to it.
  31. */
  32. /*
  33. * winMsgWindowProc - Window procedure for msg window
  34. */
  35. static
  36. LRESULT CALLBACK
  37. winMsgWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  38. {
  39. #if CYGDEBUG
  40. winDebugWin32Message("winMsgWindowProc", hwnd, message, wParam, lParam);
  41. #endif
  42. switch (message) {
  43. case WM_ENDSESSION:
  44. if (!wParam)
  45. return 0; /* shutdown is being cancelled */
  46. /*
  47. Send a WM_GIVEUP message to the X server thread so it wakes up if
  48. blocked in select(), performs GiveUp(), and then notices that GiveUp()
  49. has set the DE_TERMINATE flag so exits the msg dispatch loop.
  50. */
  51. {
  52. ScreenPtr pScreen = screenInfo.screens[0];
  53. winScreenPriv(pScreen);
  54. PostMessage(pScreenPriv->hwndScreen, WM_GIVEUP, 0, 0);
  55. }
  56. /*
  57. This process will be terminated by the system almost immediately
  58. after the last thread with a message queue returns from processing
  59. WM_ENDSESSION, so we cannot rely on any code executing after this
  60. message is processed and need to wait here until ddxGiveUp() is called
  61. and releases the termination mutex to guarantee that the lock file and
  62. unix domain sockets have been removed
  63. ofc, Microsoft doesn't document this under WM_ENDSESSION, you are supposed
  64. to read the source of CRSS to find out how it works :-)
  65. http://blogs.msdn.com/b/michen/archive/2008/04/04/application-termination-when-user-logs-off.aspx
  66. */
  67. {
  68. int iReturn = pthread_mutex_lock(&g_pmTerminating);
  69. if (iReturn != 0) {
  70. ErrorF("winMsgWindowProc - pthread_mutex_lock () failed: %d\n",
  71. iReturn);
  72. }
  73. winDebug
  74. ("winMsgWindowProc - WM_ENDSESSION termination lock acquired\n");
  75. }
  76. return 0;
  77. }
  78. return DefWindowProc(hwnd, message, wParam, lParam);
  79. }
  80. static HWND
  81. winCreateMsgWindow(void)
  82. {
  83. HWND hwndMsg;
  84. wATOM winClass;
  85. // register window class
  86. {
  87. WNDCLASSEX wcx;
  88. wcx.cbSize = sizeof(WNDCLASSEX);
  89. wcx.style = CS_HREDRAW | CS_VREDRAW;
  90. wcx.lpfnWndProc = winMsgWindowProc;
  91. wcx.cbClsExtra = 0;
  92. wcx.cbWndExtra = 0;
  93. wcx.hInstance = g_hInstance;
  94. wcx.hIcon = NULL;
  95. wcx.hCursor = 0;
  96. wcx.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  97. wcx.lpszMenuName = NULL;
  98. wcx.lpszClassName = WINDOW_CLASS_X_MSG;
  99. wcx.hIconSm = NULL;
  100. winClass = RegisterClassEx(&wcx);
  101. }
  102. // Create the msg window.
  103. hwndMsg = CreateWindowEx(0, // no extended styles
  104. WINDOW_CLASS_X_MSG, // class name
  105. "XWin Msg Window", // window name
  106. WS_OVERLAPPEDWINDOW, // overlapped window
  107. CW_USEDEFAULT, // default horizontal position
  108. CW_USEDEFAULT, // default vertical position
  109. CW_USEDEFAULT, // default width
  110. CW_USEDEFAULT, // default height
  111. (HWND) NULL, // no parent or owner window
  112. (HMENU) NULL, // class menu used
  113. GetModuleHandle(NULL), // instance handle
  114. NULL); // no window creation data
  115. if (!hwndMsg) {
  116. ErrorF("winCreateMsgWindow - Create msg window failed\n");
  117. return NULL;
  118. }
  119. winDebug("winCreateMsgWindow - Created msg window hwnd 0x%x\n", hwndMsg);
  120. return hwndMsg;
  121. }
  122. static void *
  123. winMsgWindowThreadProc(void *arg)
  124. {
  125. HWND hwndMsg;
  126. winDebug("winMsgWindowThreadProc - Hello\n");
  127. hwndMsg = winCreateMsgWindow();
  128. if (hwndMsg) {
  129. MSG msg;
  130. /* Pump the msg window message queue */
  131. while (GetMessage(&msg, hwndMsg, 0, 0) > 0) {
  132. #if CYGDEBUG
  133. winDebugWin32Message("winMsgWindowThread", msg.hwnd, msg.message,
  134. msg.wParam, msg.lParam);
  135. #endif
  136. DispatchMessage(&msg);
  137. }
  138. }
  139. winDebug("winMsgWindowThreadProc - Exit\n");
  140. return NULL;
  141. }
  142. Bool
  143. winCreateMsgWindowThread(void)
  144. {
  145. pthread_t ptMsgWindowThreadProc;
  146. /* Spawn a thread for the msg window */
  147. if (pthread_create(&ptMsgWindowThreadProc,
  148. NULL, winMsgWindowThreadProc, NULL)) {
  149. /* Bail if thread creation failed */
  150. ErrorF("winCreateMsgWindow - pthread_create failed.\n");
  151. return FALSE;
  152. }
  153. return TRUE;
  154. }