Browse Source

daemon: add game_mode_context_lookup_client method

Like game_mode_context_has_client, but will add a reference to
the client, if a match was found.
Christian Kellner 5 years ago
parent
commit
5949a988ea
2 changed files with 31 additions and 0 deletions
  1. 23 0
      daemon/gamemode-context.c
  2. 8 0
      daemon/gamemode.h

+ 23 - 0
daemon/gamemode-context.c

@@ -358,6 +358,29 @@ pid_t *game_mode_context_list_clients(GameModeContext *self, unsigned int *count
 	return res;
 }
 
+GameModeClient *game_mode_context_lookup_client(GameModeContext *self, pid_t client)
+{
+	GameModeClient *found = NULL;
+
+	pthread_rwlock_rdlock(&self->rwlock);
+
+	/* Walk all clients and find a matching pid */
+	for (GameModeClient *cl = self->client; cl; cl = cl->next) {
+		if (cl->pid == client) {
+			found = cl;
+			break;
+		}
+	}
+
+	if (found) {
+		game_mode_client_ref(found);
+	}
+
+	pthread_rwlock_unlock(&self->rwlock);
+
+	return found;
+}
+
 static int game_mode_apply_client_optimisations(GameModeContext *self, pid_t client)
 {
 	/* Store current renice and apply */

+ 8 - 0
daemon/gamemode.h

@@ -103,6 +103,14 @@ int game_mode_context_num_clients(GameModeContext *self);
  */
 pid_t *game_mode_context_list_clients(GameModeContext *self, unsigned int *count);
 
+/**
+ * Lookup up information about a client via the pid;
+ *
+ * @returns A pointer to a GameModeClient struct or NULL in case no client
+ *           with the corresponding id could be found. Adds a reference to
+ *           GameModeClient that needs to be released.
+ */
+GameModeClient *game_mode_context_lookup_client(GameModeContext *self, pid_t client);
 /**
  * Register a new game client with the context
  *