File: XGServer.m

package info (click to toggle)
gnustep-back 0.14.0-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,416 kB
  • ctags: 1,025
  • sloc: objc: 35,429; ansic: 6,048; makefile: 212; sh: 116
file content (789 lines) | stat: -rw-r--r-- 21,606 bytes parent folder | download
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
/* -*- mode:ObjC -*-
   XGServer - X11 Server Class

   Copyright (C) 1998,2002 Free Software Foundation, Inc.

   Written by:  Adam Fedor <fedor@gnu.org>
   Date: Mar 2002
   
   This file is part of the GNU Objective C User Interface Library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; see the file COPYING.LIB.
   If not, see <http://www.gnu.org/licenses/> or write to the 
   Free Software Foundation, 51 Franklin Street, Fifth Floor, 
   Boston, MA 02110-1301, USA.
*/

#include "config.h"
#include <AppKit/AppKitExceptions.h>
#include <AppKit/NSApplication.h>
#include <AppKit/NSView.h>
#include <AppKit/NSWindow.h>
#include <Foundation/NSException.h>
#include <Foundation/NSArray.h>
#include <Foundation/NSConnection.h>
#include <Foundation/NSDictionary.h>
#include <Foundation/NSData.h>
#include <Foundation/NSRunLoop.h>
#include <Foundation/NSValue.h>
#include <Foundation/NSString.h>
#include <Foundation/NSUserDefaults.h>
#include <Foundation/NSDebug.h>

#include <signal.h>
/* Terminate cleanly if we get a signal to do so */
static void
terminate(int sig)
{
  [NSApp terminate: NSApp];
}

#ifdef HAVE_WRASTER_H
#include "wraster.h"
#else
#include "x11/wraster.h"
#endif

#include "x11/XGServer.h"
#include "x11/XGInputServer.h"
#ifdef HAVE_GLX
#include "x11/XGOpenGL.h"
#endif 

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>

extern int XGErrorHandler(Display *display, XErrorEvent *err);

static NSString *
_parse_display_name(NSString *name, int *dn, int *sn)
{
  int d, s;
  NSString *host;
  NSArray *a;

  host = @"";
  d = s = 0;
  a = [name componentsSeparatedByString: @":"];
  if (name == nil)
    {
      NSLog(@"X DISPLAY environment variable not set,"
            @" assuming local X server (DISPLAY=:0.0)");
    }
  else if ([name hasPrefix: @":"] == YES)
    {
      int bnum;
      bnum = sscanf([name cString], ":%d.%d", &d, &s);
      if (bnum == 1)
        s = 0;
      if (bnum < 1)
        d = 0;
    }  
  else if ([a count] != 2)
    {
      NSLog(@"X DISPLAY environment variable has bad format,"
            @" assuming local X server (DISPLAY=:0.0)");
    }
  else
    {
      int bnum;
      NSString *dnum;
      host = [a objectAtIndex: 0];
      dnum = [a lastObject];
      bnum = sscanf([dnum cString], "%d.%d", &d, &s);
      if (bnum == 1)
        s = 0;
      if (bnum < 1)
        d = 0;
    }
  if (dn)
    *dn = d;
  if (sn)
    *sn = s;
  return host;
}

@interface XGServer (Window)
- (void) _setupRootWindow;
@end

@interface XGServer (Private)
- (void) setupRunLoopInputSourcesForMode: (NSString*)mode; 
@end

@interface XGScreenContext : NSObject
{
  RContext *rcontext;
  XGDrawMechanism drawMechanism;
}

- initForDisplay: (Display *)dpy screen: (int)screen_number;
- (XGDrawMechanism) drawMechanism;
- (RContext *) context;
@end

@implementation XGScreenContext

- (RContextAttributes *) _getXDefaults
{
  int dummy;
  RContextAttributes *attribs;

  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  attribs = (RContextAttributes *)malloc(sizeof(RContextAttributes));

  attribs->flags = 0;
  if ([defaults boolForKey: @"NSDefaultVisual"])
    attribs->flags |= RC_DefaultVisual;
  if ((dummy = [defaults integerForKey: @"NSDefaultVisual"]))
    {
      attribs->flags |= RC_VisualID;
      attribs->visualid = dummy;
    }
  if ((dummy = [defaults integerForKey: @"NSColorsPerChannel"]))
    {
      attribs->flags |= RC_ColorsPerChannel;
      attribs->colors_per_channel = dummy;
    }

  return attribs;
}

- initForDisplay: (Display *)dpy screen: (int)screen_number
{
  RContextAttributes *attribs;
  XColor testColor;
  unsigned char r, g, b;

   /* Get the visual information */
  attribs = NULL;
  //attribs = [self _getXDefaults];
  rcontext = RCreateContext(dpy, screen_number, attribs);

  /*
   * If we have shared memory available, only use it when the XGPS-Shm
   * default is set to YES
   */
  if (rcontext->attribs->use_shared_memory == True
    && [[NSUserDefaults standardUserDefaults] boolForKey: @"XGPS-Shm"] != YES)
    rcontext->attribs->use_shared_memory = False;

  /*
   *        Crude tests to see if we can accelerate creation of pixels from
   *        8-bit red, green and blue color values.
   */
  if (rcontext->depth == 12 || rcontext->depth == 16)
    {
      drawMechanism = XGDM_FAST16;
      r = 8;
      g = 9;
      b = 7;
      testColor.pixel = (((r << 5) + g) << 6) + b;
      XQueryColor(rcontext->dpy, rcontext->cmap, &testColor);
      if (((testColor.red >> 11) != r)
        || ((testColor.green >> 11) != g)
        || ((testColor.blue >> 11) != b))
        {
          NSLog(@"WARNING - XGServer is unable to use the "
            @"fast algorithm for writing to a 16-bit display on "
            @"this host - perhaps you'd like to adjust the code "
            @"to work ... and submit a patch.");
          drawMechanism = XGDM_PORTABLE;
        }
    }
  else if (rcontext->depth == 15)
    {
      drawMechanism = XGDM_FAST15;
      r = 8;
      g = 9;
      b = 7;
      testColor.pixel = (((r << 5) + g) << 5) + b;
      XQueryColor(rcontext->dpy, rcontext->cmap, &testColor);
      if (((testColor.red >> 11) != r)
        || ((testColor.green >> 11) != g)
        || ((testColor.blue >> 11) != b))
        {
          NSLog(@"WARNING - XGServer is unable to use the "
            @"fast algorithm for writing to a 15-bit display on "
            @"this host - perhaps you'd like to adjust the code "
            @"to work ... and submit a patch.");
          drawMechanism = XGDM_PORTABLE;
        }
    }
  else if (rcontext->depth == 24 || rcontext->depth == 32)
    {
      drawMechanism = XGDM_FAST32;
      r = 32;
      g = 33;
      b = 31;
      testColor.pixel = (((r << 8) + g) << 8) + b;
      XQueryColor(rcontext->dpy, rcontext->cmap, &testColor);
      if (((testColor.red >> 8) == r)
        && ((testColor.green >> 8) == g)
        && ((testColor.blue >> 8) == b))
        {
          drawMechanism = XGDM_FAST32;
        }
      else if (((testColor.red >> 8) == b)
        && ((testColor.green >> 8) == g)
        && ((testColor.blue >> 8) == r))
        {
          drawMechanism = XGDM_FAST32_BGR;
        }
      else
        {
          NSLog(@"WARNING - XGServer is unable to use the "
            @"fast algorithm for writing to a 32-bit display on "
            @"this host - perhaps you'd like to adjust the code "
            @"to work ... and submit a patch.");
          drawMechanism = XGDM_PORTABLE;
        }
    }
  else if (rcontext->depth == 8)
    {
      drawMechanism = XGDM_FAST8;
      r = 2;
      g = 3;
      b = 1;
      testColor.pixel = (((r << 3) + g) << 2) + b;
      XQueryColor(rcontext->dpy, rcontext->cmap, &testColor);
      if (((testColor.red >> 13) != r)
        || ((testColor.green >> 13) != g)
        || ((testColor.blue >> 14) != b))
        {
          NSLog(@"WARNING - XGServer is unable to use the "
            @"fast algorithm for writing to an 8-bit display on "
            @"this host - the most likely reason being "
            @"the StandardColormap RGB_BEST_MAP has not been installed.");
          drawMechanism = XGDM_PORTABLE;
        }
    }
  else
    {
      NSLog(@"WARNING - XGServer is unable to use a "
        @"fast algorithm for writing to the display on "
        @"this host - perhaps you'd like to adjust the code "
        @"to work ... and submit a patch.");
      drawMechanism = XGDM_PORTABLE;
    }
  NSDebugLLog(@"XGTrace", @"Draw mech %d for screen %d", drawMechanism,
        screen_number);
  return self;
}

- (void) dealloc
{
#ifndef HAVE_WRASTER_H
  // FIXME: context.c does not include a clean up function for Rcontext, 
  // so we try do it here.

  /*
  Note that this can be done only when we use our own version of wraster.
  If we're using an external version of wraster, the rcontext structure
  might be different (different fields, or differently used fields), which
  breaks this code.
  */
  
  if (rcontext)
    {
      XFreeGC(rcontext->dpy, rcontext->copy_gc);
      if (rcontext->drawable)
        {
          XDestroyWindow(rcontext->dpy, rcontext->drawable);
        }
      if (rcontext->pixels)
        {
          free(rcontext->pixels);
        }
      if (rcontext->colors)
        {
          free(rcontext->colors);
        }
      if (rcontext->hermes_data)
        {
          free(rcontext->hermes_data);
        }
      free(rcontext->attribs);
      free(rcontext);
    }
#endif
  [super dealloc];
}

- (XGDrawMechanism) drawMechanism
{
  return drawMechanism;
}

- (RContext *) context
{
  return rcontext;
}

@end


/**
   <unit>
   <heading>XGServer</heading>

   <p> XGServer is a concrete subclass of GSDisplayServer that handles
   X-Window client communications. The class is broken into four sections.
   The main class handles setting up and closing down the display, as well
   as providing wrapper methods to access display and screen pointers. The
   WindowOps category handles window creating, display, movement, and
   other functions detailed in the GSDisplayServer(WindowOps) category.
   The EventOps category handles events received from X-Windows and the
   window manager. It implements the methods defined in the
   GSDisplayServer(EventOps) category. The last section 
   </unit>
*/
@implementation XGServer 

/* Initialize AppKit backend */
+ (void) initializeBackend
{
  NSDebugLog(@"Initializing GNUstep x11 backend.\n");
  [GSDisplayServer setDefaultServerClass: [XGServer class]];
  signal(SIGTERM, terminate);
  signal(SIGINT, terminate);
}

/**
   Returns a pointer to the current X-Windows display variable for
   the current context.
*/
+ (Display*) currentXDisplay
{
  return [(XGServer*)GSCurrentServer() xDisplay];
}

- (id) _initXContext
{
  int screen_number, display_number;
  NSString *display_name;

  display_name = [server_info objectForKey: GSDisplayName];
  if (display_name == nil)
    {
      NSString *host = [[NSUserDefaults standardUserDefaults]
                           stringForKey: @"NSHost"];
      NSString *dn = [server_info objectForKey: GSDisplayNumber];
      NSString *sn = [server_info objectForKey: GSScreenNumber];

      if (dn || sn)
        {
          if (dn == NULL)
            dn = @"0";
          if (sn == NULL)
            sn = @"0";
          if (host == nil)
            host = @"";
          display_name = [NSString stringWithFormat: @"%@:%@.%@", host, dn,sn];
        }
      else if ((host != nil) && ([host isEqual: @""] == NO))
        {
          /**
           * If the NSHost default told us to display somewhere, we need
           * to generate a display name for X from the host name and the
           * default display and screen numbers (zero).
           */
          display_name = [NSString stringWithFormat: @"%@:0.0", host];
        }
    }

  if (display_name)
    {
      dpy = XOpenDisplay([display_name cString]);
    }
  else
    { 
      dpy = XOpenDisplay(NULL);
      display_name = [NSString stringWithCString: XDisplayName(NULL)];
    }

  if (dpy == NULL)
    {
      char *dname = XDisplayName([display_name cString]);
      [NSException raise: NSWindowServerCommunicationException
                  format: @"Unable to connect to X Server `%s'", dname];
    }

  /* Parse display information */
  _parse_display_name(display_name, &display_number, &screen_number);
  NSDebugLog(@"Opened display %@, display %d screen %d", 
             display_name, display_number, screen_number);
  [server_info setObject: display_name forKey: GSDisplayName];
  [server_info setObject: [NSNumber numberWithInt: display_number]
                  forKey: GSDisplayNumber];
  [server_info setObject: [NSNumber numberWithInt: screen_number] 
                  forKey: GSScreenNumber];

  /* Setup screen*/
  if (screenList == NULL)
    screenList = NSCreateMapTable(NSIntMapKeyCallBacks,
                                 NSObjectMapValueCallBacks, 20);

  defScreen = screen_number;

  XSetErrorHandler(XGErrorHandler);

  if (GSDebugSet(@"XSynchronize") == YES)
    XSynchronize(dpy, True);

  [self _setupRootWindow];
  inputServer = [[XIMInputServer allocWithZone: [self zone]] 
                  initWithDelegate: nil display: dpy name: @"XIM"];
  return self;
}

/**
   Opens the X display (using a helper method) and sets up basic
   display mechanisms, such as visuals and colormaps.
*/
- (id) initWithAttributes: (NSDictionary *)info
{
  [super initWithAttributes: info];
  [self _initXContext];

  [self setupRunLoopInputSourcesForMode: NSDefaultRunLoopMode]; 
  [self setupRunLoopInputSourcesForMode: NSConnectionReplyMode]; 
  [self setupRunLoopInputSourcesForMode: NSModalPanelRunLoopMode]; 
  [self setupRunLoopInputSourcesForMode: NSEventTrackingRunLoopMode]; 
  return self;
}

/**
   Closes all X resources, the X display and dealloc other ivars.
*/
- (void) dealloc
{
  NSDebugLog(@"Destroying X11 Server");
  DESTROY(inputServer);
  [self _destroyServerWindows];
  NSFreeMapTable(screenList);
  XCloseDisplay(dpy);
  [super dealloc];
}

/**
  Returns a pointer to the X windows display variable
*/
- (Display *) xDisplay
{
  return dpy;
}

- (XGScreenContext *) _screenContextForScreen: (int)screen_number
{
  int count = ScreenCount(dpy);
  XGScreenContext *screen;

  if (screen_number >= count)
    {
      [NSException raise: NSInvalidArgumentException
                   format: @"Request for invalid screen"];
    }

  screen = NSMapGet(screenList, (void *)(uintptr_t)screen_number);
  if (screen == NULL)
    {
      screen = [[XGScreenContext alloc] 
                   initForDisplay: dpy screen: screen_number];
      NSMapInsert(screenList, (void *)(uintptr_t)screen_number, (void *)screen);
      RELEASE(screen);
    }

  return screen;
}

/**
   Returns a pointer to a structure which describes aspects of the
   X windows display 
*/
- (void *) xrContextForScreen: (int)screen_number
{
  return [[self _screenContextForScreen: screen_number] context];
}

/**
   Returns the XGDrawMechanism, which roughly describes the depth of
   the screen and how pixels should be drawn to the screen for maximum
   speed.
*/
- (XGDrawMechanism) drawMechanismForScreen: (int)screen_number
{
 return [[self _screenContextForScreen: screen_number] drawMechanism];
}

/**
   Returns the root window of the display 
*/
- (Window) xDisplayRootWindowForScreen: (int)screen_number;
{
  return RootWindow(dpy, screen_number);
}

/**
   Returns the closest color in the current colormap to the indicated
   X color
*/
- (XColor) xColorFromColor: (XColor)color forScreen: (int)screen_number
{
  Status ret;
  RColor rcolor;
  RContext *context = [self xrContextForScreen: screen_number];
  XAllocColor(dpy, context->cmap, &color);
  rcolor.red   = color.red / 256;
  rcolor.green = color.green / 256;
  rcolor.blue  = color.blue / 256;
  ret = RGetClosestXColor(context, &rcolor, &color);
  if (ret == False)
    NSLog(@"Failed to alloc color (%d,%d,%d)\n",
          (int)rcolor.red, (int)rcolor.green, (int)rcolor.blue);
  return color;
}

/**
   Returns the application root window, which is used for many things
   such as window hints 
*/
- (Window) xAppRootWindow
{
  return generic.appRootWindow;
}


/**
  Wait for all contexts to finish processing. Only used with XDPS graphics.
*/
+ (void) waitAllContexts
{
  if ([[GSCurrentContext() class] 
        respondsToSelector: @selector(waitAllContexts)])
    [[GSCurrentContext() class] waitAllContexts];
}

- (void) beep
{
  XBell(dpy, 0);
}

- glContextClass
{
#ifdef HAVE_GLX
  return [XGGLContext class];
#else
  return nil;
#endif
}

- glPixelFormatClass
{
#ifdef HAVE_GLX
  return [XGGLPixelFormat class];
#else
  return nil;
#endif
}


@end

@implementation XGServer (InputMethod)
- (NSString *) inputMethodStyle
{
  return inputServer ? [(XIMInputServer *)inputServer inputMethodStyle]
    : (NSString*)nil;
}

- (NSString *) fontSize: (int *)size
{
  return inputServer ? [(XIMInputServer *)inputServer fontSize: size]
    : (NSString*)nil;
}

- (BOOL) clientWindowRect: (NSRect *)rect
{
  return inputServer
    ? [(XIMInputServer *)inputServer clientWindowRect: rect] : NO;
}

- (BOOL) statusArea: (NSRect *)rect
{
  return inputServer ? [(XIMInputServer *)inputServer statusArea: rect] : NO;
}

- (BOOL) preeditArea: (NSRect *)rect
{
  return inputServer ? [(XIMInputServer *)inputServer preeditArea: rect] : NO;
}

- (BOOL) preeditSpot: (NSPoint *)p
{
  return inputServer ? [(XIMInputServer *)inputServer preeditSpot: p] : NO;
}

- (BOOL) setStatusArea: (NSRect *)rect
{
  return inputServer
    ? [(XIMInputServer *)inputServer setStatusArea: rect] : NO;
}

- (BOOL) setPreeditArea: (NSRect *)rect
{
  return inputServer
    ? [(XIMInputServer *)inputServer setPreeditArea: rect] : NO;
}

- (BOOL) setPreeditSpot: (NSPoint *)p
{
  return inputServer
    ? [(XIMInputServer *)inputServer setPreeditSpot: p] : NO;
}

@end // XGServer (InputMethod)


//==== Additional code for NSTextView =========================================
//
//  WARNING  This section is not genuine part of the XGServer implementation.
//  -------
//
//  The methods implemented in this section override some of the internal
//  methods defined in NSTextView so that the class can support input methods
//  (XIM) in cooperation with XGServer.
//
//  Note that the orverriding is done by defining the methods in a category,
//  the name of which is not explicitly mentioned in NSTextView.h; the
//  category is called 'InputMethod'.
//

#include <AppKit/NSClipView.h>
#include <AppKit/NSTextView.h>

@implementation NSTextView (InputMethod)

- (void) _updateInputMethodState
{
  NSRect frame;
  int font_size;
  NSRect status_area;
  NSRect preedit_area;
  id displayServer = (XGServer *)GSCurrentServer();

  if (![displayServer respondsToSelector: @selector(inputMethodStyle)])
    return;

  if (![displayServer fontSize: &font_size])
    return;

  if ([[self superview] isKindOfClass: [NSClipView class]])
    frame = [[self superview] frame];
  else
    frame = [self frame];

  status_area.size.width  = 2 * font_size;
  status_area.size.height = font_size + 2;
  status_area.origin.x    = 0;
  status_area.origin.y    = frame.size.height - status_area.size.height;

  if ([[displayServer inputMethodStyle] isEqual: @"OverTheSpot"])
    {
      preedit_area.origin.x    = 0;
      preedit_area.origin.y    = 0;
      preedit_area.size.width  = frame.size.width;
      preedit_area.size.height = status_area.size.height;

      [displayServer setStatusArea: &status_area];
      [displayServer setPreeditArea: &preedit_area];
    }
  else if ([[displayServer inputMethodStyle] isEqual: @"OffTheSpot"])
    {
      preedit_area.origin.x    = status_area.size.width + 2;
      preedit_area.origin.y    = status_area.origin.y;
      preedit_area.size.width  = frame.origin.x + frame.size.width
        - preedit_area.origin.x;
      preedit_area.size.height = status_area.size.height;

      [displayServer setStatusArea: &status_area];
      [displayServer setPreeditArea: &preedit_area];
    }
  else
    {
      // Do nothing for the RootWindow style.
    }
}

- (void) _updateInputMethodWithInsertionPoint: (NSPoint)insertionPoint
{
  id displayServer = (XGServer *)GSCurrentServer();

  if (![displayServer respondsToSelector: @selector(inputMethodStyle)])
    return;

  if ([[displayServer inputMethodStyle] isEqual: @"OverTheSpot"])
    {
      id view;
      NSRect frame;
      NSPoint p;
      NSRect client_win_rect;
      NSPoint screenXY_of_frame;
      double x_offset;
      double y_offset;
      int font_size;
      NSRect doc_rect;
      NSRect doc_visible_rect;
      BOOL cond;
      float x = insertionPoint.x;
      float y = insertionPoint.y;

      [displayServer clientWindowRect: &client_win_rect];
      [displayServer fontSize: &font_size];

      cond = [[self superview] isKindOfClass: [NSClipView class]];
      if (cond)
        view = [self superview];
      else
        view = self;

      frame = [view frame];
      screenXY_of_frame = [[view window] convertBaseToScreen: frame.origin];

      // N.B. The window of NSTextView isn't necessarily the same as the input
      // method's client window.
      x_offset = screenXY_of_frame.x - client_win_rect.origin.x; 
      y_offset = (client_win_rect.origin.y + client_win_rect.size.height)
        - (screenXY_of_frame.y + frame.size.height) + font_size;

      x += x_offset;
      y += y_offset;
      if (cond) // If 'view' is of NSClipView, then
        {
          // N.B. Remember, (x, y) are the values with respect to NSTextView.
          // We need to know the corresponding insertion position with respect
          // to NSClipView.
          doc_rect = [(NSClipView *)view documentRect];
          doc_visible_rect = [view documentVisibleRect];
          y -= doc_visible_rect.origin.y - doc_rect.origin.y;
        }

      p = NSMakePoint(x, y);
      [displayServer setPreeditSpot: &p];
    }
}

@end // NSTextView
//==== End: Additional Code for NSTextView ====================================