-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContactor.c
507 lines (385 loc) · 13.6 KB
/
Contactor.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#define G_LOG_USE_STRUCTURED
#include <gtk/gtk.h>
#include <math.h>
#include "fsm.h"
#include "Main.h"
#include "Hands.h"
#include "Contactor.h"
#include "Emulator.h"
#include "Wiring.h"
#include "Common.h"
// Widgets etc
static GtkWidget *ContactorWindow;
// Needs to be global so that timertick in Hands.c can redraw the window
GtkWidget *ContactorDrawingArea;
// static GdkPixbuf *ContactorBackground_pixbuf;
static gboolean ContactorState = FALSE;
static double ContactorAngle = -1.57;
static double ContactorOffsetAngle = 0.0;
static gboolean HoldingContactor = FALSE;
// Window/Hand management
static gboolean InContactorWindow = FALSE;
static gboolean deferedMotion = FALSE;
static gdouble deferedMotionX,deferedMotionY;
static gdouble FingerPressedAtX = 0,FingerPressedAtY = 0;
//static gboolean warpToLeftHand = FALSE; Deprecated
//static gboolean warpToRightHand = FALSE; Deprecated
// Prototypes of handlers used in glade file
__attribute__((used))
gboolean
on_ContactorWindow_enter_notify_event(__attribute__((unused)) GtkWidget *drawingArea,
__attribute__((unused)) GdkEventCrossing *event,
__attribute__((unused)) gpointer data);
__attribute__((used))
gboolean
on_ContactorWindow_leave_notify_event(__attribute__((unused)) GtkWidget *drawingArea,
__attribute__((unused)) GdkEventCrossing *event,
__attribute__((unused)) gpointer data);
__attribute__((used))
gboolean
on_ContactorDrawingArea_button_press_event(__attribute__((unused)) GtkWidget *drawingArea,
__attribute__((unused)) GdkEventButton *event,
__attribute__((unused)) gpointer data);
__attribute__((used))
gboolean
on_ContactorDrawingArea_button_release_event(__attribute__((unused)) GtkWidget *drawingArea,
__attribute__((unused)) GdkEventButton *event,
__attribute__((unused)) gpointer data);
__attribute__((used))
gboolean
on_ContactorDrawingArea_motion_notify_event(__attribute__((unused)) GtkWidget *drawingArea,
__attribute__((unused)) GdkEventMotion *event,
__attribute__((unused)) gpointer data);
__attribute__((used))
gboolean
on_ContactorDrawingArea_draw(__attribute__((unused)) GtkWidget *drawingArea,
__attribute__((unused)) cairo_t *cr,
__attribute__((unused)) gpointer data);
__attribute__((used))
gboolean
on_ContactorWindow_delete_event(__attribute__((unused)) GtkWidget *widget,
__attribute__((unused)) gpointer data);
// Save the window position in the "IsolatorState" config file.
void ContactorTidy(GString *userPath)
{
GString *configText;
int windowXpos,windowYpos;
//printf("%s called\n",__FUNCTION__);
configText = g_string_new("# Contactor configuration\n");
gtk_window_get_position(GTK_WINDOW(ContactorWindow), &windowXpos, &windowYpos);
g_string_append_printf(configText,"WindowPosition %d %d\n",windowXpos,windowYpos);
updateConfigFile("IsolatorState",userPath,configText);
g_string_free(configText,TRUE);
gtk_widget_destroy(ContactorWindow);
}
// Repositon the window from config file
static int savedWindowPositionHandler(int nn)
{
int windowXpos,windowYpos;
windowXpos = atoi(getField(nn+1));
windowYpos = atoi(getField(nn+2));
gtk_window_move(GTK_WINDOW(ContactorWindow),windowXpos,windowYpos);
return TRUE;
}
static Token savedStateTokens[] = {
{"WindowPosition",0,savedWindowPositionHandler},
{NULL,0,NULL}
};
void ContactorInit(GtkBuilder *builder,
GString *sharedPath,
__attribute__((unused)) GString *userPath)
{
GString *fileName;
int width,height;
// Get widgets from the builder
ContactorWindow = GTK_WIDGET(gtk_builder_get_object_checked (builder, "ContactorWindow"));
ContactorDrawingArea = GTK_WIDGET(gtk_builder_get_object_checked(builder,"ContactorDrawingArea"));
fileName = g_string_new(NULL);
g_string_printf(fileName,"%sgraphics/contactor.xpm",sharedPath->str);
//ContactorBackground_pixbuf =
// gdk_pixbuf_new_from_file(fileName->str, NULL);
// printf("%s ContactorBackground_pixbuf = %p\n",fileName->str, ContactorBackground_pixbuf);
// These dimensions used to come from a background image which is no longer used.
width = 100;
height = 120;
gtk_window_set_default_size(GTK_WINDOW(ContactorWindow), width, height);
g_string_free(fileName,TRUE);
readConfigFile("IsolatorState",userPath,savedStateTokens);
gtk_widget_show(ContactorWindow);
}
// Simplified version
static int
ContactorWindowEnterHandler(__attribute__((unused)) int s,
__attribute__((unused)) int e,
void *p)
{
struct WindowEvent *wep = (struct WindowEvent *)p;
//printf("%s called \n",__FUNCTION__);
ConfigureLeftHandNew (50.0,50.0,SET_TARGETXY|SET_RESTINGXY|SET_FINGERXY,HAND_NOT_SHOWN);
LeftHandInfo.Fsm->state = IDLE_HAND;
LeftHandInfo.gotoRestingWhenIdle = FALSE;
// Set Right hand to mouse entry point
ConfigureRightHandNew(wep->eventX,wep->eventY,SET_TARGETXY|SET_RESTINGXY|SET_FINGERXY,HAND_ONE_FINGER);
RightHandInfo.Fsm->state = TRACKING_HAND;
RightHandInfo.gotoRestingWhenIdle = FALSE;
SetActiveWindow(CONTACTORWINDOW);
// Stop hand being released immediatly after entering the window.
setEnterDelay(100);
InContactorWindow = TRUE;
savedCursor = gdk_window_get_cursor(wep->window);
gdk_window_set_cursor(wep->window,blankCursor);
gtk_widget_queue_draw(ContactorDrawingArea);
return -1;
}
static
int
ContactorWindowLeaveHandler(__attribute__((unused)) int s,
__attribute__((unused)) int e,
void *p)
{
struct WindowEvent *wep = (struct WindowEvent *)p;
//printf("%s called \n",__FUNCTION__);
//setHandsInWindow(NOWINDOW,NEITHER_HAND,NEITHER_HAND,0.0,0.0,0.0,0.0);
SetActiveWindow(NOWINDOW);
InContactorWindow = FALSE;
gdk_window_set_cursor(wep->window,savedCursor);
gtk_widget_queue_draw(ContactorDrawingArea);
return -1;
}
static struct fsmtable ContactorWindowTable[] = {
{OUTSIDE_WINDOW, FSM_ENTER, INSIDE_WINDOW, ContactorWindowEnterHandler},
{INSIDE_WINDOW, FSM_LEAVE, OUTSIDE_WINDOW, ContactorWindowLeaveHandler},
{-1,-1,-1,NULL}
};
static struct fsm ContactorWindowFSM = { "Contactor Window FSM",0, ContactorWindowTable ,
WindowFSMStateNames,WindowFSMEventNames,0,-1};
__attribute__((used))
gboolean
on_ContactorWindow_enter_notify_event(__attribute__((unused)) GtkWidget *drawingArea,
__attribute__((unused)) GdkEventCrossing *event,
__attribute__((unused)) gpointer data)
{
//struct WindowEvent we = {CONTACTORWINDOW,event->x,event->y,ContactorEnterHandler,event->window,NULL};
struct WindowEvent we = {CONTACTORWINDOW,event->x,event->y,event->window,NULL};
//printf("%s called %d \n",__FUNCTION__,GetActiveWindow());
if(GetActiveWindow() == NOWINDOW)
{
setMouseAtXY(event->x,event->y);
doFSM(&ContactorWindowFSM,FSM_ENTER,(void *) &we);
}
return TRUE;
}
__attribute__((used))
gboolean
on_ContactorWindow_leave_notify_event(__attribute__((unused)) GtkWidget *drawingArea,
__attribute__((unused)) GdkEventCrossing *event,
__attribute__((unused)) gpointer data)
{
//struct WindowEvent we = {CONTACTORWINDOW,event->x,event->y,ContactorLeaveHandler,event->window,NULL};
struct WindowEvent we = {CONTACTORWINDOW,event->x,event->y,event->window,NULL};
//printf("%s called \n",__FUNCTION__);
if(GetActiveWindow() == CONTACTORWINDOW)
{
doFSM(&ContactorWindowFSM,FSM_LEAVE,(void *) &we);
}
return TRUE;
}
__attribute__((used))
gboolean
on_ContactorDrawingArea_button_press_event(__attribute__((unused)) GtkWidget *drawingArea,
GdkEventButton *event,
__attribute__((unused)) gpointer data)
{
HandInfo *trackingHand;
//printf("%s called\n",__FUNCTION__);
if(event->button == 3)
{
dropHand();
return TRUE;
}
trackingHand = updateHands(event->x,event->y,&FingerPressedAtX,&FingerPressedAtY);
if(trackingHand == NULL) return TRUE;
//ContactorState = !ContactorState;
{
double x,y,angle;
x = event->x - 50.0;
y = event->y - 60.0;
angle = -atan2(x,y) - G_PI/2.0;
//printf("diff = %f \n",angle-ContactorAngle);
if(fabs(angle-ContactorAngle) < 0.5)
{
HoldingContactor = TRUE;
ContactorOffsetAngle = angle-ContactorAngle;
}
else
HoldingContactor = FALSE;
}
return TRUE;
}
__attribute__((used))
gboolean
on_ContactorDrawingArea_button_release_event(__attribute__((unused)) GtkWidget *drawingArea,
__attribute__((unused)) GdkEventButton *event,
__attribute__((unused)) gpointer data)
{
//printf("%s called\n",__FUNCTION__);
HoldingContactor = FALSE;
return TRUE;
}
__attribute__((used))
gboolean
on_ContactorDrawingArea_motion_notify_event(GtkWidget *drawingArea,
__attribute__((unused)) GdkEventMotion *event,
__attribute__((unused)) gpointer data)
{
gdouble hx,hy;
if(InContactorWindow)
{
updateHands(event->x,event->y,&hx,&hy);
if(HoldingContactor)
{
double x,y,oldAngle;
x = event->x - 50.0;
y = event->y - 60.0;
oldAngle = ContactorAngle;
ContactorAngle = -atan2(x,y) - G_PI/2.0;
ContactorAngle -= ContactorOffsetAngle;
//printf("Angle = %f\n",ContactorAngle);
if(ContactorAngle < -1.57)
{
ContactorAngle = -1.57;
HoldingContactor = FALSE;
}
if(ContactorAngle > 0.0)
{
ContactorAngle = 0.0;
HoldingContactor = FALSE;
}
if((oldAngle < -0.79) && (ContactorAngle > -0.79))
{
ContactorAngle = 0.0;
HoldingContactor = FALSE;
//printf("POWER ON\n");
wiring(MAINS_SUPPLY_ON,1);
ContactorState = TRUE;
}
else if((oldAngle > -0.79) && (ContactorAngle < -0.79))
{
ContactorAngle = -1.57;
HoldingContactor = FALSE;
//printf("POWER OFF\n");
wiring(MAINS_SUPPLY_OFF,1);
ContactorState = FALSE;
}
}
gtk_widget_queue_draw(drawingArea);
}
else
{
// Cursor motion but not InWordGenWindow so entry has bee defered
// by animation in another window. Save cursor location for use
// when the defered entry happens.
deferedMotionX = event->x;
deferedMotionY = event->y;
deferedMotion = TRUE;
}
return TRUE;
}
// TO DO : This should draw the white square and render the text into a surface
// and preserve that between calls.
#define BACKGROUND 0.3
__attribute__((used))
gboolean
on_ContactorDrawingArea_draw(__attribute__((unused)) GtkWidget *drawingArea,
__attribute__((unused)) cairo_t *cr,
__attribute__((unused)) gpointer data)
{
gint width, height,size,topEdge;
static PangoLayout *layout = NULL; // Retained between calls
// Do some pango setup on the first call.
if(layout == NULL)
{
PangoFontDescription *desc;
layout = pango_cairo_create_layout (cr);
desc = pango_font_description_from_string ("Sans 8");
pango_layout_set_font_description (layout, desc);
pango_font_description_free (desc);
}
// Get the size of the drawing area
width = gtk_widget_get_allocated_width (drawingArea);
height = gtk_widget_get_allocated_height (drawingArea);
size = width - 20;
topEdge = (height - size) / 2;
// Fill whole area to black
// Set fill colour to grey
cairo_set_source_rgba (cr, BACKGROUND,BACKGROUND,BACKGROUND,1.0);
//cairo_rectangle(cr,0,0,width,height);
cairo_paint(cr);
cairo_set_source_rgba (cr, 1.0,1.0,0.0,1.0);
cairo_rectangle(cr,10,topEdge,size,size);
cairo_fill(cr);
cairo_set_line_width(cr, 6);
cairo_set_source_rgb(cr, 1.0, 0.0, 0);
cairo_translate(cr, width/2, height/2);
cairo_arc(cr, 0, 0, (size/2)-3, 0, 2 * M_PI);
cairo_stroke(cr);
cairo_fill(cr);
// Draw Red circle
cairo_set_source_rgba (cr, 1.0 , 1.0, 1.0, 1.0);
cairo_move_to(cr,0.0,0.0);
cairo_arc(cr, 0,0,(size/2)-3, 0.0 ,2.0 * G_PI);
cairo_fill(cr);
//-----------
cairo_set_source_rgb(cr, 0, 0, 0);
pango_layout_set_text (layout, "OFF", -1);
// Redo layout for new text
pango_cairo_update_layout (cr, layout);
// Retrieve the size of the rendered text
pango_layout_get_size (layout, &width, &height);
// Position the text at the end of the radial marks
cairo_move_to(cr, ((double)-width /PANGO_SCALE / 2.0),-38.0);
// Render the text
pango_cairo_show_layout (cr, layout);
cairo_stroke(cr);
pango_layout_set_text (layout, "O\nN", -1);
// Redo layout for new text
pango_cairo_update_layout (cr, layout);
// Retrieve the size of the rendered text
pango_layout_get_size (layout, &width, &height);
// Position the text at the end of the radial marks
cairo_move_to(cr, 28.0, ((double)-height /PANGO_SCALE / 2.0));
// Render the text
pango_cairo_show_layout (cr, layout);
// cairo_stroke(cr);
// ----------
cairo_rotate(cr,ContactorAngle);
cairo_set_line_width(cr, 1);
cairo_set_source_rgba (cr, 0.8 , 0.0, 0.0, 1.0);
cairo_move_to(cr,0.0,0.0);
cairo_arc(cr, 0,0,(size/2)-3, 0.1* G_PI ,1.9 * G_PI);
cairo_fill(cr);
// Knob
cairo_set_source_rgba (cr, 1.0 , 0.2, 0.0, 1.0);
cairo_move_to(cr,0.0,10.0);
cairo_line_to(cr,20.0,0.0);
cairo_line_to(cr,0.0,-10.0);
cairo_line_to(cr,-35.0,-7.0);
cairo_line_to(cr,-35.0,7.0);
// Draw and fill the trapezium
cairo_close_path(cr);
cairo_stroke_preserve(cr);
cairo_fill(cr);
cairo_identity_matrix(cr);
if(InContactorWindow)
DrawHandsNew(cr);
return TRUE;
}
__attribute__((used))
gboolean
on_ContactorWindow_delete_event(__attribute__((unused)) GtkWidget *widget,
__attribute__((unused)) gpointer data)
{
// gtk_main_quit();
if(ContactorState == FALSE) EmulatorShutdown();
return TRUE;
}