Log files should have .txt extension.
[helenos.git] / uspace / lib / ddev / src / ddev.c
blob3f5c3e01377b2b345185d28deeb357a76c9ff5fc
1 /*
2 * Copyright (c) 2019 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <async.h>
30 #include <ddev.h>
31 #include <errno.h>
32 #include <ipc/ddev.h>
33 #include <ipc/services.h>
34 #include <ipcgfx/client.h>
35 #include <loc.h>
36 #include <stdlib.h>
38 /** Open display device.
40 * @param ddname Display device service name
41 * @param rdisplay Place to store pointer to display session
42 * @return EOK on success or an error code
44 errno_t ddev_open(const char *ddname, ddev_t **rddev)
46 service_id_t ddev_svc;
47 ddev_t *ddev;
48 errno_t rc;
50 ddev = calloc(1, sizeof(ddev_t));
51 if (ddev == NULL)
52 return ENOMEM;
54 rc = loc_service_get_id(ddname, &ddev_svc, IPC_FLAG_BLOCKING);
55 if (rc != EOK) {
56 free(ddev);
57 return ENOENT;
60 ddev->sess = loc_service_connect(ddev_svc, INTERFACE_DDEV,
61 IPC_FLAG_BLOCKING);
62 if (ddev->sess == NULL) {
63 free(ddev);
64 return ENOENT;
67 *rddev = ddev;
68 return EOK;
71 /** Close display device.
73 * @param ddev Display device session
75 void ddev_close(ddev_t *ddev)
77 async_hangup(ddev->sess);
78 free(ddev);
81 /** Create graphics context for drawing to display device.
83 * @param ddev Display device
84 * @param rgc Place to store pointer to new graphics context
86 errno_t ddev_get_gc(ddev_t *ddev, gfx_context_t **rgc)
88 async_sess_t *sess;
89 async_exch_t *exch;
90 sysarg_t arg2;
91 sysarg_t arg3;
92 ipc_gc_t *gc;
93 errno_t rc;
95 exch = async_exchange_begin(ddev->sess);
96 rc = async_req_0_2(exch, DDEV_GET_GC, &arg2, &arg3);
97 if (rc != EOK) {
98 async_exchange_end(exch);
99 return rc;
102 sess = async_connect_me_to(exch, INTERFACE_GC, arg2, arg3, &rc);
103 async_exchange_end(exch);
105 if (sess == NULL)
106 return rc;
108 rc = ipc_gc_create(sess, &gc);
109 if (rc != EOK) {
110 async_hangup(sess);
111 return ENOMEM;
114 *rgc = ipc_gc_get_ctx(gc);
115 return EOK;
118 /** Get display device information.
120 * @param ddev Display device
121 * @param info Place to store information
123 errno_t ddev_get_info(ddev_t *ddev, ddev_info_t *info)
125 async_exch_t *exch;
126 errno_t retval;
127 ipc_call_t answer;
129 exch = async_exchange_begin(ddev->sess);
130 aid_t req = async_send_0(exch, DDEV_GET_INFO, &answer);
132 errno_t rc = async_data_read_start(exch, info, sizeof(ddev_info_t));
133 async_exchange_end(exch);
134 if (rc != EOK) {
135 async_forget(req);
136 return rc;
139 async_wait_for(req, &retval);
140 if (retval != EOK)
141 return rc;
143 return EOK;
146 /** @}