Skip to content

Commit

Permalink
Housekeeping.
Browse files Browse the repository at this point in the history
  • Loading branch information
gabordemooij committed Mar 31, 2024
1 parent 32c6b1c commit 52e6687
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
1 change: 0 additions & 1 deletion citrine.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,6 @@ extern ctr_object* ctr_internal_object_find_property(ctr_object* owner, ctr_obje
extern ctr_object* ctr_internal_object_property(ctr_object* owner, char* keystr, ctr_object* value);
extern double ctr_tonum(ctr_object* o);
extern char ctr_tobool(ctr_object* o);
extern char* ctr_tostr(ctr_object* o);
extern uint64_t ctr_internal_index_hash(ctr_object* key);
extern void ctr_internal_object_add_property(ctr_object* owner, ctr_object* key, ctr_object* value, int m);
extern ctr_object* ctr_internal_cast2bool( ctr_object* o );
Expand Down
28 changes: 19 additions & 9 deletions plugins/media/media.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
#include <unistd.h>



/* Old Windows versions lack these functions
*
* credit: Petar Korponaić
Expand Down Expand Up @@ -3725,12 +3724,15 @@ ctr_object* ctr_media_website(ctr_object* myself, ctr_argument* argumentList) {
default_url_opener = "xdg-open"; //Linuxy
tpl = NULL;
#endif
return ctr_internal_media_external_command(
char* str = ctr_heap_allocate_cstring(ctr_internal_cast2string(argumentList->object));
ctr_object* result = ctr_internal_media_external_command(
getenv("BROWSER"),
default_url_opener,
ctr_tostr(argumentList->object),
str,
tpl
);
ctr_heap_free(str);
return result;
}

/**
Expand All @@ -3744,12 +3746,15 @@ ctr_object* ctr_media_website(ctr_object* myself, ctr_argument* argumentList) {
* en: Speaks text using speech synthesizer system specified in SPEAK environment variable.
*/
ctr_object* ctr_media_speak(ctr_object* myself, ctr_argument* argumentList) {
return ctr_internal_media_external_command(
char* str = ctr_heap_allocate_cstring(ctr_internal_cast2string(argumentList->object));
ctr_object* result = ctr_internal_media_external_command(
getenv("SPEAK"),
"say",
ctr_tostr(argumentList->object),
str,
NULL
);
ctr_heap_free(str);
return result;
}

/**
Expand All @@ -3762,6 +3767,7 @@ ctr_object* ctr_media_speak(ctr_object* myself, ctr_argument* argumentList) {
*/
ctr_object* ctr_media_system(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* result;
char* command_str = ctr_heap_allocate_cstring(ctr_internal_cast2string(argumentList->object));
#ifndef WIN
//In Linux/BSD we cannot attach a terminal to a process afterwards so
//we always open a terminal. If users don't want a terminal they have to start
Expand All @@ -3773,15 +3779,15 @@ ctr_object* ctr_media_system(ctr_object* myself, ctr_argument* argumentList) {
result = ctr_internal_media_external_command(
getenv("TERMINAL"),
"/usr/bin/x-terminal-emulator -e",
ctr_tostr(ctr_internal_copy2string(argumentList->object)),
command_str,
NULL
);
//If it fails, start without terminal
if (result == CtrStdBoolFalse) {
result = ctr_internal_media_external_command(
NULL,
"",
ctr_tostr(ctr_internal_copy2string(argumentList->object)),
command_str,
"%s %s"
);
}
Expand All @@ -3795,12 +3801,13 @@ ctr_object* ctr_media_system(ctr_object* myself, ctr_argument* argumentList) {
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
if( !CreateProcess(NULL,ctr_tostr(ctr_internal_copy2string(argumentList->object)),NULL,NULL,FALSE,0,NULL,NULL,&si,&pi)) return CtrStdBoolFalse;
if( !CreateProcess(NULL,command_str,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi)) return CtrStdBoolFalse;
WaitForSingleObject( pi.hProcess, INFINITE );
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
result = CtrStdBoolTrue;
#endif
ctr_heap_free(command_str);
return result;
}

Expand Down Expand Up @@ -3834,7 +3841,10 @@ ctr_object* ctr_media_console_brk(ctr_object* myself, ctr_argument* argumentList
#endif

ctr_object* ctr_media_include(ctr_object* myself, ctr_argument* argumentList) {
char* pathString = ctr_tostr(argumentList->object);
ctr_object* pathStrObj = ctr_internal_cast2string(argumentList->object);
char* pathString = ctr_heap_allocate_tracked(pathStrObj->value.svalue->vlen + 1);
strncpy(pathString, pathStrObj->value.svalue->value, pathStrObj->value.svalue->vlen);
pathString[pathStrObj->value.svalue->vlen] = '\0';
SDL_RWops* asset_reader = ctr_internal_media_load_asset(pathString, 1);
if (!asset_reader) {
ctr_error("Unable to open code asset.", 0);
Expand Down
15 changes: 0 additions & 15 deletions world.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,21 +326,6 @@ char ctr_tobool(ctr_object* o) {
return (ctr_internal_cast2bool(o)->value.bvalue) ? 1 : 0;
}

/**
* ?internal
*
* toStr()
* Convenience function.
* Casts an object to a string object and extracts the string
* value from the object as a pointer to a char array.
* Note that the pointer points to the memory of the object and
* should not be freed.
*/
char* ctr_tostr(ctr_object* o) {
return ctr_internal_cast2string(o)->value.svalue->value;
}


/**
* ?internal
*
Expand Down

0 comments on commit 52e6687

Please sign in to comment.