InitOutput.c 30.5 KB
Newer Older
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
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
/*

Copyright 1993, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from The Open Group.

*/

29
30
31
#ifdef HAVE_XWIN_CONFIG_H
#include <xwin-config.h>
#endif
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
32
#include "win.h"
33
#include "winmsg.h"
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
34
#include "winconfig.h"
35
#include "winprefs.h"
36
#ifdef XWIN_CLIPBOARD
37
#include "X11/Xlocale.h"
38
#endif
39
40
41
#ifdef DPMSExtension
#include "dpmsproc.h"
#endif
42
#ifdef __CYGWIN__
43
#include <mntent.h>
44
45
#endif
#if defined(XKB) && defined(WIN32)
46
#include <xkbsrv.h>
47
#endif
48
49
#ifdef RELOCATE_PROJECTROOT
#include <shlobj.h>
50
51
52
53
54
55
56
typedef HRESULT (*SHGETFOLDERPATHPROC)(
    HWND hwndOwner,
    int nFolder,
    HANDLE hToken,
    DWORD dwFlags,
    LPTSTR pszPath
);
57
#endif
58

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
59
60

/*
61
 * References to external symbols
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
62
63
 */

64
65
66
67
extern int			g_iNumScreens;
extern winScreenInfo		g_ScreenInfo[];
extern int			g_iLastScreen;
extern char *			g_pszCommandLine;
68
extern Bool			g_fSilentFatalError;
69
70

extern char *			g_pszLogFile;
71
extern Bool			g_fLogFileChanged;
72
73
74
75
extern int			g_iLogVerbose;
Bool				g_fLogInited;

extern Bool			g_fXdmcpEnabled;
76
#ifdef HAS_DEVWINDOWS
77
extern int			g_fdMessageQueue;
78
#endif
79
80
81
82
83
84
85
86
87
88
89
extern const char *		g_pszQueryHost;
extern HINSTANCE		g_hInstance;

#ifdef XWIN_CLIPBOARD
extern Bool			g_fUnicodeClipboard;
extern Bool			g_fClipboardLaunched;
extern Bool			g_fClipboardStarted;
extern pthread_t		g_ptClipboardProc;
extern HWND			g_hwndClipboard;
extern Bool			g_fClipboard;
#endif
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
90

91
92
93
94
95
96
97
extern HMODULE			g_hmodDirectDraw;
extern FARPROC			g_fpDirectDrawCreate;
extern FARPROC			g_fpDirectDrawCreateClipper;
  
extern HMODULE			g_hmodCommonControls;
extern FARPROC			g_fpTrackMouseEvent;
extern Bool			g_fNoHelpMessageBox;                     
98
extern Bool			g_fSilentDupError;                     
99
100
  
  
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
101
/*
102
 * Function prototypes
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
103
104
 */

105
106
107
108
109
110
111
112
113
#ifdef XWIN_CLIPBOARD
static void
winClipboardShutdown (void);
#endif

#if defined(DDXOSVERRORF)
void
OsVendorVErrorF (const char *pszFormat, va_list va_args);
#endif
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
114

115
116
void
winInitializeDefaultScreens (void);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
117

118
119
static Bool
winCheckDisplayNumber (void);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
120

121
122
void
winLogCommandLine (int argc, char *argv[]);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
123

124
125
126
127
128
void
winLogVersionInfo (void);

Bool
winValidateArgs (void);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
129

130
131
132
133
134
#ifdef RELOCATE_PROJECTROOT
const char *
winGetBaseDir(void);
#endif

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
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
/*
 * For the depth 24 pixmap we default to 32 bits per pixel, but
 * we change this pixmap format later if we detect that the display
 * is going to be running at 24 bits per pixel.
 *
 * FIXME: On second thought, don't DIBs only support 32 bits per pixel?
 * DIBs are the underlying bitmap used for DirectDraw surfaces, so it
 * seems that all pixmap formats with depth 24 would be 32 bits per pixel.
 * Confirm whether depth 24 DIBs can have 24 bits per pixel, then remove/keep
 * the bits per pixel adjustment and update this comment to reflect the
 * situation.  Harold Hunt - 2002/07/02
 */

static PixmapFormatRec g_PixmapFormats[] = {
  { 1,    1,      BITMAP_SCANLINE_PAD },
  { 4,    8,      BITMAP_SCANLINE_PAD },
  { 8,    8,      BITMAP_SCANLINE_PAD },
  { 15,   16,     BITMAP_SCANLINE_PAD },
  { 16,   16,     BITMAP_SCANLINE_PAD },
  { 24,   32,     BITMAP_SCANLINE_PAD },
#ifdef RENDER
  { 32,   32,     BITMAP_SCANLINE_PAD }
#endif
};

const int NUMFORMATS = sizeof (g_PixmapFormats) / sizeof (g_PixmapFormats[0]);

162
163
164
#ifdef XWIN_CLIPBOARD
static void
winClipboardShutdown (void)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
165
{
166
167
168
169
170
171
172
173
174
175
176
177
178
  /* Close down clipboard resources */
  if (g_fClipboard && g_fClipboardLaunched && g_fClipboardStarted)
    {
      /* Synchronously destroy the clipboard window */
      if (g_hwndClipboard != NULL)
	{
	  SendMessage (g_hwndClipboard, WM_DESTROY, 0, 0);
	  /* NOTE: g_hwndClipboard is set to NULL in winclipboardthread.c */
	}
      else
	return;
      
      /* Wait for the clipboard thread to exit */
179
      pthread_join (g_ptClipboardProc, NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
180

181
182
      g_fClipboardLaunched = FALSE;
      g_fClipboardStarted = FALSE;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
183

184
185
186
187
      winDebug ("winClipboardShutdown - Clipboard thread has exited.\n");
    }
}
#endif
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
188
189


190
191
192
193
194
#if defined(DDXBEFORERESET)
/*
 * Called right before KillAllClients when the server is going to reset,
 * allows us to shutdown our seperate threads cleanly.
 */
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
195

196
197
198
199
void
ddxBeforeReset (void)
{
  winDebug ("ddxBeforeReset - Hello\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
200

201
202
203
#ifdef XWIN_CLIPBOARD
  winClipboardShutdown ();
#endif
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
204
}
205
#endif
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
206
207
208
209


/* See Porting Layer Definition - p. 57 */
void
210
ddxGiveUp (void)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
211
{
212
213
  int		i;

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
214
#if CYGDEBUG
215
  winDebug ("ddxGiveUp\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
216
217
#endif

218
219
220
221
222
223
224
225
226
  /* Perform per-screen deinitialization */
  for (i = 0; i < g_iNumScreens; ++i)
    {
      /* Delete the tray icon */
      if (!g_ScreenInfo[i].fNoTrayIcon && g_ScreenInfo[i].pScreen)
 	winDeleteNotifyIcon (winGetScreenPriv (g_ScreenInfo[i].pScreen));
    }

#ifdef XWIN_MULTIWINDOW
227
228
  /* Notify the worker threads we're exiting */
  winDeinitMultiWindowWM ();
229
#endif
230

231
#ifdef HAS_DEVWINDOWS
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
232
233
234
235
236
237
238
239
240
  /* Close our handle to our message queue */
  if (g_fdMessageQueue != WIN_FD_INVALID)
    {
      /* Close /dev/windows */
      close (g_fdMessageQueue);

      /* Set the file handle to invalid */
      g_fdMessageQueue = WIN_FD_INVALID;
    }
241
#endif
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
242

243
  if (!g_fLogInited) {
244
    LogInit (g_pszLogFile, NULL);
245
246
    g_fLogInited = TRUE;
  }  
247
  LogClose ();
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265

  /*
   * At this point we aren't creating any new screens, so
   * we are guaranteed to not need the DirectDraw functions.
   */
  if (g_hmodDirectDraw != NULL)
    {
      FreeLibrary (g_hmodDirectDraw);
      g_hmodDirectDraw = NULL;
      g_fpDirectDrawCreate = NULL;
      g_fpDirectDrawCreateClipper = NULL;
    }

  /* Unload our TrackMouseEvent funtion pointer */
  if (g_hmodCommonControls != NULL)
    {
      FreeLibrary (g_hmodCommonControls);
      g_hmodCommonControls = NULL;
266
      g_fpTrackMouseEvent = (FARPROC) (void (*)(void))NoopDDA;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
267
268
    }
  
269
270
271
272
273
274
275
276
277
278
  /* Free concatenated command line */
  if (g_pszCommandLine)
    {
      free (g_pszCommandLine);
      g_pszCommandLine = NULL;
    }

  /* Remove our keyboard hook if it is installed */
  winRemoveKeyboardHookLL ();

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
279
280
281
282
283
284
285
286
287
288
  /* Tell Windows that we want to end the app */
  PostQuitMessage (0);
}


/* See Porting Layer Definition - p. 57 */
void
AbortDDX (void)
{
#if CYGDEBUG
289
  winDebug ("AbortDDX\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
290
291
292
293
#endif
  ddxGiveUp ();
}

294
#ifdef __CYGWIN__
295
/* hasmntopt is currently not implemented for cygwin */
296
static const char *winCheckMntOpt(const struct mntent *mnt, const char *opt)
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
{
    const char *s;
    size_t len;
    if (mnt == NULL)
        return NULL;
    if (opt == NULL)
        return NULL;
    if (mnt->mnt_opts == NULL)
        return NULL;

    len = strlen(opt);
    s = strstr(mnt->mnt_opts, opt);
    if (s == NULL)
        return NULL;
    if ((s == mnt->mnt_opts || *(s-1) == ',') &&  (s[len] == 0 || s[len] == ','))
        return (char *)opt;
    return NULL;
}

316
static void
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
winCheckMount(void)
{
  FILE *mnt;
  struct mntent *ent;

  enum { none = 0, sys_root, user_root, sys_tmp, user_tmp } 
    level = none, curlevel;
  BOOL binary = TRUE;

  mnt = setmntent("/etc/mtab", "r");
  if (mnt == NULL)
  {
    ErrorF("setmntent failed");
    return;
  }

  while ((ent = getmntent(mnt)) != NULL)
  {
    BOOL system = (strcmp(ent->mnt_type, "system") == 0);
    BOOL root = (strcmp(ent->mnt_dir, "/") == 0);
    BOOL tmp = (strcmp(ent->mnt_dir, "/tmp") == 0);
    
    if (system)
    {
      if (root)
        curlevel = sys_root;
      else if (tmp)
        curlevel = sys_tmp;
      else
        continue;
    }
    else
    {
      if (root)
        curlevel = user_root;
      else if (tmp) 
        curlevel = user_tmp;
      else
        continue;
    }

    if (curlevel <= level)
      continue;
    level = curlevel;

    if (winCheckMntOpt(ent, "binmode") == NULL)
      binary = 0;
    else
      binary = 1;
  }
    
  if (endmntent(mnt) != 1)
  {
    ErrorF("endmntent failed");
    return;
  }
  
 if (!binary) 
   winMsg(X_WARNING, "/tmp mounted int textmode\n"); 
}
377
#else
378
static void
379
380
381
382
383
winCheckMount(void) 
{
}
#endif

384
#ifdef RELOCATE_PROJECTROOT
385
const char * 
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
winGetBaseDir(void)
{
    static BOOL inited = FALSE;
    static char buffer[MAX_PATH];
    if (!inited)
    {
        char *fendptr;
        HMODULE module = GetModuleHandle(NULL);
        DWORD size = GetModuleFileName(module, buffer, sizeof(buffer));
        if (sizeof(buffer) > 0)
            buffer[sizeof(buffer)-1] = 0;
    
        fendptr = buffer + size;
        while (fendptr > buffer)
        {
            if (*fendptr == '\\' || *fendptr == '/')
            {
                *fendptr = 0;
                break;
            }
            fendptr--;
        }
        inited = TRUE;
    }
    return buffer;
}
412
#endif
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
static void
winFixupPaths (void)
{
    BOOL changed_fontpath = FALSE;
    MessageType font_from = X_DEFAULT;
#ifdef RELOCATE_PROJECTROOT
    const char *basedir = winGetBaseDir();
    size_t basedirlen = strlen(basedir);
#endif

#ifdef READ_FONTDIRS
    {
        /* Open fontpath configuration file */
        FILE *fontdirs = fopen(ETCX11DIR "/font-dirs", "rt");
        if (fontdirs != NULL)
        {
            char buffer[256];
            int needs_sep = TRUE; 
            int comment_block = FALSE;

            /* get defautl fontpath */
            char *fontpath = xstrdup(defaultFontPath);
            size_t size = strlen(fontpath);

            /* read all lines */
            while (!feof(fontdirs))
            {
                size_t blen;
                char *hashchar;
                char *str;
                int has_eol = FALSE;

                /* read one line */
                str = fgets(buffer, sizeof(buffer), fontdirs);
                if (str == NULL) /* stop on error or eof */
                    break;

                if (strchr(str, '\n') != NULL)
                    has_eol = TRUE;

                /* check if block is continued comment */
                if (comment_block)
                {
                    /* ignore all input */
                    *str = 0; 
                    blen = 0; 
                    if (has_eol) /* check if line ended in this block */
                        comment_block = FALSE;
                }
                else 
                {
                    /* find comment character. ignore all trailing input */
                    hashchar = strchr(str, '#');
                    if (hashchar != NULL)
                    {
                        *hashchar = 0;
                        if (!has_eol) /* mark next block as continued comment */
                            comment_block = TRUE;
                    }
                }

                /* strip whitespaces from beginning */
                while (*str == ' ' || *str == '\t')
                    str++;

                /* get size, strip whitespaces from end */ 
                blen = strlen(str);
                while (blen > 0 && (str[blen-1] == ' ' || 
                            str[blen-1] == '\t' || str[blen-1] == '\n'))
                {
                    str[--blen] = 0;
                }

                /* still something left to add? */ 
                if (blen > 0)
                {
                    size_t newsize = size + blen;
                    /* reserve one character more for ',' */
                    if (needs_sep)
                        newsize++;

                    /* allocate memory */
                    if (fontpath == NULL)
                        fontpath = malloc(newsize+1);
                    else
                        fontpath = realloc(fontpath, newsize+1);

                    /* add separator */
                    if (needs_sep)
                    {
                        fontpath[size] = ',';
                        size++;
                        needs_sep = FALSE;
                    }

                    /* mark next line as new entry */
                    if (has_eol)
                        needs_sep = TRUE;

                    /* add block */
                    strncpy(fontpath + size, str, blen);
                    fontpath[newsize] = 0;
                    size = newsize;
                }
            }

            /* cleanup */
            fclose(fontdirs);  
            defaultFontPath = xstrdup(fontpath);
            free(fontpath);
            changed_fontpath = TRUE;
            font_from = X_CONFIG;
        }
    }
#endif /* READ_FONTDIRS */
#ifdef RELOCATE_PROJECTROOT
    {
        const char *libx11dir = PROJECTROOT "/lib/X11";
        size_t libx11dir_len = strlen(libx11dir);
        char *newfp = NULL;
        size_t newfp_len = 0;
        const char *endptr, *ptr, *oldptr = defaultFontPath;

        endptr = oldptr + strlen(oldptr);
        ptr = strchr(oldptr, ',');
        if (ptr == NULL)
            ptr = endptr;
        while (ptr != NULL)
        {
            size_t oldfp_len = (ptr - oldptr);
            size_t newsize = oldfp_len;
            char *newpath = malloc(newsize + 1);
            strncpy(newpath, oldptr, newsize);
            newpath[newsize] = 0;


            if (strncmp(libx11dir, newpath, libx11dir_len) == 0)
            {
                char *compose;
                newsize = newsize - libx11dir_len + basedirlen;
                compose = malloc(newsize + 1);  
                strcpy(compose, basedir);
                strncat(compose, newpath + libx11dir_len, newsize - basedirlen);
                compose[newsize] = 0;
                free(newpath);
                newpath = compose;
            }

            oldfp_len = newfp_len;
            if (oldfp_len > 0)
                newfp_len ++; /* space for separator */
            newfp_len += newsize;

            if (newfp == NULL)
                newfp = malloc(newfp_len + 1);
            else
                newfp = realloc(newfp, newfp_len + 1);

            if (oldfp_len > 0)
            {
                strcpy(newfp + oldfp_len, ",");
                oldfp_len++;
            }
            strcpy(newfp + oldfp_len, newpath);

            free(newpath);

            if (*ptr == 0)
            {
                oldptr = ptr;
                ptr = NULL;
            } else
            {
                oldptr = ptr + 1;
                ptr = strchr(oldptr, ',');
                if (ptr == NULL)
                    ptr = endptr;
            }
        } 

        defaultFontPath = xstrdup(newfp);
        free(newfp);
        changed_fontpath = TRUE;
    }
#endif /* RELOCATE_PROJECTROOT */
    if (changed_fontpath)
        winMsg (font_from, "FontPath set to \"%s\"\n", defaultFontPath);

#ifdef RELOCATE_PROJECTROOT
    if (getenv("XKEYSYMDB") == NULL)
    {
        char buffer[MAX_PATH];
        snprintf(buffer, sizeof(buffer), "XKEYSYMDB=%s\\XKeysymDB",
                basedir);
        buffer[sizeof(buffer)-1] = 0;
        putenv(buffer);
    }
611
612
613
614
615
616
617
618
    if (getenv("XERRORDB") == NULL)
    {
        char buffer[MAX_PATH];
        snprintf(buffer, sizeof(buffer), "XERRORDB=%s\\XErrorDB",
                basedir);
        buffer[sizeof(buffer)-1] = 0;
        putenv(buffer);
    }
619
620
621
622
623
624
625
    if (getenv("XLOCALEDIR") == NULL)
    {
        char buffer[MAX_PATH];
        snprintf(buffer, sizeof(buffer), "XLOCALEDIR=%s\\locale",
                basedir);
        buffer[sizeof(buffer)-1] = 0;
        putenv(buffer);
626
    }
627
628
    if (getenv("HOME") == NULL)
    {
629
630
        HMODULE shfolder;
        SHGETFOLDERPATHPROC shgetfolderpath = NULL;
631
632
        char buffer[MAX_PATH + 5];
        strncpy(buffer, "HOME=", 5);
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649

        /* Try to load SHGetFolderPath from shfolder.dll and shell32.dll */
        
        shfolder = LoadLibrary("shfolder.dll");
        /* fallback to shell32.dll */
        if (shfolder == NULL)
            shfolder = LoadLibrary("shell32.dll");

        /* resolve SHGetFolderPath */
        if (shfolder != NULL)
            shgetfolderpath = (SHGETFOLDERPATHPROC)GetProcAddress(shfolder, "SHGetFolderPathA");

        /* query appdata directory */
        if (shgetfolderpath &&
                shgetfolderpath(NULL, CSIDL_APPDATA|CSIDL_FLAG_CREATE, NULL, 0, 
                    buffer + 5) == 0)
        { 
650
            putenv(buffer);
651
652
653
654
655
656
        } else
        {
            winMsg (X_ERROR, "Can not determine HOME directory\n");
        } 
        if (shfolder != NULL)
            FreeLibrary(shfolder);
657
    }
658
659
660
661
662
663
664
665
666
667
668
    if (!g_fLogFileChanged) {
        static char buffer[MAX_PATH];
        DWORD size = GetTempPath(sizeof(buffer), buffer);
        if (size && size < sizeof(buffer))
        {
            snprintf(buffer + size, sizeof(buffer) - size, 
                    "XWin.%s.log", display); 
            buffer[sizeof(buffer)-1] = 0;
            g_pszLogFile = buffer;
            winMsg (X_DEFAULT, "Logfile set to \"%s\"\n", g_pszLogFile);
        }
669
670
671
672
673
674
675
676
677
    }
#ifdef XKB
    {
        static char xkbbasedir[MAX_PATH];

        snprintf(xkbbasedir, sizeof(xkbbasedir), "%s\\xkb", basedir);
        if (sizeof(xkbbasedir) > 0)
            xkbbasedir[sizeof(xkbbasedir)-1] = 0;
        XkbBaseDirectory = xkbbasedir;
Alan Hourihane's avatar
Alan Hourihane committed
678
	XkbBinDirectory = basedir;
679
680
681
682
    }
#endif /* XKB */
#endif /* RELOCATE_PROJECTROOT */
}
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
683
684
685
686

void
OsVendorInit (void)
{
687
688
689
  /* Re-initialize global variables on server reset */
  winInitializeGlobals ();

690
691
692
693
694
  LogInit (NULL, NULL);
  LogSetParameter (XLOG_VERBOSITY, g_iLogVerbose);

  winFixupPaths();

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
695
696
697
698
699
#ifdef DDXOSVERRORF
  if (!OsVendorVErrorFProc)
    OsVendorVErrorFProc = OsVendorVErrorF;
#endif

700
  if (!g_fLogInited) {
701
702
703
704
705
    /* keep this order. If LogInit fails it calls Abort which then calls
     * ddxGiveUp where LogInit is called again and creates an infinite 
     * recursion. If we set g_fLogInited to TRUE before the init we 
     * avoid the second call 
     */  
706
    g_fLogInited = TRUE;
707
    LogInit (g_pszLogFile, NULL);
708
  } 
709
710
711
712
713
714
715
  LogSetParameter (XLOG_FLUSH, 1);
  LogSetParameter (XLOG_VERBOSITY, g_iLogVerbose);
  LogSetParameter (XLOG_FILE_VERBOSITY, 1);

  /* Log the version information */
  if (serverGeneration == 1)
    winLogVersionInfo ();
716

717
718
  winCheckMount();  

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
719
720
721
  /* Add a default screen if no screens were specified */
  if (g_iNumScreens == 0)
    {
722
      winDebug ("OsVendorInit - Creating bogus screen 0\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744

      /* 
       * We need to initialize default screens if no arguments
       * were processed.  Otherwise, the default screens would
       * already have been initialized by ddxProcessArgument ().
       */
      winInitializeDefaultScreens ();

      /*
       * Add a screen 0 using the defaults set by 
       * winInitializeDefaultScreens () and any additional parameters
       * processed by ddxProcessArgument ().
       */
      g_iNumScreens = 1;
      g_iLastScreen = 0;

      /* We have to flag this as an explicit screen, even though it isn't */
      g_ScreenInfo[0].fExplicitScreen = TRUE;
    }
}


745
static void
746
winUseMsg (void)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
747
748
749
750
751
752
753
754
755
756
757
758
759
760
{
  ErrorF ("-depth bits_per_pixel\n"
	  "\tSpecify an optional bitdepth to use in fullscreen mode\n"
	  "\twith a DirectDraw engine.\n");

  ErrorF ("-emulate3buttons [timeout]\n"
	  "\tEmulate 3 button mouse with an optional timeout in\n"
	  "\tmilliseconds.\n");

  ErrorF ("-engine engine_type_id\n"
	  "\tOverride the server's automatically selected engine type:\n"
	  "\t\t1 - Shadow GDI\n"
	  "\t\t2 - Shadow DirectDraw\n"
	  "\t\t4 - Shadow DirectDraw4 Non-Locking\n"
761
762
763
764
#ifdef XWIN_NATIVEGDI
	  "\t\t16 - Native GDI - experimental\n"
#endif
	  );
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
765
766
767
768
769
770
771
772

  ErrorF ("-fullscreen\n"
	  "\tRun the server in fullscreen mode.\n");
  
  ErrorF ("-refresh rate_in_Hz\n"
	  "\tSpecify an optional refresh rate to use in fullscreen mode\n"
	  "\twith a DirectDraw engine.\n");

773
  ErrorF ("-screen scr_num [width height [x y] | [[WxH[+X+Y]][@m]] ]\n"
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
774
	  "\tEnable screen scr_num and optionally specify a width and\n"
775
776
777
	  "\theight and initial position for that screen. Additionally\n"
	  "\ta monitor number can be specified to start the server on,\n"
	  "\tat which point, all coordinates become relative to that\n"
778
      "\tmonitor (Not for Windows NT4 and 95). Examples:\n"
779
780
781
      "\t -screen 0 800x600+100+100@2 ; 2nd monitor offset 100,100 size 800x600\n"
      "\t -screen 0 1024x768@3        ; 3rd monitor size 1024x768\n"
      "\t -screen 0 @1 ; on 1st monitor using its full resolution (the default)\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
782
783
784

  ErrorF ("-lesspointer\n"
	  "\tHide the windows mouse pointer when it is over an inactive\n"
Alexander Gottwald's avatar
Alexander Gottwald committed
785
          "\t" PROJECT_NAME " window.  This prevents ghost cursors appearing where\n"
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
786
787
788
789
790
791
	  "\tthe Windows cursor is drawn overtop of the X cursor\n");

  ErrorF ("-nodecoration\n"
          "\tDo not draw a window border, title bar, etc.  Windowed\n"
	  "\tmode only.\n");

792
793
794
#ifdef XWIN_MULTIWINDOWEXTWM
  ErrorF ("-mwextwm\n"
	  "\tRun the server in multi-window external window manager mode.\n");
Kensuke Matsuzaki's avatar
Kensuke Matsuzaki committed
795
796
797

  ErrorF ("-internalwm\n"
	  "\tRun the internal window manager.\n");
798
799
#endif

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
800
  ErrorF ("-rootless\n"
801
	  "\tRun the server in rootless mode.\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
802

803
#ifdef XWIN_MULTIWINDOW
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
804
  ErrorF ("-multiwindow\n"
805
806
	  "\tRun the server in multi-window mode.\n");
#endif
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
807
808
809
810
811

  ErrorF ("-multiplemonitors\n"
	  "\tEXPERIMENTAL: Use the entire virtual screen if multiple\n"
	  "\tmonitors are present.\n");

812
#ifdef XWIN_CLIPBOARD
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
813
  ErrorF ("-clipboard\n"
814
815
816
817
818
819
	  "\tRun the clipboard integration module.\n"
	  "\tDo not use at the same time as 'xwinclip'.\n");

  ErrorF ("-nounicodeclipboard\n"
	  "\tDo not use Unicode clipboard even if NT-based platform.\n");
#endif
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
820
821
822
823
824
825

  ErrorF ("-scrollbars\n"
	  "\tIn windowed mode, allow screens bigger than the Windows desktop.\n"
	  "\tMoreover, if the window has decorations, one can now resize\n"
	  "\tit.\n");

826
827
828
829
830
831
  ErrorF ("-[no]trayicon\n"
          "\tDo not create a tray icon.  Default is to create one\n"
	  "\ticon per screen.  You can globally disable tray icons with\n"
	  "\t-notrayicon, then enable it for specific screens with\n"
	  "\t-trayicon for those screens.\n");

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
832
833
834
835
836
  ErrorF ("-clipupdates num_boxes\n"
	  "\tUse a clipping region to constrain shadow update blits to\n"
	  "\tthe updated region when num_boxes, or more, are in the\n"
	  "\tupdated region.  Currently supported only by `-engine 1'.\n");

837
#ifdef XWIN_EMULATEPSEUDO
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
838
839
840
841
842
843
  ErrorF ("-emulatepseudo\n"
	  "\tCreate a depth 8 PseudoColor visual when running in\n"
	  "\tdepths 15, 16, 24, or 32, collectively known as TrueColor\n"
	  "\tdepths.  The PseudoColor visual does not have correct colors,\n"
	  "\tand it may crash, but it at least allows you to run your\n"
	  "\tapplication in TrueColor modes.\n");
844
#endif
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
845
846
847
848
849
850
851

  ErrorF ("-[no]unixkill\n"
          "\tCtrl+Alt+Backspace exits the X Server.\n");

  ErrorF ("-[no]winkill\n"
          "\tAlt+F4 exits the X Server.\n");

852
#ifdef XWIN_XF86CONFIG
Egbert Eich's avatar
Egbert Eich committed
853
  ErrorF ("-config\n"
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
854
855
856
857
858
859
          "\tSpecify a configuration file.\n");

  ErrorF ("-keyboard\n"
	  "\tSpecify a keyboard device from the configuration file.\n");
#endif

860
861
862
#ifdef XKB
  ErrorF ("-xkbrules XKBRules\n"
	  "\tEquivalent to XKBRules in XF86Config files.\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
863

864
865
  ErrorF ("-xkbmodel XKBModel\n"
	  "\tEquivalent to XKBModel in XF86Config files.\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
866

867
868
869
  ErrorF ("-xkblayout XKBLayout\n"
	  "\tEquivalent to XKBLayout in XF86Config files.\n"
	  "\tFor example: -xkblayout de\n");
870

871
872
873
  ErrorF ("-xkbvariant XKBVariant\n"
	  "\tEquivalent to XKBVariant in XF86Config files.\n"
	  "\tFor example: -xkbvariant nodeadkeys\n");
874

875
876
877
  ErrorF ("-xkboptions XKBOptions\n"
	  "\tEquivalent to XKBOptions in XF86Config files.\n");
#endif
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
878

879
880
  ErrorF ("-logfile filename\n"
	  "\tWrite logmessages to <filename> instead of /tmp/Xwin.log.\n");
881

882
883
884
885
886
887
888
  ErrorF ("-logverbose verbosity\n"
	  "\tSet the verbosity of logmessages. [NOTE: Only a few messages\n"
	  "\trespect the settings yet]\n"
	  "\t\t0 - only print fatal error.\n"
	  "\t\t1 - print additional configuration information.\n"
	  "\t\t2 - print additional runtime information [default].\n"
	  "\t\t3 - print debugging and tracing information.\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
889

890
891
892
  ErrorF ("-[no]keyhook\n"
	  "\tGrab special windows key combinations like Alt-Tab or the Menu "
          "key.\n These keys are discarded by default.\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
893

894
895
896
897
  ErrorF ("-swcursor\n"
	  "\tDisable the usage of the windows cursor and use the X11 software "
	  "cursor instead\n");
}
898

899
900
901
902
903
/* See Porting Layer Definition - p. 57 */
void
ddxUseMsg(void)
{
  /* Set a flag so that FatalError won't give duplicate warning message */
904
  g_fSilentFatalError = TRUE;
905
906
  
  winUseMsg();  
907

908
909
910
911
912
913
914
915
916
  /* Log file will not be opened for UseMsg unless we open it now */
  if (!g_fLogInited) {
    LogInit (g_pszLogFile, NULL);
    g_fLogInited = TRUE;
  }  
  LogClose ();

  /* Notify user where UseMsg text can be found.*/
  if (!g_fNoHelpMessageBox)
Alexander Gottwald's avatar
Alexander Gottwald committed
917
    winMessageBoxF ("The " PROJECT_NAME " help text has been printed to "
918
919
920
		  "/tmp/XWin.log.\n"
		  "Please open /tmp/XWin.log to read the help text.\n",
		  MB_ICONINFORMATION);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
921
922
}

923
924
925
926
/* ddxInitGlobals - called by |InitGlobals| from os/util.c */
void ddxInitGlobals(void)
{
}
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
927
928
929
930
931
932
933
934
935
936
937
938

/* See Porting Layer Definition - p. 20 */
/*
 * Do any global initialization, then initialize each screen.
 * 
 * NOTE: We use ddxProcessArgument, so we don't need to touch argc and argv
 */

void
InitOutput (ScreenInfo *screenInfo, int argc, char *argv[])
{
  int		i;
939
940
941

  /* Log the command line */
  winLogCommandLine (argc, argv);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
942
943

#if CYGDEBUG
944
  winDebug ("InitOutput\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
945
946
#endif

947
948
949
950
951
952
953
954
955
956
  /* Validate command-line arguments */
  if (serverGeneration == 1 && !winValidateArgs ())
    {
      FatalError ("InitOutput - Invalid command-line arguments found.  "
		  "Exiting.\n");
    }

  /* Check for duplicate invocation on same display number.*/
  if (serverGeneration == 1 && !winCheckDisplayNumber ())
    {
957
958
      if (g_fSilentDupError)
        g_fSilentFatalError = TRUE;  
959
960
961
962
963
      FatalError ("InitOutput - Duplicate invocation on display "
		  "number: %s.  Exiting.\n", display);
    }

#ifdef XWIN_XF86CONFIG
Egbert Eich's avatar
Egbert Eich committed
964
  /* Try to read the xorg.conf-style configuration file */
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
965
  if (!winReadConfigfile ())
966
967
968
969
970
    winErrorFVerb (1, "InitOutput - Error reading config file\n");
#else
  winMsg(X_INFO, "XF86Config is not supported\n");
  winMsg(X_INFO, "See http://x.cygwin.com/docs/faq/cygwin-x-faq.html "
         "for more information\n");
971
  winConfigFiles ();
972
973
974
975
#endif

  /* Load preferences from XWinrc file */
  LoadPreferences();
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
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

  /* Setup global screen info parameters */
  screenInfo->imageByteOrder = IMAGE_BYTE_ORDER;
  screenInfo->bitmapScanlinePad = BITMAP_SCANLINE_PAD;
  screenInfo->bitmapScanlineUnit = BITMAP_SCANLINE_UNIT;
  screenInfo->bitmapBitOrder = BITMAP_BIT_ORDER;
  screenInfo->numPixmapFormats = NUMFORMATS;
  
  /* Describe how we want common pixmap formats padded */
  for (i = 0; i < NUMFORMATS; i++)
    {
      screenInfo->formats[i] = g_PixmapFormats[i];
    }

  /* Load pointers to DirectDraw functions */
  winGetDDProcAddresses ();
  
  /* Detect supported engines */
  winDetectSupportedEngines ();

  /* Load common controls library */
  g_hmodCommonControls = LoadLibraryEx ("comctl32.dll", NULL, 0);

  /* Load TrackMouseEvent function pointer */  
  g_fpTrackMouseEvent = GetProcAddress (g_hmodCommonControls,
					 "_TrackMouseEvent");
  if (g_fpTrackMouseEvent == NULL)
    {
1004
      winErrorFVerb (1, "InitOutput - Could not get pointer to function\n"
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1005
1006
1007
1008
1009
1010
1011
1012
1013
	      "\t_TrackMouseEvent in comctl32.dll.  Try installing\n"
	      "\tInternet Explorer 3.0 or greater if you have not\n"
	      "\talready.\n");

      /* Free the library since we won't need it */
      FreeLibrary (g_hmodCommonControls);
      g_hmodCommonControls = NULL;

      /* Set function pointer to point to no operation function */
1014
      g_fpTrackMouseEvent = (FARPROC) (void (*)(void))NoopDDA;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1015
1016
1017
1018
1019
1020
    }

  /* Store the instance handle */
  g_hInstance = GetModuleHandle (NULL);

  /* Initialize each screen */
1021
  for (i = 0; i < g_iNumScreens; ++i)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1022
1023
1024
1025
1026
1027
1028
1029
    {
      /* Initialize the screen */
      if (-1 == AddScreen (winScreenInit, argc, argv))
	{
	  FatalError ("InitOutput - Couldn't add screen %d", i);
	}
    }

1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
#if defined(XWIN_CLIPBOARD) || defined(XWIN_MULTIWINDOW)

#if defined(XCSECURITY)
  /* Generate a cookie used by internal clients for authorization */
  if (g_fXdmcpEnabled)
    winGenerateAuthorization ();
#endif

  /* Perform some one time initialization */
  if (1 == serverGeneration)
    {
      /*
       * setlocale applies to all threads in the current process.
       * Apply locale specified in LANG environment variable.
       */
      setlocale (LC_ALL, "");
    }
#endif
1048

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1049
#if CYGDEBUG || YES
1050
  winDebug ("InitOutput - Returning.\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1051
1052
#endif
}
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


/*
 * winCheckDisplayNumber - Check if another instance of Cygwin/X is
 * already running on the same display number.  If no one exists,
 * make a mutex to prevent new instances from running on the same display.
 *
 * return FALSE if the display number is already used.
 */

static Bool
winCheckDisplayNumber ()
{
  int			nDisp;
  HANDLE		mutex;
  char			name[MAX_PATH];
  char *		pszPrefix = '\0';
  OSVERSIONINFO		osvi = {0};

  /* Check display range */
  nDisp = atoi (display);
  if (nDisp < 0 || nDisp > 65535)
    {
      ErrorF ("winCheckDisplayNumber - Bad display number: %d\n", nDisp);
      return FALSE;
    }

  /* Set first character of mutex name to null */
  name[0] = '\0';

  /* Get operating system version information */
  osvi.dwOSVersionInfoSize = sizeof (osvi);
  GetVersionEx (&osvi);

  /* Want a mutex shared among all terminals on NT > 4.0 */
  if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT
      && osvi.dwMajorVersion >= 5)
    {
      pszPrefix = "Global\\";
    }

  /* Setup Cygwin/X specific part of name */
1095
  snprintf (name, sizeof(name), "%sCYGWINX_DISPLAY:%d", pszPrefix, nDisp);
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

  /* Windows automatically releases the mutex when this process exits */
  mutex = CreateMutex (NULL, FALSE, name);
  if (!mutex)
    {
      LPVOID lpMsgBuf;

      /* Display a fancy error message */
      FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | 
		     FORMAT_MESSAGE_FROM_SYSTEM | 
		     FORMAT_MESSAGE_IGNORE_INSERTS,
		     NULL,
		     GetLastError (),
		     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		     (LPTSTR) &lpMsgBuf,
		     0, NULL);
      ErrorF ("winCheckDisplayNumber - CreateMutex failed: %s\n",
	      (LPSTR)lpMsgBuf);
      LocalFree (lpMsgBuf);

      return FALSE;
    }
  if (GetLastError () == ERROR_ALREADY_EXISTS)
    {
      ErrorF ("winCheckDisplayNumber - "
Alexander Gottwald's avatar
Alexander Gottwald committed
1121
	      PROJECT_NAME " is already running on display %d\n",
1122
1123
1124
1125
1126
1127
	      nDisp);
      return FALSE;
    }

  return TRUE;
}
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144

#ifdef DPMSExtension
Bool DPMSSupported(void)
{
  return FALSE;
}

void DPMSSet(int level)
{
  return;
}

int DPMSGet(int *plevel)
{
  return 0;
}
#endif