2020-08-09 15:03:02 -04:00
|
|
|
#include "pebbleobject.h"
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-08-09 15:03:02 -04:00
|
|
|
#include "calc.h"
|
|
|
|
#include "object.h"
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object pebbleOther(enum PebbleType type, void* data, void (* cleanup)(Object*),
|
|
|
|
Object (* clone)(struct Other*))
|
2020-08-09 15:03:02 -04:00
|
|
|
{
|
|
|
|
struct Object o = otherObject();
|
2022-01-07 16:55:03 -05:00
|
|
|
struct PebbleObject* po = malloc(sizeof(struct PebbleObject));
|
2020-08-09 15:03:02 -04:00
|
|
|
o.other->data = po;
|
|
|
|
o.other->cleanup = cleanup;
|
|
|
|
o.other->clone = clone;
|
|
|
|
po->type = type;
|
|
|
|
po->ptr = data;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum PebbleType getPebbleType(const Object o1)
|
|
|
|
{
|
2022-01-07 16:55:03 -05:00
|
|
|
if (o1.type == TYPE_OTHER) {
|
|
|
|
struct PebbleObject* po = o1.other->data;
|
2020-08-09 15:03:02 -04:00
|
|
|
enum PebbleType t = po->type;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
return P_ERROR;
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
struct PebbleObject* accessPebbleObject(Object obj)
|
|
|
|
{
|
|
|
|
if (getPebbleType(obj) != P_ERROR) {
|
2020-08-09 15:03:02 -04:00
|
|
|
return obj.other->data;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
void custom_window_load(Window* window)
|
|
|
|
{
|
2020-08-09 15:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
void custom_window_unload(Window* window)
|
|
|
|
{
|
2020-08-09 15:03:02 -04:00
|
|
|
window_destroy(window);
|
|
|
|
}
|
|
|
|
|
2022-04-02 06:51:06 -04:00
|
|
|
Object createWindow(Object* params, int length, struct Environment* env)
|
2022-01-07 16:55:03 -05:00
|
|
|
{
|
|
|
|
Window* window = window_create();
|
|
|
|
WindowHandlers wh = {.load = custom_window_load,
|
|
|
|
.unload = custom_window_unload};
|
2020-08-09 15:03:02 -04:00
|
|
|
window_set_window_handlers(window, wh);
|
|
|
|
return pebbleOther(WINDOW, window, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2022-04-02 06:51:06 -04:00
|
|
|
Object deleteWindow(Object* params, int length, struct Environment* env)
|
2022-01-07 16:55:03 -05:00
|
|
|
{
|
2022-04-02 06:51:06 -04:00
|
|
|
Object window = params[0];
|
2020-08-09 15:03:02 -04:00
|
|
|
/* Maintain a list of layers to delete? */
|
2022-01-07 16:55:03 -05:00
|
|
|
if (getPebbleType(window) == WINDOW) {
|
2020-08-09 15:03:02 -04:00
|
|
|
window_stack_remove(accessPebbleObject(window)->window, true);
|
2022-03-31 10:29:06 -04:00
|
|
|
return trueObject();
|
2020-08-09 15:03:02 -04:00
|
|
|
}
|
2022-03-31 10:29:06 -04:00
|
|
|
return falseObject();
|
2020-08-09 15:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-04-02 06:51:06 -04:00
|
|
|
Object pushWindow(Object* params, int length, struct Environment* env)
|
2022-01-07 16:55:03 -05:00
|
|
|
{
|
2022-04-02 06:51:06 -04:00
|
|
|
Object window = params[0];
|
2022-01-07 16:55:03 -05:00
|
|
|
if (getPebbleType(window) == WINDOW) {
|
2020-08-09 15:03:02 -04:00
|
|
|
window_stack_push(accessPebbleObject(window)->window, true);
|
2022-03-31 10:29:06 -04:00
|
|
|
return trueObject();
|
2020-08-09 15:03:02 -04:00
|
|
|
}
|
2022-03-31 10:29:06 -04:00
|
|
|
return falseObject();
|
2020-08-09 15:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-04-02 06:51:06 -04:00
|
|
|
Object updateTextLayer(Object* params, int length, struct Environment* env)
|
2022-01-07 16:55:03 -05:00
|
|
|
{
|
2022-04-02 06:51:06 -04:00
|
|
|
Object textLayer = params[0];
|
|
|
|
Object text = params[1];
|
2022-01-07 16:55:03 -05:00
|
|
|
if (getPebbleType(textLayer) == TEXT_LAYER) {
|
|
|
|
struct PebbleObject* po = accessPebbleObject(textLayer);
|
2022-04-02 06:51:06 -04:00
|
|
|
size_t l;
|
|
|
|
char* string = stringObj(&text, &l);
|
|
|
|
snprintf(po->textLayer->text, 999, "%s", string);
|
|
|
|
free(string);
|
2020-08-09 15:03:02 -04:00
|
|
|
text_layer_set_text(po->textLayer->layer, po->textLayer->text);
|
2022-03-31 10:29:06 -04:00
|
|
|
return trueObject();
|
2020-08-09 15:03:02 -04:00
|
|
|
}
|
2022-03-31 10:29:06 -04:00
|
|
|
return falseObject();
|
2020-08-09 15:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-04-02 06:51:06 -04:00
|
|
|
Object addTextLayer(Object* params, int length, struct Environment* env)
|
2022-01-07 16:55:03 -05:00
|
|
|
{
|
2022-04-02 06:51:06 -04:00
|
|
|
Object window = params[0];
|
|
|
|
Object text = params[1];
|
2022-01-07 16:55:03 -05:00
|
|
|
if (getPebbleType(window) != WINDOW) {
|
2020-08-09 15:03:02 -04:00
|
|
|
return errorObject(0);
|
|
|
|
}
|
2022-01-07 16:55:03 -05:00
|
|
|
Layer* window_layer =
|
|
|
|
window_get_root_layer(accessPebbleObject(window)->window);
|
2020-08-09 15:03:02 -04:00
|
|
|
GRect bounds = layer_get_bounds(window_layer);
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
struct PLTextLayer* textLayer = malloc(sizeof(struct PLTextLayer));
|
2020-08-09 15:03:02 -04:00
|
|
|
textLayer->text = calloc(sizeof(char), 100);
|
|
|
|
|
|
|
|
textLayer->layer = text_layer_create(bounds);
|
2022-04-02 06:51:06 -04:00
|
|
|
size_t l;
|
|
|
|
char* string = stringObj(&text, &l);
|
|
|
|
snprintf(textLayer->text, 999, "%s", string);
|
|
|
|
free(string);
|
2020-08-09 15:03:02 -04:00
|
|
|
text_layer_set_text(textLayer->layer, textLayer->text);
|
|
|
|
text_layer_set_font(textLayer->layer,
|
2022-01-07 16:55:03 -05:00
|
|
|
fonts_get_system_font(FONT_KEY_BITHAM_42_BOLD));
|
2020-08-09 15:03:02 -04:00
|
|
|
layer_add_child(window_layer, text_layer_get_layer(textLayer->layer));
|
|
|
|
|
|
|
|
return pebbleOther(TEXT_LAYER, textLayer, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
Object subscription;
|
2022-01-07 16:55:03 -05:00
|
|
|
struct Environment* subscriptionEnv;
|
2020-08-09 15:03:02 -04:00
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
static void subscriptionHandler(struct tm* tick_time, TimeUnits changed)
|
|
|
|
{
|
2020-08-09 15:03:02 -04:00
|
|
|
eval(&subscription, subscriptionEnv);
|
|
|
|
}
|
|
|
|
|
2022-04-03 19:17:02 -04:00
|
|
|
int getUnit(Object time)
|
|
|
|
{
|
|
|
|
if (time.type != TYPE_NUMBER) {
|
|
|
|
return MINUTE_UNIT;
|
|
|
|
}
|
|
|
|
switch (time.number) {
|
|
|
|
case 1:
|
|
|
|
return SECOND_UNIT;
|
|
|
|
case 2:
|
|
|
|
return MINUTE_UNIT;
|
|
|
|
case 3:
|
|
|
|
return HOUR_UNIT;
|
|
|
|
case 4:
|
|
|
|
return DAY_UNIT;
|
|
|
|
case 5:
|
|
|
|
return MONTH_UNIT;
|
|
|
|
case 6:
|
|
|
|
return YEAR_UNIT;
|
|
|
|
default:
|
|
|
|
return MINUTE_UNIT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 06:51:06 -04:00
|
|
|
Object subscribe(Object* params, int length, struct Environment* env)
|
2022-01-07 16:55:03 -05:00
|
|
|
{
|
2022-04-03 19:17:02 -04:00
|
|
|
Object subscription = cloneObject(params[1]);
|
|
|
|
if (subscription.type == TYPE_LAMBDA) {
|
|
|
|
int unit = getUnit(params[0]);
|
2020-08-09 15:03:02 -04:00
|
|
|
subscriptionEnv = env;
|
|
|
|
tick_timer_service_subscribe(unit, subscriptionHandler);
|
2022-03-31 10:29:06 -04:00
|
|
|
return trueObject();
|
2022-04-03 19:17:02 -04:00
|
|
|
} else {
|
|
|
|
printf("Is not lambda, is %d\n", subscription.type);
|
2020-08-09 15:03:02 -04:00
|
|
|
}
|
2022-03-31 10:29:06 -04:00
|
|
|
return falseObject();
|
2020-08-09 15:03:02 -04:00
|
|
|
}
|