#include "pebbleobject.h" #include "calc.h" #include "object.h" Object pebbleOther( enum PebbleType type, void* data, void (*cleanup)(Object*), Object (*clone)(struct Other*)) { struct Object o = otherObject(); struct PebbleObject *po = malloc(sizeof(struct PebbleObject)); 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) { if(o1.type == TYPE_OTHER) { struct PebbleObject *po = o1.other->data; enum PebbleType t = po->type; return t; } return P_ERROR; } struct PebbleObject* accessPebbleObject(Object obj) { if(getPebbleType(obj) != P_ERROR) { return obj.other->data; } return NULL; } void custom_window_load(Window *window) { } void custom_window_unload(Window *window) { window_destroy(window); } Object createWindow(Object o1, Object o2, struct Environment *env) { Window *window = window_create(); WindowHandlers wh = { .load = custom_window_load, .unload = custom_window_unload }; window_set_window_handlers(window, wh); return pebbleOther(WINDOW, window, NULL, NULL); } Object deleteWindow(Object window, Object o2, struct Environment *env) { /* Maintain a list of layers to delete? */ if(getPebbleType(window) == WINDOW) { window_stack_remove(accessPebbleObject(window)->window, true); return boolObject(1); } return boolObject(0); } Object pushWindow(Object window, Object o2, struct Environment *env) { if(getPebbleType(window) == WINDOW) { window_stack_push(accessPebbleObject(window)->window, true); return boolObject(1); } return boolObject(0); } Object updateTextLayer(Object textLayer, Object text, struct Environment *env) { if(getPebbleType(textLayer) == TEXT_LAYER) { struct PebbleObject *po = accessPebbleObject(textLayer); stringObj(po->textLayer->text, &text); text_layer_set_text(po->textLayer->layer, po->textLayer->text); return boolObject(1); } return boolObject(0); } Object addTextLayer(Object window, Object text, struct Environment *env) { if(getPebbleType(window) != WINDOW) { return errorObject(0); } Layer *window_layer = window_get_root_layer(accessPebbleObject(window)->window); GRect bounds = layer_get_bounds(window_layer); struct PLTextLayer *textLayer = malloc(sizeof(struct PLTextLayer)); textLayer->text = calloc(sizeof(char), 100); textLayer->layer = text_layer_create(bounds); stringObj(textLayer->text, &text); text_layer_set_text(textLayer->layer, textLayer->text); text_layer_set_font(textLayer->layer, fonts_get_system_font(FONT_KEY_BITHAM_42_BOLD)); layer_add_child(window_layer, text_layer_get_layer(textLayer->layer)); return pebbleOther(TEXT_LAYER, textLayer, NULL, NULL); } Object subscription; struct Environment *subscriptionEnv; static void subscriptionHandler(struct tm *tick_time, TimeUnits changed) { eval(&subscription, subscriptionEnv); } Object subscribe(Object function, Object time, struct Environment *env) { if(function.type == TYPE_LAMBDA) { subscription = cloneObject(function); subscriptionEnv = env; int unit = time.type != TYPE_NUMBER ? MINUTE_UNIT : time.number == 1 ? SECOND_UNIT : time.number == 2 ? MINUTE_UNIT : time.number == 3 ? HOUR_UNIT : time.number == 4 ? DAY_UNIT : time.number == 5 ? MONTH_UNIT : time.number == 6 ? YEAR_UNIT : MINUTE_UNIT; tick_timer_service_subscribe(unit, subscriptionHandler); return boolObject(1); } return boolObject(0); }