Beginnings of support for window stations and desktops.

diff --git a/server/Makefile.in b/server/Makefile.in
index 17239a5..c3fc30e 100644
--- a/server/Makefile.in
+++ b/server/Makefile.in
@@ -44,7 +44,8 @@
 	trace.c \
 	unicode.c \
 	user.c \
-	window.c
+	window.c \
+	winstation.c
 
 PROGRAMS = wineserver
 
diff --git a/server/object.c b/server/object.c
index 479b09c..90aa35c 100644
--- a/server/object.c
+++ b/server/object.c
@@ -129,6 +129,15 @@
     obj->name = ptr;
 }
 
+/* get the name of an existing object */
+const WCHAR *get_object_name( struct object *obj, size_t *len )
+{
+    struct object_name *ptr = obj->name;
+    if (!ptr) return NULL;
+    *len = ptr->len;
+    return ptr->name;
+}
+
 /* allocate and initialize an object */
 void *alloc_object( const struct object_ops *ops )
 {
diff --git a/server/object.h b/server/object.h
index 313ecab..9fd6581 100644
--- a/server/object.h
+++ b/server/object.h
@@ -88,6 +88,7 @@
 extern void *mem_alloc( size_t size );  /* malloc wrapper */
 extern void *memdup( const void *data, size_t len );
 extern void *alloc_object( const struct object_ops *ops );
+extern const WCHAR *get_object_name( struct object *obj, size_t *len );
 extern void dump_object_name( struct object *obj );
 extern void *create_named_object( struct namespace *namespace, const struct object_ops *ops,
                                   const WCHAR *name, size_t len );
diff --git a/server/process.c b/server/process.c
index eb869ec..e69fe1b 100644
--- a/server/process.c
+++ b/server/process.c
@@ -280,6 +280,7 @@
     process->queue           = NULL;
     process->peb             = NULL;
     process->ldt_copy        = NULL;
+    process->winstation      = 0;
     process->exe.file        = NULL;
     process->exe.dbg_offset  = 0;
     process->exe.dbg_size    = 0;
@@ -376,6 +377,10 @@
             return NULL;
     }
 
+    /* connect to the window station and desktop */
+    connect_process_winstation( process, NULL, 0 );
+    connect_thread_desktop( current, NULL, 0 );
+
     /* set the process console */
     if (!set_process_console( process, parent_thread, info, reply )) return NULL;
 
diff --git a/server/process.h b/server/process.h
index d203ebd..c82848b 100644
--- a/server/process.h
+++ b/server/process.h
@@ -72,6 +72,7 @@
     struct startup_info *startup_info;    /* startup info while init is in progress */
     struct event        *idle_event;      /* event for input idle */
     struct msg_queue    *queue;           /* main message queue */
+    obj_handle_t         winstation;      /* main handle to process window station */
     struct token        *token;           /* security token associated with this process */
     struct process_dll   exe;             /* main exe file */
     struct list          dlls;            /* list of loaded dlls */
diff --git a/server/protocol.def b/server/protocol.def
index b828174..108e57b 100644
--- a/server/protocol.def
+++ b/server/protocol.def
@@ -2023,6 +2023,101 @@
 @END
 
 
+/* Create a window station */
+@REQ(create_winstation)
+    unsigned int flags;           /* window station flags */
+    unsigned int access;          /* wanted access rights */
+    int          inherit;         /* inherit flag */
+    VARARG(name,unicode_str);     /* object name */
+@REPLY
+    obj_handle_t handle;          /* handle to the window station */
+@END
+
+
+/* Open a handle to a window station */
+@REQ(open_winstation)
+    unsigned int access;          /* wanted access rights */
+    int          inherit;         /* inherit flag */
+    VARARG(name,unicode_str);     /* object name */
+@REPLY
+    obj_handle_t handle;          /* handle to the window station */
+@END
+
+
+/* Close a window station */
+@REQ(close_winstation)
+    obj_handle_t handle;          /* handle to the window station */
+@END
+
+
+/* Get the process current window station */
+@REQ(get_process_winstation)
+@REPLY
+    obj_handle_t handle;          /* handle to the window station */
+@END
+
+
+/* Set the process current window station */
+@REQ(set_process_winstation)
+    obj_handle_t handle;          /* handle to the window station */
+@END
+
+
+/* Create a desktop */
+@REQ(create_desktop)
+    unsigned int flags;           /* desktop flags */
+    unsigned int access;          /* wanted access rights */
+    int          inherit;         /* inherit flag */
+    VARARG(name,unicode_str);     /* object name */
+@REPLY
+    obj_handle_t handle;          /* handle to the desktop */
+@END
+
+
+/* Open a handle to a desktop */
+@REQ(open_desktop)
+    unsigned int flags;           /* desktop flags */
+    unsigned int access;          /* wanted access rights */
+    int          inherit;         /* inherit flag */
+    VARARG(name,unicode_str);     /* object name */
+@REPLY
+    obj_handle_t handle;          /* handle to the desktop */
+@END
+
+
+/* Close a desktop */
+@REQ(close_desktop)
+    obj_handle_t handle;          /* handle to the desktop */
+@END
+
+
+/* Get the thread current desktop */
+@REQ(get_thread_desktop)
+    thread_id_t  tid;             /* thread id */
+@REPLY
+    obj_handle_t handle;          /* handle to the desktop */
+@END
+
+
+/* Set the thread current desktop */
+@REQ(set_thread_desktop)
+    obj_handle_t handle;          /* handle to the desktop */
+@END
+
+
+/* Get/set information about a user object (window station or desktop) */
+@REQ(set_user_object_info)
+    obj_handle_t handle;          /* handle to the object */
+    unsigned int flags;           /* information to set */
+    unsigned int obj_flags;       /* new object flags */
+@REPLY
+    int          is_desktop;      /* is object a desktop? */
+    unsigned int old_obj_flags;   /* old object flags */
+    VARARG(name,unicode_str);     /* object name */
+@END
+#define SET_USER_OBJECT_FLAGS 1
+
+
 /* Attach (or detach) thread inputs */
 @REQ(attach_thread_input)
     thread_id_t    tid_from;       /* thread to be attached */
diff --git a/server/request.h b/server/request.h
index 8172cdb..b8cd447 100644
--- a/server/request.h
+++ b/server/request.h
@@ -266,6 +266,17 @@
 DECL_HANDLER(remove_window_property);
 DECL_HANDLER(get_window_property);
 DECL_HANDLER(get_window_properties);
+DECL_HANDLER(create_winstation);
+DECL_HANDLER(open_winstation);
+DECL_HANDLER(close_winstation);
+DECL_HANDLER(get_process_winstation);
+DECL_HANDLER(set_process_winstation);
+DECL_HANDLER(create_desktop);
+DECL_HANDLER(open_desktop);
+DECL_HANDLER(close_desktop);
+DECL_HANDLER(get_thread_desktop);
+DECL_HANDLER(set_thread_desktop);
+DECL_HANDLER(set_user_object_info);
 DECL_HANDLER(attach_thread_input);
 DECL_HANDLER(get_thread_input);
 DECL_HANDLER(get_last_input_time);
@@ -465,6 +476,17 @@
     (req_handler)req_remove_window_property,
     (req_handler)req_get_window_property,
     (req_handler)req_get_window_properties,
+    (req_handler)req_create_winstation,
+    (req_handler)req_open_winstation,
+    (req_handler)req_close_winstation,
+    (req_handler)req_get_process_winstation,
+    (req_handler)req_set_process_winstation,
+    (req_handler)req_create_desktop,
+    (req_handler)req_open_desktop,
+    (req_handler)req_close_desktop,
+    (req_handler)req_get_thread_desktop,
+    (req_handler)req_set_thread_desktop,
+    (req_handler)req_set_user_object_info,
     (req_handler)req_attach_thread_input,
     (req_handler)req_get_thread_input,
     (req_handler)req_get_last_input_time,
diff --git a/server/thread.c b/server/thread.c
index 2d7a61b..9d69bf1 100644
--- a/server/thread.c
+++ b/server/thread.c
@@ -137,6 +137,7 @@
     thread->priority        = THREAD_PRIORITY_NORMAL;
     thread->affinity        = 1;
     thread->suspend         = 0;
+    thread->desktop         = 0;
     thread->creation_time   = time(NULL);
     thread->exit_time       = 0;
 
@@ -213,6 +214,7 @@
     free_msg_queue( thread );
     cleanup_clipboard_thread(thread);
     destroy_thread_windows( thread );
+    close_thread_desktop( thread );
     for (i = 0; i < MAX_INFLIGHT_FDS; i++)
     {
         if (thread->inflight[i].client != -1)
@@ -226,6 +228,7 @@
     thread->request_fd = NULL;
     thread->reply_fd = NULL;
     thread->wait_fd = NULL;
+    thread->desktop = 0;
 
     if (thread == booting_thread)  /* killing booting thread */
     {
@@ -831,6 +834,7 @@
     if ((thread = create_thread( request_fd, current->process )))
     {
         if (req->suspend) thread->suspend++;
+        thread->desktop = current->desktop;
         reply->tid = get_thread_id( thread );
         if ((reply->handle = alloc_handle( current->process, thread,
                                            THREAD_ALL_ACCESS, req->inherit )))
diff --git a/server/thread.h b/server/thread.h
index d471686..749b277 100644
--- a/server/thread.h
+++ b/server/thread.h
@@ -82,6 +82,7 @@
     int                    priority;      /* priority level */
     int                    affinity;      /* affinity mask */
     int                    suspend;       /* suspend count */
+    obj_handle_t           desktop;       /* desktop handle */
     time_t                 creation_time; /* Thread creation time */
     time_t                 exit_time;     /* Thread exit time */
     struct token          *token;         /* security token associated with this thread */
diff --git a/server/trace.c b/server/trace.c
index d5fb96a..70129bd 100644
--- a/server/trace.c
+++ b/server/trace.c
@@ -2515,6 +2515,115 @@
     dump_varargs_properties( cur_size );
 }
 
+static void dump_create_winstation_request( const struct create_winstation_request *req )
+{
+    fprintf( stderr, " flags=%08x,", req->flags );
+    fprintf( stderr, " access=%08x,", req->access );
+    fprintf( stderr, " inherit=%d,", req->inherit );
+    fprintf( stderr, " name=" );
+    dump_varargs_unicode_str( cur_size );
+}
+
+static void dump_create_winstation_reply( const struct create_winstation_reply *req )
+{
+    fprintf( stderr, " handle=%p", req->handle );
+}
+
+static void dump_open_winstation_request( const struct open_winstation_request *req )
+{
+    fprintf( stderr, " access=%08x,", req->access );
+    fprintf( stderr, " inherit=%d,", req->inherit );
+    fprintf( stderr, " name=" );
+    dump_varargs_unicode_str( cur_size );
+}
+
+static void dump_open_winstation_reply( const struct open_winstation_reply *req )
+{
+    fprintf( stderr, " handle=%p", req->handle );
+}
+
+static void dump_close_winstation_request( const struct close_winstation_request *req )
+{
+    fprintf( stderr, " handle=%p", req->handle );
+}
+
+static void dump_get_process_winstation_request( const struct get_process_winstation_request *req )
+{
+}
+
+static void dump_get_process_winstation_reply( const struct get_process_winstation_reply *req )
+{
+    fprintf( stderr, " handle=%p", req->handle );
+}
+
+static void dump_set_process_winstation_request( const struct set_process_winstation_request *req )
+{
+    fprintf( stderr, " handle=%p", req->handle );
+}
+
+static void dump_create_desktop_request( const struct create_desktop_request *req )
+{
+    fprintf( stderr, " flags=%08x,", req->flags );
+    fprintf( stderr, " access=%08x,", req->access );
+    fprintf( stderr, " inherit=%d,", req->inherit );
+    fprintf( stderr, " name=" );
+    dump_varargs_unicode_str( cur_size );
+}
+
+static void dump_create_desktop_reply( const struct create_desktop_reply *req )
+{
+    fprintf( stderr, " handle=%p", req->handle );
+}
+
+static void dump_open_desktop_request( const struct open_desktop_request *req )
+{
+    fprintf( stderr, " flags=%08x,", req->flags );
+    fprintf( stderr, " access=%08x,", req->access );
+    fprintf( stderr, " inherit=%d,", req->inherit );
+    fprintf( stderr, " name=" );
+    dump_varargs_unicode_str( cur_size );
+}
+
+static void dump_open_desktop_reply( const struct open_desktop_reply *req )
+{
+    fprintf( stderr, " handle=%p", req->handle );
+}
+
+static void dump_close_desktop_request( const struct close_desktop_request *req )
+{
+    fprintf( stderr, " handle=%p", req->handle );
+}
+
+static void dump_get_thread_desktop_request( const struct get_thread_desktop_request *req )
+{
+    fprintf( stderr, " tid=%04x", req->tid );
+}
+
+static void dump_get_thread_desktop_reply( const struct get_thread_desktop_reply *req )
+{
+    fprintf( stderr, " handle=%p", req->handle );
+}
+
+static void dump_set_thread_desktop_request( const struct set_thread_desktop_request *req )
+{
+    fprintf( stderr, " handle=%p", req->handle );
+}
+
+static void dump_set_user_object_info_request( const struct set_user_object_info_request *req )
+{
+    fprintf( stderr, " handle=%p,", req->handle );
+    fprintf( stderr, " flags=%08x,", req->flags );
+    fprintf( stderr, " obj_flags=%08x", req->obj_flags );
+}
+
+static void dump_set_user_object_info_reply( const struct set_user_object_info_reply *req )
+{
+    fprintf( stderr, " is_desktop=%d,", req->is_desktop );
+    fprintf( stderr, " old_obj_flags=%08x,", req->old_obj_flags );
+    fprintf( stderr, " name=" );
+    dump_varargs_unicode_str( cur_size );
+}
+
 static void dump_attach_thread_input_request( const struct attach_thread_input_request *req )
 {
     fprintf( stderr, " tid_from=%04x,", req->tid_from );
@@ -3106,6 +3215,17 @@
     (dump_func)dump_remove_window_property_request,
     (dump_func)dump_get_window_property_request,
     (dump_func)dump_get_window_properties_request,
+    (dump_func)dump_create_winstation_request,
+    (dump_func)dump_open_winstation_request,
+    (dump_func)dump_close_winstation_request,
+    (dump_func)dump_get_process_winstation_request,
+    (dump_func)dump_set_process_winstation_request,
+    (dump_func)dump_create_desktop_request,
+    (dump_func)dump_open_desktop_request,
+    (dump_func)dump_close_desktop_request,
+    (dump_func)dump_get_thread_desktop_request,
+    (dump_func)dump_set_thread_desktop_request,
+    (dump_func)dump_set_user_object_info_request,
     (dump_func)dump_attach_thread_input_request,
     (dump_func)dump_get_thread_input_request,
     (dump_func)dump_get_last_input_time_request,
@@ -3302,6 +3422,17 @@
     (dump_func)dump_remove_window_property_reply,
     (dump_func)dump_get_window_property_reply,
     (dump_func)dump_get_window_properties_reply,
+    (dump_func)dump_create_winstation_reply,
+    (dump_func)dump_open_winstation_reply,
+    (dump_func)0,
+    (dump_func)dump_get_process_winstation_reply,
+    (dump_func)0,
+    (dump_func)dump_create_desktop_reply,
+    (dump_func)dump_open_desktop_reply,
+    (dump_func)0,
+    (dump_func)dump_get_thread_desktop_reply,
+    (dump_func)0,
+    (dump_func)dump_set_user_object_info_reply,
     (dump_func)0,
     (dump_func)dump_get_thread_input_reply,
     (dump_func)dump_get_last_input_time_reply,
@@ -3498,6 +3629,17 @@
     "remove_window_property",
     "get_window_property",
     "get_window_properties",
+    "create_winstation",
+    "open_winstation",
+    "close_winstation",
+    "get_process_winstation",
+    "set_process_winstation",
+    "create_desktop",
+    "open_desktop",
+    "close_desktop",
+    "get_thread_desktop",
+    "set_thread_desktop",
+    "set_user_object_info",
     "attach_thread_input",
     "get_thread_input",
     "get_last_input_time",
diff --git a/server/user.h b/server/user.h
index 0f66a4d..ff8af79 100644
--- a/server/user.h
+++ b/server/user.h
@@ -118,4 +118,10 @@
 extern atom_t get_class_atom( struct window_class *class );
 extern void *get_class_client_ptr( struct window_class *class );
 
+/* windows station functions */
+
+extern void connect_process_winstation( struct process *process, const WCHAR *name, size_t len );
+extern void connect_thread_desktop( struct thread *thread, const WCHAR *name, size_t len );
+extern void close_thread_desktop( struct thread *thread );
+
 #endif  /* __WINE_SERVER_USER_H */
diff --git a/server/winstation.c b/server/winstation.c
new file mode 100644
index 0000000..f25e210
--- /dev/null
+++ b/server/winstation.c
@@ -0,0 +1,487 @@
+/*
+ * Server-side window stations and desktops handling
+ *
+ * Copyright (C) 2002, 2005 Alexandre Julliard
+ *
+ * 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.1 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; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include "config.h"
+#include "wine/port.h"
+
+#include <stdio.h>
+#include <stdarg.h>
+
+#include "windef.h"
+#include "winbase.h"
+#include "winuser.h"
+
+#include "object.h"
+#include "handle.h"
+#include "request.h"
+#include "process.h"
+#include "wine/unicode.h"
+
+struct winstation
+{
+    struct object      obj;                /* object header */
+    unsigned int       flags;              /* winstation flags */
+    struct list        entry;              /* entry in global winstation list */
+    struct list        desktops;           /* list of desktops of this winstation */
+};
+
+struct desktop
+{
+    struct object      obj;           /* object header */
+    unsigned int       flags;         /* desktop flags */
+    struct winstation *winstation;    /* winstation this desktop belongs to */
+    struct list        entry;         /* entry in winstation list of desktops */
+};
+
+static struct list winstation_list = LIST_INIT(winstation_list);
+static struct winstation *interactive_winstation;
+static struct namespace *winstation_namespace;
+
+static void winstation_dump( struct object *obj, int verbose );
+static void winstation_destroy( struct object *obj );
+static void desktop_dump( struct object *obj, int verbose );
+static void desktop_destroy( struct object *obj );
+
+static const struct object_ops winstation_ops =
+{
+    sizeof(struct winstation),    /* size */
+    winstation_dump,              /* dump */
+    no_add_queue,                 /* add_queue */
+    NULL,                         /* remove_queue */
+    NULL,                         /* signaled */
+    NULL,                         /* satisfied */
+    no_signal,                    /* signal */
+    no_get_fd,                    /* get_fd */
+    winstation_destroy            /* destroy */
+};
+
+
+static const struct object_ops desktop_ops =
+{
+    sizeof(struct desktop),       /* size */
+    desktop_dump,                 /* dump */
+    no_add_queue,                 /* add_queue */
+    NULL,                         /* remove_queue */
+    NULL,                         /* signaled */
+    NULL,                         /* satisfied */
+    no_signal,                    /* signal */
+    no_get_fd,                    /* get_fd */
+    desktop_destroy               /* destroy */
+};
+
+#define DESKTOP_ALL_ACCESS 0x01ff
+
+/* create a winstation object */
+static struct winstation *create_winstation( const WCHAR *name, size_t len, unsigned int flags )
+{
+    struct winstation *winstation;
+
+    if (!winstation_namespace && !(winstation_namespace = create_namespace( 7, FALSE )))
+        return NULL;
+
+    if (memchrW( name, '\\', len / sizeof(WCHAR) ))  /* no backslash allowed in name */
+    {
+        set_error( STATUS_INVALID_PARAMETER );
+        return NULL;
+    }
+
+    if ((winstation = create_named_object( winstation_namespace, &winstation_ops, name, len )))
+    {
+        if (get_error() != STATUS_OBJECT_NAME_COLLISION)
+        {
+            /* initialize it if it didn't already exist */
+            winstation->flags = flags;
+            list_add_tail( &winstation_list, &winstation->entry );
+            list_init( &winstation->desktops );
+        }
+    }
+    return winstation;
+}
+
+static void winstation_dump( struct object *obj, int verbose )
+{
+    struct winstation *winstation = (struct winstation *)obj;
+
+    fprintf( stderr, "Winstation flags=%x ", winstation->flags );
+    dump_object_name( &winstation->obj );
+    fputc( '\n', stderr );
+}
+
+static void winstation_destroy( struct object *obj )
+{
+    struct winstation *winstation = (struct winstation *)obj;
+
+    if (winstation == interactive_winstation) interactive_winstation = NULL;
+    list_remove( &winstation->entry );
+}
+
+/* retrieve the process window station, checking the handle access rights */
+inline static struct winstation *get_process_winstation( struct process *process,
+                                                         unsigned int access )
+{
+    return (struct winstation *)get_handle_obj( process, process->winstation,
+                                                access, &winstation_ops );
+}
+
+/* build the full name of a desktop object */
+static WCHAR *build_desktop_name( const WCHAR *name, size_t len,
+                                  struct winstation *winstation, size_t *res_len )
+{
+    const WCHAR *winstation_name;
+    WCHAR *full_name;
+    size_t winstation_len;
+
+    if (memchrW( name, '\\', len / sizeof(WCHAR) ))
+    {
+        set_error( STATUS_INVALID_PARAMETER );
+        return NULL;
+    }
+
+    if (!(winstation_name = get_object_name( &winstation->obj, &winstation_len )))
+        winstation_len = 0;
+
+    *res_len = winstation_len + len + sizeof(WCHAR);
+    if (!(full_name = mem_alloc( *res_len ))) return NULL;
+    memcpy( full_name, winstation_name, winstation_len );
+    full_name[winstation_len / sizeof(WCHAR)] = '\\';
+    memcpy( full_name + (winstation_len + 1) / sizeof(WCHAR), name, len );
+    return full_name;
+}
+
+/* create a desktop object */
+static struct desktop *create_desktop( const WCHAR *name, size_t len, unsigned int flags,
+                                       struct winstation *winstation )
+{
+    struct desktop *desktop;
+    WCHAR *full_name;
+    size_t full_len;
+
+    if (!(full_name = build_desktop_name( name, len, winstation, &full_len ))) return NULL;
+
+    if ((desktop = create_named_object( winstation_namespace, &desktop_ops, full_name, full_len )))
+    {
+        if (get_error() != STATUS_OBJECT_NAME_COLLISION)
+        {
+            /* initialize it if it didn't already exist */
+            desktop->flags = flags;
+            desktop->winstation = (struct winstation *)grab_object( winstation );
+            list_add_tail( &winstation->desktops, &desktop->entry );
+        }
+    }
+    free( full_name );
+    return desktop;
+}
+
+static void desktop_dump( struct object *obj, int verbose )
+{
+    struct desktop *desktop = (struct desktop *)obj;
+
+    fprintf( stderr, "Desktop flags=%x winstation=%p ", desktop->flags, desktop->winstation );
+    dump_object_name( &desktop->obj );
+    fputc( '\n', stderr );
+}
+
+static void desktop_destroy( struct object *obj )
+{
+    struct desktop *desktop = (struct desktop *)obj;
+
+    list_remove( &desktop->entry );
+    release_object( desktop->winstation );
+}
+
+/* close a desktop handle if allowed */
+static void close_desktop_handle( struct process *process, obj_handle_t handle )
+{
+    struct thread *thread;
+
+    /* make sure it's not in use by any thread in the process */
+    LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
+    {
+        if (thread->desktop == handle)
+        {
+            set_error( STATUS_DEVICE_BUSY );
+            return;
+        }
+    }
+    close_handle( process, handle, NULL );
+}
+
+/* connect a process to its window station */
+void connect_process_winstation( struct process *process, const WCHAR *name, size_t len )
+{
+    struct winstation *winstation;
+
+    if (process->winstation) return;  /* already has one */
+
+    /* check for an inherited winstation handle (don't ask...) */
+    if ((process->winstation = find_inherited_handle( process, &winstation_ops )) != 0) return;
+
+    if (name)
+    {
+        winstation = create_winstation( name, len, 0 );
+    }
+    else
+    {
+        if (!interactive_winstation)
+        {
+            static const WCHAR winsta0W[] = {'W','i','n','S','t','a','0'};
+            interactive_winstation = create_winstation( winsta0W, sizeof(winsta0W), 0 );
+            winstation = interactive_winstation;
+        }
+        else winstation = (struct winstation *)grab_object( interactive_winstation );
+    }
+    if (winstation)
+    {
+        if ((process->winstation = alloc_handle( process, winstation, WINSTA_ALL_ACCESS, FALSE )))
+        {
+            int fd = -1;
+            /* FIXME: Windows doesn't do it this way */
+            set_handle_info( process, process->winstation, HANDLE_FLAG_PROTECT_FROM_CLOSE,
+                             HANDLE_FLAG_PROTECT_FROM_CLOSE, &fd );
+        }
+        release_object( winstation );
+    }
+    clear_error();  /* ignore errors */
+}
+
+/* connect a thread to its desktop */
+void connect_thread_desktop( struct thread *thread, const WCHAR *name, size_t len )
+{
+    struct desktop *desktop;
+    struct winstation *winstation;
+    struct process *process = thread->process;
+
+    if (thread->desktop) return;  /* already has one */
+
+    if ((winstation = get_process_winstation( process, WINSTA_CREATEDESKTOP )))
+    {
+        static const WCHAR defaultW[] = {'D','e','f','a','u','l','t'};
+
+        if (!name)
+        {
+            name = defaultW;
+            len = sizeof(defaultW);
+        }
+        if ((desktop = create_desktop( name, len, 0, winstation )))
+        {
+            thread->desktop = alloc_handle( process, desktop, DESKTOP_ALL_ACCESS, FALSE );
+            release_object( desktop );
+        }
+        release_object( winstation );
+    }
+    clear_error();  /* ignore errors */
+}
+
+/* close the desktop of a given thread */
+void close_thread_desktop( struct thread *thread )
+{
+    obj_handle_t handle = thread->desktop;
+
+    thread->desktop = 0;
+    if (handle) close_desktop_handle( thread->process, handle );
+    clear_error();  /* ignore errors */
+}
+
+
+/* create a window station */
+DECL_HANDLER(create_winstation)
+{
+    struct winstation *winstation;
+
+    reply->handle = 0;
+    if ((winstation = create_winstation( get_req_data(), get_req_data_size(), req->flags )))
+    {
+        reply->handle = alloc_handle( current->process, winstation, req->access, req->inherit );
+        release_object( winstation );
+    }
+}
+
+/* open a handle to a window station */
+DECL_HANDLER(open_winstation)
+{
+    if (winstation_namespace)
+        reply->handle = open_object( winstation_namespace, get_req_data(), get_req_data_size(),
+                                     &winstation_ops, req->access, req->inherit );
+    else
+        set_error( STATUS_OBJECT_NAME_NOT_FOUND );
+}
+
+
+/* close a window station */
+DECL_HANDLER(close_winstation)
+{
+    struct winstation *winstation;
+
+    if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
+                                                           0, &winstation_ops )))
+    {
+        if (req->handle != current->process->winstation)
+            close_handle( current->process, req->handle, NULL );
+        else
+            set_error( STATUS_ACCESS_DENIED );
+        release_object( winstation );
+    }
+}
+
+
+/* get the process current window station */
+DECL_HANDLER(get_process_winstation)
+{
+    reply->handle = current->process->winstation;
+}
+
+
+/* set the process current window station */
+DECL_HANDLER(set_process_winstation)
+{
+    struct winstation *winstation;
+
+    if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
+                                                           0, &winstation_ops )))
+    {
+        /* FIXME: should we close the old one? */
+        current->process->winstation = req->handle;
+        release_object( winstation );
+    }
+}
+
+/* create a desktop */
+DECL_HANDLER(create_desktop)
+{
+    struct desktop *desktop;
+    struct winstation *winstation;
+
+    reply->handle = 0;
+    if ((winstation = get_process_winstation( current->process, WINSTA_CREATEDESKTOP )))
+    {
+        if ((desktop = create_desktop( get_req_data(), get_req_data_size(),
+                                       req->flags, winstation )))
+        {
+            reply->handle = alloc_handle( current->process, desktop, req->access, req->inherit );
+            release_object( desktop );
+        }
+        release_object( winstation );
+    }
+}
+
+/* open a handle to a desktop */
+DECL_HANDLER(open_desktop)
+{
+    struct winstation *winstation;
+
+    if ((winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
+    {
+        size_t full_len;
+        WCHAR *full_name;
+
+        if ((full_name = build_desktop_name( get_req_data(), get_req_data_size(),
+                                             winstation, &full_len )))
+        {
+            reply->handle = open_object( winstation_namespace, full_name, full_len,
+                                         &desktop_ops, req->access, req->inherit );
+            free( full_name );
+        }
+        release_object( winstation );
+    }
+}
+
+
+/* close a desktop */
+DECL_HANDLER(close_desktop)
+{
+    struct desktop *desktop;
+
+    /* make sure it is a desktop handle */
+    if ((desktop = (struct desktop *)get_handle_obj( current->process, req->handle,
+                                                     0, &desktop_ops )))
+    {
+        close_desktop_handle( current->process, req->handle );
+        release_object( desktop );
+    }
+}
+
+
+/* get the thread current desktop */
+DECL_HANDLER(get_thread_desktop)
+{
+    struct thread *thread;
+
+    if (!(thread = get_thread_from_id( req->tid ))) return;
+    reply->handle = thread->desktop;
+    release_object( thread );
+}
+
+
+/* set the thread current desktop */
+DECL_HANDLER(set_thread_desktop)
+{
+    struct desktop *desktop;
+
+    if ((desktop = (struct desktop *)get_handle_obj( current->process, req->handle, 0, &desktop_ops )))
+    {
+        /* FIXME: should we close the old one? */
+        current->desktop = req->handle;
+        release_object( desktop );
+    }
+}
+
+
+/* get/set information about a user object (window station or desktop) */
+DECL_HANDLER(set_user_object_info)
+{
+    struct object *obj;
+
+    if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
+
+    if (obj->ops == &desktop_ops)
+    {
+        struct desktop *desktop = (struct desktop *)obj;
+        reply->is_desktop = 1;
+        reply->old_obj_flags = desktop->flags;
+        if (req->flags & SET_USER_OBJECT_FLAGS) desktop->flags = req->obj_flags;
+    }
+    else if (obj->ops == &winstation_ops)
+    {
+        struct winstation *winstation = (struct winstation *)obj;
+        reply->is_desktop = 0;
+        reply->old_obj_flags = winstation->flags;
+        if (req->flags & SET_USER_OBJECT_FLAGS) winstation->flags = req->obj_flags;
+    }
+    else
+    {
+        set_error( STATUS_OBJECT_TYPE_MISMATCH );
+        release_object( obj );
+        return;
+    }
+    if (get_reply_max_size())
+    {
+        size_t len;
+        const WCHAR *ptr, *name = get_object_name( obj, &len );
+
+        /* if there is a backslash return the part of the name after it */
+        if (name && (ptr = memchrW( name, '\\', len )))
+        {
+            name = ptr + 1;
+            len -= (ptr - name) * sizeof(WCHAR);
+        }
+        if (name) set_reply_data( name, min( len, get_reply_max_size() ));
+    }
+    release_object( obj );
+}