summaryrefslogtreecommitdiff
path: root/xc/programs/Xserver/hw/svga/svgaInit.c
blob: 2f107b8b9023795f668132ea2524ddfc6dacccfa (plain)
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
/* $XConsortium: svgaInit.c,v 1.8 93/09/26 12:26:33 rws Exp $ */
/*
 * Copyright 1990,91,92,93 by Thomas Roell, Germany.
 * Copyright 1991,92,93    by SGCS (Snitily Graphics Consulting Services), USA.
 *
 * 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, and that the name of Thomas Roell nor
 * SGCS be used in advertising or publicity pertaining to distribution
 * of the software without specific, written prior permission.
 * Thomas Roell nor SGCS makes no representations about the suitability
 * of this software for any purpose. It is provided "as is" without
 * express or implied warranty.
 *
 * THOMAS ROELL AND SGCS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL THOMAS ROELL OR SGCS BE LIABLE FOR ANY
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "svga.h"
#include "servermd.h"
#include "mi.h"
#include "mipointer.h"
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <sys/kd.h>
#include <sys/proc.h>
#include <sys/seg.h>
#include <sys/stat.h>
#include <sys/sysi86.h>
#include <sys/tss.h>
#include <sys/v86.h>
#include <sys/vt.h>

extern char *display;
extern Bool mieqInit();
extern void ProcessInputEvents();

#ifndef PATH_MAX
#define PATH_MAX 256
#endif

char *svgaconfig = SVGACONFIG;

static const PixmapFormatRec svgaFormats[] = {
  {  1,  1, BITMAP_SCANLINE_PAD },
  {  8,  8, BITMAP_SCANLINE_PAD },
};

#define NUMFORMATS (sizeof(svgaFormats) / sizeof(svgaFormats[0]))

int svgaConsoleFd = -1;
static int svgaDefaultMode = -1;
static int svgaVTNo = -1;

/* ARGSUSED */

static void
svgaWakeup(
    pointer blockData,
    int     err,
    pointer pReadmask
)
{
  if (svgaInputPending) ProcessInputEvents();
  if (svgaVTRequestsPending) svgaVTSwitch();
}

void
OsVendorInit()
{
  FILE             *fp;
  int              fd, pid;
  char             fname[PATH_MAX];
  struct vt_mode   VT;
  static Bool      inited = FALSE;
  static struct    kd_quemode xqueMode;

  if (!inited)
    {
      inited = TRUE;

      /*
       * USL compatibilty stuff ...
       */
      sprintf(fname, "/dev/X/server.%s.pid", display);

      /*
       * First check whether there is such a file, and if so check
       * for the server whether it is running, or just a dead file ...
       */
      if ((fp = fopen(fname, "r"))) {
	fscanf(fp, "%d", &pid);

	if (kill(pid, 0) == 0) {
	  sprintf(fname, "/dev/X/server.%s", display);
	  if (open(fname, O_RDWR) != -1) {
	    ErrorF("Server on display %s is already running\n", display);
	    exit(1);
	  }
	}
      }

      /*
       * Ok, no other server is running, just setup all files for this
       * new one ....
       */
      fclose(fp);
      sprintf(fname, "/dev/X/Nserver.%s",    display); unlink(fname);
      sprintf(fname, "/dev/X/server.%s",     display); unlink(fname);
      sprintf(fname, "/dev/X/server.%s.pid", display); unlink(fname);

      if (!mkdir("/dev/X", 0777)) chmod("/dev/X", 0777);

      if ((fp = fopen(fname, "w"))) {
	fprintf(fp, "%-5ld\n", getpid());
	fclose(fp);
      }
      else
	ErrorF("Cannot write to %s\n", fname);

      /*
       * Get the correct VT number once at startup time
       */
      if (svgaVTNo == -1){
	if ((fd = open("/dev/console",O_WRONLY | O_NOCTTY)) <0) 
	  FatalError("Cannot open /dev/console\n");

	if (ioctl(fd, VT_OPENQRY, &svgaVTNo) < 0 || svgaVTNo == -1) 
	  FatalError("Cannot find a free VT\n");

	close(fd);
      }

      sprintf(fname, "/dev/vt%02d", svgaVTNo);

      fclose(stdin);
      fclose(stdout);

      /*
       * For XQUEUE device we MUST force a new process group in order to
       * let the kd driver be attached to the mouse device ...
       */
      setpgrp ();

      if ((svgaConsoleFd = open(fname, O_RDWR | O_NONBLOCK)) < 0)
	FatalError ("Couldn't open %s\n", fname);

      if ((svgaDefaultMode = ioctl(svgaConsoleFd, CONS_GET, 0)) < 0)
	FatalError("CONS_GET failed\n");

      if (ioctl(svgaConsoleFd, VT_GETMODE, &VT) < 0) 
        FatalError ("VT_GETMODE failed\n");

      VT.mode = VT_PROCESS;
      VT.relsig = SIGUSR2;
      VT.acqsig = SIGUSR2;

      if (ioctl(svgaConsoleFd, VT_SETMODE, &VT) < 0) 
	FatalError ("VT_SETMODE VT_PROCESS failed\n");

      if (ioctl(svgaConsoleFd, KDSETMODE, KD_GRAPHICS) < 0)
	FatalError ("KDSETMODE KD_GRAPHICS failed\n");

      if (open("/dev/mouse", O_RDONLY | O_NONBLOCK) < 0)
	FatalError("Cannot open /dev/mouse\n");
  
      xqueMode.qsize = 64;    /* max events */
      xqueMode.signo = SIGUSR1;
      if (ioctl(svgaConsoleFd, KDQUEMODE, &xqueMode) < 0)
	FatalError("Cannot set KDQUEMODE\n");
      
      XqueQaddr = (xqEventQueue *)xqueMode.qaddr;
      XqueQaddr->xq_sigenable = 0; /* LOCK */
    }

  /*
   * Get the IOPL for EVERY port
   */
  if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) == -1) {
    FatalError("Failed to aquire IO permissions\n");
  }
}

/*
 * InitOutput --
 *	Initialize screenInfo for all actually accessible framebuffers.
 *      That includes vt-manager setup, querying all possible devices and
 *      collecting the pixmap formats.
 */

void
InitOutput(
    ScreenInfo *pScreenInfo,
    int        argc,
    char       **argv
)
{
  int i;
  
  /*
   * now force to get the VT
   */
  if (ioctl(svgaConsoleFd, VT_ACTIVATE, svgaVTNo) != 0)
    ErrorF("VT_ACTIVATE failed\n");
  if (ioctl(svgaConsoleFd, VT_WAITACTIVE, svgaVTNo) != 0)
    ErrorF("VT_WAITACTIVE failed\n");

  svgaVTActive = TRUE;

  /*
   * Use the previous collected parts to setup pScreenInfo
   */
  pScreenInfo->imageByteOrder = IMAGE_BYTE_ORDER;
  pScreenInfo->bitmapScanlineUnit = BITMAP_SCANLINE_UNIT;
  pScreenInfo->bitmapScanlinePad = BITMAP_SCANLINE_PAD;
  pScreenInfo->bitmapBitOrder = BITMAP_BIT_ORDER;
  pScreenInfo->numPixmapFormats = NUMFORMATS;
  for ( i=0; i < NUMFORMATS; i++ ) pScreenInfo->formats[i] = svgaFormats[i];

  AddScreen(svgaSVGAScreenInit, argc, argv);

  RegisterBlockAndWakeupHandlers((BlockHandlerProcPtr)NoopDDA,
				 svgaWakeup, (void *)0);
}



/*
 * InitInput --
 *      Initialize all supported input devices...what else is there
 *      besides pointer and keyboard? Two DeviceRec's are allocated and
 *      registered as the system pointer and keyboard devices.
 */

/* ARGSUSED */
void
InitInput(
    int  argc,
    char **argv
)
{
  DevicePtr pKeyboard, pPointer;

  svgaVTRequestsPending = FALSE;

  (void) OsSignal(SIGUSR1, XqueRequest);
  (void) OsSignal(SIGUSR2, svgaVTRequest);
  
  pKeyboard = AddInputDevice(XqueKeyboardProc, TRUE); 
  pPointer  = AddInputDevice(XquePointerProc, TRUE);

  if (pKeyboard && pPointer) {
    RegisterKeyboardDevice(pKeyboard); 
    RegisterPointerDevice(pPointer); 

    miRegisterPointerDevice(screenInfo.screens[0], pPointer);
    mieqInit(pKeyboard, pPointer);
  }

  svgaInputPending = FALSE;
}



/*
 * ddxGiveUp --
 *      Device dependent cleanup. Called by by dix before normal server death.
 *      For SVR4.2 we must switch the terminal back to normal mode. No error-
 *      checking here, since there should be restored as much as possible.
 */

void
ddxGiveUp()
{
  struct vt_mode VT;
  char           fname[PATH_MAX];

  /*
   * If we are on the active VT we have to restore text mode correctly,
   * hence leave all graphics modes and unmap everything ...
   */

  (void)ioctl(svgaConsoleFd, KDQUEMODE, NULL);
  (void)ioctl(svgaConsoleFd, VT_ACTIVATE, svgaVTNo);
  (void)ioctl(svgaConsoleFd, VT_WAITACTIVE, 0);

  (void)ioctl(svgaConsoleFd, MODESWITCH | svgaDefaultMode, 0);
  (void)ioctl(svgaConsoleFd, KDSETMODE, KD_TEXT0);

  if (ioctl(svgaConsoleFd, VT_GETMODE, &VT) != -1) {
    VT.mode = VT_AUTO;
    (void)ioctl(svgaConsoleFd, VT_SETMODE, &VT);
  }

  /*
   * Garbage collection ...
   */
  sprintf(fname, "/dev/X/Nserver.%s",    display); unlink(fname);
  sprintf(fname, "/dev/X/server.%s",     display); unlink(fname);
  sprintf(fname, "/dev/X/server.%s.pid", display); unlink(fname);

  close(svgaConsoleFd);                    /* make the vt-manager happy */
}



/*
 * AbortDDX --
 *      DDX - specific abort routine.  Called by AbortServer(). The attempt is
 *      made to restore all original setting of the displays. Also all devices
 *      are closed.
 */

void
AbortDDX()
{
  DevicePtr pKeyboard, pPointer;

  /*
   * try to deinitialize all input devices
   */
  if ((pKeyboard = LookupKeyboardDevice()) != NULL)
    (void)XqueKeyboardProc((DeviceIntPtr)pKeyboard, DEVICE_CLOSE);
  if ((pPointer = LookupPointerDevice()) != NULL)
    (void)XquePointerProc((DeviceIntPtr)pPointer, DEVICE_CLOSE);

  if (screenInfo.screens[0]) svgaSVPMISetText(screenInfo.screens[0]);

  /*
   * This is needed for a abnormal server exit, since the normal exit stuff
   * MUST also be performed (i.e. the vt must be left in a defined state)
   */

  ddxGiveUp();
}


/*
 * ddxProcessArgument --
 *	Process device-dependent command line args. Returns 0 if argument is
 *      not device dependent, otherwise Count of number of elements of argv
 *      that are part of a device dependent commandline option.
 */

/* ARGSUSED */
int
ddxProcessArgument(
    int  argc,
    char *argv[],
    int  i
)
{
  if (!strncmp(argv[i], "vt", 2)) {
    if (sscanf(argv[i] +2, "%d", &svgaVTNo) != 1) {
      ErrorF("bad argument \"%s\"\n", argv[i]);
      return 0;
    }
    return 1;
  }
  else if (!strcmp(argv[i], "-config")) {
    if (i + 1 < argc)
      svgaconfig = argv[i + 1];
    return 2;
  }

  return 0;
}


/*
 * ddxUseMsg --
 *	Print out correct use of device dependent commandline options.
 *      Maybe the user now knows what really to do ...
 */

void
ddxUseMsg()
{
  ErrorF("\n");
  ErrorF("\n");
  ErrorF("Device Dependent Usage:\n");
  ErrorF("\n");
  ErrorF("vtxx                   server should use virtual terminal xx\n");
  ErrorF("-config \"PMIFILE:XxY\"  configuration\n");
  ErrorF("\n");
}