/**
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.
Typically a backend library will provide a concrete subclass which implements the device specific methods described below.
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).
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.
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.
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).
*/ + (GSDisplayServer *) serverWithAttributes: (NSDictionary *)attributes { GSDisplayServer *server; if (windowmaps == NULL) { windowmaps = NSCreateMapTable(NSNonOwnedPointerMapKeyCallBacks, NSNonOwnedPointerMapValueCallBacks, 20); } 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]; } /**Display
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 Window
variable. */
- (void *) windowDevice: (int)win
{
[self subclassResponsibility: _cmd];
return NULL;
}
/** Play the System Beep */
- (void) beep
{
[self subclassResponsibility: _cmd];
}
@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
{
NSMapInsert (windowmaps, (void*)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.
Note that frame is the frame of the drawable window and does not include
any external window decorations. If handlesWindowDecorations returns YES,
a window manager (or something equivalent) might add decorations outside
the drawable window. Use -styleoffsets::::: to determine the extent of
those decorations.
*/
- (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];
}
/** 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) win
{
[self subclassResponsibility: _cmd];
}
/** Causes the window to be ordered onto or off the screen depending
on the value of op. The window is ordered relative to otherWin.
The window will never be ordered in front of the current key/main
window except in the special case where otherWin is negative (This
is a special feature that [NSWindow-orderWindow:relativeTo:] uses
to place the window correctly).
*/
- (void) orderwindow: (int) op : (int) otherWin : (int) win
{
[self subclassResponsibility: _cmd];
}
/** Moves the bottom left cornder of the window to loc */
- (void) movewindow: (NSPoint)loc : (int) win
{
[self subclassResponsibility: _cmd];
}
/** Moves and resizes the window on the screen as described by frame. */
- (void) placewindow: (NSRect)frame : (int) win
{
[self subclassResponsibility: _cmd];
}
/** Retuns the frame of the window on the screen */
- (NSRect) windowbounds: (int) win
{
[self subclassResponsibility: _cmd];
return NSZeroRect;
}
/** Set the level of the window as in [NSWindow -setLevel] */
- (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;
}
/** Returns the list of windows that the server controls */
- (NSArray *) windowlist
{
[self subclassResponsibility: _cmd];
return nil;
}
/** Returns the depth of the window */
- (int) windowdepth: (int) win
{
[self subclassResponsibility: _cmd];
return 0;
}
/** Set the maximum size of the window */
- (void) setmaxsize: (NSSize)size : (int) win
{
[self subclassResponsibility: _cmd];
}
/** Set the minimum size 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 */
- (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. The values returned
may be approximations. 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];
}
/** 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. */
- (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. */
- (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];
}
/** 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 : (int)w : (int)h : (int) colors
: (const char*) 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
{
[self subclassResponsibility: _cmd];
}
@end
/* ----------------------------------------------------------------------- */
/* GNUstep Event Operations */
/* ----------------------------------------------------------------------- */
@implementation GSDisplayServer (EventOps)
- (NSEvent*) getEventMatchingMask: (unsigned)mask
beforeDate: (NSDate*)limit
inMode: (NSString*)mode
dequeue: (BOOL)flag
{
unsigned pos = 0; /* Position in queue scanned so far */
NSRunLoop *loop = nil;
do
{
unsigned count = [event_queue count];
NSEvent *event;
unsigned 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)
{
unsigned 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 positon 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 */
}
- (void) discardEventsMatchingMask: (unsigned)mask
beforeEvent: (NSEvent*)limit
{
unsigned 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];
}
}
}
}
- (void) postEvent: (NSEvent*)anEvent atStart: (BOOL)flag
{
if (flag)
[event_queue insertObject: anEvent atIndex: 0];
else
[event_queue addObject: anEvent];
}
@end