Compare commits

...

2 Commits

Author SHA1 Message Date
2ea1e96401 logging changes 2023-11-25 13:59:28 +01:00
62bf51d706 add pid field to client 2023-11-25 13:57:49 +01:00
5 changed files with 63 additions and 18 deletions

View File

@ -20,6 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "client.h"
#include "util.h"
#include "wm.h"
#include <X11/Xlib.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
@ -74,6 +75,7 @@ Client* wm_client_create(Wm *wm, Window w)
c->hidden = true;
c->is_floating = false;
c->name = NULL;
c->is_pid_set = false;
if (xtp.value) {
strln = strlen((char*)xtp.value);
@ -88,8 +90,21 @@ Client* wm_client_create(Wm *wm, Window w)
wm_client_set_atom(wm, c, "_NET_WM_DESKTOP", (unsigned char*)&c->ws->index,
XA_CARDINAL, 1);
unsigned char *prop_ret = NULL;
unsigned long nitems_ret;
Atom type;
bool is_normal = false;
type = wm_client_get_atom(wm, c, "_NET_WM_PID", &prop_ret, &nitems_ret);
if (!nitems_ret || !prop_ret) goto ret;
c->pid = *((int*)prop_ret);
DEBUG_PRINT("pid: %d\n", c->pid);
c->is_pid_set = true;
ret:
XFree(xtp.value);
if (prop_ret) XFree(prop_ret);
return c;
}
@ -308,11 +323,6 @@ Atom wm_client_get_atom(Wm *wm, Client *c, const char *name, unsigned char **ato
DEBUG_PRINT("actual format return: %d\n", format_ret)
DEBUG_PRINT("actual number of items return: %ld\n", *nitems_ret)
DEBUG_PRINT("bytes remaining: %ld\n", *nitems_ret)
for (int i = 0; i < *nitems_ret; i++) {
char *atom_name = XGetAtomName(wm->display, ((Atom*)*atom_ret)[i]);
printf("property return str: %s\n", atom_name);
XFree(atom_name);
}
return type_ret;
}

View File

@ -44,6 +44,8 @@ struct Client {
bool focused;
bool has_border;
bool is_floating;
pid_t pid;
bool is_pid_set;
};
XWindowChanges wm_client_to_xwchanges(Client *c);

View File

@ -148,7 +148,14 @@ char* wm_treenode_to_str(TreeNode *node)
json_object_object_add(pos, "w", json_object_new_int(node->pos.w));
json_object_object_add(pos, "h", json_object_new_int(node->pos.h));
json_object_object_add(jobj, "pos", pos);
json_object_object_add(jobj, "client", json_object_new_uint64((uint64_t)node->client));
if (node->type == NODE_CLIENT && node->client) {
json_object *client = json_object_new_object();
json_object_object_add(client, "address", json_object_new_uint64((unsigned long)node->client));
json_object_object_add(client, "name", json_object_new_string(node->client->name));
json_object_object_add(client, "is_pid_set", json_object_new_boolean(node->client->is_pid_set));
json_object_object_add(client, "pid", json_object_new_int(node->client->pid));
json_object_object_add(jobj, "client", client);
}
json_object_object_add(jobj, "id", json_object_new_uint64((uint64_t)node->id));
char *ret = strdup(json_object_to_json_string_ext(jobj, JSON_C_TO_STRING_PRETTY));
@ -164,11 +171,17 @@ PtrArray wm_nonempty_workspaces_to_strptrarray(Wm *wm)
for (size_t i = 0; i < wm->smon->wscount; i++) {
TreeNode *node = wm->smon->workspaces[i].tree;
if (!wm_treenode_is_empty(node) || (node->type == NODE_CLIENT && node->client))
wm_ptrarray_push(&ret, wm_treenode_to_str(wm->smon->workspaces[i].tree));
if (!wm_treenode_is_empty(node) || (node->type == NODE_CLIENT && node->client)) {
WmWorkspaceToStrRet *ws_str_with_index = malloc(sizeof(WmWorkspaceToStrRet));
*ws_str_with_index = (WmWorkspaceToStrRet) {
.ws_index = i,
.str = wm_treenode_to_str(wm->smon->workspaces[i].tree)
};
wm_ptrarray_push(&ret, ws_str_with_index);
}
}
DEBUG_PRINT("%s returning %d\n", __func__, ret.size);
DEBUG_PRINT("%s returning %ld\n", __func__, ret.size);
return ret;
}
@ -178,19 +191,32 @@ void wm_log_state(Wm *wm, const char *prefixstr, const char* logfile)
RETURN_IF_NULL(prefixstr);
RETURN_IF_NULL(logfile);
PtrArray ws_str = wm_nonempty_workspaces_to_strptrarray(wm);
FILE* fptr = fopen(logfile, "a");
FILE *fptr = fopen(logfile, "a");
if (!fptr) {
fprintf(stderr, "wm: could not open log file %s\n", logfile);
return;
}
if (prefixstr) fprintf(fptr, "%lu %s\n", time(NULL), prefixstr);
PtrArray ws_str = wm_nonempty_workspaces_to_strptrarray(wm);
char str[128] = {0};
char prefix[WM_PREFIX_LEN + 32] = {0};
snprintf(prefix, sizeof(prefix), "%lu_%s", time(NULL), prefixstr);
json_object *log_entry_obj = json_object_new_object();
json_object_object_add(log_entry_obj, prefix, json_object_new_object());
for (size_t i = 0; i < ws_str.size; i++) {
fprintf(fptr, "%s\n", (char*)ws_str.ptrs[i]);
free(ws_str.ptrs[i]);
WmWorkspaceToStrRet *ws_with_index = (WmWorkspaceToStrRet*)ws_str.ptrs[i];
snprintf(str, sizeof(str), "%ld", ws_with_index->ws_index);
json_object_object_add(json_object_object_get(log_entry_obj, prefix), str, json_tokener_parse(ws_with_index->str));
fprintf(fptr, "%s,\n", json_object_to_json_string_ext(log_entry_obj, JSON_C_TO_STRING_PRETTY));
free(ws_with_index->str);
free(ws_with_index);
}
json_object_put(log_entry_obj);
fflush(fptr);
fclose(fptr);
wm_ptrarray_free(&ws_str);
}

View File

@ -61,6 +61,11 @@ struct TreeNode {
unsigned int id;
};
typedef struct {
size_t ws_index;
char *str;
} WmWorkspaceToStrRet;
TreeNode* wm_treenode_new(NodeType type, TreeNode *parent);
void wm_treenode_free(TreeNode *node);
bool wm_treenode_is_empty(TreeNode *node);

View File

@ -65,16 +65,18 @@ if (c == NULL) \
#define CURRENT_WS(wm) (wm)->smon->workspaces[(wm)->smon->selws]
#define WM_PREFIX_LEN 4096
#define WM_LOGFILENAME "./log"
#define WM_LOG_STATE_START(wm) do { \
wm_log_state(wm, __func__, WM_LOGFILENAME); \
} while (0);
#define WM_LOG_STATE_END(wm) do { \
char __prefix[4096] = {0}; \
snprintf(__prefix, 4096, "after %s", __func__); \
wm_log_state(wm, __prefix, WM_LOGFILENAME); \
#define WM_LOG_STATE_END(wm) do { \
char __prefix[WM_PREFIX_LEN] = {0}; \
snprintf(__prefix, WM_PREFIX_LEN, "after %s", __func__); \
wm_log_state(wm, __prefix, WM_LOGFILENAME); \
} while (0);
typedef struct Client Client;