summaryrefslogtreecommitdiff
path: root/console-client/console.c
blob: 019ea3f1572855446ff535974c9df460fe666e63 (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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/* console.c -- A pluggable console client.
   Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
   Written by Marcus Brinkmann.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   This program 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
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#include <argp.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <wchar.h>
#include <error.h>
#include <assert.h>

#include <cthreads.h>

#include <hurd/console.h>
#include <hurd/cons.h>

#include <version.h>

#include "driver.h"
#include "timer.h"
#include "trans.h"

const char *cons_client_name = "console";
const char *cons_client_version = HURD_VERSION;

/* The default node on which the console node is started.  */
#define DEFAULT_CONSOLE_NODE	"/dev/cons"


/* The global lock protects the active_vcons variable, and thus all
   operations on the virtual console that is currently active.  */
static struct mutex global_lock;

/* The active virtual console.  This is the one currently
   displayed.  */
static vcons_t active_vcons = NULL;

/* Contains the VT id when switched away.  */
static int saved_id = 0;

/* The console, used to switch back.  */
static cons_t saved_cons;

/* The file name of the node on which the console translator is
   set.  */
static char *console_node;


/* Callbacks for input source drivers.  */

/* Switch the active console to console ID or DELTA (relative to the
   active console).  */
error_t
console_switch (int id, int delta)
{
  error_t err = 0;
  vcons_t vcons;
  vcons_t new_vcons;

  /* We must give up our global lock before we can call back into
     libcons.  This is because cons_switch will lock CONS, and as
     other functions in libcons lock CONS while calling back into our
     functions which take the global lock (like cons_vcons_add), we
     would deadlock.  So we acquire a reference for VCONS to make sure
     it isn't deallocated while we are outside of the global lock.  We
     also know that */

  mutex_lock (&global_lock);
  vcons = active_vcons;
  if (!vcons)
    {
      mutex_unlock (&global_lock);
      return EINVAL;
    }
  ports_port_ref (vcons);
  mutex_unlock (&global_lock);

  err = cons_switch (vcons, id, delta, &new_vcons);
  if (!err)
    {
      mutex_lock (&global_lock);
      if (active_vcons != new_vcons)
        {
          cons_vcons_close (active_vcons);
          active_vcons = new_vcons;
        }
      mutex_unlock (&new_vcons->lock);
      ports_port_deref (vcons);
      mutex_unlock (&global_lock);
    }
  return err;
}


/* Enter SIZE bytes from the buffer BUF into the currently active
   console.  This can be called by the input driver at any time.  */
error_t
console_input (char *buf, size_t size)
{
  error_t err = 0;
  vcons_t vcons;

  mutex_lock (&global_lock);
  vcons = active_vcons;
  if (!vcons)
    {
      mutex_unlock (&global_lock);
      return EINVAL;
    }
  ports_port_ref (vcons);
  mutex_unlock (&global_lock);

  if (vcons)
    {
      err = cons_vcons_input (vcons, buf, size);
      ports_port_deref (vcons);
    }
  return err;
}


/* Report the mouse event EV to the currently active console.  This
   can be called by the input driver at any time.  */
error_t
console_move_mouse (mouse_event_t ev)
{
  error_t err;
  vcons_t vcons;

  mutex_lock (&global_lock);
  
  vcons = active_vcons;
  if (!vcons)
    {
      mutex_unlock (&global_lock);
      return EINVAL;
    }
  ports_port_ref (vcons);
  mutex_unlock (&global_lock);

  if (vcons)
    {
      err = cons_vcons_move_mouse (vcons, ev);
      ports_port_deref (vcons);
    }

  mutex_unlock (&global_lock);

  return 0;
}


/* Scroll the active console by TYPE and VALUE as specified by
   cons_vcons_scrollback.  */
int
console_scrollback (cons_scroll_t type, float value)
{
  int nr = 0;
  vcons_t vcons;

  mutex_lock (&global_lock);
  vcons = active_vcons;
  if (!vcons)
    {
      mutex_unlock (&global_lock);
      return EINVAL;
    }
  ports_port_ref (vcons);
  mutex_unlock (&global_lock);

  if (vcons)
    {
      nr = cons_vcons_scrollback (vcons, type, value);
      ports_port_deref (vcons);
    }
  return nr;
}


/* Switch away from the console an external use of the console like
   XFree.  */
void
console_switch_away (void)
{
  mutex_lock (&global_lock);

  driver_iterate
    if (driver->ops->save_status)
      driver->ops->save_status (driver->handle);

  saved_id = active_vcons->id;
  saved_cons = active_vcons->cons;
  cons_vcons_close (active_vcons);
  active_vcons = NULL;
  mutex_unlock (&global_lock);
}

/* Switch back to the console client from an external user of the
   console like XFree.  */
void
console_switch_back (void)
{
  vcons_list_t conslist;
  mutex_lock (&global_lock);

  driver_iterate
    if (driver->ops->restore_status)
      driver->ops->restore_status (driver->handle);
  
  if (saved_cons)
    {
      error_t err;

      err = cons_lookup (saved_cons, saved_id, 1, &conslist);
      if (err)
	{
	  mutex_unlock (&global_lock);
	  return;
	}

      err = cons_vcons_open (saved_cons, conslist, &active_vcons);
      if (err)
	{
	  mutex_unlock (&global_lock);
	  return;
	}
	
      conslist->vcons = active_vcons;
      saved_cons = NULL;
      mutex_unlock (&active_vcons->lock);
    }
  mutex_unlock (&global_lock);
}


/* Exit the console client.  Does not return.  */
void
console_exit (void)
{
  driver_fini ();
  exit (0);
}

/* Signal an error to the user.  */
void console_error (const wchar_t *const err_msg)
{
  mutex_lock (&global_lock);
  bell_iterate
    if (bell->ops->beep)
      bell->ops->beep (bell->handle);
  mutex_unlock (&global_lock);
}

#if QUAERENDO_INVENIETIS
void
console_deprecated (int key)
{
  mutex_lock (&global_lock);
  input_iterate
    if (input->ops->deprecated)
      (*input->ops->deprecated) (input->handle, key);
  display_iterate
    if (display->ops->deprecated)
      (*display->ops->deprecated) (display->handle, key);
  bell_iterate
    if (bell->ops->deprecated)
      (*bell->ops->deprecated) (bell->handle, key);
  mutex_unlock (&global_lock);
}
#endif	/* QUAERENDO_INVENIETIS */


/* Callbacks for libcons.  */

/* The user may define this function.  It is called after a
   virtual console entry was added.  CONS is locked.  */
void
cons_vcons_add (cons_t cons, vcons_list_t vcons_entry)
{
  error_t err = 0;
  mutex_lock (&global_lock);
  if (!active_vcons)
    {
      vcons_t vcons;

      /* The first virtual console added to the list is automatically
	 opened after startup.  */
      err = cons_vcons_open (cons, vcons_entry, &vcons);
      if (!err)
        {
          vcons_entry->vcons = vcons;
          active_vcons = vcons;
          mutex_unlock (&vcons->lock);
        }
    }
  mutex_unlock (&global_lock);
}


/* The user may define this function.  Make the changes from
   cons_vcons_write, cons_vcons_set_cursor_pos,
   cons_vcons_set_cursor_status and cons_vcons_scroll active.  VCONS
   is locked and will have been continuously locked from the first
   change since the last update on.  This is the latest possible point
   the user must make the changes visible from.  The user can always
   make the changes visible at a more convenient, earlier time.  */
void
cons_vcons_update (vcons_t vcons)
{
  mutex_lock (&global_lock);
  if (vcons == active_vcons)
    display_iterate
      if (display->ops->update)
	display->ops->update (display->handle);
  mutex_unlock (&global_lock);
}


/* The user must define this function.  Set the cursor on virtual
   console VCONS, which is locked, to position COL and ROW.  */
void
cons_vcons_set_cursor_pos (vcons_t vcons, uint32_t col, uint32_t row)
{
  mutex_lock (&global_lock);
  if (vcons == active_vcons)
    display_iterate
      if (display->ops->set_cursor_pos)
	display->ops->set_cursor_pos (display->handle, col, row);
  mutex_unlock (&global_lock);
}


/* The user must define this function.  Set the cursor status of
   virtual console VCONS, which is locked, to STATUS.  */
void
cons_vcons_set_cursor_status (vcons_t vcons, uint32_t status)
{
  mutex_lock (&global_lock);
  if (vcons == active_vcons)
    display_iterate
      if (display->ops->set_cursor_status)
	display->ops->set_cursor_status (display->handle, status);
  mutex_unlock (&global_lock);
}


/* The user must define this function.  Scroll the content of virtual
   console VCONS, which is locked, up by DELTA if DELTA is positive or
   down by -DELTA if DELTA is negative.  DELTA will never be zero, and
   the absolute value if DELTA will be smaller than or equal to the
   height of the screen matrix.

   See <hurd/cons.h> for more information about this function.  */
void
cons_vcons_scroll (vcons_t vcons, int delta)
{
  mutex_lock (&global_lock);
  if (vcons == active_vcons)
    display_iterate
      if (display->ops->scroll)
	display->ops->scroll (display->handle, delta);
  mutex_unlock (&global_lock);
}


/* The user must define this function.  Deallocate the scarce
   resources (like font glyph slots, colors etc) in the LENGTH entries
   of the screen matrix starting from position COL and ROW.  This call
   is immediately followed by a subsequent cons_vcons_write call with
   the same LENGTH, COL and ROW arguments, and should help to make the
   write successful.  If there are no scarce resources, the caller
   might do nothing.  */
void cons_vcons_clear (vcons_t vcons, size_t length,
		       uint32_t col, uint32_t row)
{
  mutex_lock (&global_lock);
  if (vcons == active_vcons)
    display_iterate
      if (display->ops->clear)
	display->ops->clear (display->handle, length, col, row);
  mutex_unlock (&global_lock);
}


/* The user must define this function.  Write LENGTH characters
   starting from STR on the virtual console VCONS, which is locked,
   starting from position COL and ROW.  */
void
cons_vcons_write (vcons_t vcons, conchar_t *str, size_t length,
		  uint32_t col, uint32_t row)
{
  mutex_lock (&global_lock);
  if (vcons == active_vcons)
    display_iterate
      if (display->ops->write)
	display->ops->write (display->handle, str, length, col, row);
  mutex_unlock (&global_lock);
}


/* The user must define this function.  Make the virtual console
   VCONS, which is locked, beep audibly.  */
void
cons_vcons_beep (vcons_t vcons)
{
  mutex_lock (&global_lock);
  if (vcons == active_vcons)
    bell_iterate
      if (bell->ops->beep)
	bell->ops->beep (bell->handle);
  mutex_unlock (&global_lock);
}


/* The user must define this function.  Make the virtual console
   VCONS, which is locked, flash visibly.  */
void
cons_vcons_flash (vcons_t vcons)
{
  mutex_lock (&global_lock);
  if (vcons == active_vcons)
    display_iterate
      if (display->ops->flash)
	display->ops->flash (display->handle);
  mutex_unlock (&global_lock);
}


/* The user must define this function.  Notice the current status of
   the scroll lock flag.  */
void
cons_vcons_set_scroll_lock (vcons_t vcons, int onoff)
{
  mutex_lock (&global_lock);
  if (vcons == active_vcons)
    input_iterate
      if (input->ops->set_scroll_lock_status)
	input->ops->set_scroll_lock_status (input->handle, onoff);
  mutex_unlock (&global_lock);
}


/* The user must define this function.  Clear the existing screen
   matrix and set the size of the screen matrix to the dimension COL x
   ROW.  This call will be immediately followed by a call to
   cons_vcons_write that covers the whole new screen matrix.  */
error_t
cons_vcons_set_dimension (vcons_t vcons, uint32_t col, uint32_t row)
{
  mutex_lock (&global_lock);
  if (vcons == active_vcons)
    display_iterate
      if (display->ops->set_dimension)
	display->ops->set_dimension (display->handle, col, row);
  mutex_unlock (&global_lock);
  return 0;
}


error_t
cons_vcons_set_mousecursor_pos (vcons_t vcons, float x, float y)
{
  mutex_lock (&global_lock);
  if (vcons == active_vcons)
    display_iterate
      if (display->ops->set_mousecursor_pos)
	display->ops->set_mousecursor_pos (display->handle, x, y);
  mutex_unlock (&global_lock);
  return 0;
}


error_t
cons_vcons_set_mousecursor_status (vcons_t vcons, int status)
{
  mutex_lock (&global_lock);
  if (vcons == active_vcons)
    display_iterate
      if (display->ops->set_mousecursor_status)
	display->ops->set_mousecursor_status (display->handle, status);
  mutex_unlock (&global_lock);
  return 0;

}


/* Console-specific options.  */
static const struct argp_option
options[] =
  {
    {"driver-path", 'D', "PATH", 0, "Specify search path for driver modules" },
    {"driver", 'd', "NAME", 0, "Add driver NAME to the console" },
    {"console-node", 'c', "FILE", OPTION_ARG_OPTIONAL,
     "Set a translator on the node FILE (default: " DEFAULT_CONSOLE_NODE ")" },
    {0}
  };

/* Parse a command line option.  */
static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
  static int devcount = 0;
  error_t err;

  switch (key)
    {
    case 'D':
      {
	char *s;
	char *d;

	if (driver_path)
	  free (driver_path);
	driver_path = malloc (strlen (arg) + 2);
	if (!driver_path)
	  {
	    argp_failure (state, 1, ENOMEM, "adding driver path failed");
	    return EINVAL;
	  }
	s = arg;
	d = driver_path;
	while (*s)
	  {
	    *(d++) = (*s == ':') ? '\0' : *s;
	    s++;
	  }
	*(d++) = '\0';
	*d = '\0';
      }
      break;

    case 'd':
      err = driver_add (arg /* XXX */, arg,
			state->argc, state->argv, &state->next, 0);
      if (err)
	{
	  argp_failure (state, 1, err, "loading driver `%s' failed", arg);
	  return EINVAL;
	}
      devcount++;
      break;

    case 'c':
      console_node = arg ? arg : DEFAULT_CONSOLE_NODE;
      if (!console_node)
	return ENOMEM;
      break;
      
    case ARGP_KEY_SUCCESS:
      if (!devcount)
	{
	  argp_error (state, "at least one --driver argument required");
	  return EINVAL;
	}
      break;

    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

/* Add our startup arguments to the standard cons set.  */
static const struct argp_child startup_children[] =
  { { &cons_startup_argp }, { 0 } };
static struct argp startup_argp = {options, parse_opt, 0,
				   0, startup_children};

int
main (int argc, char *argv[])
{
  error_t err;
  char *errname;

  driver_init ();

  /* Parse our command line.  This shouldn't ever return an error.  */
  argp_parse (&startup_argp, argc, argv, ARGP_IN_ORDER, 0, 0);

  err = driver_start (&errname);
  if (err)
    error (1, err, "Starting driver %s failed", errname);
    
  mutex_init (&global_lock);

  err = cons_init ();
  if (err)
    {
      driver_fini ();
      error (1, err, "Console library initialization failed");
    }

  err = timer_init ();
  if (err)
    {
      driver_fini ();
      error (1, err, "Timer thread initialization failed");
    }

  if (console_node)
    console_setup_node (console_node);
  
  cons_server_loop ();

  /* Never reached.  */
  driver_fini ();
  return 0;
}