Kore
Loading...
Searching...
No Matches
system.h
Go to the documentation of this file.
1#pragma once
2
3#include <kinc/global.h>
4
5#include <kinc/log.h>
6
7#include <stdbool.h>
8#include <stddef.h>
9#include <stdint.h>
10
14
15#ifdef __cplusplus
16extern "C" {
17#endif
18
21
28KINC_FUNC int kinc_init(const char *name, int width, int height, struct kinc_window_options *win, struct kinc_framebuffer_options *frame);
29
34KINC_FUNC const char *kinc_application_name(void);
35
40KINC_FUNC void kinc_set_application_name(const char *name);
41
46KINC_FUNC int kinc_width(void);
47
52KINC_FUNC int kinc_height(void);
53
58KINC_FUNC void kinc_load_url(const char *url);
59
64KINC_FUNC const char *kinc_system_id(void);
65
70KINC_FUNC const char *kinc_language(void);
71
75KINC_FUNC void kinc_vibrate(int milliseconds);
76
82KINC_FUNC float kinc_safe_zone(void);
83
88KINC_FUNC bool kinc_automatic_safe_zone(void);
89
94KINC_FUNC void kinc_set_safe_zone(float value);
95
96typedef uint64_t kinc_ticks_t;
97
102KINC_FUNC double kinc_frequency(void);
103
108KINC_FUNC kinc_ticks_t kinc_timestamp(void);
109
114KINC_FUNC int kinc_cpu_cores(void);
115
120KINC_FUNC int kinc_hardware_threads(void);
121
126KINC_FUNC double kinc_time(void);
127
131KINC_FUNC void kinc_start(void);
132
136KINC_FUNC void kinc_stop(void);
137
141KINC_FUNC void kinc_login(void);
142
147KINC_FUNC bool kinc_waiting_for_login(void);
148
153KINC_FUNC void kinc_unlock_achievement(int id);
154
158KINC_FUNC void kinc_disallow_user_change(void);
159
163KINC_FUNC void kinc_allow_user_change(void);
164
169KINC_FUNC void kinc_set_keep_screen_on(bool on);
170
174KINC_INLINE void kinc_debug_break(void) {
175#ifndef NDEBUG
176#if defined(_MSC_VER)
177 __debugbreak();
178#elif defined(__clang__)
179 __builtin_debugtrap();
180#else
181#if defined(__aarch64__)
182 __asm__ volatile(".inst 0xd4200000");
183#elif defined(__x86_64__)
184 __asm__ volatile("int $0x03");
185#else
186 kinc_log(KINC_LOG_LEVEL_WARNING, "Oh no, kinc_debug_break is not implemented for the current compiler and CPU.");
187#endif
188#endif
189#endif
190}
191
196KINC_FUNC bool kinc_debugger_attached(void);
197
202KINC_FUNC void kinc_copy_to_clipboard(const char *text);
203
209KINC_FUNC void kinc_set_update_callback(void (*callback)(void *), void *data);
210
216KINC_FUNC void kinc_set_foreground_callback(void (*callback)(void *), void *data);
217
223KINC_FUNC void kinc_set_resume_callback(void (*callback)(void *), void *data);
224
230KINC_FUNC void kinc_set_pause_callback(void (*callback)(void *), void *data);
231
237KINC_FUNC void kinc_set_background_callback(void (*callback)(void *), void *data);
238
244KINC_FUNC void kinc_set_shutdown_callback(void (*callback)(void *), void *data);
245
251KINC_FUNC void kinc_set_drop_files_callback(void (*callback)(wchar_t *, void *), void *data);
252
259KINC_FUNC void kinc_set_cut_callback(char *(*callback)(void *), void *data);
260
267KINC_FUNC void kinc_set_copy_callback(char *(*callback)(void *), void *data);
268
275KINC_FUNC void kinc_set_paste_callback(void (*callback)(char *, void *), void *data);
276
282KINC_FUNC void kinc_set_login_callback(void (*callback)(void *), void *data);
283
289KINC_FUNC void kinc_set_logout_callback(void (*callback)(void *), void *data);
290
291#ifdef KINC_VTUNE
292#include <ittnotify.h>
293
294extern __itt_domain *kinc_itt_domain;
295#endif
296
297#ifdef KINC_SUPERLUMINAL
298#include <Superluminal/PerformanceAPI_capi.h>
299#endif
300
306static inline void kinc_marker_start(const char *name, uint32_t color) {
307#ifdef KINC_VTUNE
308 __itt_task_begin(kinc_itt_domain, __itt_null, __itt_null, __itt_string_handle_create(name));
309#endif
310
311#ifdef KINC_SUPERLUMINAL
312 PerformanceAPI_BeginEvent(name, NULL, color);
313#endif
314}
315
319static inline void kinc_marker_end(const char *name) {
320#ifdef KINC_VTUNE
321 __itt_task_end(kinc_itt_domain);
322#endif
323
324#ifdef KINC_SUPERLUMINAL
325 PerformanceAPI_EndEvent();
326#endif
327}
328
339KINC_FUNC void kinc_LivePP_start(void);
340
346KINC_FUNC void kinc_LivePP_stop(void);
347
348bool kinc_internal_frame(void);
349const char *kinc_internal_save_path(void);
350bool kinc_internal_handle_messages(void);
351void kinc_internal_shutdown(void);
352void kinc_internal_update_callback(void);
353void kinc_internal_foreground_callback(void);
354void kinc_internal_resume_callback(void);
355void kinc_internal_pause_callback(void);
356void kinc_internal_background_callback(void);
357void kinc_internal_shutdown_callback(void);
358void kinc_internal_drop_files_callback(wchar_t *);
359char *kinc_internal_cut_callback(void);
360char *kinc_internal_copy_callback(void);
361void kinc_internal_paste_callback(char *);
362void kinc_internal_login_callback(void);
363void kinc_internal_logout_callback(void);
364
365#ifdef KINC_IMPLEMENTATION_ROOT
366#define KINC_IMPLEMENTATION
367#endif
368
369#ifdef KINC_IMPLEMENTATION
370
371#ifdef KINC_IMPLEMENTATION_ROOT
372#undef KINC_IMPLEMENTATION
373#endif
374#include <kinc/window.h>
375#ifdef KINC_IMPLEMENTATION_ROOT
376#define KINC_IMPLEMENTATION
377#endif
378
379#undef KINC_IMPLEMENTATION
380#include <kinc/io/filereader.h>
381#include <kinc/io/filewriter.h>
382#define KINC_IMPLEMENTATION
383
384#include <stdlib.h>
385#include <string.h>
386
387#if !defined(KINC_WASM) && !defined(KINC_EMSCRIPTEN) && !defined(KINC_ANDROID) && !defined(KINC_WINDOWS) && !defined(KINC_CONSOLE)
388double kinc_time(void) {
389 return kinc_timestamp() / kinc_frequency();
390}
391#endif
392
393static void (*update_callback)(void *) = NULL;
394static void *update_callback_data = NULL;
395static void (*foreground_callback)(void *) = NULL;
396static void *foreground_callback_data = NULL;
397static void (*background_callback)(void *) = NULL;
398static void *background_callback_data = NULL;
399static void (*pause_callback)(void *) = NULL;
400static void *pause_callback_data = NULL;
401static void (*resume_callback)(void *) = NULL;
402static void *resume_callback_data = NULL;
403static void (*shutdown_callback)(void *) = NULL;
404static void *shutdown_callback_data = NULL;
405static void (*drop_files_callback)(wchar_t *, void *) = NULL;
406static void *drop_files_callback_data = NULL;
407static char *(*cut_callback)(void *) = NULL;
408static void *cut_callback_data = NULL;
409static char *(*copy_callback)(void *) = NULL;
410static void *copy_callback_data = NULL;
411static void (*paste_callback)(char *, void *) = NULL;
412static void *paste_callback_data = NULL;
413static void (*login_callback)(void *) = NULL;
414static void *login_callback_data = NULL;
415static void (*logout_callback)(void *) = NULL;
416static void *logout_callback_data = NULL;
417
418#if defined(KINC_IOS) || defined(KINC_MACOS)
419bool withAutoreleasepool(bool (*f)(void));
420#endif
421
422void kinc_set_update_callback(void (*callback)(void *), void *data) {
423 update_callback = callback;
424 update_callback_data = data;
425}
426
427void kinc_set_foreground_callback(void (*callback)(void *), void *data) {
428 foreground_callback = callback;
429 foreground_callback_data = data;
430}
431
432void kinc_set_resume_callback(void (*callback)(void *), void *data) {
433 resume_callback = callback;
434 resume_callback_data = data;
435}
436
437void kinc_set_pause_callback(void (*callback)(void *), void *data) {
438 pause_callback = callback;
439 pause_callback_data = data;
440}
441
442void kinc_set_background_callback(void (*callback)(void *), void *data) {
443 background_callback = callback;
444 background_callback_data = data;
445}
446
447void kinc_set_shutdown_callback(void (*callback)(void *), void *data) {
448 shutdown_callback = callback;
449 shutdown_callback_data = data;
450}
451
452void kinc_set_drop_files_callback(void (*callback)(wchar_t *, void *), void *data) {
453 drop_files_callback = callback;
454 drop_files_callback_data = data;
455}
456
457void kinc_set_cut_callback(char *(*callback)(void *), void *data) {
458 cut_callback = callback;
459 cut_callback_data = data;
460}
461
462void kinc_set_copy_callback(char *(*callback)(void *), void *data) {
463 copy_callback = callback;
464 copy_callback_data = data;
465}
466
467void kinc_set_paste_callback(void (*callback)(char *, void *), void *data) {
468 paste_callback = callback;
469 paste_callback_data = data;
470}
471
472void kinc_set_login_callback(void (*callback)(void *), void *data) {
473 login_callback = callback;
474 login_callback_data = data;
475}
476
477void kinc_set_logout_callback(void (*callback)(void *), void *data) {
478 logout_callback = callback;
479 logout_callback_data = data;
480}
481
482void kinc_internal_update_callback(void) {
483 if (update_callback != NULL) {
484 update_callback(update_callback_data);
485 }
486}
487
488void kinc_internal_foreground_callback(void) {
489 if (foreground_callback != NULL) {
490 foreground_callback(foreground_callback_data);
491 }
492}
493
494void kinc_internal_resume_callback(void) {
495 if (resume_callback != NULL) {
496 resume_callback(resume_callback_data);
497 }
498}
499
500void kinc_internal_pause_callback(void) {
501 if (pause_callback != NULL) {
502 pause_callback(pause_callback_data);
503 }
504}
505
506void kinc_internal_background_callback(void) {
507 if (background_callback != NULL) {
508 background_callback(background_callback_data);
509 }
510}
511
512void kinc_internal_shutdown_callback(void) {
513 if (shutdown_callback != NULL) {
514 shutdown_callback(shutdown_callback_data);
515 }
516}
517
518void kinc_internal_drop_files_callback(wchar_t *filePath) {
519 if (drop_files_callback != NULL) {
520 drop_files_callback(filePath, drop_files_callback_data);
521 }
522}
523
524char *kinc_internal_cut_callback(void) {
525 if (cut_callback != NULL) {
526 return cut_callback(cut_callback_data);
527 }
528 return NULL;
529}
530
531char *kinc_internal_copy_callback(void) {
532 if (copy_callback != NULL) {
533 return copy_callback(copy_callback_data);
534 }
535 return NULL;
536}
537
538void kinc_internal_paste_callback(char *value) {
539 if (paste_callback != NULL) {
540 paste_callback(value, paste_callback_data);
541 }
542}
543
544void kinc_internal_login_callback(void) {
545 if (login_callback != NULL) {
546 login_callback(login_callback_data);
547 }
548}
549
550void kinc_internal_logout_callback(void) {
551 if (logout_callback != NULL) {
552 logout_callback(logout_callback_data);
553 }
554}
555
556static bool running = false;
557// static bool showWindowFlag = true;
558static char application_name[1024] = {"Kinc Application"};
559
560const char *kinc_application_name(void) {
561 return application_name;
562}
563
564void kinc_set_application_name(const char *name) {
565 strcpy(application_name, name);
566}
567
568void kinc_stop(void) {
569 running = false;
570
571 // TODO (DK) destroy graphics + windows, but afaik Application::~Application() was never called, so it's the same behavior now as well
572
573 // for (int windowIndex = 0; windowIndex < sizeof(windowIds) / sizeof(int); ++windowIndex) {
574 // Graphics::destroy(windowIndex);
575 //}
576}
577
578bool kinc_internal_frame(void) {
579 kinc_internal_update_callback();
580 kinc_internal_handle_messages();
581 return running;
582}
583
584void kinc_start(void) {
585 running = true;
586
587#if !defined(KINC_WASM) && !defined(KINC_EMSCRIPTEN)
588 // if (Graphics::hasWindow()) Graphics::swapBuffers();
589
590#if defined(KINC_IOS) || defined(KINC_MACOS)
591 while (withAutoreleasepool(kinc_internal_frame)) {
592 }
593#else
594 while (kinc_internal_frame()) {
595 }
596#endif
597 kinc_internal_shutdown();
598#endif
599}
600
601int kinc_width(void) {
602 return kinc_window_width(0);
603}
604
605int kinc_height(void) {
606 return kinc_window_height(0);
607}
608
609#ifndef KHA
610void kinc_memory_emergency(void) {}
611#endif
612
613#if !defined(KINC_SONY) && !defined(KINC_NINTENDO)
614static float safe_zone = 0.9f;
615
616float kinc_safe_zone(void) {
617#ifdef KINC_ANDROID
618 return 1.0f;
619#else
620 return safe_zone;
621#endif
622}
623
624bool kinc_automatic_safe_zone(void) {
625#ifdef KINC_ANDROID
626 return true;
627#else
628 return false;
629#endif
630}
631
632void kinc_set_safe_zone(float value) {
633 safe_zone = value;
634}
635#endif
636
637#if !defined(KINC_SONY)
638bool is_save_load_initialized(void) {
639 return true;
640}
641
642bool is_ps4_japanese_button_style(void) {
643 return false;
644}
645
646bool is_save_load_broken(void) {
647 return false;
648}
649#endif
650
651#if !defined(KINC_CONSOLE)
652
653#define SAVE_RESULT_NONE 0
654#define SAVE_RESULT_SUCCESS 1
655#define SAVE_RESULT_FAILURE 2
656volatile int save_result = SAVE_RESULT_SUCCESS;
657
658void kinc_disallow_user_change(void) {}
659
660void kinc_allow_user_change(void) {}
661
662static uint8_t *current_file = NULL;
663static size_t current_file_size = 0;
664
665bool kinc_save_file_loaded(void) {
666 return true;
667}
668
669uint8_t *kinc_get_save_file(void) {
670 return current_file;
671}
672
673size_t kinc_get_save_file_size(void) {
674 return current_file_size;
675}
676
677void kinc_load_save_file(const char *filename) {
678 free(current_file);
679 current_file = NULL;
680 current_file_size = 0;
681
682 kinc_file_reader_t reader;
683 if (kinc_file_reader_open(&reader, filename, KINC_FILE_TYPE_SAVE)) {
684 current_file_size = kinc_file_reader_size(&reader);
685 current_file = (uint8_t *)malloc(current_file_size);
686 kinc_file_reader_read(&reader, current_file, current_file_size);
687 kinc_file_reader_close(&reader);
688 }
689}
690
691void kinc_save_save_file(const char *filename, uint8_t *data, size_t size) {
692 kinc_file_writer_t writer;
693 if (kinc_file_writer_open(&writer, filename)) {
694 kinc_file_writer_write(&writer, data, (int)size);
695 kinc_file_writer_close(&writer);
696 }
697}
698
699bool kinc_save_is_saving(void) {
700 return false;
701}
702
703bool kinc_waiting_for_login(void) {
704 return false;
705}
706
707#if !defined(KINC_WINDOWS) && !defined(KINC_LINUX) && !defined(KINC_MACOS)
708void kinc_copy_to_clipboard(const char *text) {
709 kinc_log(KINC_LOG_LEVEL_WARNING, "Oh no, kinc_copy_to_clipboard is not implemented for this system.");
710}
711#endif
712
713#endif
714
715#endif
716
717#ifdef __cplusplus
718}
719#endif
Provides an API very similar to fread and friends but handles the intricacies of where files are actu...
KINC_FUNC bool kinc_file_reader_open(kinc_file_reader_t *reader, const char *filepath, int type)
Opens a file for reading.
KINC_FUNC size_t kinc_file_reader_size(kinc_file_reader_t *reader)
Figures out the size of a file.
KINC_FUNC bool kinc_file_reader_close(kinc_file_reader_t *reader)
Closes a file.
KINC_FUNC size_t kinc_file_reader_read(kinc_file_reader_t *reader, void *data, size_t size)
Reads data from a file starting from the current reading-position and increases the reading-position ...
Provides an API very similar to fwrite and friends but uses a directory that can actually used for pe...
KINC_FUNC bool kinc_file_writer_open(kinc_file_writer_t *writer, const char *filepath)
Opens a file for writing.
KINC_FUNC void kinc_file_writer_write(kinc_file_writer_t *writer, void *data, int size)
Writes data to a file starting from the current writing-position and increases the writing-position a...
KINC_FUNC void kinc_file_writer_close(kinc_file_writer_t *writer)
Closes a file.
Provides basic functionality that's used all over the place. There's usually no need to manually incl...
Contains basic logging functionality.
KINC_FUNC void kinc_log(kinc_log_level_t log_level, const char *format,...)
Logging function similar to printf including some system-specific bonuses.
Definition window.h:15
Definition window.h:36
KINC_FUNC void kinc_set_cut_callback(char *(*callback)(void *), void *data)
Sets a callback which is called when the application is instructed to cut, typically via ctrl+x or cm...
KINC_FUNC bool kinc_waiting_for_login(void)
Returns true if kinc_login was called and the login-process is still ongoing.
KINC_FUNC void kinc_vibrate(int milliseconds)
Vibrates the whole system if supported. This is primarily supported on mobile phones but don't blame ...
KINC_FUNC bool kinc_debugger_attached(void)
Returns whether a debugger is currently attached to the running program. This is not yet working thou...
static void kinc_marker_start(const char *name, uint32_t color)
Starts a marker for profiling. Please match it with a marker_end-call in the same function.
Definition system.h:306
KINC_FUNC void kinc_set_login_callback(void(*callback)(void *), void *data)
Sets a callback which is called when a user logs in.
KINC_FUNC int kinc_height(void)
Returns the current height of the initial application-window which is equivalent to calling kinc_wind...
KINC_FUNC const char * kinc_system_id(void)
Returns an ID representing the current type of target-system.
KINC_FUNC const char * kinc_application_name(void)
Returns the current application name as set by kinc_init or kinc_set_application_name.
KINC_FUNC bool kinc_automatic_safe_zone(void)
Returns whether the system itself handles configuration of the safe-zone.
KINC_FUNC void kinc_set_safe_zone(float value)
Sets the safe-zone for systems which return false for kinc_automatic_safe_zone.
KINC_FUNC void kinc_set_foreground_callback(void(*callback)(void *), void *data)
Sets a callback which is called whenever the application is brought to the foreground.
KINC_FUNC const char * kinc_language(void)
Returns the current system-language.
KINC_FUNC void kinc_set_shutdown_callback(void(*callback)(void *), void *data)
Sets a callback which is called whenever the application is about to shut down.
KINC_FUNC void kinc_allow_user_change(void)
Allows the system to logout the current user.
KINC_FUNC void kinc_unlock_achievement(int id)
Unlocks an achievement or trophy or however you prefer to call it.
KINC_FUNC int kinc_init(const char *name, int width, int height, struct kinc_window_options *win, struct kinc_framebuffer_options *frame)
Initializes a Kinc application and creates an initial window for systems which support windows (syste...
KINC_FUNC kinc_ticks_t kinc_timestamp(void)
Returns a timestamp for right now in a system-specific unit.
KINC_FUNC void kinc_set_update_callback(void(*callback)(void *), void *data)
Sets the update-callback which drives the application and is called for every frame.
KINC_FUNC void kinc_set_keep_screen_on(bool on)
Instructs the system whether it is allowed to turn off the screen while the application is running.
KINC_FUNC void kinc_set_drop_files_callback(void(*callback)(wchar_t *, void *), void *data)
Sets a callback which is called when files are dropped on the application-window.
KINC_FUNC void kinc_set_paste_callback(void(*callback)(char *, void *), void *data)
Sets a callback which is called when the application is instructed to paste, typically via ctrl+v or ...
KINC_FUNC double kinc_time(void)
Returns the current time. This can also be calculated ala kinc_timestamp() / kinc_frequency() but kin...
KINC_FUNC void kinc_set_application_name(const char *name)
Changes the application-name that was initially set by kinc_init.
KINC_FUNC void kinc_LivePP_start(void)
Currently only supported on Windows. Starts up Live++ (see https://liveplusplus.tech),...
KINC_FUNC void kinc_set_pause_callback(void(*callback)(void *), void *data)
Sets a callback which is called whenever the application is paused.
KINC_FUNC void kinc_set_logout_callback(void(*callback)(void *), void *data)
Sets a callback which is called when a user logs out.
KINC_INLINE void kinc_debug_break(void)
Tries to halt program-execution in an attached debugger when compiled in debug-mode (aka when NDEBUG ...
Definition system.h:174
KINC_FUNC int kinc_cpu_cores(void)
Returns the number of proper CPU-cores (not the number of hardware-threads).
static void kinc_marker_end(const char *name)
Ends the previously started marker of the same function.
Definition system.h:319
KINC_FUNC int kinc_width(void)
Returns the current width of the initial application-window which is equivalent to calling kinc_windo...
KINC_FUNC void kinc_start(void)
Starts Kinc's main-loop. kinc_set_update_callback should be called before kinc_start so the main-loop...
KINC_FUNC void kinc_stop(void)
Stops Kinc's main loop and thereby returns to the function which called kinc_start.
KINC_FUNC void kinc_copy_to_clipboard(const char *text)
Copies the provided string to the system's clipboard.
KINC_FUNC int kinc_hardware_threads(void)
Returns the number of hardware-threads.
KINC_FUNC void kinc_set_resume_callback(void(*callback)(void *), void *data)
Sets a callback which is called whenever the application was paused and is being resumed.
KINC_FUNC void kinc_set_background_callback(void(*callback)(void *), void *data)
Sets a callback which is called whenever the application is brought to the background.
KINC_FUNC void kinc_LivePP_stop(void)
Stops Live++, typically called right after kinc_start().
KINC_FUNC double kinc_frequency(void)
Returns the frequency of system-timestamps.
KINC_FUNC void kinc_disallow_user_change(void)
Disallows the system to logout the current user.
KINC_FUNC float kinc_safe_zone(void)
Returns the portion of the screen which can be safely used for important content. This is mostly rele...
KINC_FUNC void kinc_load_url(const char *url)
Instruct the system to load up the provided URL which will usually open it in the system's default br...
KINC_FUNC void kinc_set_copy_callback(char *(*callback)(void *), void *data)
Sets a callback which is called when the application is instructed to copy, typically via ctrl+c or c...
KINC_FUNC void kinc_login(void)
Instructs the system to login a user if that is supported.
Provides functionality for creating and handling windows.
KINC_FUNC int kinc_window_width(int window)
Returns the width of a window.
KINC_FUNC int kinc_window_height(int window)
Returns the height of a window.