dark-hammer / src / engine / prf-mgr.c

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/***********************************************************************************
 * Copyright (c) 2012, Sepehr Taghdisian
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, 
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation 
 *   and/or other materials provided with the distribution.
 *
 ***********************************************************************************/

#include "core/core.h"
#include "core/array.h"
#include "core/hash.h"
#include "core/json.h"
#include "core/timer.h"
#include "core/stack-alloc.h"
#include "core/atomic.h"
#include "core/mutex.h"

#include "prf-mgr.h"
#include "mongoose/mongoose.h"
#include "mem-ids.h"
#include "engine.h"
#include "gfx-device.h"
#include "scene-mgr.h"
#include "camera.h"
#include "script.h"
#include "phx-device.h"

#define AJAX_HEADER	"/json/"
#define HSEED	2874
#define SAMPLES_BUFFER_SIZE (64*1024)
#define PROTECT_CMD() if (!g_prf.init)   return JSON_NULL;

/*************************************************************************************************
 * types
 */
typedef json_t (*pfn_ajax_cmd)(const char* param1, const char* param2);

struct prf_cmd_desc
{
	const char* name;
	uint32 hash;
	pfn_ajax_cmd cmd_fn;
};

struct prf_node
{
    char name[32];
    uint64 start_tick;
    fl64 start_tm;
    fl64 tm;

    struct linked_list l;   /* linked list node */
    struct linked_list* childs; /* children linked-list */
    struct prf_node* parent;
};

struct prf_samples
{
    struct stack_alloc alloc; /* stack allocator */
    struct linked_list* nodes; /* performance nodes, data=prf_node */
    struct prf_node* node_cur; /* cursor node */
    fl32 duration;
};

struct prf_mgr
{
    atomint_t init;
	struct mg_context* ctx;	/* context for web-server */
	char root_dir[DH_PATH_MAX]; /* root-directory for web-server */
	struct array cmds; /* commands (item: cmd_desc)*/
    atomptr_t samples_back; /* type: prf_samples*, the one that is being created by engine */
    atomptr_t samples_front; /* type: prf_samples*, the one that is presentable to user */
    mutex_t samples_mtx;    /* mutex for front-buffer protection */
};

/*************************************************************************************************
 * globals
 */
struct prf_mgr g_prf;

/*************************************************************************************************
 * fwd declarations
 */
result_t webserver_init(int32 port);
void webserver_release();
void* webserver_malloc(size_t s);
void webserver_free(void* ptr);
void* webserver_realloc(void* ptr, size_t s);
void* webserver_request_callback(enum mg_event event, struct mg_connection* conn);
void webserver_runajax(struct mg_connection* conn, const char* cmd, const char* param1,
		const char* param2);
void prf_register_cmd(const char* name, pfn_ajax_cmd callback_fn);
pfn_ajax_cmd prf_find_cmd(const char* name);
json_t prf_create_node_json(json_t parent, struct prf_node* node);
struct prf_samples* prf_create_samples();
void prf_destroy_samples(struct prf_samples* s);

/*************************************************************************************************
 * commands (in form of pfn_ajax_cmd signature)
 * these commands run in multi-threaded mode (web-service), so they must be thread-safe
 */
json_t prf_cmd_heapinfo(const char* param1, const char* param2);
json_t prf_cmd_heapsubinfo(const char* param1, const char* param2);
json_t prf_cmd_gpumem(const char* param1, const char* param2);
json_t prf_cmd_profilergantt(const char* param1, const char* param2);
json_t prf_cmd_buffersmem(const char* param1, const char* param2);
json_t prf_cmd_getcaminfo(const char* param1, const char* param2);

/*************************************************************************************************
 * inlines
 */
 json_t prf_cmd_createkeyvalue_n(const char* key, fl64 value, bool_t isgraph)
{
	json_t p = json_create_obj();
	json_additem_toobj(p, "key", json_create_str(key));
	json_additem_toobj(p, "value", json_create_num(value));
    if (isgraph)
        json_additem_toobj(p, "isgraph", json_create_bool(TRUE));
	return p;
}


INLINE json_t prf_cmd_createkeyvalue_s(const char* key, const char* value)
{
	json_t p = json_create_obj();
	json_additem_toobj(p, "key", json_create_str(key));
	json_additem_toobj(p, "value", json_create_str(value));
	return p;
}

INLINE json_t prf_cmd_createkeyvalue_b(const char* key, bool_t value)
{
	json_t p = json_create_obj();
	json_additem_toobj(p, "key", json_create_str(key));
	json_additem_toobj(p, "value", json_create_bool(value));
	return p;
}

INLINE struct prf_node* prf_create_node(const char* name, const char* file, uint32 line, 
    struct prf_node* parent)
{
    struct prf_samples* s = g_prf.samples_back;

    struct prf_node* node = mem_stack_alloc(&s->alloc, sizeof(struct prf_node), MID_PRF);
    ASSERT(node);
    memset(node, 0x00, sizeof(struct prf_node));

    strcpy(node->name, name);

    /* add to hierarchy */
    if (parent != NULL) {
        node->parent = parent;
        list_addlast(&parent->childs, &node->l, node);
    }   else    {
        list_addlast(&s->nodes, &node->l, node);
    }

    return node;
}

INLINE void prf_destroy_node(struct prf_node* node)
{
    struct prf_samples* s = g_prf.samples_back;

    /* remove from hiearchy */
    if (node->parent != NULL)   {
        node->parent = NULL;
        list_remove(&node->parent->childs, &node->l);
    }   else    {
        list_remove(&s->nodes, &node->l);
    }
}

/*************************************************************************************************/
void prf_zero()
{
	memset(&g_prf, 0x00, sizeof(g_prf));
}

result_t prf_initmgr()
{
	result_t r;

    /* samples */
    g_prf.samples_back = prf_create_samples();
    g_prf.samples_front = prf_create_samples();
    if (g_prf.samples_back == NULL || g_prf.samples_front == NULL)
        return RET_FAIL;

    mt_createmutex(&g_prf.samples_mtx);

	/* web server */
	r = webserver_init(eng_get_params()->dev.webserver_port);
	if (IS_FAIL(r))
		return RET_FAIL;

	/* commands */
	r = arr_create(mem_heap(), &g_prf.cmds, sizeof(struct prf_cmd_desc), 20, 20, MID_PRF);
	if (IS_FAIL(r))
		return RET_OUTOFMEMORY;

	/* register commands */
	prf_register_cmd("mem-heap", prf_cmd_heapinfo);
    prf_register_cmd("mem-heap-sub", prf_cmd_heapsubinfo);
    prf_register_cmd("mem-gpu", prf_cmd_gpumem);
    prf_register_cmd("prf-gantt", prf_cmd_profilergantt);
    prf_register_cmd("mem-buffers", prf_cmd_buffersmem);
    prf_register_cmd("info-cam", prf_cmd_getcaminfo);

    ATOMIC_SET(g_prf.init, TRUE);
	return RET_OK;
}

void prf_releasemgr()
{
    ATOMIC_SET(g_prf.init, FALSE);
    mt_destroymutex(&g_prf.samples_mtx);
	webserver_release();
	arr_destroy(&g_prf.cmds);
    if (g_prf.samples_front != NULL)
        prf_destroy_samples(g_prf.samples_front);
    if (g_prf.samples_back != NULL)
        prf_destroy_samples(g_prf.samples_back);
	prf_zero();
}

result_t webserver_init(int32 port)
{
	char port_str[32];
	mg_setmalloc(webserver_malloc, webserver_free, webserver_realloc);
	str_itos(port_str, port);
	const char* options[] = {
			"listening_ports", port_str, NULL
	};
	g_prf.ctx = mg_start(webserver_request_callback, NULL, options);
	if (g_prf.ctx == NULL)	{
		log_printf(LOG_WARNING, "starting httpd server failed - service will not be available.");
		return RET_FAIL;
	}

	/* web interface files are in "/data/web" */
	util_getexedir(g_prf.root_dir);
	path_makevalid(g_prf.root_dir, path_goup(g_prf.root_dir, g_prf.root_dir));
	strcat(g_prf.root_dir, "data/web");

	log_printf(LOG_TEXT, "httpd debug server started on port '%s'", port_str);

	return RET_OK;
}

void webserver_release()
{
	if (g_prf.ctx != NULL)	{
		mg_stop(g_prf.ctx);
		g_prf.ctx = NULL;
		log_print(LOG_TEXT, "httpd debug server closed.");
	}
}

void* webserver_malloc(size_t s)
{
	return ALLOC(s, MID_NET);
}

void webserver_free(void* ptr)
{
	FREE(ptr);
}

void* webserver_realloc(void* ptr, size_t s)
{
	void* tmp = ALLOC(s, MID_NET);
	if (tmp == NULL)
		return NULL;
	memcpy(tmp, ptr, mem_size(ptr));
	FREE(ptr);
	return tmp;
}

void* webserver_request_callback(enum mg_event event, struct mg_connection* conn)
{
	const struct mg_request_info* rinfo = mg_get_request_info(conn);
	if (event == MG_NEW_REQUEST)	{
		/* check ajax json requests
		 * else just send the file to the client
		 *  */
		if (strstr(rinfo->uri, AJAX_HEADER) == rinfo->uri)	{
			char post_data[512];
			char param1[64];
			char param2[64];
			param1[0] = 0;
			param2[0] = 0;

			const char* cmd = rinfo->uri + strlen(AJAX_HEADER);

			/* read POST data for parameters */
			int32 post_datalen = mg_read(conn, post_data, sizeof(post_data));

			mg_get_var(post_data, post_datalen, "p1", param1, sizeof(param1));
			mg_get_var(post_data, post_datalen, "p2", param2, sizeof(param2));

			webserver_runajax(conn, cmd, param1, param2);
		}	else	{
			/* set default 'index.html' for urls with no file references */
			char path[DH_PATH_MAX];

			strcpy(path, g_prf.root_dir);
			strcat(path, rinfo->uri);
			if (util_pathisdir(path))	{
				if (path[strlen(path)-1] != '/')
					strcat(path, "/index.html");
				else
					strcat(path, "index.html");
			}

			/* send back the file to the client */
			mg_send_file(conn, path);
		}
		return "";
	}	else	{
		return NULL;
	}
}

void webserver_runajax(struct mg_connection* conn, const char* cmd, const char* param1,
		const char* param2)
{
	pfn_ajax_cmd cmd_fn = prf_find_cmd(cmd);
	if (cmd_fn != NULL)	{
		json_t j = cmd_fn(param1, param2);
		if (j != JSON_NULL)	{
			size_t json_size;
			char* json_data = json_savetobuffer(j, &json_size, TRUE);
			if (json_data != NULL)	{
				mg_write(conn, json_data, json_size);
				json_deletebuffer(json_data);
			}
			json_destroy(j);
		}	else	{
			mg_send_error(conn, 400, "invalid arguments");
		}
	}	else	{
		mg_send_error(conn, 404, "ajax command not found");
	}
}

void prf_register_cmd(const char* name, pfn_ajax_cmd callback_fn)
{
	struct prf_cmd_desc* c = arr_add(&g_prf.cmds);
	ASSERT(c);
	c->name = name;
	c->hash = hash_murmur32(name, strlen(name), HSEED);
	c->cmd_fn = callback_fn;
}

pfn_ajax_cmd prf_find_cmd(const char* name)
{
	uint32 hash = hash_murmur32(name, strlen(name), HSEED);

	struct prf_cmd_desc* cmds = g_prf.cmds.buffer;
	for (uint32 i = 0, cnt = g_prf.cmds.item_cnt; i < cnt; i++)	{
		if (cmds[i].hash == hash)
			return cmds[i].cmd_fn;
	}
	return NULL;
}

json_t prf_cmd_heapinfo(const char* param1, const char* param2)
{
    PROTECT_CMD();

	json_t root = json_create_obj();
	json_t data = json_create_arr();

    /* data is an array of key,value items */
	json_additem_toobj(root, "data", data);

	struct mem_stats stats;
	mem_getstats(&stats);

	json_additem_toarr(data, prf_cmd_createkeyvalue_n("alloc-cnt", (fl64)stats.alloc_cnt, FALSE));
	json_additem_toarr(data, prf_cmd_createkeyvalue_n("alloc-total", (fl64)stats.alloc_bytes, TRUE)); 
    json_additem_toarr(data, prf_cmd_createkeyvalue_n("max-limit", (fl64)stats.limit_bytes, FALSE));
	json_additem_toarr(data, prf_cmd_createkeyvalue_n("trace-total", (fl64)stats.tracer_alloc_bytes, 
        TRUE));
	return root;
}

json_t prf_cmd_buffersmem(const char* param1, const char* param2)
{
    PROTECT_CMD();

    struct eng_mem_stats stats;
    struct sct_memstats sm_stats;
    struct phx_memstats px_stats;

    eng_get_memstats(&stats);
    sct_getmemstats(&sm_stats);
    phx_getmemstats(&px_stats);

    json_t root = json_create_obj();
    json_t data = json_create_arr();
    
    json_additem_toobj(root, "data", data);

    json_additem_toarr(data, prf_cmd_createkeyvalue_n("LSR-max", (fl64)stats.lsr_max, TRUE));
    json_additem_toarr(data, prf_cmd_createkeyvalue_n("LSR-alloc", (fl64)stats.lsr_size, TRUE));
    json_additem_toarr(data, prf_cmd_createkeyvalue_n("data-max", (fl64)stats.data_max, TRUE));
    json_additem_toarr(data, prf_cmd_createkeyvalue_n("data-alloc", (fl64)stats.data_size, TRUE));
    json_additem_toarr(data, prf_cmd_createkeyvalue_n("tmp0-max", (fl64)stats.tmp0_total, TRUE));
    json_additem_toarr(data, prf_cmd_createkeyvalue_n("tmp0-maxalloc", (fl64)stats.tmp0_max, TRUE));
    json_additem_toarr(data, prf_cmd_createkeyvalue_n("tmp0-maxframeid", 
        (fl64)stats.tmp0_max_frameid, FALSE));
    json_additem_toarr(data, prf_cmd_createkeyvalue_n("script", (fl64)sm_stats.buff_alloc, TRUE));
    json_additem_toarr(data, prf_cmd_createkeyvalue_n("script-max", (fl64)sm_stats.buff_max, TRUE));
    json_additem_toarr(data, prf_cmd_createkeyvalue_n("physics", (fl64)px_stats.buff_alloc, TRUE));
    json_additem_toarr(data, prf_cmd_createkeyvalue_n("physics-max", (fl32)px_stats.buff_max, TRUE));

    return root;
}


json_t prf_cmd_heapsubinfo(const char* param1, const char* param2)
{
    PROTECT_CMD();

    json_t root = json_create_obj();
    json_t data = json_create_arr();

    /* data is an array of key,value items */
    json_additem_toobj(root, "data", data);

	struct mem_stats stats;
	mem_getstats(&stats);

    json_additem_toarr(data, prf_cmd_createkeyvalue_n("total", (fl64)stats.alloc_bytes, TRUE));
    json_additem_toarr(data, prf_cmd_createkeyvalue_n("base", (fl64)mem_sizebyid(MID_BASE), TRUE));
    json_additem_toarr(data, prf_cmd_createkeyvalue_n("graphics", (fl64)mem_sizebyid(MID_GFX), TRUE));
    json_additem_toarr(data, prf_cmd_createkeyvalue_n("res-mgr", (fl64)mem_sizebyid(MID_RES), TRUE));
    json_additem_toarr(data, prf_cmd_createkeyvalue_n("gui", (fl64)mem_sizebyid(MID_GUI), TRUE));
    json_additem_toarr(data, prf_cmd_createkeyvalue_n("scene-mgr", (fl64)mem_sizebyid(MID_SCN), TRUE));
    json_additem_toarr(data, prf_cmd_createkeyvalue_n("network", (fl64)mem_sizebyid(MID_NET), TRUE));
    json_additem_toarr(data, prf_cmd_createkeyvalue_n("components", (fl64)mem_sizebyid(MID_CMP), TRUE));
    json_additem_toarr(data, prf_cmd_createkeyvalue_n("profiler", (fl64)mem_sizebyid(MID_PRF), TRUE));
    json_additem_toarr(data, prf_cmd_createkeyvalue_n("script", (fl64)mem_sizebyid(MID_SCT), TRUE));
    json_additem_toarr(data, prf_cmd_createkeyvalue_n("data", (fl64)mem_sizebyid(MID_DATA), TRUE));
    return root;
}

json_t prf_cmd_gpumem(const char* param1, const char* param2)
{
    PROTECT_CMD();

    json_t root = json_create_obj();
    json_t data = json_create_arr();

    /* data is an array of key,value items */
    json_additem_toobj(root, "data", data);

    const struct gfx_gpu_memstats* stats = gfx_get_memstats();

    json_additem_toarr(data, prf_cmd_createkeyvalue_n("texture-cnt", stats->texture_cnt, FALSE));
    json_additem_toarr(data, prf_cmd_createkeyvalue_n("texture-total", (fl64)stats->textures, TRUE));
    json_additem_toarr(data, prf_cmd_createkeyvalue_n("render-target-cnt", stats->rttexture_cnt, 
    		FALSE));
    json_additem_toarr(data, prf_cmd_createkeyvalue_n("render-target-total", (fl64)stats->rt_textures,
    		TRUE));
    json_additem_toarr(data, prf_cmd_createkeyvalue_n("buffer-cnt", stats->buffer_cnt, FALSE));
    json_additem_toarr(data, prf_cmd_createkeyvalue_n("buffer-total", (fl64)stats->buffers, TRUE));
    return root;
}

json_t prf_cmd_profilergantt(const char* param1, const char* param2)
{
    PROTECT_CMD();

    /* protect front buffer during data creation */
    mt_lockmutex(&g_prf.samples_mtx);
    struct prf_samples* s = g_prf.samples_front;

    json_t root = json_create_obj();
    json_t data = json_create_obj();
    json_additem_toobj(root, "data", data);

    json_t jsamples = json_create_arr();
    json_additem_toobj(data, "duration", json_create_num(s->duration));
    json_additem_toobj(data, "samples", jsamples);
    
    struct linked_list* l = s->nodes;
    while (l != NULL)   {
        prf_create_node_json(jsamples, l->data);
        l = l->next;
    }

    mt_unlockmutex(&g_prf.samples_mtx);
    return root;
}

json_t prf_create_node_json(json_t parent, struct prf_node* node)
{
    PROTECT_CMD();

    json_t jnode = json_create_obj();
    json_additem_toarr(parent, jnode);

    json_additem_toobj(jnode, "name", json_create_str(node->name));
    json_additem_toobj(jnode, "start", json_create_num(node->start_tm));
    json_additem_toobj(jnode, "duration", json_create_num(node->tm));

    /* recurse for children */
    struct linked_list* child = node->childs;
    if (child != NULL)  {
        json_t jchilds = json_create_arr();
        json_additem_toobj(jnode, "childs", jchilds);

        while (child != NULL)   {
            prf_create_node_json(jchilds ,child->data);
            child = child->next;
        }
    }
    
    return jnode;
}

void prf_opensample(const char* name, const char* file, uint32 line)
{
    if (g_prf.samples_back == NULL)
        return;

    struct prf_samples* s = g_prf.samples_back;
    struct prf_node* n = prf_create_node(name, file, line, s->node_cur);

    /* save time */
    n->start_tick = timer_querytick();
    n->start_tm = timer_calctm(eng_get_framestats()->start_tick, n->start_tick)*1000.0;

    s->node_cur = n;
}

void prf_closesample()
{
    if (g_prf.samples_back == NULL)
        return;

    struct prf_samples* s = g_prf.samples_back;
    struct prf_node* n = s->node_cur;
    ASSERT(n != NULL);

    /* calculate time elapsed since start_tick */
    n->tm = timer_calctm(n->start_tick, timer_querytick())*1000.0;

    /* go back to parent */
    s->node_cur = n->parent;
}

struct prf_samples* prf_create_samples()
{
    struct prf_samples* s = ALLOC(sizeof(struct prf_samples), MID_PRF);
    if (s == NULL)
        return NULL;
    memset(s, 0x00, sizeof(struct prf_samples));

    result_t r;

    r = mem_stack_create(mem_heap(), &s->alloc, SAMPLES_BUFFER_SIZE, MID_PRF);
    if (IS_FAIL(r)) {
        prf_destroy_samples(s);
        return NULL;
    }

    return s;    
}

void prf_destroy_samples(struct prf_samples* s)
{
    mem_stack_destroy(&s->alloc);
    FREE(s);
}

void prf_presentsamples(fl64 ft)
{
    if (!g_prf.init)
        return;

    /* save whole frame duration for built sampels */
    struct prf_samples* prev_back = g_prf.samples_back;
    prev_back->duration = ft*1000.0;

    /* block presenting front buffer until we are done with json data creation */
    mt_lockmutex(&g_prf.samples_mtx);
    atomptr_t tmp = g_prf.samples_front;
    ATOMIC_SET_PTR(g_prf.samples_front, g_prf.samples_back);
    g_prf.samples_back = tmp;
    mt_unlockmutex(&g_prf.samples_mtx);

    /* reset back buffer stack allocator */
    struct prf_samples* s = tmp;
    mem_stack_reset(&s->alloc);
    s->node_cur = NULL;
    s->nodes = NULL;
}

json_t prf_cmd_getcaminfo(const char* param1, const char* param2)
{
    PROTECT_CMD();

    struct camera* cam = scn_getcam();

    json_t jroot = json_create_obj();
    json_t jcam = json_create_obj();

    /* data is an array of key,value items */
    json_additem_toobj(jroot, "cam", jcam);
    if (cam == NULL)
        return jroot;
    
    json_additem_toobj(jcam, "x", json_create_num(cam->pos.x));
    json_additem_toobj(jcam, "y", json_create_num(cam->pos.y));
    json_additem_toobj(jcam, "z", json_create_num(cam->pos.z));

    json_additem_toobj(jcam, "qx", json_create_num(cam->rot.x));
    json_additem_toobj(jcam, "qy", json_create_num(cam->rot.y));
    json_additem_toobj(jcam, "qz", json_create_num(cam->rot.z));
    json_additem_toobj(jcam, "qw", json_create_num(cam->rot.w));
    
    return jroot;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.