| 1 | /*
|
|---|
| 2 | * Copyright (c) 2023 SimonJRiddix
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup launcher
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file Launcher
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include "include/window.h"
|
|---|
| 36 | #include "include/label.h"
|
|---|
| 37 | #include "include/button.h"
|
|---|
| 38 | #include "include/control.h"
|
|---|
| 39 |
|
|---|
| 40 | #include <task.h>
|
|---|
| 41 |
|
|---|
| 42 | #define newVector(name) vector name; vector_init(&name);
|
|---|
| 43 | #define vector_get(v, pos) v.Get(&v, pos)
|
|---|
| 44 | #define vector_count(v) v.Count(&v)
|
|---|
| 45 |
|
|---|
| 46 | typedef struct sApp application;
|
|---|
| 47 | struct sApp
|
|---|
| 48 | {
|
|---|
| 49 | char* name;
|
|---|
| 50 | char* location;
|
|---|
| 51 | char** arguments;
|
|---|
| 52 | };
|
|---|
| 53 |
|
|---|
| 54 | typedef struct scategory category;
|
|---|
| 55 | struct scategory
|
|---|
| 56 | {
|
|---|
| 57 | char* name;
|
|---|
| 58 | //icon_t icon;
|
|---|
| 59 | vector applications;
|
|---|
| 60 | };
|
|---|
| 61 |
|
|---|
| 62 | // init application categories vector
|
|---|
| 63 | vector categories;
|
|---|
| 64 |
|
|---|
| 65 | // init window ui
|
|---|
| 66 | Window window;
|
|---|
| 67 |
|
|---|
| 68 | // extern application launcher code
|
|---|
| 69 | extern int app_launchl(const char *, ...);
|
|---|
| 70 |
|
|---|
| 71 | // on tile click
|
|---|
| 72 | static void pb_clicked(ui_pbutton_t *pbutton, void * args)
|
|---|
| 73 | {
|
|---|
| 74 | if(app_launchl(((application *) args)->location, NULL) == EOK)
|
|---|
| 75 | {
|
|---|
| 76 | window.destroy(&window);
|
|---|
| 77 | task_kill(task_get_id());
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | int main(int argc, char *argv[])
|
|---|
| 82 | {
|
|---|
| 83 | vector_init(&categories);
|
|---|
| 84 |
|
|---|
| 85 | /// ACCESSORIES ///
|
|---|
| 86 |
|
|---|
| 87 | category category_accessory;
|
|---|
| 88 | vector_init(&category_accessory.applications);
|
|---|
| 89 | category_accessory.name = (char*) "Accessory";
|
|---|
| 90 |
|
|---|
| 91 | application notepad;
|
|---|
| 92 | notepad.name = (char*) "Notepad";
|
|---|
| 93 | notepad.location = (char*) "/app/notepad";
|
|---|
| 94 | category_accessory.applications.Add(&category_accessory.applications, ¬epad);
|
|---|
| 95 |
|
|---|
| 96 | application calc;
|
|---|
| 97 | calc.name = (char*) "Calculator";
|
|---|
| 98 | calc.location = (char*) "/app/calculator";
|
|---|
| 99 | category_accessory.applications.Add(&category_accessory.applications, &calc);
|
|---|
| 100 |
|
|---|
| 101 | categories.Add(&categories, &category_accessory);
|
|---|
| 102 |
|
|---|
| 103 | /// TEST ///
|
|---|
| 104 |
|
|---|
| 105 | category category_test;
|
|---|
| 106 | vector_init(&category_test.applications);
|
|---|
| 107 | category_test.name = (char*) "Test";
|
|---|
| 108 |
|
|---|
| 109 | application ui_demo;
|
|---|
| 110 | ui_demo.name = (char*) "UI Demo";
|
|---|
| 111 | ui_demo.location = (char*) "/app/uidemo";
|
|---|
| 112 | category_test.applications.Add(&category_test.applications, &ui_demo);
|
|---|
| 113 |
|
|---|
| 114 | application gfx_demo;
|
|---|
| 115 | gfx_demo.name = (char*) "GFX Test";
|
|---|
| 116 | gfx_demo.location = (char*) "/app/gfxdemo";
|
|---|
| 117 | char* gfx_demo_args[1];
|
|---|
| 118 | gfx_demo_args[0] = (char*) "ui";
|
|---|
| 119 | gfx_demo.arguments = gfx_demo_args;
|
|---|
| 120 | category_test.applications.Add(&category_test.applications, &gfx_demo);
|
|---|
| 121 |
|
|---|
| 122 | categories.Add(&categories, &category_test);
|
|---|
| 123 |
|
|---|
| 124 | /// SYSTEM ///
|
|---|
| 125 |
|
|---|
| 126 | category category_system;
|
|---|
| 127 | category category_left_menu;
|
|---|
| 128 | vector_init(&category_system.applications);
|
|---|
| 129 | vector_init(&category_left_menu.applications);
|
|---|
| 130 | category_system.name = (char*) "System";
|
|---|
| 131 | category_left_menu.name = (char*) "System";
|
|---|
| 132 |
|
|---|
| 133 | application system_shutdown;
|
|---|
| 134 | system_shutdown.name = (char*) "Shutdown";
|
|---|
| 135 | system_shutdown.location = (char*) "/app/systemshutdown";
|
|---|
| 136 | category_left_menu.applications.Add(&category_left_menu.applications, &system_shutdown);
|
|---|
| 137 |
|
|---|
| 138 | application system_restart;
|
|---|
| 139 | system_restart.name = (char*) "Restart";
|
|---|
| 140 | system_restart.location = (char*) "/app/systemrestart";
|
|---|
| 141 | category_left_menu.applications.Add(&category_left_menu.applications, &system_restart);
|
|---|
| 142 |
|
|---|
| 143 | application system_setting;
|
|---|
| 144 | system_setting.name = (char*) "Setting";
|
|---|
| 145 | system_setting.location = (char*) "/app/setting";
|
|---|
| 146 | category_system.applications.Add(&category_system.applications, &system_setting);
|
|---|
| 147 | category_left_menu.applications.Add(&category_left_menu.applications, &system_setting);
|
|---|
| 148 |
|
|---|
| 149 | application sys_terminal;
|
|---|
| 150 | sys_terminal.name = (char*) "Terminal";
|
|---|
| 151 | sys_terminal.location = (char*) "/app/terminal";
|
|---|
| 152 | category_system.applications.Add(&category_system.applications, &sys_terminal);
|
|---|
| 153 | category_left_menu.applications.Add(&category_left_menu.applications, &sys_terminal);
|
|---|
| 154 |
|
|---|
| 155 | categories.Add(&categories, &category_system);
|
|---|
| 156 |
|
|---|
| 157 | // Create window
|
|---|
| 158 | init_window(&window, "window1", "Application Launcher");
|
|---|
| 159 |
|
|---|
| 160 | // Create small tiles on the left for system actions (shutdown, reboot, terminal...)
|
|---|
| 161 | for(int c = 0; c < vector_count(category_left_menu.applications); c++)
|
|---|
| 162 | {
|
|---|
| 163 | Button button_left;
|
|---|
| 164 | //char button_left_name[256];
|
|---|
| 165 | //sprintf(button_left_name, "btnl%d", c);
|
|---|
| 166 | init_button(&button_left, "button1", &window);
|
|---|
| 167 | button_left.rect.p0.x = 10;
|
|---|
| 168 | button_left.rect.p0.y = window.params.rect.p1.y - ( (c + 1) * (30 + 10));
|
|---|
| 169 | button_left.rect.p1.x = 50;
|
|---|
| 170 | button_left.rect.p1.y = button_left.rect.p0.y + 30;
|
|---|
| 171 | button_left.update_rect(&button_left);
|
|---|
| 172 | application* a = vector_get(category_left_menu.applications, c);
|
|---|
| 173 | button_left.set_text(&button_left, a->name);
|
|---|
| 174 | button_left.paint(&button_left);
|
|---|
| 175 | button_left.set_callback(&button_left, pb_clicked, a);
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | size_t separators = 0;
|
|---|
| 179 |
|
|---|
| 180 | int row = 0;
|
|---|
| 181 | int col = 0;
|
|---|
| 182 |
|
|---|
| 183 | // Populate categories and big application tiles
|
|---|
| 184 | for(int current_category = 0; current_category < vector_count(categories); current_category++)
|
|---|
| 185 | {
|
|---|
| 186 | category* cat = vector_get(categories, current_category);
|
|---|
| 187 |
|
|---|
| 188 | Label label_category;
|
|---|
| 189 | label_category.rect.p0.x = 60;
|
|---|
| 190 | label_category.rect.p0.y = (30*row) + ( row * 10 ) + (row * 80) + 40;
|
|---|
| 191 | label_category.rect.p1.x = label_category.rect.p0.x + 170;
|
|---|
| 192 | label_category.rect.p1.y = label_category.rect.p0.y + 10;
|
|---|
| 193 | //char label_category_name[256];
|
|---|
| 194 | //sprintf(label_category_name, "lbl%d", current_category);
|
|---|
| 195 | init_label(&label_category, "label1", &window);
|
|---|
| 196 | label_category.set_text(&label_category, cat->name);
|
|---|
| 197 | label_category.update_rect(&label_category);
|
|---|
| 198 | label_category.paint(&label_category);
|
|---|
| 199 |
|
|---|
| 200 | // Populate big application tiles
|
|---|
| 201 | for(int application_in_category = 0; application_in_category < vector_count(cat->applications); application_in_category++)
|
|---|
| 202 | {
|
|---|
| 203 | application* app;
|
|---|
| 204 | app = cat->applications.Get(&cat->applications, application_in_category);
|
|---|
| 205 |
|
|---|
| 206 | // if application name is empty, is not an application but a separator
|
|---|
| 207 | if(str_cmp("", app->name) == 0)
|
|---|
| 208 | {
|
|---|
| 209 | separators++;
|
|---|
| 210 | }
|
|---|
| 211 | else
|
|---|
| 212 | {
|
|---|
| 213 | int c_pos = 70 + ( col * 20 ) + ( col * 80 );
|
|---|
| 214 | if((c_pos + 80) > window.params.rect.p1.x)
|
|---|
| 215 | {
|
|---|
| 216 | col = 0;
|
|---|
| 217 | c_pos = 70 + ( col * 20 ) + ( col * 80 );
|
|---|
| 218 | row++;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | Button button_application;
|
|---|
| 222 | //char button_application_name[256];
|
|---|
| 223 | //sprintf(button_application_name, "btn%d", (application_in_category+1)*c);
|
|---|
| 224 | init_button(&button_application, "button1", &window);
|
|---|
| 225 | button_application.rect.p0.x = c_pos;
|
|---|
| 226 | button_application.rect.p0.y = 20 + (30*row) + ( row * 10 ) + (row * 80) + 40;
|
|---|
| 227 | button_application.rect.p1.x = button_application.rect.p0.x + 80;
|
|---|
| 228 | button_application.rect.p1.y = button_application.rect.p0.y + 80;
|
|---|
| 229 | button_application.update_rect(&button_application);
|
|---|
| 230 | button_application.set_text(&button_application, app->name);
|
|---|
| 231 | button_application.set_callback(&button_application, pb_clicked, app);
|
|---|
| 232 |
|
|---|
| 233 | col++;
|
|---|
| 234 | }
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | row++;
|
|---|
| 238 | col = 0;
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | window.draw(&window);
|
|---|
| 242 |
|
|---|
| 243 | window.destroy(&window);
|
|---|
| 244 |
|
|---|
| 245 | return 0;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | /** @}
|
|---|
| 249 | */
|
|---|