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 {
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 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);
}
static void
+_tdm_server_vblank_cb_set_name(struct wl_client *client, struct wl_resource *resource, const char *name)
+{
+ tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
+
+ tdm_vblank_set_name(vblank_info->vblank, name);
+}
+
+static void
_tdm_server_vblank_cb_set_fps(struct wl_client *client, struct wl_resource *resource, uint32_t fps)
{
tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
tdm_server_output_info *output_info = vblank_info->output_info;
tdm_private_server *private_server = output_info->private_server;
tdm_server_wait_info *wait_info;
+ unsigned int enable_fake = 0;
tdm_error ret;
TDM_TRACE_COUNT(ServerWaitVBlank, req_id);
- wait_info = calloc(1, sizeof *wait_info);
+ wait_info = calloc(1, sizeof * wait_info);
if (!wait_info) {
TDM_ERR("alloc failed");
ret = TDM_ERROR_OUT_OF_MEMORY;
ret = tdm_vblank_wait(vblank_info->vblank, req_sec, req_usec, interval, _tdm_server_cb_vblank, wait_info);
- if (!tdm_vblank_get_enable_fake(vblank_info->vblank) && ret == TDM_ERROR_DPMS_OFF)
+ tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
+ if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
goto wait_failed;
TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
tdm_server_output_info *output_info = vblank_info->output_info;
tdm_private_server *private_server = output_info->private_server;
tdm_server_wait_info *wait_info;
+ unsigned int enable_fake = 0;
tdm_error ret;
TDM_TRACE_COUNT(ServerWaitVBlank, req_id);
- wait_info = calloc(1, sizeof *wait_info);
+ wait_info = calloc(1, sizeof * wait_info);
if (!wait_info) {
TDM_ERR("alloc failed");
ret = TDM_ERROR_OUT_OF_MEMORY;
ret = tdm_vblank_wait_seq(vblank_info->vblank, req_sec, req_usec, sequence, _tdm_server_cb_vblank, wait_info);
- if (!tdm_vblank_get_enable_fake(vblank_info->vblank) && ret == TDM_ERROR_DPMS_OFF)
+ tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
+ if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
goto wait_failed;
TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
static const struct wl_tdm_vblank_interface tdm_vblank_implementation = {
_tdm_server_vblank_cb_destroy,
+ _tdm_server_vblank_cb_set_name,
_tdm_server_vblank_cb_set_fps,
_tdm_server_vblank_cb_set_offset,
_tdm_server_vblank_cb_set_enable_fake,
return;
}
- vblank_info = calloc(1, sizeof *vblank_info);
+ vblank_info = calloc(1, sizeof * vblank_info);
if (!vblank_info) {
wl_resource_post_no_memory(resource);
wl_resource_destroy(vblank_resource);
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_private_server *private_server = wl_resource_get_user_data(resource);
tdm_private_loop *private_loop = private_server->private_loop;
-
char message[TDM_SERVER_REPLY_MSG_LEN];
int size = sizeof(message);
+ uid_t uid;
+
+ wl_client_get_credentials(client, NULL, &uid, NULL);
+
+ if (uid != 0) {
+ snprintf(message, size, "tdm-monitor: SHOULD be a superuser.\n");
+ TDM_ERR("%s", message);
+ } else {
+ tdm_monitor_server_command(private_loop->dpy, options, message, &size);
+ }
- tdm_monitor_server_command(private_loop->dpy, options, message, &size);
wl_tdm_send_debug_done(resource, message);
}
+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_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 const struct wl_tdm_interface tdm_implementation = {
- _tdm_server_cb_create_output,
_tdm_server_cb_debug,
+ _tdm_server_cb_create_output,
+ _tdm_server_cb_set_client_vblank_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 void
+_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;
+
+ dir = getenv("XDG_RUNTIME_DIR");
+ if (!dir) {
+ TDM_WRN("getting XDG_RUNTIME_DIR failed");
+ return;
+ }
+
+ snprintf(socket_path, sizeof(socket_path), "%s/%s", dir, "tdm-socket");
+
+ ret = chmod(socket_path, 509);
+ if (ret < 0) {
+ TDM_WRN("changing modes of socket file failed:%s (%m)", socket_path);
+ return;
+ }
+
+ ret = _tdm_getgrnam_r("root");
+ if (ret < 0) {
+ TDM_WRN("getting uid failed");
+ return;
+ }
+ uid = ret;
+
+ ret = _tdm_getgrnam_r("display");
+ if (ret < 0) {
+ TDM_WRN("getting gid failed");
+ return;
+ }
+ gid = ret;
+
+ ret = chown(socket_path, uid, gid);
+ if (ret < 0) {
+ TDM_WRN("changing owner of socket file failed:%s (%m)", socket_path);
+ return;
+ }
}
INTERN tdm_error
{
tdm_private_server *private_server;
- if (private_loop->private_server)
- return TDM_ERROR_NONE;
-
TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
+ 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");
return TDM_ERROR_OPERATION_FAILED;
}
+ _tdm_socket_init(private_loop);
+
private_server = calloc(1, sizeof * private_server);
if (!private_server) {
TDM_ERR("alloc failed");
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;
+
+ LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
+ LIST_DEL(&c->link);
+ free(c);
+ }
+}
+
+INTERN const char*
+tdm_server_get_client_name(pid_t pid)
+{
+ tdm_server_client_info *c = NULL;
+
+ LIST_FOR_EACH_ENTRY(c, &client_list, link) {
+ if (c->pid == pid)
+ return (const char*)c->name;
+ }
+
+ return NULL;
}