When creating threads allow an Id to be returned as well; For Win32 threading, use...
[SquirrelJME.git] / nanocoat / lib / scritchui / win32 / win32Loop.c
blob3a2b59753d80a98d40b801010f9e5ee8c0513b7e
1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
10 #include "lib/scritchui/win32/win32.h"
11 #include "lib/scritchui/win32/win32Intern.h"
13 sjme_errorCode sjme_scritchui_win32_loopExecuteLater(
14 sjme_attrInNotNull sjme_scritchui inState,
15 sjme_attrInNotNull sjme_thread_mainFunc callback,
16 sjme_attrInNullable sjme_thread_parameter anything)
18 BOOL result;
19 if (inState == NULL || callback == NULL)
20 return SJME_ERROR_NULL_ARGUMENTS;
22 /* Send to the event thread. */
23 SetLastError(0);
24 if ((result = PostThreadMessage(
25 inState->loopThreadId,
26 WM_USER,
27 (WPARAM)callback, (LPARAM)anything)) == 0)
28 return inState->implIntern->getLastError(inState,
29 SJME_ERROR_LOOP_ENQUEUE_FAILED);
31 /* Success! */
32 return SJME_ERROR_NONE;
35 sjme_errorCode sjme_scritchui_win32_loopIterate(
36 sjme_attrInNotNull sjme_scritchui inState,
37 sjme_attrInValue sjme_jboolean blocking,
38 sjme_attrOutNullable sjme_jboolean* outHasTerminated)
40 MSG message;
41 BOOL messageResult;
43 if (inState == NULL)
44 return SJME_ERROR_NULL_ARGUMENTS;
46 /* Read next message for the event thread. */
47 memset(&message, 0, sizeof(message));
48 if (blocking)
50 /* Wait for the next message. */
51 messageResult = GetMessage(&message, NULL,
52 0, 0);
54 /* Quitting? */
55 if (messageResult == 0)
57 if (outHasTerminated != NULL)
58 *outHasTerminated = SJME_JNI_TRUE;
59 return SJME_ERROR_NONE;
62 else
64 /* Grab the next message. */
65 messageResult = PeekMessage(&message, NULL,
66 0, 0, PM_REMOVE);
68 /* No message? */
69 if (messageResult == 0)
70 return SJME_ERROR_NONE;
72 /* Quitting? */
73 if (message.message == WM_QUIT)
75 if (outHasTerminated != NULL)
76 *outHasTerminated = SJME_JNI_TRUE;
77 return SJME_ERROR_NONE;
81 /* If there is no window, we handle it ourselves. */
82 if (message.hwnd == NULL && !(message.message == WM_TIMER))
83 return inState->implIntern->windowProc(inState,
84 message.hwnd, message.message, message.wParam, message.lParam,
85 NULL);
87 /* Otherwise dispatch to a window procedure handler. */
88 SetLastError(0);
89 TranslateMessage(&message);
90 DispatchMessage(&message);
92 /* Success? */
93 return inState->implIntern->getLastError(inState, SJME_ERROR_NONE);