1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
|
/********************************************************************************************
Clean OS Windows library module version 1.2.1.
This module is part of the Clean Object I/O library, version 1.2.1,
for the Windows platform.
********************************************************************************************/
/********************************************************************************************
About this module:
cCrossCall_121 defines the infrastructure required by the Object I/O library to call
system procedures that interact with the Windows callback mechanism.
The basic principle in cCrossCall_121 is to have a minimal cross call kernel. If Clean
code requires extension of the functionality of the OS thread, then this functionality
must be registered before being applicable.
In this version the request codes are still statically fixed and are assumed to be
globally available both in the OS thread and the Clean thread. In a future version this
will probably be replaced by a dynamic allocation of cross call request codes.
********************************************************************************************/
/********************************************************************************************
Include section.
********************************************************************************************/
#include "cCrossCall_121.h"
#include "cCrossCallWindows_121.h" // Contains the implementation of cursors.
#include <gdk/gdkkeysyms.h>
#include <pthread.h>
#include <Rts.h>
#include <RtsAPI.h>
/**********************************************************************************************
External global data section.
**********************************************************************************************/
CrossCallInfo gCci; /* The global cross call information struct. */
GtkWidget *gTooltip = NULL; /* The tooltip control. */
CrossCallProcedureTable gCrossCallProcedureTable;
/**********************************************************************************************
Internal global data section.
**********************************************************************************************/
static pthread_mutex_t gHaskellMutex;
static pthread_mutex_t gOSMutex;
static pthread_t gOSThread;
static gboolean gOSThreadIsRunning = FALSE;
static CrossCallInfo *MakeQuitCci (CrossCallInfo * pcci);
/* GetModifiers returns the modifiers that are currently pressed.
*/
int GetModifiers (void)
{
int mods = 0;
GdkModifierType state;
gdk_event_get_state(gtk_get_current_event(), &state);
if (state & GDK_SHIFT_MASK)
mods |= SHIFTBIT;
if (state & GDK_CONTROL_MASK)
mods |= CTRLBIT;
if (state & GDK_MOD1_MASK)
mods |= ALTBIT;
return mods;
}
/* Translate virtual key codes to the codes shared with Clean.
This procedure has been filtered from TranslateKeyboardMessage.
If the keycode could not be translated, zero is returned.
*/
int CheckVirtualKeyCode (int keycode)
{
int c = 0;
switch (keycode)
{
case GDK_Up:
c = WinUpKey;
break;
case GDK_Down:
c = WinDownKey;
break;
case GDK_Left:
c = WinLeftKey;
break;
case GDK_Right:
c = WinRightKey;
break;
case GDK_Page_Up:
c = WinPgUpKey;
break;
case GDK_Page_Down:
c = WinPgDownKey;
break;
case GDK_End:
c = WinEndKey;
break;
case GDK_Begin:
c = WinBeginKey;
break;
case GDK_BackSpace:
c = WinBackSpKey;
break;
case GDK_Delete:
c = WinDelKey;
break;
case GDK_Tab:
c = WinTabKey;
break;
case GDK_Return:
c = WinReturnKey;
break;
case GDK_Escape:
c = WinEscapeKey;
break;
case GDK_Help:
c = WinHelpKey;
break;
case GDK_F1:
c = WinF1Key;
break;
case GDK_F2:
c = WinF2Key;
break;
case GDK_F3:
c = WinF3Key;
break;
case GDK_F4:
c = WinF4Key;
break;
case GDK_F5:
c = WinF5Key;
break;
case GDK_F6:
c = WinF6Key;
break;
case GDK_F7:
c = WinF7Key;
break;
case GDK_F8:
c = WinF8Key;
break;
case GDK_F9:
c = WinF9Key;
break;
case GDK_F10:
c = WinF10Key;
break;
case GDK_F11:
c = WinF11Key;
break;
case GDK_F12:
c = WinF12Key;
break;
}
return c;
}
static void TimerCallback (gpointer data)
{
SendMessage0ToClean (CcWmIDLETIMER);
};
void HandleCleanRequest (CrossCallInfo * pcci)
{
switch (pcci->mess)
{
case CcRqDOMESSAGE: // idleTimerOn, sleeptime; no result.
{
gboolean gIdleTimerOn = (gboolean) pcci->p1;
gint interval = (gint) pcci->p2;
if (gIdleTimerOn)
{
GSource *source = g_timeout_source_new(interval);
g_source_set_callback(source,TimerCallback,NULL,NULL);
g_source_attach(source,NULL);
gtk_main_iteration();
g_source_destroy(source);
}
else
{
gtk_main_iteration();
}
MakeReturn0Cci (pcci);
}
break;
default:
{
CrossCallProcedure action;
action = FindCrossCallEntry (gCrossCallProcedureTable, pcci->mess);
if (action == NULL)
{ // Cross call request code not installed.
printf("\'HandleCleanRequest\' got uninstalled CcRq request code from Haskell: %d\n", pcci->mess);
exit(1);
}
else
{ // Cross call request code found. Apply it to pcci.
action (pcci);
}
}
}
KickCleanThread (pcci);
} /* HandleCleanRequest */
void InitGTK()
{
static gboolean gInitiated = FALSE;
if (!gInitiated)
{
int margc;
char **margv;
gtk_set_locale();
getProgArgv(&margc, &margv);
gtk_init(&margc,&margv);
setProgArgv(margc, margv);
};
gInitiated = TRUE;
} /* InitGTK */
static gpointer OsThreadFunction (gpointer param);
void WinStartOsThread()
{
pthread_attr_t attr;
InitGTK();
// The cross call procedure table is set to the empty table.
gCrossCallProcedureTable = EmptyCrossCallProcedureTable ();
pthread_mutex_init(&gHaskellMutex,NULL);
pthread_mutex_lock(&gHaskellMutex);
pthread_mutex_init(&gOSMutex,NULL);
pthread_mutex_lock(&gOSMutex);
gOSThreadIsRunning = TRUE;
pthread_attr_init(&attr);
pthread_create(&gOSThread,&attr,OsThreadFunction,NULL);
pthread_attr_destroy(&attr);
} /* WinStartOsThread */
void WinKillOsThread ()
{
if (gOSThread != NULL)
{
gOSThreadIsRunning = FALSE;
gOSThread = NULL;
DeleteCursors();
if (gCrossCallProcedureTable)
FreeCrossCallProcedureTable (gCrossCallProcedureTable);
};
} /*WinKillOsThread*/
void WinKickOsThread (int imess,
int ip1, int ip2, int ip3,
int ip4, int ip5, int ip6,
int *omess,
int *op1, int *op2, int *op3,
int *op4, int *op5, int *op6
)
{
gCci.mess = imess;
gCci.p1 = ip1;
gCci.p2 = ip2;
gCci.p3 = ip3;
gCci.p4 = ip4;
gCci.p5 = ip5;
gCci.p6 = ip6;
if (gOSThread != NULL)
{
pthread_mutex_unlock(&gHaskellMutex);
pthread_mutex_lock(&gOSMutex);
*omess = gCci.mess;
*op1 = gCci.p1;
*op2 = gCci.p2;
*op3 = gCci.p3;
*op4 = gCci.p4;
*op5 = gCci.p5;
*op6 = gCci.p6;
}
else
{
*omess = CcWASQUIT;
*op1 = 0;
*op2 = 0;
*op3 = 0;
*op4 = 0;
*op5 = 0;
*op6 = 0;
}
} /* WinKickOsThread */
void KickCleanThread (CrossCallInfo * pcci)
{
if (pcci != &gCci)
gCci = *pcci;
pthread_mutex_unlock(&gOSMutex);
pthread_mutex_lock(&gHaskellMutex);
if (pcci != &gCci)
*pcci = gCci;
} /* KickCleanThread */
void SendMessageToClean (int mess, int p1, int p2, int p3, int p4, int p5, int p6)
{
gCci.mess = mess;
gCci.p1 = p1;
gCci.p2 = p2;
gCci.p3 = p3;
gCci.p4 = p4;
gCci.p5 = p5;
gCci.p6 = p6;
KickCleanThread (&gCci);
while (!IsReturnCci (&gCci))
{
HandleCleanRequest (&gCci);
}
}
CrossCallInfo *MakeReturn0Cci (CrossCallInfo * pcci)
{
pcci->mess = CcRETURN0;
return pcci;
}
CrossCallInfo *MakeReturn1Cci (CrossCallInfo * pcci, int v1)
{
pcci->mess = CcRETURN1;
pcci->p1 = v1;
return pcci;
}
CrossCallInfo *MakeReturn2Cci (CrossCallInfo * pcci, int v1, int v2)
{
pcci->mess = CcRETURN2;
pcci->p1 = v1;
pcci->p2 = v2;
return pcci;
}
CrossCallInfo *MakeReturn3Cci (CrossCallInfo * pcci, int v1, int v2, int v3)
{
pcci->mess = CcRETURN3;
pcci->p1 = v1;
pcci->p2 = v2;
pcci->p3 = v3;
return pcci;
}
CrossCallInfo *MakeReturn4Cci (CrossCallInfo * pcci, int v1, int v2, int v3, int v4)
{
pcci->mess = CcRETURN4;
pcci->p1 = v1;
pcci->p2 = v2;
pcci->p3 = v3;
pcci->p4 = v4;
return pcci;
}
CrossCallInfo *MakeReturn5Cci (CrossCallInfo * pcci, int v1, int v2, int v3, int v4, int v5)
{
pcci->mess = CcRETURN5;
pcci->p1 = v1;
pcci->p2 = v2;
pcci->p3 = v3;
pcci->p4 = v4;
pcci->p5 = v5;
return pcci;
}
CrossCallInfo *MakeReturn6Cci (CrossCallInfo * pcci, int v1, int v2, int v3, int v4, int v5, int v6)
{
pcci->mess = CcRETURN6;
pcci->p1 = v1;
pcci->p2 = v2;
pcci->p3 = v3;
pcci->p4 = v4;
pcci->p5 = v5;
pcci->p6 = v6;
return pcci;
}
gboolean IsReturnCci (CrossCallInfo * pcci)
{
if (pcci->mess >= CcRETURNmin && pcci->mess <= CcRETURNmax)
return TRUE;
return FALSE;
}
static gpointer OsThreadFunction (gpointer param)
{
gTooltip = gtk_tooltips_new();
pthread_mutex_lock(&gHaskellMutex);
while (gOSThreadIsRunning)
{
HandleCleanRequest (&gCci);
}
pthread_mutex_unlock(&gHaskellMutex);
pthread_mutex_destroy(&gOSMutex);
pthread_mutex_destroy(&gHaskellMutex);
return NULL;
} /* OsThreadFunction */
|