This repository has been archived by the owner on May 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpage-01-drive-select.c
241 lines (197 loc) · 8.15 KB
/
page-01-drive-select.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* This file is part of vos-installer.
* Copyright (C) 2016 Velt Technologies, Aidan Shafran <zelbrium@gmail.com>
* Licensed under the Apache License 2 <www.apache.org/licenses/LICENSE-2.0>.
*/
#include "pages.h"
#include "sd-utils.h"
#include <string.h>
struct _PageDriveSelect
{
CmkWidget parent;
CmkLabel *helpLabel;
CmkScrollBox *driveListBox;
CmkButton *selectedDriveButton;
CmkButton *nextButton;
gconstpointer driveMonitor;
};
static void on_dispose(GObject *self_);
static void on_allocate(ClutterActor *self_, const ClutterActorBox *box, ClutterAllocationFlags flags);
static void on_drive_added(StorageDevice *device, PageDriveSelect *self);
static void on_drive_removed(StorageDevice *device, PageDriveSelect *self);
extern void g_01b_selected_drive_changed(void);
StorageDevice *gSelectedDevice;
G_DEFINE_TYPE(PageDriveSelect, page_drive_select, CMK_TYPE_WIDGET);
CmkWidget * page_drive_select_new(void)
{
return CMK_WIDGET(g_object_new(page_drive_select_get_type(), NULL));
}
static void page_drive_select_class_init(PageDriveSelectClass *class)
{
G_OBJECT_CLASS(class)->dispose = on_dispose;
CLUTTER_ACTOR_CLASS(class)->allocate = on_allocate;
}
static void page_drive_select_init(PageDriveSelect *self)
{
self->driveListBox = cmk_scroll_box_new(CLUTTER_SCROLL_HORIZONTALLY);
ClutterBoxLayout *listLayout = CLUTTER_BOX_LAYOUT(clutter_box_layout_new());
clutter_box_layout_set_orientation(listLayout, CLUTTER_ORIENTATION_HORIZONTAL);
clutter_box_layout_set_spacing(listLayout, 10);
clutter_actor_set_x_align(CLUTTER_ACTOR(self->driveListBox), CLUTTER_ACTOR_ALIGN_CENTER);
clutter_actor_set_layout_manager(CLUTTER_ACTOR(self->driveListBox), CLUTTER_LAYOUT_MANAGER(listLayout));
clutter_actor_add_child(CLUTTER_ACTOR(self), CLUTTER_ACTOR(self->driveListBox));
self->helpLabel = cmk_label_new_full("Please select a parition to install VeltOS.\nYou must use a utility such as cgdisk to create partitions.", TRUE);
cmk_label_set_line_alignment(self->helpLabel, PANGO_ALIGN_CENTER);
clutter_actor_add_child(CLUTTER_ACTOR(self), CLUTTER_ACTOR(self->helpLabel));
self->driveMonitor = monitor_storage_devices(
(StorageDeviceAddedCb)on_drive_added,
(StorageDeviceRemovedCb)on_drive_removed,
self);
self->nextButton = cmk_button_new_with_text("Use Selected Partition", CMK_BUTTON_TYPE_RAISED);
cmk_widget_set_disabled(CMK_WIDGET(self->nextButton), TRUE);
clutter_actor_add_child(CLUTTER_ACTOR(self), CLUTTER_ACTOR(self->nextButton));
//g_signal_connect(self->nextButton, "activate", G_CALLBACK(on_next_select), self);
g_signal_connect_swapped(self->nextButton, "activate", G_CALLBACK(cmk_widget_replace), self);
}
static void on_dispose(GObject *self_)
{
PageDriveSelect *self = PAGE_DRIVE_SELECT(self_);
stop_monitoring_storage_devices(self->driveMonitor);
G_OBJECT_CLASS(page_drive_select_parent_class)->dispose(self_);
}
static void on_allocate(ClutterActor *self_, const ClutterActorBox *box, ClutterAllocationFlags flags)
{
PageDriveSelect *self = PAGE_DRIVE_SELECT(self_);
gfloat width = clutter_actor_box_get_width(box);
gfloat height = clutter_actor_box_get_height(box);
gfloat pad = CMK_DP(self_, 30);
gfloat minW, minH, natW, natH;
clutter_actor_get_preferred_size(CLUTTER_ACTOR(self->nextButton), &minW, &minH, &natW, &natH);
gfloat hminH, hnatH;
clutter_actor_get_preferred_height(CLUTTER_ACTOR(self->helpLabel), width-pad*2, &hminH, &hnatH);
ClutterActorBox nextButton = {
width-pad - natW,
height-pad - natH,
width-pad,
height-pad
};
ClutterActorBox help = {
pad,
pad*2,
width-pad,
pad*2+hnatH
};
clutter_actor_get_preferred_height(CLUTTER_ACTOR(self->driveListBox), width, NULL, &natH);
ClutterActorBox b = {0, height/2-natH/2-pad/2, width, height/2+natH/2+pad/2};
clutter_actor_allocate(CLUTTER_ACTOR(self->helpLabel), &help, flags);
clutter_actor_allocate(CLUTTER_ACTOR(self->driveListBox), &b, flags);
clutter_actor_allocate(CLUTTER_ACTOR(self->nextButton), &nextButton, flags);
CLUTTER_ACTOR_CLASS(page_drive_select_parent_class)->allocate(self_, box, flags);
}
static void on_drive_select(CmkButton *driveButton, PageDriveSelect *self)
{
if(self->selectedDriveButton)
{
//clutter_actor_save_easing_state(CLUTTER_ACTOR(self->selectedDriveButton));
//clutter_actor_set_easing_duration(CLUTTER_ACTOR(self->selectedDriveButton), 1000);
//cmk_widget_set_background_color(CMK_WIDGET(self->selectedDriveButton), "hover");
//clutter_actor_restore_easing_state(CLUTTER_ACTOR(self->selectedDriveButton));
cmk_button_set_selected(self->selectedDriveButton, FALSE);
}
self->selectedDriveButton = driveButton;
if(gSelectedDevice)
{
free_storage_device(gSelectedDevice);
gSelectedDevice = NULL;
}
if(driveButton)
{
cmk_button_set_selected(driveButton, TRUE);
//clutter_actor_save_easing_state(CLUTTER_ACTOR(driveButton));
//clutter_actor_set_easing_duration(CLUTTER_ACTOR(driveButton), 5000);
//cmk_widget_set_background_color(CMK_WIDGET(driveButton), "accent");
//clutter_actor_restore_easing_state(CLUTTER_ACTOR(driveButton));
gSelectedDevice = copy_storage_device(g_object_get_data(G_OBJECT(driveButton), "device"));
}
cmk_widget_set_disabled(CMK_WIDGET(self->nextButton), driveButton == NULL);
g_01b_selected_drive_changed();
}
static gboolean add_drive(StorageDevice *device)
{
// if(strncmp(device->fs, "ext4", 4))
// return;
// Don't show EFI System Partitions (if you want to install VOS onto your
// EFI partition you should probably be using a different tool...)
if(device->efi)
{
// Parameter 'device' is a copy of the device monitor's StorageDevice,
// we must free it
free_storage_device(device);
return G_SOURCE_REMOVE;
}
PageDriveSelect *self = (PageDriveSelect *)device->userdata;
const char *iconName = device->removable ? "drive-removable-media" : "drive-harddisk";
CmkButton *button = cmk_button_new(CMK_BUTTON_TYPE_FLAT);
g_object_set_data_full(G_OBJECT(button), "device", device, (GDestroyNotify)free_storage_device);
clutter_actor_set_name(CLUTTER_ACTOR(button), device->node);
cmk_button_set_type(button, CMK_BUTTON_TYPE_FLAT);
CmkWidget *box = cmk_widget_new();
ClutterBoxLayout *list = CLUTTER_BOX_LAYOUT(clutter_box_layout_new());
clutter_box_layout_set_orientation(list, CLUTTER_ORIENTATION_VERTICAL);
clutter_actor_set_layout_manager(CLUTTER_ACTOR(box), CLUTTER_LAYOUT_MANAGER(list));
CmkIcon *icon = cmk_icon_new_from_name(iconName, 96);
clutter_actor_add_child(CLUTTER_ACTOR(box), CLUTTER_ACTOR(icon));
CmkLabel *label = cmk_label_new_with_text(device->name);
clutter_actor_add_child(CLUTTER_ACTOR(box), CLUTTER_ACTOR(label));
CmkLabel *node = cmk_label_new_with_text(device->node);
clutter_actor_add_child(CLUTTER_ACTOR(box), CLUTTER_ACTOR(node));
cmk_button_set_content(button, box);
g_signal_connect(button, "activate", G_CALLBACK(on_drive_select), self);
clutter_actor_add_child(CLUTTER_ACTOR(self->driveListBox), CLUTTER_ACTOR(button));
return G_SOURCE_REMOVE;
}
static gboolean remove_drive(StorageDevice *device)
{
PageDriveSelect *self = (PageDriveSelect *)device->userdata;
ClutterActorIter iter;
ClutterActor *child;
clutter_actor_iter_init(&iter, CLUTTER_ACTOR(self->driveListBox));
while(clutter_actor_iter_next(&iter, &child))
{
if(g_strcmp0(clutter_actor_get_name(child), device->node) == 0)
{
if(self->selectedDriveButton == CMK_BUTTON(child))
on_drive_select(NULL, self);
clutter_actor_destroy(child);
break;
}
}
// Parameter 'device' is a copy of the device monitor's StorageDevice,
// we must free it
free_storage_device(device);
return G_SOURCE_REMOVE;
}
// Called from separate monitor thread
static void on_drive_added(StorageDevice *device, PageDriveSelect *self)
{
StorageDevice *copy = copy_storage_device(device);
copy->userdata = self;
// clutter_threads_add_idle_full is Clutter's way of communicating from
// a secondary thread to the main thread
clutter_threads_add_idle_full(
G_PRIORITY_HIGH,
(GSourceFunc)add_drive,
copy,
NULL);
}
// Called from separate monitor thread
static void on_drive_removed(StorageDevice *device, PageDriveSelect *self)
{
StorageDevice *copy = copy_storage_device(device);
copy->userdata = self;
clutter_threads_add_idle_full(
G_PRIORITY_HIGH,
(GSourceFunc)remove_drive,
copy,
NULL);
}