console.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2006 Josef Cejka
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00035 #include <libc.h>
00036 #include <ipc/kbd.h>
00037 #include <io/keycode.h>
00038 #include <ipc/mouse.h>
00039 #include <ipc/fb.h>
00040 #include <ipc/services.h>
00041 #include <ipc/ns.h>
00042 #include <errno.h>
00043 #include <str_error.h>
00044 #include <ipc/console.h>
00045 #include <unistd.h>
00046 #include <async.h>
00047 #include <adt/fifo.h>
00048 #include <sys/mman.h>
00049 #include <stdio.h>
00050 #include <str.h>
00051 #include <sysinfo.h>
00052 #include <event.h>
00053 #include <devmap.h>
00054 #include <fcntl.h>
00055 #include <vfs/vfs.h>
00056 #include <fibril_synch.h>
00057 #include <io/style.h>
00058 #include <io/screenbuffer.h>
00059 #include <inttypes.h>
00060 
00061 #include "console.h"
00062 #include "gcons.h"
00063 #include "keybuffer.h"
00064 
00065 
00066 #define NAME       "console"
00067 #define NAMESPACE  "term"
00068 
00069 #define HOTPLUG_WATCH_INTERVAL (1000 * 250)
00070 
00071 /* Kernel defines 32 but does not export it. */
00072 #define MAX_IPC_OUTGOING_PHONES 128
00073 
00074 static ipc_callid_t driver_phones[MAX_IPC_OUTGOING_PHONES] = { 0 };
00075 
00077 static int kbd_phone;
00078 
00080 static int mouse_phone;
00081 
00083 struct {
00084         int phone;           
00085         sysarg_t cols;       
00086         sysarg_t rows;       
00087         sysarg_t color_cap;  
00088 } fb_info;
00089 
00090 typedef struct {
00091         size_t index;             
00092         size_t refcount;          
00093         devmap_handle_t devmap_handle;  
00094         keybuffer_t keybuffer;    
00095         screenbuffer_t scr;       
00097 } console_t;
00098 
00099 
00100 
00102 static console_t consoles[CONSOLE_COUNT];
00103 
00104 static console_t *active_console = &consoles[0];
00105 static console_t *prev_console = &consoles[0];
00106 static console_t *kernel_console = &consoles[KERNEL_CONSOLE];
00107 
00110 static keyfield_t *interbuffer = NULL;
00111 
00113 struct {
00114         sysarg_t col;  
00115         sysarg_t row;  
00116         sysarg_t cnt;  
00117 } fb_pending;
00118 
00119 static FIBRIL_MUTEX_INITIALIZE(input_mutex);
00120 static FIBRIL_CONDVAR_INITIALIZE(input_cv);
00121 
00122 static void curs_visibility(bool visible)
00123 {
00124         async_msg_1(fb_info.phone, FB_CURSOR_VISIBILITY, visible); 
00125 }
00126 
00127 static void curs_hide_sync(void)
00128 {
00129         async_req_1_0(fb_info.phone, FB_CURSOR_VISIBILITY, false); 
00130 }
00131 
00132 static void curs_goto(sysarg_t x, sysarg_t y)
00133 {
00134         async_msg_2(fb_info.phone, FB_CURSOR_GOTO, x, y);
00135 }
00136 
00137 static void screen_clear(void)
00138 {
00139         async_msg_0(fb_info.phone, FB_CLEAR);
00140 }
00141 
00142 static void screen_yield(void)
00143 {
00144         async_req_0_0(fb_info.phone, FB_SCREEN_YIELD);
00145 }
00146 
00147 static void screen_reclaim(void)
00148 {
00149         async_req_0_0(fb_info.phone, FB_SCREEN_RECLAIM);
00150 }
00151 
00152 static void kbd_yield(void)
00153 {
00154         async_req_0_0(kbd_phone, KBD_YIELD);
00155 }
00156 
00157 static void kbd_reclaim(void)
00158 {
00159         async_req_0_0(kbd_phone, KBD_RECLAIM);
00160 }
00161 
00162 static void set_style(uint8_t style)
00163 {
00164         async_msg_1(fb_info.phone, FB_SET_STYLE, style);
00165 }
00166 
00167 static void set_color(uint8_t fgcolor, uint8_t bgcolor, uint8_t flags)
00168 {
00169         async_msg_3(fb_info.phone, FB_SET_COLOR, fgcolor, bgcolor, flags);
00170 }
00171 
00172 static void set_rgb_color(uint32_t fgcolor, uint32_t bgcolor)
00173 {
00174         async_msg_2(fb_info.phone, FB_SET_RGB_COLOR, fgcolor, bgcolor); 
00175 }
00176 
00177 static void set_attrs(attrs_t *attrs)
00178 {
00179         switch (attrs->t) {
00180         case at_style:
00181                 set_style(attrs->a.s.style);
00182                 break;
00183         case at_idx:
00184                 set_color(attrs->a.i.fg_color, attrs->a.i.bg_color,
00185                     attrs->a.i.flags);
00186                 break;
00187         case at_rgb:
00188                 set_rgb_color(attrs->a.r.fg_color, attrs->a.r.bg_color);
00189                 break;
00190         }
00191 }
00192 
00193 static int ccap_fb_to_con(sysarg_t ccap_fb, sysarg_t *ccap_con)
00194 {
00195         switch (ccap_fb) {
00196         case FB_CCAP_NONE:
00197                 *ccap_con = CONSOLE_CCAP_NONE;
00198                 break;
00199         case FB_CCAP_STYLE:
00200                 *ccap_con = CONSOLE_CCAP_STYLE;
00201                 break;
00202         case FB_CCAP_INDEXED:
00203                 *ccap_con = CONSOLE_CCAP_INDEXED;
00204                 break;
00205         case FB_CCAP_RGB:
00206                 *ccap_con = CONSOLE_CCAP_RGB;
00207                 break;
00208         default:
00209                 return EINVAL;
00210         }
00211         
00212         return EOK;
00213 }
00214 
00216 static void fb_update_area(console_t *cons, sysarg_t x0, sysarg_t y0, sysarg_t width, sysarg_t height)
00217 {
00218         if (interbuffer) {
00219                 sysarg_t x;
00220                 sysarg_t y;
00221                 
00222                 for (y = 0; y < height; y++) {
00223                         for (x = 0; x < width; x++) {
00224                                 interbuffer[y * width + x] =
00225                                     *get_field_at(&cons->scr, x0 + x, y0 + y);
00226                         }
00227                 }
00228                 
00229                 async_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA,
00230                     x0, y0, width, height);
00231         }
00232 }
00233 
00235 static void fb_pending_flush(void)
00236 {
00237         if (fb_pending.cnt > 0) {
00238                 fb_update_area(active_console, fb_pending.col,
00239                     fb_pending.row, fb_pending.cnt, 1);
00240                 fb_pending.cnt = 0;
00241         }
00242 }
00243 
00250 static void cell_mark_changed(sysarg_t col, sysarg_t row)
00251 {
00252         if (fb_pending.cnt != 0) {
00253                 if ((col != fb_pending.col + fb_pending.cnt)
00254                     || (row != fb_pending.row)) {
00255                         fb_pending_flush();
00256                 }
00257         }
00258         
00259         if (fb_pending.cnt == 0) {
00260                 fb_pending.col = col;
00261                 fb_pending.row = row;
00262         }
00263         
00264         fb_pending.cnt++;
00265 }
00266 
00268 static void fb_putchar(wchar_t c, sysarg_t col, sysarg_t row)
00269 {
00270         async_msg_3(fb_info.phone, FB_PUTCHAR, c, col, row);
00271 }
00272 
00274 static void write_char(console_t *cons, wchar_t ch)
00275 {
00276         bool flush_cursor = false;
00277         
00278         switch (ch) {
00279         case '\n':
00280                 fb_pending_flush();
00281                 flush_cursor = true;
00282                 cons->scr.position_y++;
00283                 cons->scr.position_x = 0;
00284                 break;
00285         case '\r':
00286                 break;
00287         case '\t':
00288                 cons->scr.position_x += 8;
00289                 cons->scr.position_x -= cons->scr.position_x % 8;
00290                 break;
00291         case '\b':
00292                 if (cons->scr.position_x == 0)
00293                         break;
00294                 cons->scr.position_x--;
00295                 if (cons == active_console)
00296                         cell_mark_changed(cons->scr.position_x, cons->scr.position_y);
00297                 screenbuffer_putchar(&cons->scr, ' ');
00298                 break;
00299         default:
00300                 if (cons == active_console)
00301                         cell_mark_changed(cons->scr.position_x, cons->scr.position_y);
00302                 
00303                 screenbuffer_putchar(&cons->scr, ch);
00304                 cons->scr.position_x++;
00305         }
00306         
00307         if (cons->scr.position_x >= cons->scr.size_x) {
00308                 flush_cursor = true;
00309                 cons->scr.position_y++;
00310         }
00311         
00312         if (cons->scr.position_y >= cons->scr.size_y) {
00313                 fb_pending_flush();
00314                 cons->scr.position_y = cons->scr.size_y - 1;
00315                 screenbuffer_clear_line(&cons->scr, cons->scr.top_line);
00316                 cons->scr.top_line = (cons->scr.top_line + 1) % cons->scr.size_y;
00317                 
00318                 if (cons == active_console)
00319                         async_msg_1(fb_info.phone, FB_SCROLL, 1);
00320         }
00321         
00322         if (cons == active_console && flush_cursor)
00323                 curs_goto(cons->scr.position_x, cons->scr.position_y);
00324         cons->scr.position_x = cons->scr.position_x % cons->scr.size_x;
00325 }
00326 
00328 static void change_console(console_t *cons)
00329 {
00330         if (cons == active_console) {
00331                 return;
00332         }
00333         
00334         fb_pending_flush();
00335         
00336         if (cons == kernel_console) {
00337                 async_serialize_start();
00338                 curs_hide_sync();
00339                 gcons_in_kernel();
00340                 screen_yield();
00341                 kbd_yield();
00342                 async_serialize_end();
00343                 
00344                 if (__SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE)) {
00345                         prev_console = active_console;
00346                         active_console = kernel_console;
00347                 } else
00348                         cons = active_console;
00349         }
00350         
00351         if (cons != kernel_console) {
00352                 async_serialize_start();
00353                 
00354                 if (active_console == kernel_console) {
00355                         screen_reclaim();
00356                         kbd_reclaim();
00357                         gcons_redraw_console();
00358                 }
00359                 
00360                 active_console = cons;
00361                 gcons_change_console(cons->index);
00362                 
00363                 set_attrs(&cons->scr.attrs);
00364                 curs_visibility(false);
00365                 
00366                 sysarg_t x;
00367                 sysarg_t y;
00368                 int rc = 0;
00369                 
00370                 if (interbuffer) {
00371                         for (y = 0; y < cons->scr.size_y; y++) {
00372                                 for (x = 0; x < cons->scr.size_x; x++) {
00373                                         interbuffer[y * cons->scr.size_x + x] =
00374                                             *get_field_at(&cons->scr, x, y);
00375                                 }
00376                         }
00377                         
00378                         /* This call can preempt, but we are already at the end */
00379                         rc = async_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA,
00380                             0, 0, cons->scr.size_x,
00381                             cons->scr.size_y);
00382                 }
00383                 
00384                 if ((!interbuffer) || (rc != 0)) {
00385                         set_attrs(&cons->scr.attrs);
00386                         screen_clear();
00387                         
00388                         for (y = 0; y < cons->scr.size_y; y++)
00389                                 for (x = 0; x < cons->scr.size_x; x++) {
00390                                         keyfield_t *field = get_field_at(&cons->scr, x, y);
00391                                         
00392                                         if (!attrs_same(cons->scr.attrs, field->attrs))
00393                                                 set_attrs(&field->attrs);
00394                                         
00395                                         cons->scr.attrs = field->attrs;
00396                                         if ((field->character == ' ') &&
00397                                             (attrs_same(field->attrs, cons->scr.attrs)))
00398                                                 continue;
00399                                         
00400                                         fb_putchar(field->character, x, y);
00401                                 }
00402                 }
00403                 
00404                 curs_goto(cons->scr.position_x, cons->scr.position_y);
00405                 curs_visibility(cons->scr.is_cursor_visible);
00406                 
00407                 async_serialize_end();
00408         }
00409 }
00410 
00411 static void close_driver_phone(ipc_callid_t hash)
00412 {
00413         int i;
00414         for (i = 0; i < MAX_IPC_OUTGOING_PHONES; i++) {
00415                 if (driver_phones[i] == hash) {
00416                         printf("Device %" PRIxn " gone.\n", hash);
00417                         driver_phones[i] = 0;
00418                         async_hangup(i);
00419                         return;
00420                 }
00421         }
00422 }
00423 
00425 static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
00426 {
00427         /* Ignore parameters, the connection is already opened */
00428         while (true) {
00429                 ipc_call_t call;
00430                 ipc_callid_t callid = async_get_call(&call);
00431                 
00432                 int retval;
00433                 console_event_t ev;
00434                 
00435                 switch (IPC_GET_IMETHOD(call)) {
00436                 case IPC_M_PHONE_HUNGUP:
00437                         /* TODO: Handle hangup */
00438                         close_driver_phone(iid);
00439                         return;
00440                 case KBD_EVENT:
00441                         /* Got event from keyboard driver. */
00442                         retval = 0;
00443                         ev.type = IPC_GET_ARG1(call);
00444                         ev.key = IPC_GET_ARG2(call);
00445                         ev.mods = IPC_GET_ARG3(call);
00446                         ev.c = IPC_GET_ARG4(call);
00447                         
00448                         if ((ev.key >= KC_F1) && (ev.key < KC_F1 +
00449                             CONSOLE_COUNT) && ((ev.mods & KM_CTRL) == 0)) {
00450                                 if (ev.key == KC_F1 + KERNEL_CONSOLE)
00451                                         change_console(kernel_console);
00452                                 else
00453                                         change_console(&consoles[ev.key - KC_F1]);
00454                                 break;
00455                         }
00456                         
00457                         fibril_mutex_lock(&input_mutex);
00458                         keybuffer_push(&active_console->keybuffer, &ev);
00459                         fibril_condvar_broadcast(&input_cv);
00460                         fibril_mutex_unlock(&input_mutex);
00461                         break;
00462                 default:
00463                         retval = ENOENT;
00464                 }
00465                 async_answer_0(callid, retval);
00466         }
00467 }
00468 
00470 static void mouse_events(ipc_callid_t iid, ipc_call_t *icall)
00471 {
00472         /* Ignore parameters, the connection is already opened */
00473         while (true) {
00474                 ipc_call_t call;
00475                 ipc_callid_t callid = async_get_call(&call);
00476                 
00477                 int retval;
00478                 
00479                 switch (IPC_GET_IMETHOD(call)) {
00480                 case IPC_M_PHONE_HUNGUP:
00481                         /* TODO: Handle hangup */
00482                         close_driver_phone(iid);
00483                         return;
00484                 case MEVENT_BUTTON:
00485                         if (IPC_GET_ARG1(call) == 1) {
00486                                 int newcon = gcons_mouse_btn((bool) IPC_GET_ARG2(call));
00487                                 if (newcon != -1) {
00488                                         change_console(&consoles[newcon]);
00489                                 }
00490                         }
00491                         retval = 0;
00492                         break;
00493                 case MEVENT_MOVE:
00494                         gcons_mouse_move((int) IPC_GET_ARG1(call),
00495                             (int) IPC_GET_ARG2(call));
00496                         retval = 0;
00497                         break;
00498                 default:
00499                         retval = ENOENT;
00500                 }
00501 
00502                 async_answer_0(callid, retval);
00503         }
00504 }
00505 
00506 static void cons_write(console_t *cons, ipc_callid_t rid, ipc_call_t *request)
00507 {
00508         void *buf;
00509         size_t size;
00510         int rc = async_data_write_accept(&buf, false, 0, 0, 0, &size);
00511         
00512         if (rc != EOK) {
00513                 async_answer_0(rid, rc);
00514                 return;
00515         }
00516         
00517         async_serialize_start();
00518         
00519         size_t off = 0;
00520         while (off < size) {
00521                 wchar_t ch = str_decode(buf, &off, size);
00522                 write_char(cons, ch);
00523         }
00524         
00525         async_serialize_end();
00526         
00527         gcons_notify_char(cons->index);
00528         async_answer_1(rid, EOK, size);
00529         
00530         free(buf);
00531 }
00532 
00533 static void cons_read(console_t *cons, ipc_callid_t rid, ipc_call_t *request)
00534 {
00535         ipc_callid_t callid;
00536         size_t size;
00537         if (!async_data_read_receive(&callid, &size)) {
00538                 async_answer_0(callid, EINVAL);
00539                 async_answer_0(rid, EINVAL);
00540                 return;
00541         }
00542         
00543         char *buf = (char *) malloc(size);
00544         if (buf == NULL) {
00545                 async_answer_0(callid, ENOMEM);
00546                 async_answer_0(rid, ENOMEM);
00547                 return;
00548         }
00549         
00550         size_t pos = 0;
00551         console_event_t ev;
00552         fibril_mutex_lock(&input_mutex);
00553         
00554 recheck:
00555         while ((keybuffer_pop(&cons->keybuffer, &ev)) && (pos < size)) {
00556                 if (ev.type == KEY_PRESS) {
00557                         buf[pos] = ev.c;
00558                         pos++;
00559                 }
00560         }
00561         
00562         if (pos == size) {
00563                 (void) async_data_read_finalize(callid, buf, size);
00564                 async_answer_1(rid, EOK, size);
00565                 free(buf);
00566         } else {
00567                 fibril_condvar_wait(&input_cv, &input_mutex);
00568                 goto recheck;
00569         }
00570         
00571         fibril_mutex_unlock(&input_mutex);
00572 }
00573 
00574 static void cons_get_event(console_t *cons, ipc_callid_t rid, ipc_call_t *request)
00575 {
00576         console_event_t ev;
00577         
00578         fibril_mutex_lock(&input_mutex);
00579         
00580 recheck:
00581         if (keybuffer_pop(&cons->keybuffer, &ev)) {
00582                 async_answer_4(rid, EOK, ev.type, ev.key, ev.mods, ev.c);
00583         } else {
00584                 fibril_condvar_wait(&input_cv, &input_mutex);
00585                 goto recheck;
00586         }
00587         
00588         fibril_mutex_unlock(&input_mutex);
00589 }
00590 
00592 static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
00593 {
00594         console_t *cons = NULL;
00595         
00596         size_t i;
00597         for (i = 0; i < CONSOLE_COUNT; i++) {
00598                 if (i == KERNEL_CONSOLE)
00599                         continue;
00600                 
00601                 if (consoles[i].devmap_handle == (devmap_handle_t) IPC_GET_ARG1(*icall)) {
00602                         cons = &consoles[i];
00603                         break;
00604                 }
00605         }
00606         
00607         if (cons == NULL) {
00608                 async_answer_0(iid, ENOENT);
00609                 return;
00610         }
00611         
00612         ipc_callid_t callid;
00613         ipc_call_t call;
00614         sysarg_t arg1;
00615         sysarg_t arg2;
00616         sysarg_t arg3;
00617         
00618         int rc;
00619         
00620         async_serialize_start();
00621         if (cons->refcount == 0)
00622                 gcons_notify_connect(cons->index);
00623         
00624         cons->refcount++;
00625         
00626         /* Accept the connection */
00627         async_answer_0(iid, EOK);
00628         
00629         while (true) {
00630                 async_serialize_end();
00631                 callid = async_get_call(&call);
00632                 async_serialize_start();
00633                 
00634                 arg1 = 0;
00635                 arg2 = 0;
00636                 arg3 = 0;
00637                 
00638                 switch (IPC_GET_IMETHOD(call)) {
00639                 case IPC_M_PHONE_HUNGUP:
00640                         cons->refcount--;
00641                         if (cons->refcount == 0)
00642                                 gcons_notify_disconnect(cons->index);
00643                         return;
00644                 case VFS_OUT_READ:
00645                         async_serialize_end();
00646                         cons_read(cons, callid, &call);
00647                         async_serialize_start();
00648                         continue;
00649                 case VFS_OUT_WRITE:
00650                         async_serialize_end();
00651                         cons_write(cons, callid, &call);
00652                         async_serialize_start();
00653                         continue;
00654                 case VFS_OUT_SYNC:
00655                         fb_pending_flush();
00656                         if (cons == active_console) {
00657                                 async_req_0_0(fb_info.phone, FB_FLUSH);
00658                                 curs_goto(cons->scr.position_x, cons->scr.position_y);
00659                         }
00660                         break;
00661                 case CONSOLE_CLEAR:
00662                         /* Send message to fb */
00663                         if (cons == active_console)
00664                                 async_msg_0(fb_info.phone, FB_CLEAR);
00665                         
00666                         screenbuffer_clear(&cons->scr);
00667                         
00668                         break;
00669                 case CONSOLE_GOTO:
00670                         screenbuffer_goto(&cons->scr,
00671                             IPC_GET_ARG1(call), IPC_GET_ARG2(call));
00672                         if (cons == active_console)
00673                                 curs_goto(IPC_GET_ARG1(call),
00674                                     IPC_GET_ARG2(call));
00675                         break;
00676                 case CONSOLE_GET_POS:
00677                         arg1 = cons->scr.position_x;
00678                         arg2 = cons->scr.position_y;
00679                         break;
00680                 case CONSOLE_GET_SIZE:
00681                         arg1 = fb_info.cols;
00682                         arg2 = fb_info.rows;
00683                         break;
00684                 case CONSOLE_GET_COLOR_CAP:
00685                         rc = ccap_fb_to_con(fb_info.color_cap, &arg1);
00686                         if (rc != EOK) {
00687                                 async_answer_0(callid, rc);
00688                                 continue;
00689                         }
00690                         break;
00691                 case CONSOLE_SET_STYLE:
00692                         fb_pending_flush();
00693                         arg1 = IPC_GET_ARG1(call);
00694                         screenbuffer_set_style(&cons->scr, arg1);
00695                         if (cons == active_console)
00696                                 set_style(arg1);
00697                         break;
00698                 case CONSOLE_SET_COLOR:
00699                         fb_pending_flush();
00700                         arg1 = IPC_GET_ARG1(call);
00701                         arg2 = IPC_GET_ARG2(call);
00702                         arg3 = IPC_GET_ARG3(call);
00703                         screenbuffer_set_color(&cons->scr, arg1, arg2, arg3);
00704                         if (cons == active_console)
00705                                 set_color(arg1, arg2, arg3);
00706                         break;
00707                 case CONSOLE_SET_RGB_COLOR:
00708                         fb_pending_flush();
00709                         arg1 = IPC_GET_ARG1(call);
00710                         arg2 = IPC_GET_ARG2(call);
00711                         screenbuffer_set_rgb_color(&cons->scr, arg1, arg2);
00712                         if (cons == active_console)
00713                                 set_rgb_color(arg1, arg2);
00714                         break;
00715                 case CONSOLE_CURSOR_VISIBILITY:
00716                         fb_pending_flush();
00717                         arg1 = IPC_GET_ARG1(call);
00718                         cons->scr.is_cursor_visible = arg1;
00719                         if (cons == active_console)
00720                                 curs_visibility(arg1);
00721                         break;
00722                 case CONSOLE_GET_EVENT:
00723                         async_serialize_end();
00724                         cons_get_event(cons, callid, &call);
00725                         async_serialize_start();
00726                         continue;
00727                 case CONSOLE_KCON_ENABLE:
00728                         change_console(kernel_console);
00729                         break;
00730                 }
00731                 async_answer_3(callid, EOK, arg1, arg2, arg3);
00732         }
00733 }
00734 
00735 static void interrupt_received(ipc_callid_t callid, ipc_call_t *call)
00736 {
00737         change_console(prev_console);
00738 }
00739 
00740 static int async_connect_to_me_hack(int phone, sysarg_t arg1, sysarg_t arg2,
00741 sysarg_t arg3, async_client_conn_t client_receiver, ipc_callid_t *hash)
00742 {
00743         sysarg_t task_hash;
00744         sysarg_t phone_hash;
00745         int rc = async_req_3_5(phone, IPC_M_CONNECT_TO_ME, arg1, arg2, arg3,
00746             NULL, NULL, NULL, &task_hash, &phone_hash);
00747         if (rc != EOK)
00748                 return rc;
00749 
00750         if (client_receiver != NULL)
00751                 async_new_connection(task_hash, phone_hash, phone_hash, NULL,
00752                     client_receiver);
00753 
00754         if (hash != NULL) {
00755                 *hash = phone_hash;
00756         }
00757 
00758         return EOK;
00759 }
00760 
00761 static int connect_keyboard_or_mouse(const char *devname,
00762     async_client_conn_t handler, const char *path)
00763 {
00764         int fd = open(path, O_RDONLY);
00765         if (fd < 0) {
00766                 return fd;
00767         }
00768         
00769         int phone = fd_phone(fd);
00770         close(fd);
00771         if (phone < 0) {
00772                 printf(NAME ": Failed to connect to input device\n");
00773                 return phone;
00774         }
00775         
00776         ipc_callid_t hash;
00777         int rc = async_connect_to_me_hack(phone, SERVICE_CONSOLE, 0, phone,
00778             handler, &hash);
00779         if (rc != EOK) {
00780                 async_hangup(phone);
00781                 printf(NAME ": " \
00782                     "Failed to create callback from input device: %s.\n",
00783                     str_error(rc));
00784                 return rc;
00785         }
00786         
00787         driver_phones[phone] = hash;
00788 
00789         printf(NAME ": found %s \"%s\" (%" PRIxn ").\n", devname, path, hash);
00790 
00791         return phone;
00792 }
00793 
00794 static int connect_keyboard(const char *path)
00795 {
00796         return connect_keyboard_or_mouse("keyboard", keyboard_events, path);
00797 }
00798 
00799 static int connect_mouse(const char *path)
00800 {
00801         return connect_keyboard_or_mouse("mouse", mouse_events, path);
00802 }
00803 
00804 struct hid_class_info {
00805         char *classname;
00806         int (*connection_func)(const char *);
00807 };
00808 
00814 static int check_new_device_fibril(void *arg)
00815 {
00816         struct hid_class_info *dev_info = arg;
00817 
00818         size_t index = 1;
00819 
00820         while (true) {
00821                 async_usleep(HOTPLUG_WATCH_INTERVAL);
00822                 char *path;
00823                 int rc = asprintf(&path, "/dev/class/%s\\%zu",
00824                     dev_info->classname, index);
00825                 if (rc < 0) {
00826                         continue;
00827                 }
00828                 rc = 0;
00829                 rc = dev_info->connection_func(path);
00830                 if (rc > 0) {
00831                         /* We do not allow unplug. */
00832                         index++;
00833                 }
00834 
00835                 free(path);
00836         }
00837 
00838         return EOK;
00839 }
00840 
00841 
00844 static void check_new_devices_in_background(int (*connection_func)(const char *),
00845     const char *classname)
00846 {
00847         struct hid_class_info *dev_info = malloc(sizeof(struct hid_class_info));
00848         if (dev_info == NULL) {
00849                 printf(NAME ": " \
00850                     "out of memory, will not start hot-plug-watch fibril.\n");
00851                 return;
00852         }
00853         int rc;
00854 
00855         rc = asprintf(&dev_info->classname, "%s", classname);
00856         if (rc < 0) {
00857                 printf(NAME ": failed to format classname: %s.\n",
00858                     str_error(rc));
00859                 return;
00860         }
00861         dev_info->connection_func = connection_func;
00862 
00863         fid_t fid = fibril_create(check_new_device_fibril, (void *)dev_info);
00864         if (!fid) {
00865                 printf(NAME
00866                     ": failed to create hot-plug-watch fibril for %s.\n",
00867                     classname);
00868                 return;
00869         }
00870         fibril_add_ready(fid);
00871 }
00872 
00873 static bool console_init(char *input)
00874 {
00875         /* Connect to input device */
00876         kbd_phone = connect_keyboard(input);
00877         if (kbd_phone < 0) {
00878                 return false;
00879         }
00880 
00881         mouse_phone = connect_mouse("/dev/hid_in/mouse");
00882         if (mouse_phone < 0) {
00883                 printf(NAME ": Failed to connect to mouse device: %s.\n",
00884                     str_error(mouse_phone));
00885         }
00886         
00887         /* Connect to framebuffer driver */
00888         fb_info.phone = service_connect_blocking(SERVICE_VIDEO, 0, 0);
00889         if (fb_info.phone < 0) {
00890                 printf(NAME ": Failed to connect to video service\n");
00891                 return -1;
00892         }
00893         
00894         /* Register driver */
00895         int rc = devmap_driver_register(NAME, client_connection);
00896         if (rc < 0) {
00897                 printf(NAME ": Unable to register driver (%d)\n", rc);
00898                 return false;
00899         }
00900         
00901         /* Initialize gcons */
00902         gcons_init(fb_info.phone);
00903         
00904         /* Synchronize, the gcons could put something in queue */
00905         async_req_0_0(fb_info.phone, FB_FLUSH);
00906         async_req_0_2(fb_info.phone, FB_GET_CSIZE, &fb_info.cols, &fb_info.rows);
00907         async_req_0_1(fb_info.phone, FB_GET_COLOR_CAP, &fb_info.color_cap);
00908         
00909         /* Set up shared memory buffer. */
00910         size_t ib_size = sizeof(keyfield_t) * fb_info.cols * fb_info.rows;
00911         interbuffer = as_get_mappable_page(ib_size);
00912         
00913         if (as_area_create(interbuffer, ib_size, AS_AREA_READ |
00914             AS_AREA_WRITE | AS_AREA_CACHEABLE) != interbuffer)
00915                 interbuffer = NULL;
00916         
00917         if (interbuffer) {
00918                 if (async_share_out_start(fb_info.phone, interbuffer,
00919                     AS_AREA_READ) != EOK) {
00920                         as_area_destroy(interbuffer);
00921                         interbuffer = NULL;
00922                 }
00923         }
00924         
00925         fb_pending.cnt = 0;
00926         
00927         /* Inititalize consoles */
00928         size_t i;
00929         for (i = 0; i < CONSOLE_COUNT; i++) {
00930                 if (i != KERNEL_CONSOLE) {
00931                         if (screenbuffer_init(&consoles[i].scr,
00932                             fb_info.cols, fb_info.rows) == NULL) {
00933                                 printf(NAME ": Unable to allocate screen buffer %zu\n", i);
00934                                 return false;
00935                         }
00936                         screenbuffer_clear(&consoles[i].scr);
00937                         keybuffer_init(&consoles[i].keybuffer);
00938                         consoles[i].index = i;
00939                         consoles[i].refcount = 0;
00940                         
00941                         char vc[DEVMAP_NAME_MAXLEN + 1];
00942                         snprintf(vc, DEVMAP_NAME_MAXLEN, "%s/vc%zu", NAMESPACE, i);
00943                         
00944                         if (devmap_device_register(vc, &consoles[i].devmap_handle) != EOK) {
00945                                 printf(NAME ": Unable to register device %s\n", vc);
00946                                 return false;
00947                         }
00948                 }
00949         }
00950         
00951         /* Disable kernel output to the console */
00952         __SYSCALL0(SYS_DEBUG_DISABLE_CONSOLE);
00953         
00954         /* Initialize the screen */
00955         async_serialize_start();
00956         gcons_redraw_console();
00957         set_style(STYLE_NORMAL);
00958         screen_clear();
00959         curs_goto(0, 0);
00960         curs_visibility(active_console->scr.is_cursor_visible);
00961         async_serialize_end();
00962         
00963         /* Receive kernel notifications */
00964         async_set_interrupt_received(interrupt_received);
00965         if (event_subscribe(EVENT_KCONSOLE, 0) != EOK)
00966                 printf(NAME ": Error registering kconsole notifications\n");
00967         
00968         /* Start fibril for checking on hot-plugged keyboards. */
00969         check_new_devices_in_background(connect_keyboard, "keyboard");
00970         check_new_devices_in_background(connect_mouse, "mouse");
00971 
00972         return true;
00973 }
00974 
00975 static void usage(void)
00976 {
00977         printf("Usage: console <input>\n");
00978 }
00979 
00980 int main(int argc, char *argv[])
00981 {
00982         if (argc < 2) {
00983                 usage();
00984                 return -1;
00985         }
00986         
00987         printf(NAME ": HelenOS Console service\n");
00988         
00989         if (!console_init(argv[1]))
00990                 return -1;
00991 
00992         printf(NAME ": Accepting connections\n");
00993         async_manager();
00994         
00995         return 0;
00996 }
00997 

Generated on Thu Jun 2 07:45:46 2011 for HelenOS/USB by  doxygen 1.4.7