Ignore:
Timestamp:
2012-08-17T17:05:06Z (12 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
01e397ac
Parents:
ad78054
Message:

Factor out input protocol client code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/hid/compositor/compositor.c

    rad78054 r111d2d6  
    4949#include <adt/prodcons.h>
    5050#include <adt/list.h>
     51#include <io/input.h>
    5152#include <ipc/graph.h>
    52 #include <ipc/input.h>
    5353#include <ipc/window.h>
    5454
     
    143143static LIST_INITIALIZE(viewport_list);
    144144
    145 static async_sess_t *input_sess;
     145/** Input server proxy */
     146static input_t *input;
     147
     148static int comp_key_press(input_t *, kbd_event_type_t, keycode_t, keymod_t, wchar_t);
     149static int comp_mouse_move(input_t *, int, int);
     150static int comp_abs_move(input_t *, unsigned, unsigned, unsigned, unsigned);
     151static int comp_mouse_button(input_t *, int, int);
     152
     153static input_ev_ops_t input_ev_ops = {
     154        .key = comp_key_press,
     155        .move = comp_mouse_move,
     156        .abs_move = comp_abs_move,
     157        .button = comp_mouse_button
     158};
     159
     160static void input_disconnect(void);
     161
     162
     163static pointer_t *input_pointer(input_t *input)
     164{
     165        return input->user;
     166}
    146167
    147168static pointer_t *pointer_create()
     
    759780                fibril_mutex_unlock(&viewport_list_mtx);
    760781                loc_service_unregister(winreg_id);
    761                 async_hangup(input_sess);
     782                input_disconnect();
    762783
    763784                /* Close all clients and their windows. */
     
    10551076}
    10561077
    1057 static void comp_mouse_move(pointer_t *pointer, ipc_callid_t iid, ipc_call_t *icall)
    1058 {
    1059         int dx = (int) IPC_GET_ARG1(*icall);
    1060         int dy = (int) IPC_GET_ARG2(*icall);
     1078static int comp_abs_move(input_t *input, unsigned x , unsigned y,
     1079    unsigned max_x, unsigned max_y)
     1080{
     1081        /* XXX TODO */
     1082        return EOK;
     1083}
     1084
     1085static int comp_mouse_move(input_t *input, int dx, int dy)
     1086{
     1087        pointer_t *pointer = input_pointer(input);
    10611088
    10621089        /* Update pointer position. */
     
    11181145        }
    11191146
    1120         async_answer_0(iid, EOK);
    1121 }
    1122 
    1123 static void comp_mouse_button(pointer_t *pointer, ipc_callid_t iid, ipc_call_t *icall)
    1124 {
    1125         sysarg_t btn_num = IPC_GET_ARG1(*icall);
    1126         bool pressed = (bool) IPC_GET_ARG2(*icall);
    1127 
    1128         if (pressed) {
     1147        return EOK;
     1148}
     1149
     1150static int comp_mouse_button(input_t *input, int bnum, int bpress)
     1151{
     1152        pointer_t *pointer = input_pointer(input);
     1153
     1154        if (bpress) {
    11291155                pointer->btn_hpos = pointer->hpos;
    11301156                pointer->btn_vpos = pointer->vpos;
    1131                 pointer->btn_num = btn_num;
     1157                pointer->btn_num = bnum;
    11321158                pointer->pressed = true;
    11331159
     
    11371163                if (!win || !win->surface) {
    11381164                        fibril_mutex_unlock(&window_list_mtx);
    1139                         async_answer_0(iid, EOK);
    1140                         return;
     1165                        return EOK;
    11411166                }
    11421167                sysarg_t x, y, width, height;
     
    11541179                                event->data.pos.pos_id = pointer->id;
    11551180                                event->data.pos.type = POS_PRESS;
    1156                                 event->data.pos.btn_num = btn_num;
     1181                                event->data.pos.btn_num = bnum;
    11571182                                event->data.pos.hpos = x;
    11581183                                event->data.pos.vpos = y;
    11591184                                comp_post_event(event);
    11601185                        } else {
    1161                                 async_answer_0(iid, ENOMEM);
    1162                                 return;
    1163                         }
    1164                 }
    1165         } else if (pointer->pressed && pointer->btn_num == btn_num) {
     1186                                return ENOMEM;
     1187                        }
     1188                }
     1189        } else if (pointer->pressed && pointer->btn_num == (unsigned)bnum) {
    11661190                pointer->pressed = false;
    11671191
     
    11911215                        pointer->grab_flags = GF_EMPTY;
    11921216                        fibril_mutex_unlock(&window_list_mtx);
    1193                         async_answer_0(iid, EOK);
    1194                         return;
     1217                        return EOK;
    11951218                }
    11961219
     
    12571280                                event->data.pos.pos_id = pointer->id;
    12581281                                event->data.pos.type = POS_RELEASE;
    1259                                 event->data.pos.btn_num = btn_num;
     1282                                event->data.pos.btn_num = bnum;
    12601283                                event->data.pos.hpos = point_x;
    12611284                                event->data.pos.vpos = point_y;
     
    12631286                        pointer->grab_flags = GF_EMPTY;
    12641287                       
    1265                 } else if (within_client && (pointer->grab_flags == GF_EMPTY) && (btn_num == 1)) {
     1288                } else if (within_client && (pointer->grab_flags == GF_EMPTY) && (bnum == 1)) {
    12661289
    12671290                        /* Bring the window to the foreground. */
     
    12861309        }
    12871310
    1288         async_answer_0(iid, EOK);
    1289 }
    1290 
    1291 static void comp_key_press(ipc_callid_t iid, ipc_call_t *icall)
    1292 {
    1293         kbd_event_type_t type = IPC_GET_ARG1(*icall);
    1294         keycode_t key = IPC_GET_ARG2(*icall);
    1295         keymod_t mods = IPC_GET_ARG3(*icall);
    1296         wchar_t c = IPC_GET_ARG4(*icall);
    1297 
     1311        return EOK;
     1312}
     1313
     1314static int comp_key_press(input_t *input, kbd_event_type_t type, keycode_t key,
     1315    keymod_t mods, wchar_t c)
     1316{
    12981317        bool win_transform = (mods & KM_ALT) && (
    12991318            key == KC_W || key == KC_S || key == KC_A || key == KC_D ||
     
    13781397                        if (event == NULL) {
    13791398                                fibril_mutex_unlock(&window_list_mtx);
    1380                                 async_answer_0(iid, ENOMEM);
    1381                                 return;
     1399                                return ENOMEM;
    13821400                        }
    13831401
     
    14481466        } else if (win_close) {
    14491467                window_event_t *event = (window_event_t *) malloc(sizeof(window_event_t));
    1450                 if (event == NULL) {
    1451                         async_answer_0(iid, ENOMEM);
    1452                         return;
    1453                 }
     1468                if (event == NULL)
     1469                        return ENOMEM;
    14541470
    14551471                link_initialize(&event->link);
     
    15941610        } else {
    15951611                window_event_t *event = (window_event_t *) malloc(sizeof(window_event_t));
    1596                 if (event == NULL) {
    1597                         async_answer_0(iid, ENOMEM);
    1598                         return;
    1599                 }
     1612                if (event == NULL)
     1613                        return ENOMEM;
    16001614
    16011615                link_initialize(&event->link);
     
    16091623        }
    16101624
    1611         async_answer_0(iid, EOK);
    1612 }
    1613 
    1614 static void input_events(ipc_callid_t iid, ipc_call_t *icall, void *arg)
    1615 {
     1625        return EOK;
     1626}
     1627
     1628static int input_connect(const char *svc)
     1629{
     1630        async_sess_t *sess;
     1631        service_id_t dsid;
     1632
     1633        int rc = loc_service_get_id(svc, &dsid, 0);
     1634        if (rc != EOK) {
     1635                printf("%s: Input service %s not found\n", NAME, svc);
     1636                return rc;
     1637        }
     1638
     1639        sess = loc_service_connect(EXCHANGE_ATOMIC, dsid, 0);
     1640        if (sess == NULL) {
     1641                printf("%s: Unable to connect to input service %s\n", NAME,
     1642                    svc);
     1643                return EIO;
     1644        }
     1645
    16161646        fibril_mutex_lock(&pointer_list_mtx);
    16171647        pointer_t *pointer = pointer_create();
     
    16221652        fibril_mutex_unlock(&pointer_list_mtx);
    16231653
    1624         /* Ignore parameters, the connection is already opened. */
    1625         while (true) {
    1626                 ipc_call_t call;
    1627                 ipc_callid_t callid = async_get_call(&call);
    1628 
    1629                 if (!IPC_GET_IMETHOD(call)) {
    1630                         fibril_mutex_lock(&pointer_list_mtx);
    1631                         if (pointer != NULL) {
    1632                                 list_remove(&pointer->link);
    1633                                 pointer_destroy(pointer);
    1634                         }
    1635                         fibril_mutex_unlock(&pointer_list_mtx);
    1636                         async_hangup(input_sess);
    1637                         return;
    1638                 }
    1639 
    1640                 switch (IPC_GET_IMETHOD(call)) {
    1641                 case INPUT_EVENT_KEY:
    1642                         comp_key_press(callid, &call);
    1643                         break;
    1644                 case INPUT_EVENT_MOVE:
    1645                         comp_mouse_move(pointer, callid, &call);
    1646                         break;
    1647                 case INPUT_EVENT_BUTTON:
    1648                         comp_mouse_button(pointer, callid, &call);
    1649                         break;
    1650                 default:
    1651                         async_answer_0(callid, EINVAL);
    1652                 }
    1653         }
    1654 }
    1655 
    1656 static async_sess_t *input_connect(const char *svc)
    1657 {
    1658         async_sess_t *sess;
    1659         service_id_t dsid;
    1660 
    1661         int rc = loc_service_get_id(svc, &dsid, 0);
    1662         if (rc == EOK) {
    1663                 sess = loc_service_connect(EXCHANGE_ATOMIC, dsid, 0);
    1664                 if (sess == NULL) {
    1665                         printf("%s: Unable to connect to input service %s\n", NAME, svc);
    1666                         return NULL;
    1667                 }
    1668         } else
    1669                 return NULL;
    1670 
    1671         async_exch_t *exch = async_exchange_begin(sess);
    1672         rc = async_connect_to_me(exch, 0, 0, 0, input_events, NULL);
    1673         async_exchange_end(exch);
    1674 
     1654        if (pointer == NULL) {
     1655                printf("%s: Cannot create pointer.\n", NAME);
     1656                async_hangup(sess);
     1657                return ENOMEM;
     1658        }
     1659
     1660        rc = input_open(sess, &input_ev_ops, pointer, &input);
    16751661        if (rc != EOK) {
    16761662                async_hangup(sess);
    1677                 printf("%s: Unable to create callback connection to service %s (%s)\n",
     1663                printf("%s: Unable to communicate with service %s (%s)\n",
    16781664                    NAME, svc, str_error(rc));
    1679                 return NULL;
    1680         }
    1681 
    1682         return sess;
     1665                return rc;
     1666        }
     1667
     1668        return EOK;
     1669}
     1670
     1671static void input_disconnect(void)
     1672{
     1673        pointer_t *pointer = input->user;
     1674        input_close(input);
     1675        pointer_destroy(pointer);
    16831676}
    16841677
     
    17331726
    17341727        /* Establish input bidirectional connection. */
    1735         input_sess = input_connect(input_svc);
    1736         if (input_sess == NULL) {
    1737                 return -1;
    1738         }
     1728        rc = input_connect(input_svc);
     1729        if (rc != EOK)
     1730                return rc;
    17391731
    17401732        /* Create viewports and connect them to visualizers. */
     
    17421734        rc = loc_category_get_id("visualizer", &cat_id, IPC_FLAG_BLOCKING);
    17431735        if (rc != EOK) {
    1744                 async_hangup(input_sess);
     1736                input_disconnect();
    17451737                return -1;
    17461738        }
     
    17501742        rc = loc_category_get_svcs(cat_id, &svcs, &svcs_cnt);
    17511743        if (rc != EOK || svcs_cnt == 0) {
    1752                 async_hangup(input_sess);
     1744                input_disconnect();
    17531745                return -1;
    17541746        }
     
    17661758       
    17671759        if (list_empty(&viewport_list)) {
    1768                 async_hangup(input_sess);
     1760                input_disconnect();
    17691761                return -1;
    17701762        }
Note: See TracChangeset for help on using the changeset viewer.