File: GSDisplayServer.m

package info (click to toggle)
gnustep-gui 0.25.0-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,088 kB
  • ctags: 3,417
  • sloc: objc: 153,800; ansic: 18,239; cpp: 579; yacc: 462; makefile: 143; sh: 5
file content (1124 lines) | stat: -rw-r--r-- 31,794 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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
/** <title>GSDisplayServer</title>

   <abstract>Abstract display server class.</abstract>

   Copyright (C) 2002 Free Software Foundation, Inc.

   Author: 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.
*/

#import <Foundation/NSArray.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/NSData.h>
#import <Foundation/NSLock.h>
#import <Foundation/NSRunLoop.h>
#import <Foundation/NSSet.h>
#import <Foundation/NSThread.h>
#import <Foundation/NSGeometry.h>

#import "AppKit/NSApplication.h"
#import "AppKit/NSEvent.h"
#import "AppKit/NSImage.h"
#import "AppKit/NSWindow.h"
#import "GNUstepGUI/GSDisplayServer.h"
#import "GNUstepGUI/GSDragView.h"

#import "GSSlideView.h"

/* Display attributes */
NSString * GSDisplayName = @"DisplayName";
NSString * GSDisplayNumber = @"DisplayNumber";
NSString * GSScreenNumber = @"ScreenNumber";

/* The memory zone where all server objects are allocated from (Contexts
   are also allocated from this zone) */
static NSZone *_globalGSZone = NULL;

/* The current concrete class */
static Class defaultServerClass = NULL;

/* Maps windows to a server */
static NSMapTable *windowmaps = NULL;

/* Lock for use when creating contexts */
static NSRecursiveLock  *serverLock = nil;

static NSString *NSCurrentServerThreadKey;

/** Returns the GSDisplayServer that created the interal
    representation for window. If the internal representation has not
    yet been created (for instance, if the window is deferred), it
    returns the current server */
GSDisplayServer *
GSServerForWindow(NSWindow *window)
{
  int num;

  if (windowmaps == NULL)
    {
      NSLog(@"GSServerForWindow: No window server");
      return nil;
    }

  num = [window windowNumber];
  if (num == 0)
    {
      /* Backend window hasn't been initialized yet, assume current server. */
      return GSCurrentServer();
    }
  return NSMapGet(windowmaps, (void *)(intptr_t)num);
}

/** Returns the current GSDisplayServer */
GSDisplayServer *
GSCurrentServer(void)
{
  NSMutableDictionary *dict = [[NSThread currentThread] threadDictionary];

  return (GSDisplayServer*) [dict objectForKey: NSCurrentServerThreadKey];
}

/**
  <unit>
  <heading>GSDisplayServer</heading>

  <p>This is an abstract class which provides a framework for a device
  independant window server. A window server handles the very basic control
  of the computer display and input. This includes basic window
  creation and handling, event handling, cursors, and providing
  miscellaneous information about the display.
  </p>
  
  <p>Typically a backend library will provide a concrete subclass
  which implements the device specific methods described below.
  </p>

  <p>In almost all cases, you should not call these methods directly
  in an application. You should use the equivalent methods available
  elsewhere in the library (e.g. NSWindow, NSScreen, etc).
  </p>

  </unit> */
  
@implementation GSDisplayServer

+ (void) initialize
{
  if (serverLock == nil)
    {
      [gnustep_global_lock lock];
      if (serverLock == nil)
        {
          serverLock = [NSRecursiveLock new];
          _globalGSZone = NSDefaultMallocZone();
          defaultServerClass = [GSDisplayServer class];
          NSCurrentServerThreadKey  = @"NSCurrentServerThreadKey";
          windowmaps = NSCreateMapTable(NSNonOwnedPointerMapKeyCallBacks,
                                        NSNonOwnedPointerMapValueCallBacks, 20);
        }
      [gnustep_global_lock unlock];
    }
}

/** Set the concrete subclass that will provide the device dependant
    implementation.
*/
+ (void) setDefaultServerClass: (Class)aClass
{
  defaultServerClass = aClass;
}

/** 
    <p>Create a window server with attributes, which contains key/value
    pairs which describe the specifics of how the window server is to
    be initialized. Typically these values are specific to the
    concrete implementation. The current set of attributes that can be
    used with GSDisplayServer is.
   </p>
   <list>
     <item>GSDisplayName</item>
     <item>GSDisplayNumber</item>
     <item>GSScreenNumber</item>
   </list>
   <p>
   GSDisplayName is window server specific and shouldn't be used when
   creating a GSDisplayServer (although you can retrieve the value with
   the -attributes method). On X-Windows the value might be set to something
   like "host:d.s" where host is the host name, d is the display number and
   s is the screen number. GSDisplayNumber indicates the number of the
   display to open. GSScreenNumber indicates the number of the screen to
   display on. If not explicitly set, these attributes may be taked from
   environment variables or from other operating specific information.
   </p>
    <p>In almost all applications one would only create a
    single instance of a window server. Although it is possible, it is
    unlikely that you would need more than one window server (and you
    would have to be very careful how you handled window creation and
    events in this case).</p>
*/
+ (GSDisplayServer *) serverWithAttributes: (NSDictionary *)attributes
{
  GSDisplayServer *server;

  if (self == [GSDisplayServer class])
    {
      server = [[defaultServerClass allocWithZone: _globalGSZone]
	       initWithAttributes: attributes];
    }
  else
    server = [[self allocWithZone: _globalGSZone] 
	       initWithAttributes: attributes];
 
  return AUTORELEASE(server);
}

/** 
    Sets the current server that will be handling windows, events,
    etc. This method must be called after a window server is created
    in order to make it available to the rest of the GUI library
*/
+ (void) setCurrentServer: (GSDisplayServer *)server
{
  NSMutableDictionary *dict = [[NSThread currentThread] threadDictionary];
  if (server)
    [dict setObject: server forKey: NSCurrentServerThreadKey];
  else
    [dict removeObjectForKey: NSCurrentServerThreadKey];
}

/** <init />
    Initializes the server. This typically causes the receiver to 
    <em>connect</em> to the display (e.g. XOpenDisplay () on an X-Windows
    server). 
*/
- (id) initWithAttributes: (NSDictionary *)attributes
{
  self = [super init];
  if (nil == self)
    return nil;

  server_info = [attributes mutableCopy];
  event_queue = [[NSMutableArray allocWithZone: [self zone]]
			initWithCapacity: 32];
  drag_types = NSCreateMapTable(NSIntMapKeyCallBacks,
                NSObjectMapValueCallBacks, 0);

  return self;
}

/** Return information used to create the server */
- (NSDictionary *) attributes
{
  return AUTORELEASE([server_info copy]);
}

/**
   Causes the server to disconnect from the display. If the receiver
   is the current server, it removes itself and sets the current 
   server to nil. Sending any more messages to the receiver after this
   is likely to cause severe problems and probably crash the
   application. 
*/
- (void) closeServer
{
  if (self == GSCurrentServer())
    [GSDisplayServer setCurrentServer: nil];
}

- (void) dealloc
{
  NSMapEnumerator	enumerator;
  void			*key;
  void			*val;

  if (windowmaps != NULL)
    {
      /*
       * Remove the display server from the windows map.
       * This depends on a property of GNUstep map tables, that an
       * enumerated object can safely be removed from the map.
       */
      enumerator = NSEnumerateMapTable(windowmaps);
      while (NSNextMapEnumeratorPair(&enumerator, &key, &val))
        {
          if (val == (void*)self)
            {
              NSMapRemove(windowmaps, key);
            }
        }
      NSEndMapTableEnumeration(&enumerator);
    }

  DESTROY(server_info);
  DESTROY(event_queue);
  NSFreeMapTable(drag_types);
  [super dealloc];
}

- glContextClass
{
  return nil;
}

- glPixelFormatClass
{
  return nil;
}

/** Returns YES if the backend handles window decorations and NO
 * if the gui library must do that instead.
 */
- (BOOL) handlesWindowDecorations
{
  return YES;
}


/* Drag and drop support. */
/** Convienience method that calls -addDragTypes:toWindow: using the
    server that controls win.
*/
+ (BOOL) addDragTypes: (NSArray*)types toWindow: (NSWindow *)win
{
  return [GSServerForWindow(win) addDragTypes: types toWindow: win];
}

/** Convienience method that calls -removeDragTypes:fromWindow: using the
    server that controls win.
*/
+ (BOOL) removeDragTypes: (NSArray*)types fromWindow: (NSWindow *)win
{
  return [GSServerForWindow(win) removeDragTypes: types fromWindow: win];
}

/** Convienience method that calls -dragTypesForWindow: using the
    server that controls win.
*/
+ (NSCountedSet*) dragTypesForWindow: (NSWindow *)win
{
  return [GSServerForWindow(win) dragTypesForWindow: win];
}

/**
 * Add (increment count by 1) each drag type to those registered
 * for the window.  If this results in a change to the types registered
 * in the counted set, return YES, otherwise return NO.
 * Subclasses should override this method, call 'super' and take
 * appropriate action if the method returns 'YES'.
 */
- (BOOL) addDragTypes: (NSArray*)types toWindow: (NSWindow *)win
{
  NSCountedSet	*old = (NSCountedSet*)NSMapGet(drag_types, (void*)win);
  NSEnumerator *drag_enum = [types objectEnumerator];
  id            type;
  NSUInteger	originalCount;

  /*
   * Make sure the set exists.
   */
  if (old == nil)
    {
      old = [NSCountedSet new];
      NSMapInsert(drag_types, (void*)win, (void*)(gsaddr)old);
      RELEASE(old);
    }
  originalCount = [old count];

  while ((type = [drag_enum nextObject]))
    {
      [old addObject: type];
    }
  if ([old count] == originalCount)
    return NO;
  return YES;
}

/**
 * Remove (decrement count by 1) each drag type from those registered
 * for the window.  If this results in a change to the types registered
 * in the counted set, return YES, otherwise return NO.
 * If given 'nil' as the array of types, remove ALL.
 * Subclasses should override this method, call 'super' and take
 * appropriate action if the method returns 'YES'.
 */
- (BOOL) removeDragTypes: (NSArray*)types fromWindow: (NSWindow *)win
{
  NSCountedSet	*old = (NSCountedSet*)NSMapGet(drag_types, (void*)win);
  NSEnumerator *drag_enum = [types objectEnumerator];

  if (types == nil)
    {
      if (old == nil)
	return NO;
      NSMapRemove(drag_types, (void*)win);
      return YES;
    }
  else if (old == nil)
    {
      return NO;
    }
  else
    {
      unsigned	originalCount = [old count];
      id o;

      while ((o = [drag_enum nextObject]))
	{
	  [old removeObject: o];
	}
      if ([old count] == originalCount)
	return NO;
      return YES;
    }
}

/** Returns the drag types set for the window win. */
- (NSCountedSet*) dragTypesForWindow: (NSWindow *)win
{
  return (NSCountedSet*)NSMapGet(drag_types, (void *)win);
}

/** Returns an instance of a class which implements the NSDraggingInfo
    protocol. */
- (id <NSDraggingInfo>) dragInfo
{
  return [GSDragView sharedDragView];
}

- (BOOL) slideImage: (NSImage*)image from: (NSPoint)from to: (NSPoint)to
{
  return [GSSlideView _slideImage: image from: from to: to];
}

- (void) restrictWindow: (int)win toImage: (NSImage*)image
{
  [self subclassResponsibility: _cmd];
}

- (int) findWindowAt: (NSPoint)screenLocation 
           windowRef: (int*)windowRef 
           excluding: (int)win
{
  [self subclassResponsibility: _cmd];
  return 0;
}

/* Screen information */
/** Returns the resolution, in points, for the indicated screen of the
    display. */
- (NSSize) resolutionForScreen: (int)screen
{
  /*[self subclassResponsibility: _cmd];*/
  return NSMakeSize(72, 72);
}

/** Returns the bounds, in pixels, for the indicated screen of the
    display. */
- (NSRect) boundsForScreen: (int)screen
{
  [self subclassResponsibility: _cmd];
  return NSZeroRect;
}

/** Returns the default depth of windows that are created on screen. */
- (NSWindowDepth) windowDepthForScreen: (int)screen
{
  [self subclassResponsibility: _cmd];
  return 0;
}

/** Returns a null terminated list of possible window depths for
    screen. */
- (const NSWindowDepth *) availableDepthsForScreen: (int)screen
{
  [self subclassResponsibility: _cmd];
  return NULL;
}

/**
   Returns an array of NSNumbers, where each number describes a screen
   that is available on this display. The default screen is listed first.
 */
- (NSArray *) screenList
{
  [self subclassResponsibility: _cmd];
  return nil;
}

/**
   Returns a display dependant pointer that describes the internal
   connection to the display. On X-Windows, for example, this is a
   pointer to the <code>Display</code> variable.  */
- (void *) serverDevice
{
  [self subclassResponsibility: _cmd];
  return NULL;
}

/**
   Returns a display dependant pointer that describes the internal
   window representation for win. On X-Windows, for example, this is a
   pointer to the <code>Window</code> variable. */
- (void *) windowDevice: (int)win
{
  [self subclassResponsibility: _cmd];
  return NULL;
}

/** Play the System Beep */
- (void) beep
{
  [self subclassResponsibility: _cmd];
}

/** 
   Returns a display dependent NSImage which will be used as the background
   image for AppIcons and MiniWindows.  Under Windowmaker, for example this 
   could be a user specified gradient. */
- (NSImage *) iconTileImage
{
  return [NSImage imageNamed: @"common_Tile"];
}


/** Returns the size of icons and miniwindows for screen. */
- (NSSize) iconSize
{
  return NSMakeSize(64.0, 64.0);
}

/** 
 * Returns a screenshot of the specified rectangle of the specified screen.
 * The mouse cursor should be ommitted from the returned image.
 */
- (NSImage *) contentsOfScreen: (int)screen inRect: (NSRect)rect
{
  return nil;
}

@end

/* ----------------------------------------------------------------------- */
/* GNUstep Window operations */
/* ----------------------------------------------------------------------- */
@implementation GSDisplayServer (WindowOps)

/** Tells the receiver that it owns the window described by
    win. Concrete subclasses must call this function when creating a
    window. Do not call this method in any other case, particularly
    for a window that has already been created */
- (void) _setWindowOwnedByServer: (int)win
{
  if (windowmaps != NULL)
    {
      NSMapInsert(windowmaps, (void*)(intptr_t)win,  self);
    }
}

/** Creates a window whose location and size is described by frame and
    whose backing store is described by type. This window is not
    mapped to the screen by this call.<br />

    Note that frame is the frame of the entire GNUstep window including
    borders, titlebar and other standard decorations.<br />
    If -handlesWindowDecorations returns YES, the backend will produce
    (and return the identifier of) a smaller drawable window inside this
    decorated area.<br />
    Use -styleoffsets::::: to determine the extent of the decorations
    and determine the size of the drawable area inside them.
*/
- (int) window: (NSRect)frame : (NSBackingStoreType)type : (unsigned int)style
{
  int sn = [[server_info objectForKey: GSScreenNumber] intValue];

  return [self window: frame : type : style : sn];
}

/** Like -window::: only there is an additional argument to specify which
    screen the window will display on */
- (int) window: (NSRect)frame : (NSBackingStoreType)type : (unsigned int)style
	      : (int)screen
{
  [self subclassResponsibility: _cmd];
  return 0;
}

/** Destroys the representation of the window and frees and memory
    associated with it. */
- (void) termwindow: (int) win
{
  [self subclassResponsibility: _cmd];
}

/** Create all the backend structures for a reference to a native window and 
    return the extend, backing type, style and screen for that window. */ 
- (int) nativeWindow: (void *)winref
		    : (NSRect*)frame
		    : (NSBackingStoreType*)type 
		    : (unsigned int*)style
		    : (int*)screen
{
  [self subclassResponsibility: _cmd];
  return 0;
}

/** Sets the style of the window. See [NSWindow-styleMask] for a
    description of the available styles */
- (void) stylewindow: (unsigned int) style : (int) win
{
  [self subclassResponsibility: _cmd];
}

/** Changes window's the backing store to type */
- (void) windowbacking: (NSBackingStoreType)type : (int) win
{
  [self subclassResponsibility: _cmd];
}

/** Sets the window title */
- (void) titlewindow: (NSString *) window_title : (int) win
{
  [self subclassResponsibility: _cmd];
}

/** Miniaturizes the window */
- (void) miniwindow: (int) win
{
  [self subclassResponsibility: _cmd];
}

/** Returns YES if the application should create the miniwindow counterpart
    to the full size window and own it. Some display systems handle the
    miniwindow themselves. In this case the backend subclass should
    override this method to return NO. */
- (BOOL) appOwnsMiniwindow
{
  return YES;
}


/** Sets the window device information for the current NSGraphicsContext,
    typically by calling [NSGraphicsContext-GSSetDevice:::],
    although depending on the concrete implmentation, more information
    than this may need to be exchanged. */
- (void) windowdevice: (int)winNum
{
  [self setWindowdevice: winNum forContext: GSCurrentContext()];
}

/** Sets the window device information for the NSGraphicsContext,
    typically by calling [NSGraphicsContext-GSSetDevice:::],
    although depending on the concrete implmentation, more information
    than this may need to be exchanged. */
- (void) setWindowdevice: (int)win forContext: (NSGraphicsContext *)ctxt
{
  [self subclassResponsibility: _cmd];
}

/**
 * <p>Causes the window to be ordered onto or off the screen depending
 * on the value of op. The window is ordered relative to otherWin.
 * </p>
 * <p>The effect of the various combinations of op and otherWin are:
 * </p>
 * <deflist>
 *   <term>op is NSWindowOut</term>
 *   <desc>
 *     The window is removed from the display and otherWinm is ignored.
 *   </desc>
 *   <term>op is NSWindowAbove and otherWin is zero</term>
 *   <desc>
 *     The window is placed above all other windows at the same level
 *     unless doing the current key window is at this level (in which
 *     case the window will be placed immediately below that).
 *   </desc>
 *   <term>op is NSWindowAbove and otherWin is minus one</term>
 *   <desc>
 *     The window is placed above all other windows at the same level
 *     even if doing that would place it above the current key window.<br />
 *     This is a special feature that [NSWindow-orderWindow:relativeTo:] uses
 *     to place the window correctly.
 *   </desc>
 *   <term>op is NSWindowBelow and otherWin is zero</term>
 *   <desc>
 *     The window is placed above all other windows at the same level.
 *   </desc>
 *   <term>op is NSWindowAbove and otherWin is a window on the display</term>
 *   <desc>
 *     The level of the window is set to be the same as that of
 *     otherWin and the window is placed immediately above otherWin.
 *   </desc>
 *   <term>op is NSWindowBelow and otherWin is a window on the display</term>
 *   <desc>
 *     The level of the window is set to be the same as that of
 *     otherWin and the window is placed immediately below otherWin.
 *   </desc>
 * </deflist>
 */
- (void) orderwindow: (int) op : (int) otherWin : (int) win
{
  [self subclassResponsibility: _cmd];
}

/** Moves the bottom left corner of the window (including any border)
 * to loc.<br />
 * The position is expressed as an offset from the bottom left
 * corner of the screen.
 */ 
- (void) movewindow: (NSPoint)loc : (int) win
{
  [self subclassResponsibility: _cmd];
}

/** Moves and resizes the window on the screen as described by frame.
 * The value of frame is a rectangle containing the entire window, including
 * any border/decorations.  Its position is expressed as an offset from
 * the bottom left corner of the screen.
 */
- (void) placewindow: (NSRect)frame : (int) win
{
  [self subclassResponsibility: _cmd];
}

/** Returns the frame of the window on the screen.<br />
 * The value of frame is a rectangle containing the entire window, including
 * any border/decorations.  Its position is expressed as an offset from
 * the bottom left corner of the screen.
 */
- (NSRect) windowbounds: (int) win
{
  [self subclassResponsibility: _cmd];
  return NSZeroRect;
}

/** Set the level of the window as in the [NSWindow -setLevel] method.<br />
 * The use of window levels organises the window hierarchy into groups
 * of windows at each level.  It effects the operation of the
 * -orderwindow::: method in the case where the position is 'above' or
 * 'below' and the other window number is zero.
 */
- (void) setwindowlevel: (int) level : (int) win
{
  [self subclassResponsibility: _cmd];
}

/** Returns the window level as in [NSWindow -level] */
- (int) windowlevel: (int) win
{
  [self subclassResponsibility: _cmd];
  return 0;
}

/** Backends can override this method to return an array of window numbers 
    ordered front to back.  The front most window being the first object
    in the array.  
    The default implementation returns the visible windows in an
    unspecified order.
 */
- (NSArray *) windowlist
{
  NSMutableArray *list = [NSMutableArray arrayWithArray:[NSApp windows]];
  int c = [list count];

  while (c-- > 0)
    {
       if (![[list objectAtIndex:c] isVisible])
         {
	   [list removeObjectAtIndex:c];
         }
    }
  return [list valueForKey:@"windowNumber"];
}

/** Returns the depth of the window */
- (int) windowdepth: (int) win
{
  [self subclassResponsibility: _cmd];
  return 0;
}

/** Set the maximum size (pixels) of the window */
- (void) setmaxsize: (NSSize)size : (int) win
{
  [self subclassResponsibility: _cmd];
}

/** Set the minimum size (pixels) of the window */
- (void) setminsize: (NSSize)size : (int) win
{
  [self subclassResponsibility: _cmd];
}

/** Set the resize incremenet of the window */
- (void) setresizeincrements: (NSSize)size : (int) win
{
  [self subclassResponsibility: _cmd];
}

/** Causes buffered graphics to be flushed to the screen.
 * The value of rect is expressed in OpenStep window coordinates.
 */
- (void) flushwindowrect: (NSRect)rect : (int) win
{
  [self subclassResponsibility: _cmd];
}

/**
 * Returns the dimensions of window decorations added outside the drawable
 * window frame by a window manager or equivalent. For instance, t
 * gives the height of the title bar for the window.<br />
 * If -handlesWindowDecorations returns NO, there
 * are no decorations outside the drawable window frame and this method
 * shouldn't be called.
 * */
- (void) styleoffsets: (float*) l : (float*) r : (float*) t : (float*) b 
		     : (unsigned int) style
{
  [self subclassResponsibility: _cmd];
}

/** Sets the document edited flag for the window */
- (void) docedited: (int) edited : (int) win
{
  [self subclassResponsibility: _cmd];
}

/** Sets the input state for the window given by the
    GSWindowInputState constant.  Instructs the window manager that the
    specified window is 'key', 'main', or just a normal window.  */
- (void) setinputstate: (int)state : (int)win
{
  [self subclassResponsibility: _cmd];
}

/** Forces focus to the window so that all key events are sent to this
    window */
- (void) setinputfocus: (int) win
{
  [self subclassResponsibility: _cmd];
}

/** Sets the transparancy value for the whole window */
- (void) setalpha: (float)alpha : (int) win
{
  //[self subclassResponsibility: _cmd];
}

/** Sets the window shadow */
- (void) setShadow: (BOOL)hasShadow : (int)win
{
  //[self subclassResponsibility: _cmd];
}

/** Returns the current mouse location on the default screen. If the
 * pointer is not on the default screen, an invalid point (-1,-1} is
 * returned.<br />
 * The location is expressed as an offset from the bottom left corner
 * of the screen.
 */
- (NSPoint) mouselocation
{
  [self subclassResponsibility: _cmd];
  return NSZeroPoint;
}

/** Returns the current mouse location on aScreen. If the pointer is
 * not on aScreen, this method acts like -mouselocation. If aScreen is -1,
 * then the location of the mouse on any screen is returned. The
 * win pointer returns the window number of the GNUstep window
 * that the mouse is in or 0 if it is not in a window.<br />
 * The location is expressed as an offset from the bottom left corner
 * of the screen.
 */
- (NSPoint) mouseLocationOnScreen: (int)aScreen window: (int *)win
{
  [self subclassResponsibility: _cmd];
  return NSZeroPoint;
}

/** Grabs the pointer device so that all future mouse events will be
    directed only to the window win. If successful, the return value
    is YES and this message must be balanced by a -releasemouse
    message.  */
- (BOOL) capturemouse: (int) win
{
  [self subclassResponsibility: _cmd];
  return NO;
}

/** Release a previous captured mouse from -capturemouse: */
- (void) releasemouse
{
  [self subclassResponsibility: _cmd];
}

/** Set mouse cursor position. */
- (void) setMouseLocation: (NSPoint)mouseLocation onScreen: (int)aScreen
{
  [self subclassResponsibility: _cmd];
}

/** Hides the cursor */
- (void) hidecursor
{
  [self subclassResponsibility: _cmd];
}

/** Show a previously hidden cursor */
- (void) showcursor
{
  [self subclassResponsibility: _cmd];
}

/** Create a standard cursor (such as an arror or IBeam). Returns a
    pointer to the internal device representation that can be used
    later to make this cursor the current one
*/
- (void) standardcursor: (int) style : (void**) cid
{
  [self subclassResponsibility: _cmd];
}

/** Create a cursor from an image. Returns a pointer to the internal
    device representation that can be used later to make this cursor
    the current one */
- (void) imagecursor: (NSPoint)hotp : (NSImage *) image : (void**) cid
{
  [self subclassResponsibility: _cmd];
}

/** Set the cursor given by the cid representation as being the
    current cursor. The cursor has a foreground color fg and a
    background color bg. To keep the default color for the cursor, pass
    nil for fg and bg. */
- (void) setcursorcolor: (NSColor *)fg : (NSColor *)bg : (void*) cid
{
  NSLog(@"Call to obsolete method -setcursorcolor:::");
  [self recolorcursor: fg : bg : cid];
  [self setcursor: cid];
}

/** Recolour the cursor given by the cid representation into having
    a foreground color fg and a background color bg. */
- (void) recolorcursor: (NSColor *)fg : (NSColor *)bg : (void*) cid
{
  [self subclassResponsibility: _cmd];
}

/** Set the cursor given by the cid representation as being the
    current cursor. */
- (void) setcursor: (void*) cid
{
  [self subclassResponsibility: _cmd];
}

/** Free the cursor given by the cid representation. */
- (void) freecursor: (void*) cid
{
  [self subclassResponsibility: _cmd];
}

- (void) setParentWindow: (int)parentWin 
          forChildWindow: (int)childWin
{
  [self subclassResponsibility: _cmd];
}

- (void) setIgnoreMouse: (BOOL)ignoreMouse : (int)win
{
  // Do nothing if not overridden by subclass
}

@end

/* ----------------------------------------------------------------------- */
/* GNUstep Event Operations */
/* ----------------------------------------------------------------------- */
@implementation GSDisplayServer (EventOps)

/**
 * Scans through the event queue to find the first event whose type matches
 * mask.  If no event is found, then the current run loop is run in the
 * specified mode to allow more events to arrive.<br />
 * If a matching event is found, it is returned and either removed from or
 * left in the queue according to flag.<br />
 * If no matching event is found and the limit date is reached, this method
 * returns nil.
 */
- (NSEvent*) getEventMatchingMask: (unsigned)mask
		       beforeDate: (NSDate*)limit
			   inMode: (NSString*)mode
			  dequeue: (BOOL)flag
{
  NSUInteger pos = 0;	/* Position in queue scanned so far	*/
  NSRunLoop *loop = nil;

  do
    {
      NSUInteger count = [event_queue count];
      NSEvent *event;
      NSUInteger i = 0;

      if (count == 0)
	{
	  event = nil;
	}
      else if (mask == NSAnyEventMask)
	{
	  /*
	   * Special case - if the mask matches any event, we just get the
	   * first event on the queue.
	   */
	  event = [event_queue objectAtIndex: 0];
	}
      else
	{
	  event = nil;
	  /*
	   * Scan the queue from the last position we have seen, up to the end.
	   */
	  if (count > pos)
	    {
	      NSUInteger end = count - pos;
	      NSRange	r = NSMakeRange(pos, end);
	      NSEvent	*events[end];

	      [event_queue getObjects: events range: r];
	      for (i = 0; i < end; i++)
		{
		  if (mask & NSEventMaskFromType([events[i] type]))
		    {
		      event = events[i];
		      break;
		    }
		}
	    }
	}

      /*
       * Note the position we have read up to.
       */
      pos += i;

      /*
       * If we found a matching event, we (depending on the flag) de-queue it.
       * We return the event RETAINED - the caller must release it.
       */
      if (event)
	{
	  RETAIN(event);
	  if (flag)
	    {
	      [event_queue removeObjectAtIndex: pos];
	    }
	  return AUTORELEASE(event);
	}
      if (loop == nil)
	{
	  loop = [NSRunLoop currentRunLoop];
	}
      if ([loop runMode: mode beforeDate: limit] == NO)
	{
	  break;	// Nothing we can do ... no input handlers.
	}
    }
  while ([limit timeIntervalSinceNow] > 0.0);

  return nil;	/* No events in specified time	*/
}

/**
 * Steps through the event queue and removes all events whose timestamp
 * is earlier than that of limit wand which match the supplied mask
 * of event types.
 */
- (void) discardEventsMatchingMask: (unsigned)mask
		       beforeEvent: (NSEvent*)limit
{
  NSUInteger index = [event_queue count];

  /*
   *	If there is a range to use - remove all the matching events in it
   *    which were created before the specified event.
   */
  if (index > 0)
    {
      NSTimeInterval when = [limit timestamp];
      NSEvent *events[index];

      [event_queue getObjects: events];

      while (index-- > 0)
	{
	  NSEvent *event = events[index];

	  if ([event timestamp] < when)
	    {	
	      if ((mask == NSAnyEventMask)
		|| (mask & NSEventMaskFromType([event type])))
		{
		  [event_queue removeObjectAtIndex: index];
		}
	    }
	}
    }
}

/** Posts an event to the event queue.  The value of flag determines
 * whether the event is inserted at the start of the queue or appended
 * at the end.
 */
- (void) postEvent: (NSEvent*)anEvent atStart: (BOOL)flag
{
  if (flag)
    [event_queue insertObject: anEvent atIndex: 0];
  else
    [event_queue addObject: anEvent];
}

- (void) _printEventQueue
{
  NSUInteger index = [event_queue count];

  if (index > 0)
    {
      NSEvent *events[index];
      NSUInteger i;

      NSLog(@"Dumping events from queue");
      [event_queue getObjects: events];

      for (i = 0; i < index; i++)
	{
	  NSEvent *event = events[i];

          NSLog(@"index %lu %@", (unsigned long) i, event);
        }
    }
  else
    {
      NSLog(@"Event queue is empty");
    }
}

@end