struct wl_resource *resource;
tdm_output *output;
struct list_head vblank_list;
+ unsigned int watch_output_changes;
} tdm_server_output_info;
typedef struct _tdm_server_vblank_info {
struct list_head link;
- struct list_head valid_link;
-
tdm_server_output_info *output_info;
struct wl_resource *resource;
unsigned int req_id;
} tdm_server_wait_info;
+typedef struct _tdm_server_client_info {
+ struct list_head link;
+ pid_t pid;
+ char name[TDM_NAME_LEN];
+} tdm_server_client_info;
+
static tdm_private_server *keep_private_server;
-static struct list_head valid_vblank_list;
+static struct list_head client_list;
static void destroy_wait(tdm_server_wait_info *wait_info);
+static void
+_tdm_server_get_process_name(pid_t pid, char *name, unsigned int size)
+{
+ char proc[TDM_NAME_LEN], pname[TDM_NAME_LEN];
+ FILE *h;
+ size_t len;
+
+ snprintf(name, size, "Unknown");
+
+ snprintf(proc, TDM_NAME_LEN, "/proc/%d/cmdline", pid);
+ h = fopen(proc, "r");
+ if (!h)
+ return;
+
+ len = fread(pname, sizeof(char), TDM_NAME_LEN, h);
+ if (len == 0) {
+ char *p = strncpy(pname, "NO NAME", sizeof(pname) - 1);
+ len = p - pname;
+ }
+ pname[len - 1] = '\0';
+
+ strncpy(name, pname, size - 1);
+ name[size - 1] = '\0';
+
+ fclose(h);
+}
+
static tdm_output*
_tdm_server_find_output(tdm_private_server *private_server, const char *name)
{
tdm_value value, void *user_data)
{
tdm_server_output_info *output_info = user_data;
+ struct wl_client *client;
+ pid_t pid = 0;
TDM_RETURN_IF_FAIL(output_info != NULL);
+ client = wl_resource_get_client(output_info->resource);
+ TDM_RETURN_IF_FAIL(client != NULL);
+
+ wl_client_get_credentials(client, &pid, NULL, NULL);
+
+ if (!output_info->watch_output_changes) {
+ TDM_DBG("skip sending the output changes: pid(%d)", (unsigned int)pid);
+ return;
+ }
+
+ TDM_DBG("send the output changes: %d", (unsigned int)pid);
+
switch (type) {
case TDM_OUTPUT_CHANGE_DPMS:
wl_tdm_output_send_dpms(output_info->resource, value.u32);
}
LIST_DEL(&vblank_info->link);
- LIST_DEL(&vblank_info->valid_link);
-
free(vblank_info);
}
}
LIST_ADDTAIL(&vblank_info->link, &output_info->vblank_list);
- LIST_ADDTAIL(&vblank_info->valid_link, &valid_vblank_list);
-
vblank_info->output_info = output_info;
vblank_info->resource = vblank_resource;
vblank_info->vblank = vblank;
+ tdm_vblank_set_resource(vblank, vblank_resource);
+
wl_resource_set_implementation(vblank_resource, &tdm_vblank_implementation,
vblank_info, destroy_vblank_callback);
return;
}
+static void
+_tdm_server_output_cb_watch_output_changes(struct wl_client *client, struct wl_resource *resource, unsigned int enable)
+{
+ tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
+
+ TDM_RETURN_IF_FAIL(output_info != NULL);
+
+ output_info->watch_output_changes = enable;
+}
+
static const struct wl_tdm_output_interface tdm_output_implementation = {
_tdm_server_output_cb_destroy,
_tdm_server_output_cb_create_vblank,
+ _tdm_server_output_cb_watch_output_changes,
};
static void
_tdm_server_cb_set_client_vblank_fps(struct wl_client *client, struct wl_resource *resource,
unsigned int pid, const char *name, unsigned int fps)
{
- tdm_error ret = tdm_server_set_client_vblank_fps(pid, name, fps);
+ tdm_error ret = tdm_vblank_set_client_vblank_fps(pid, name, fps);
TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
TDM_INFO("'%s' vblank fps(PID: '%u'): %u", name, pid, fps);
};
static void
+destroy_client(struct wl_resource *resource)
+{
+ tdm_server_client_info *c = NULL, *cc = NULL;
+ struct wl_client *client;
+ pid_t pid = -1;
+
+ client = wl_resource_get_client(resource);
+ TDM_RETURN_IF_FAIL(client != NULL);
+
+ wl_client_get_credentials(client, &pid, NULL, NULL);
+
+ LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
+ if (c->pid == pid) {
+ LIST_DEL(&c->link);
+ free(c);
+ return;
+ }
+ }
+}
+
+static void
_tdm_server_bind(struct wl_client *client, void *data,
uint32_t version, uint32_t id)
{
struct wl_resource *resource;
+ tdm_server_client_info *cinfo;
resource = wl_resource_create(client, &wl_tdm_interface, version, id);
if (!resource) {
return;
}
- wl_resource_set_implementation(resource, &tdm_implementation, data, NULL);
+ cinfo = calloc(1, sizeof(tdm_server_client_info));
+ if (!cinfo) {
+ wl_client_post_no_memory(client);
+ wl_resource_destroy(resource);
+ return;
+ }
+
+ LIST_ADDTAIL(&cinfo->link, &client_list);
+ wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
+ _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
+
+ wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
+}
+
+static int
+_tdm_getgrnam_r(const char *name)
+{
+ struct group *grp = NULL;
+ struct group *grp_res = NULL;
+ char* buf = NULL;
+ size_t buf_len;
+ int ret;
+ int id;
+
+ buf_len = sysconf(_SC_GETGR_R_SIZE_MAX);
+ if (buf_len == -1)
+ buf_len = 2048;
+
+ buf = calloc(1, buf_len * sizeof(char));
+ if (!buf) {
+ TDM_ERR("creating buffer failed");
+ goto failed;
+ }
+
+ grp = calloc(1, sizeof(struct group));
+ if (!grp) {
+ TDM_ERR("creating group failed");
+ goto failed;
+ }
+
+ ret = getgrnam_r(name, grp, buf, buf_len, &grp_res);
+ if (ret < 0) {
+ TDM_ERR("getgrnam_r failed errno:%d(%m)", ret);
+ goto failed;
+ }
+
+ if (grp_res == NULL) {
+ TDM_ERR("finding name:%s group failed", name);
+ goto failed;
+ }
+
+ id = grp->gr_gid;
+ free(buf);
+ free(grp);
+
+ return id;
+
+failed:
+ if (buf)
+ free(buf);
+ if (grp)
+ free(grp);
+
+ return -1;
+}
+
+static int
+_tdm_socket_init(tdm_private_loop *private_loop)
+{
+ const char *dir = NULL;
+ char socket_path[128];
+ int ret = -1;
+ uid_t uid;
+ gid_t gid;
+
+ if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
+ TDM_ERR("createing a tdm-socket failed");
+ return 0;
+ }
+
+ dir = getenv("XDG_RUNTIME_DIR");
+ if (!dir) {
+ TDM_ERR("getting XDG_RUNTIME_DIR failed");
+ return 0;
+ }
+
+ snprintf(socket_path, sizeof(socket_path), "%s/%s", dir, "tdm-socket");
+
+ ret = chmod(socket_path, 509);
+ if (ret < 0) {
+ TDM_ERR("changing modes of socket file failed:%s (%m)", socket_path);
+ return 0;
+ }
+
+ ret = _tdm_getgrnam_r("root");
+ if (ret < 0) {
+ TDM_ERR("getting uid failed");
+ return 0;
+ }
+ uid = ret;
+
+ ret = _tdm_getgrnam_r("display");
+ if (ret < 0) {
+ TDM_ERR("getting gid failed");
+ return 0;
+ }
+ gid = ret;
+
+ ret = chown(socket_path, uid, gid);
+ if (ret < 0) {
+ TDM_ERR("changing owner of socket file failed:%s (%m)", socket_path);
+ return 0;
+ }
+
+ return 1;
}
INTERN tdm_error
if (private_loop->private_server)
return TDM_ERROR_NONE;
- if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
- TDM_ERR("createing a tdm-socket failed");
+ if (!_tdm_socket_init(private_loop)) {
+ TDM_ERR("initializing tdm-socket failed");
return TDM_ERROR_OPERATION_FAILED;
}
return TDM_ERROR_OUT_OF_MEMORY;
}
- LIST_INITHEAD(&valid_vblank_list);
-
private_server->private_loop = private_loop;
private_loop->private_server = private_server;
keep_private_server = private_server;
+ LIST_INITHEAD(&client_list);
+
return TDM_ERROR_NONE;
}
{
tdm_server_output_info *o = NULL, *oo = NULL;
tdm_server_wait_info *w = NULL, *ww = NULL;
+ tdm_server_client_info *c = NULL, *cc = NULL;
tdm_private_server *private_server;
if (!private_loop->private_server)
free(private_server);
private_loop->private_server = NULL;
keep_private_server = NULL;
-}
-
-INTERN tdm_error
-tdm_server_set_client_vblank_fps(unsigned int pid, const char *name, unsigned int fps)
-{
- tdm_server_vblank_info *v;
-
- TDM_RETURN_VAL_IF_FAIL(pid > 0, TDM_ERROR_INVALID_PARAMETER);
- TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
-
- LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
- struct wl_client *client = wl_resource_get_client(v->resource);
- pid_t client_pid = 0;
- const char *vblank_name = NULL;
-
- if (!client)
- continue;
- wl_client_get_credentials(client, &client_pid, NULL, NULL);
-
- if (client_pid != pid)
- continue;
-
- if (name && strncmp(name, TDM_VBLANK_DEFAULT_NAME, TDM_NAME_LEN)) {
- tdm_vblank_get_name(v->vblank, &vblank_name);
- if (strncmp(vblank_name, name, TDM_NAME_LEN))
- continue;
- }
-
- tdm_vblank_set_fps(v->vblank, fps);
+ LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
+ LIST_DEL(&c->link);
+ free(c);
}
-
- return TDM_ERROR_NONE;
}
-static void
-_tdm_server_get_process_name(pid_t pid, char *name, unsigned int size)
+INTERN const char*
+tdm_server_get_client_name(pid_t pid)
{
- char proc[TDM_NAME_LEN], pname[TDM_NAME_LEN];
- FILE *h;
- size_t len;
+ tdm_server_client_info *c = NULL;
- snprintf(proc, TDM_NAME_LEN, "/proc/%d/cmdline", pid);
- h = fopen(proc, "r");
- if (!h)
- return;
-
- len = fread(pname, sizeof(char), TDM_NAME_LEN, h);
- if (len == 0) {
- char *p = strncpy(pname, "NO NAME", sizeof(pname) - 1);
- len = p - pname;
+ LIST_FOR_EACH_ENTRY(c, &client_list, link) {
+ if (c->pid == pid)
+ return (const char*)c->name;
}
- pname[len - 1] = '\0';
- strncpy(name, pname, size - 1);
- name[size - 1] = '\0';
-
- fclose(h);
+ return NULL;
}
-
-INTERN void
-tdm_server_get_vblank_list_information(tdm_display *dpy, char *reply, int *len)
-{
- tdm_server_vblank_info *v;
-
- TDM_SNPRINTF(reply, len, "[Client Vblank List]\n");
- TDM_SNPRINTF(reply, len, "---------------------------------------------------------------\n");
- TDM_SNPRINTF(reply, len, "name fps offset fake process\n");
- TDM_SNPRINTF(reply, len, "---------------------------------------------------------------\n");
-
- LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
- struct wl_client *client = wl_resource_get_client(v->resource);
- const char *vbl_name = NULL;
- char proc_name[TDM_NAME_LEN];
- unsigned int fps = 0, fake = 0;
- int offset = 0;
- pid_t pid = 0;
-
- tdm_vblank_get_name(v->vblank, &vbl_name);
- tdm_vblank_get_fps(v->vblank, &fps);
- tdm_vblank_get_offset(v->vblank, &offset);
- tdm_vblank_get_enable_fake(v->vblank, &fake);
-
- snprintf(proc_name, TDM_NAME_LEN, "Unknown");
- if (client) {
- wl_client_get_credentials(client, &pid, NULL, NULL);
- _tdm_server_get_process_name(pid, proc_name, TDM_NAME_LEN);
- }
-
- TDM_SNPRINTF(reply, len, "%-12s %u %d %u %s (pid: %u)\n",
- vbl_name, fps, offset, fake, proc_name, pid);
- }
-
- TDM_SNPRINTF(reply, len, "\n");
-}
-