]> code.delx.au - gnu-emacs/blobdiff - src/w32xfns.c
*** empty log message ***
[gnu-emacs] / src / w32xfns.c
index 9929248e92b2393b6b9cd1e6a43be73bb46a8253..0e937e9950ce3466ced67fe295cfa1831c022d32 100644 (file)
@@ -1,5 +1,6 @@
-/* Functions taken directly from X sources
-   Copyright (C) 1989, 1992, 1993, 1994, 1995 Free Software Foundation.
+/* Functions taken directly from X sources for use with the Microsoft W32 API.
+   Copyright (C) 1989, 1992, 1993, 1994, 1995, 1999, 2002, 2003, 2004,
+                 2005, 2006 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -15,14 +16,17 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA.  */
 
-#include <signal.h>
 #include <config.h>
+#include <signal.h>
 #include <stdio.h>
 #include "lisp.h"
+#include "keyboard.h"
 #include "frame.h"
+#include "charset.h"
+#include "fontset.h"
 #include "blockinput.h"
 #include "w32term.h"
 #include "windowsx.h"
@@ -30,83 +34,126 @@ Boston, MA 02111-1307, USA.  */
 #define myalloc(cb) GlobalAllocPtr (GPTR, cb)
 #define myfree(lp) GlobalFreePtr (lp)
 
-CRITICAL_SECTION critsect[ CRIT_TOTAL ];
+CRITICAL_SECTION critsect;
 extern HANDLE keyboard_handle;
-HANDLE h_input_available = NULL;
+HANDLE input_available = NULL;
+HANDLE interrupt_handle = NULL;
 
-void 
+void
 init_crit ()
 {
-  int i;
+  InitializeCriticalSection (&critsect);
 
-  for (i = 0; i < CRIT_TOTAL; i++)
-    InitializeCriticalSection (&critsect[i]);
-  /* For safety, h_input_available should only be reset by get_next_msg
+  /* For safety, input_available should only be reset by get_next_msg
      when the input queue is empty, so make it a manual reset event. */
-  keyboard_handle = h_input_available = CreateEvent (NULL, TRUE, FALSE, NULL);
+  keyboard_handle = input_available = CreateEvent (NULL, TRUE, FALSE, NULL);
+
+  /* interrupt_handle is signalled when quit (C-g) is detected, so that
+     blocking system calls can be interrupted.  We make it a manual
+     reset event, so that if we should ever have multiple threads
+     performing system calls, they will all be interrupted (I'm guessing
+     that would the right response).  Note that we use PulseEvent to
+     signal this event, so that it never remains signalled.  */
+  interrupt_handle = CreateEvent (NULL, TRUE, FALSE, NULL);
 }
 
-void 
+void
 delete_crit ()
 {
-  int i;
+  DeleteCriticalSection (&critsect);
 
-  for (i = 0; i < CRIT_TOTAL; i++)
-    DeleteCriticalSection (&critsect[i]);
-  if (h_input_available)
+  if (input_available)
+    {
+      CloseHandle (input_available);
+      input_available = NULL;
+    }
+  if (interrupt_handle)
     {
-      CloseHandle (h_input_available);
-      h_input_available = NULL;
+      CloseHandle (interrupt_handle);
+      interrupt_handle = NULL;
     }
 }
 
-/* Get a DC for frame and select palette for drawing; force an update of
-   all frames if palette's mapping changes.  */
-HDC
-GetFrameDC (FRAME_PTR f)
+void
+signal_quit ()
 {
-  HDC hDC;
+  /* Make sure this event never remains signalled; if the main thread
+     isn't in a blocking call, then this should do nothing.  */
+  PulseEvent (interrupt_handle);
+}
 
-  enter_crit (CRIT_GDI);
+void
+select_palette (FRAME_PTR f, HDC hdc)
+{
+  struct w32_display_info *display_info = FRAME_W32_DISPLAY_INFO (f);
+
+  if (!display_info->has_palette)
+    return;
 
-  hDC = GetDC (f->output_data.win32->window_desc);
+  if (display_info->palette == 0)
+    return;
 
-  if (!NILP (Vwin32_enable_palette))
-    f->output_data.win32->h_old_palette =
-      SelectPalette (hDC, one_win32_display_info.h_palette, FALSE);
+  if (!NILP (Vw32_enable_palette))
+    f->output_data.w32->old_palette =
+      SelectPalette (hdc, display_info->palette, FALSE);
   else
-    f->output_data.win32->h_old_palette = NULL;
+    f->output_data.w32->old_palette = NULL;
 
-  if (RealizePalette (hDC))
+  if (RealizePalette (hdc))
+  {
+    Lisp_Object frame, framelist;
+    FOR_EACH_FRAME (framelist, frame)
     {
-      Lisp_Object frame, framelist;
-      FOR_EACH_FRAME (framelist, frame)
-       {
-         SET_FRAME_GARBAGED (XFRAME (frame));
-       }
+      SET_FRAME_GARBAGED (XFRAME (frame));
     }
+  }
+}
 
-  return hDC;
+void
+deselect_palette (FRAME_PTR f, HDC hdc)
+{
+  if (f->output_data.w32->old_palette)
+    SelectPalette (hdc, f->output_data.w32->old_palette, FALSE);
+}
+
+/* Get a DC for frame and select palette for drawing; force an update of
+   all frames if palette's mapping changes.  */
+HDC
+get_frame_dc (FRAME_PTR f)
+{
+  HDC hdc;
+
+  if (f->output_method != output_w32)
+    abort ();
+
+  enter_crit ();
+
+  hdc = GetDC (f->output_data.w32->window_desc);
+
+  /* If this gets called during startup before the frame is valid,
+     there is a chance of corrupting random data or crashing. */
+  if (hdc)
+    select_palette (f, hdc);
+
+  return hdc;
 }
 
 int
-ReleaseFrameDC (FRAME_PTR f, HDC hDC)
+release_frame_dc (FRAME_PTR f, HDC hdc)
 {
   int ret;
 
-  if (f->output_data.win32->h_old_palette)
-    SelectPalette (hDC, f->output_data.win32->h_old_palette, FALSE);
-
-  ret = ReleaseDC (f->output_data.win32->window_desc, hDC);
+  deselect_palette (f, hdc);
+  ret = ReleaseDC (f->output_data.w32->window_desc, hdc);
 
-  leave_crit (CRIT_GDI);
+  leave_crit ();
 
   return ret;
 }
 
 typedef struct int_msg
 {
-  Win32Msg w32msg;
+  W32Msg w32msg;
   struct int_msg *lpNext;
 } int_msg;
 
@@ -114,33 +161,33 @@ int_msg *lpHead = NULL;
 int_msg *lpTail = NULL;
 int nQueue = 0;
 
-BOOL 
+BOOL
 get_next_msg (lpmsg, bWait)
-     Win32Msg * lpmsg;
+     W32Msg * lpmsg;
      BOOL bWait;
 {
   BOOL bRet = FALSE;
-  
-  enter_crit (CRIT_MSG);
-  
+
+  enter_crit ();
+
   /* The while loop takes care of multiple sets */
-  
+
   while (!nQueue && bWait)
     {
-      leave_crit (CRIT_MSG);
-      WaitForSingleObject (h_input_available, INFINITE);
-      enter_crit (CRIT_MSG);
+      leave_crit ();
+      WaitForSingleObject (input_available, INFINITE);
+      enter_crit ();
     }
-  
+
   if (nQueue)
     {
-      bcopy (&(lpHead->w32msg), lpmsg, sizeof (Win32Msg));
+      bcopy (&(lpHead->w32msg), lpmsg, sizeof (W32Msg));
 
       {
        int_msg * lpCur = lpHead;
-           
+
        lpHead = lpHead->lpNext;
-           
+
        myfree (lpCur);
       }
 
@@ -150,40 +197,78 @@ get_next_msg (lpmsg, bWait)
     }
 
   if (nQueue == 0)
-    ResetEvent (h_input_available);
-  
-  leave_crit (CRIT_MSG);
-  
+    ResetEvent (input_available);
+
+  leave_crit ();
+
   return (bRet);
 }
 
-BOOL 
+BOOL
 post_msg (lpmsg)
-     Win32Msg * lpmsg;
+     W32Msg * lpmsg;
 {
   int_msg * lpNew = (int_msg *) myalloc (sizeof (int_msg));
 
   if (!lpNew)
     return (FALSE);
 
-  bcopy (lpmsg, &(lpNew->w32msg), sizeof (Win32Msg));
+  bcopy (lpmsg, &(lpNew->w32msg), sizeof (W32Msg));
   lpNew->lpNext = NULL;
 
-  enter_crit (CRIT_MSG);
+  enter_crit ();
 
   if (nQueue++)
-    lpTail->lpNext = lpNew;
-  else 
-    lpHead = lpNew;
+    {
+      lpTail->lpNext = lpNew;
+    }
+  else
+    {
+      lpHead = lpNew;
+    }
 
   lpTail = lpNew;
-  SetEvent (h_input_available);
-    
-  leave_crit (CRIT_MSG);
+  SetEvent (input_available);
+
+  leave_crit ();
+
+  return (TRUE);
+}
+
+BOOL
+prepend_msg (W32Msg *lpmsg)
+{
+  int_msg * lpNew = (int_msg *) myalloc (sizeof (int_msg));
+
+  if (!lpNew)
+    return (FALSE);
+
+  bcopy (lpmsg, &(lpNew->w32msg), sizeof (W32Msg));
+
+  enter_crit ();
+
+  nQueue++;
+  lpNew->lpNext = lpHead;
+  lpHead = lpNew;
+
+  leave_crit ();
 
   return (TRUE);
 }
 
+/* Process all messages in the current thread's queue.  */
+void
+drain_message_queue ()
+{
+  MSG msg;
+  while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
+    {
+      TranslateMessage (&msg);
+      DispatchMessage (&msg);
+    }
+}
+
+
 /*
  *    XParseGeometry parses strings of the form
  *   "=<width>x<height>{+-}<xoffset>{+-}<yoffset>", where
@@ -193,7 +278,7 @@ post_msg (lpmsg)
  *   It returns a bitmask that indicates which of the four values
  *   were actually found in the string.  For each value found,
  *   the corresponding argument is updated;  for each value
- *   not found, the corresponding argument is left unchanged. 
+ *   not found, the corresponding argument is left unchanged.
  */
 
 static int
@@ -203,7 +288,7 @@ read_integer (string, NextString)
 {
   register int Result = 0;
   int Sign = 1;
-  
+
   if (*string == '+')
     string++;
   else if (*string == '-')
@@ -222,7 +307,7 @@ read_integer (string, NextString)
     return (-Result);
 }
 
-int 
+int
 XParseGeometry (string, x, y, width, height)
      char *string;
      int *x, *y;
@@ -233,23 +318,23 @@ XParseGeometry (string, x, y, width, height)
   unsigned int tempWidth, tempHeight;
   int tempX, tempY;
   char *nextCharacter;
-  
+
   if ((string == NULL) || (*string == '\0')) return (mask);
   if (*string == '=')
     string++;  /* ignore possible '=' at beg of geometry spec */
-  
+
   strind = (char *)string;
-  if (*strind != '+' && *strind != '-' && *strind != 'x') 
+  if (*strind != '+' && *strind != '-' && *strind != 'x')
     {
       tempWidth = read_integer (strind, &nextCharacter);
-      if (strind == nextCharacter) 
+      if (strind == nextCharacter)
        return (0);
       strind = nextCharacter;
       mask |= WidthValue;
     }
-  
-  if (*strind == 'x' || *strind == 'X') 
-    {  
+
+  if (*strind == 'x' || *strind == 'X')
+    {
       strind++;
       tempHeight = read_integer (strind, &nextCharacter);
       if (strind == nextCharacter)
@@ -257,10 +342,10 @@ XParseGeometry (string, x, y, width, height)
       strind = nextCharacter;
       mask |= HeightValue;
     }
-  
-  if ((*strind == '+') || (*strind == '-')) 
+
+  if ((*strind == '+') || (*strind == '-'))
     {
-      if (*strind == '-') 
+      if (*strind == '-')
        {
          strind++;
          tempX = -read_integer (strind, &nextCharacter);
@@ -271,7 +356,7 @@ XParseGeometry (string, x, y, width, height)
 
        }
       else
-       {       
+       {
          strind++;
          tempX = read_integer (strind, &nextCharacter);
          if (strind == nextCharacter)
@@ -279,9 +364,9 @@ XParseGeometry (string, x, y, width, height)
          strind = nextCharacter;
        }
       mask |= XValue;
-      if ((*strind == '+') || (*strind == '-')) 
+      if ((*strind == '+') || (*strind == '-'))
        {
-         if (*strind == '-') 
+         if (*strind == '-')
            {
              strind++;
              tempY = -read_integer (strind, &nextCharacter);
@@ -302,12 +387,12 @@ XParseGeometry (string, x, y, width, height)
          mask |= YValue;
        }
     }
-  
+
   /* If strind isn't at the end of the string the it's an invalid
      geometry specification. */
-  
+
   if (*strind != '\0') return (0);
-  
+
   if (mask & XValue)
     *x = tempX;
   if (mask & YValue)
@@ -319,17 +404,12 @@ XParseGeometry (string, x, y, width, height)
   return (mask);
 }
 
-/* We can use mouse menus when we wish.  */
-int
-have_menus_p (void)
-{
-  return 1;
-}
-
-/* x_sync is a no-op on Win32.  */
+/* x_sync is a no-op on W32.  */
 void
 x_sync (f)
      void *f;
 {
 }
 
+/* arch-tag: 4fab3695-4ad3-4cc6-a2b1-fd2c67dc46be
+   (do not change this comment) */