-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathvnoc_gui.cpp
1829 lines (1591 loc) · 59.6 KB
/
vnoc_gui.cpp
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "vnoc_gui.h"
////////////////////////////////////////////////////////////////////////////////
//
// statics
//
////////////////////////////////////////////////////////////////////////////////
static Bool test_if_exposed (Display *disp, XEvent *event_ptr, XPointer dummy)
{
// Returns True if the event passed in is an exposure event. Note that
// the bool type returned by this function is defined in Xlib.h.
if ( event_ptr->type == Expose) {
return (True);
}
return (False);
}
////////////////////////////////////////////////////////////////////////////////
//
// drawing
//
////////////////////////////////////////////////////////////////////////////////
void GUI_GRAPHICS::set_graphics_state( bool show_graphics_val, int wfui_automode_val)
{
// Sets the static show_graphics and wait_for_user_input_automode variables to the
// desired values. They control if graphics are enabled and, if so,
// how often the user is prompted for input.
_is_gui_usable = show_graphics_val;
_wait_for_user_input_automode = wfui_automode_val;
}
void GUI_GRAPHICS::update_screen( int priority, char *msg,
PICTURE_TYPE pic_on_screen_val)
{
// Updates the screen if the user has requested graphics. The priority
// value controls whether or not the Proceed button must be clicked to
// continue. Saves the pic_on_screen_val to allow pan and zoom redraws.
if ( !_is_gui_usable)
return;
// If it's the type of picture displayed has changed, set up the proper
// buttons.
if ( _pic_on_screen != pic_on_screen_val) {
if ( pic_on_screen_val == ROUTERS && _pic_on_screen == NO_PICTURE) {
create_button( "Window", "Toggle Links", TOGGLE_LINKS);
}
}
// Save the main message.
strncpy( _default_message, msg, BUFFER_SIZE);
_pic_on_screen = pic_on_screen_val;
update_message( msg);
drawscreen();
if ( priority >= _wait_for_user_input_automode) { // priority can be 0,1,2;
event_loop();
} else {
flushinput();
}
}
void GUI_GRAPHICS::drawscreen (void)
{
// This is the screen redrawing routine that event_loop assumes exists.
// It erases whatever is on screen, then calls redraw_screen to redraw
// it.
clearscreen();
redraw_screen();
}
void GUI_GRAPHICS::redraw_screen (void)
{
// The screen redrawing routine called by drawscreen and
// highlight_blocks. Call this routine instead of drawscreen if
// you know you don't need to erase the current graphics, and want
// to avoid a screen "flash".
if ( _pic_on_screen == ROUTERS) {
// (a) first bring in the info about the occupancy of routers in the
// network;
long routers_count = _vnoc->routers_count();
long physical_ports_count = 1 + 2 * _topology->cube_size();
long vc_number = _topology->virtual_channel_number();
// (b) first go thru all routers and compute current_max_router_occupancy;
float current_max_router_occupancy = 0;
for ( long r_i = 0; r_i < routers_count; r_i++) {
ROUTER *this_router = _vnoc->router( r_i);
long this_router_occ = 0;
// calculate on the fly how many flits occupy currently this rouyer;
for ( long i = 0; i < physical_ports_count; i ++) {
for ( long j = 0; j < vc_number; j ++) {
long this_r_input_occ =
this_router->input().input_buff(i,j).size();
this_router_occ += (this_r_input_occ > 0) ? this_r_input_occ : 0;
}
long this_r_output_occ =
this_router->output().out_buffer(i).size();
this_router_occ += (this_r_output_occ > 0) ? this_r_output_occ : 0;
}
// record it as well;
_router_occupancy[ r_i] = this_router_occ;
if ( this_router_occ > current_max_router_occupancy) {
current_max_router_occupancy = this_router_occ;
}
}
current_max_router_occupancy = max(current_max_router_occupancy, 1);
// (c) set up colors for routers based on occupancy;
for ( long r_i = 0; r_i < routers_count; r_i++) {
// before I used _max_router_occupancy, but all routers were same color...
// for low congestion in entire network;
float occ_fraction = _router_occupancy[ r_i] / current_max_router_occupancy;
if ( occ_fraction >= 0.8 && occ_fraction <= 1.0) {
_router_color[ r_i] = BLACK;
} else if ( occ_fraction >= 0.6 && occ_fraction < 0.8) {
_router_color[ r_i] = DARKGREY;
} else if ( occ_fraction >= 0.4 && occ_fraction < 0.6) {
_router_color[ r_i] = LIGHTGREY;
} else if ( occ_fraction >= 0.2 && occ_fraction < 0.4) {
_router_color[ r_i] = GREY35;
} else if ( occ_fraction > 0.0 && occ_fraction < 0.2) {
_router_color[ r_i] = GREY15;
} else if ( occ_fraction == 0.0) {
_router_color[ r_i] = WHITE;
} else {
assert(0); // impossible;
}
}
// (b) do the drawing with the updated occupancies;
draw_routers();
}
}
void GUI_GRAPHICS::toggle_links(void)
{
// Enables/disables drawing of nets when a the user clicks on a button.
// Also disables drawing of routing resources. See graphics.c for details
// of how buttons work.
_show_links = !_show_links;
_show_congestion = false;
update_message( _default_message);
drawscreen();
}
void GUI_GRAPHICS::alloc_draw_structs (void)
{
// Allocate the structures needed to draw things
// Set up the default colors
long routers_count = _vnoc->routers_count();
_x_router_left.resize( routers_count);
_y_router_bottom.resize( routers_count);
_router_color.resize( routers_count);
_router_occupancy.resize( routers_count);
_router_has_PE.resize( routers_count);
deselect_all();
}
void GUI_GRAPHICS::deselect_all (void)
{
// Sets the color of all to the default.
long routers_count = _vnoc->routers_count();
for ( long i = 0; i < routers_count; i++) {
_router_color[i] = LIGHTGREY;
}
}
void GUI_GRAPHICS::init_draw_coords ( float router_width_val,
float channel_width_val)
{
// Load the arrays containing the left and bottom coordinates of the routers
if ( !_is_gui_usable)
return;
long routers_count = _vnoc->routers_count();
long i = 0;
long nx = _topology->ary_size();
long ny = nx;
// set now numerical values of interest for dimensions and coloring
// to represent congestion;
_router_width = router_width_val;
_channel_width = channel_width_val;
long physical_ports_count = 1 + 2 * _topology->cube_size();
long vc_number = _topology->virtual_channel_number();
long in_out_buffer_size = _topology->input_buffer_size() +
_topology->output_buffer_size();
_max_router_occupancy = physical_ports_count * vc_number * in_out_buffer_size;
_x_router_left[0] = 0.;
for ( i = 1; i < nx; i++) {
_x_router_left[i] = _x_router_left[i-1] + _router_width + _channel_width + 1.;
}
_y_router_bottom[0] = 0.;
for ( i = 1; i < ny; i++) {
_y_router_bottom[i] = _y_router_bottom[i-1] + _router_width + _channel_width + 1.;
}
for ( i = 0; i < routers_count; i++) {
_router_occupancy[i] = 0;
_router_has_PE[i] = true;
}
init_world(
0., _y_router_bottom[ ny - 1] + _router_width,
_x_router_left[ nx - 1] + _router_width, 0.);
}
void GUI_GRAPHICS::draw_routers (void)
{
// draws routers. occupancy of each router is also written;
// colors are grey nuances to show occupancy/congestion; empty
// ones are left white and have a dashed border; each PE for routers
// that have PE are small red rects;
float x1=0, y1=0, x2=0, y2=0;
float x1_pe=0, y1_pe=0, x2_pe=0, y2_pe=0;
float pe_width_height = _router_width / 4;
long i=0, j=0;
char buf[BUFFER_SIZE];
long nx = _topology->ary_size();
long ny = nx;
setlinewidth(0);
// routers show;
for ( i = 0; i < nx; i++) {
x1 = _x_router_left[i];
x2 = x1 + _router_width;
for ( j = 0; j < ny; j++) {
long this_router_id = i * nx + j;
y1 = _y_router_bottom[j];
y2 = y1 + _router_width;
if ( _router_occupancy[ this_router_id] > 0) {
setlinestyle( SOLID);
setcolor ( _router_color[ this_router_id]);
fillrect ( x1,y1,x2,y2);
setcolor ( BLACK);
drawrect ( x1,y1,x2,y2);
if ( _router_color[ this_router_id] > GREEN) { // see COLOR_TYPES;
setcolor ( CYAN);
} else {
setcolor ( BLACK);
}
sprintf ( buf, "ID %d Occ %.0f", this_router_id,
_router_occupancy[ this_router_id]);
drawtext ( (x1 + x2)/2., (y1 + y2)/2., buf, _router_width);
}
else {
setlinestyle (DASHED);
setcolor (BLACK);
drawrect (x1,y1,x2,y2);
}
// draw the PE;
x1_pe = x1; x2_pe = x1 + pe_width_height;
y1_pe = y1; y2_pe = y1 + pe_width_height;
setlinestyle( SOLID);
setcolor ( CYAN);
fillrect ( x1_pe, y1_pe, x2_pe, y2_pe);
setcolor ( BLACK);
drawrect ( x1_pe, y1_pe, x2_pe, y2_pe);
drawtext ( (x1_pe + x2_pe)/2., (y1_pe + y2_pe)/2.,
"PE", 2*pe_width_height);
}
}
}
////////////////////////////////////////////////////////////////////////////////
//
// action functions
//
////////////////////////////////////////////////////////////////////////////////
//void GUI_GRAPHICS::zoom_in (void (*drawscreen) (void))
void GUI_GRAPHICS::zoom_in ()
{
// Zooms in by a factor of 1.666.
float xdiff, ydiff;
xdiff = _xright - _xleft;
ydiff = _ybot - _ytop;
_xleft += xdiff/5.;
_xright -= xdiff/5.;
_ytop += ydiff/5.;
_ybot -= ydiff/5.;
update_transform();
drawscreen();
}
//void GUI_GRAPHICS::zoom_out (void (*drawscreen) (void))
void GUI_GRAPHICS::zoom_out ()
{
// Zooms out by a factor of 1.666.
float xdiff, ydiff;
xdiff = _xright - _xleft;
ydiff = _ybot - _ytop;
_xleft -= xdiff/3.;
_xright += xdiff/3.;
_ytop -= ydiff/3.;
_ybot += ydiff/3.;
update_transform ();
drawscreen();
}
//void GUI_GRAPHICS::zoom_fit (void (*drawscreen) (void))
void GUI_GRAPHICS::zoom_fit ()
{
// Sets the view back to the initial view set by init_world (i.e. a full
// view) of all the graphics.
_xleft = _saved_xleft;
_xright = _saved_xright;
_ytop = _saved_ytop;
_ybot = _saved_ybot;
update_transform();
drawscreen();
}
//void GUI_GRAPHICS::translate_up (void (*drawscreen) (void))
void GUI_GRAPHICS::translate_up ()
{
// Moves view 1/2 screen up.
float ystep;
ystep = (_ybot - _ytop)/2.;
_ytop -= ystep;
_ybot -= ystep;
update_transform();
drawscreen();
}
//void GUI_GRAPHICS::translate_down (void (*drawscreen) (void))
void GUI_GRAPHICS::translate_down ()
{
// Moves view 1/2 screen down.
float ystep;
ystep = (_ybot - _ytop)/2.;
_ytop += ystep;
_ybot += ystep;
update_transform();
drawscreen();
}
//void GUI_GRAPHICS::translate_left (void (*drawscreen) (void))
void GUI_GRAPHICS::translate_left ()
{
// Moves view 1/2 screen left.
float xstep;
xstep = (_xright - _xleft)/2.;
_xleft -= xstep;
_xright -= xstep;
update_transform();
drawscreen();
}
//void GUI_GRAPHICS::translate_right (void (*drawscreen) (void))
void GUI_GRAPHICS::translate_right ()
{
// Moves view 1/2 screen right.
float xstep;
xstep = (_xright - _xleft)/2.;
_xleft += xstep;
_xright += xstep;
update_transform();
drawscreen();
}
//void GUI_GRAPHICS::update_win (int x[2], int y[2], void (*drawscreen)(void))
void GUI_GRAPHICS::update_win (int x[2], int y[2])
{
float x1, x2, y1, y2;
x[0] = min(x[0], _top_width-MWIDTH); // Can't go under menu
x[1] = min(x[1], _top_width-MWIDTH);
y[0] = min(y[0], _top_height-T_AREA_HEIGHT); // Can't go under text area
y[1] = min(y[1], _top_height-T_AREA_HEIGHT);
if ((x[0] == x[1]) || (y[0] == y[1])) {
printf("Illegal (zero area) window. Window unchanged.\n");
return;
}
x1 = XTOWORLD(min(x[0], x[1]));
x2 = XTOWORLD(max(x[0], x[1]));
y1 = YTOWORLD(min(y[0], y[1]));
y2 = YTOWORLD(max(y[0], y[1]));
_xleft = x1;
_xright = x2;
_ytop = y1;
_ybot = y2;
update_transform();
drawscreen();
}
//void GUI_GRAPHICS::adjustwin (void (*drawscreen) (void))
void GUI_GRAPHICS::adjustwin ()
{
// The window button was pressed. Let the user click on the two
//diagonally opposed corners, and zoom in on this area.
XEvent report;
int corner, xold, yold, x[2], y[2];
corner = 0;
xold = -1;
yold = -1; // Don't need to init yold, but stops compiler warning.
while (corner<2) {
XNextEvent ( _display, &report);
switch (report.type) {
case Expose:
if (report.xexpose.count != 0)
break;
if (report.xexpose.window == _menu)
drawmenu();
else if (report.xexpose.window == _toplevel) {
drawscreen();
xold = -1; // No rubber band on screen
}
else if (report.xexpose.window == _textarea)
draw_message();
break;
case ConfigureNotify:
_top_width = report.xconfigure.width;
_top_height = report.xconfigure.height;
update_transform();
break;
case ButtonPress:
if (report.xbutton.window != _toplevel) break;
x[corner] = report.xbutton.x;
y[corner] = report.xbutton.y;
if (corner == 0) {
XSelectInput ( _display, _toplevel, ExposureMask |
StructureNotifyMask | ButtonPressMask | PointerMotionMask);
}
else {
update_win(x,y); // drawscreen
}
corner++;
break;
case MotionNotify:
if (xold >= 0) { // xold set -ve before we draw first box
XDrawRectangle( _display, _toplevel, _gcxor, min(x[0],xold),
min(y[0],yold), abs(x[0]-xold), abs(y[0]-yold));
}
// Don't allow user to window under menu region
xold = min(report.xmotion.x, _top_width - 1 - MWIDTH);
yold = report.xmotion.y;
XDrawRectangle( _display, _toplevel, _gcxor, min(x[0],xold),
min(y[0],yold), abs(x[0]-xold), abs(y[0]-yold));
break;
}
}
XSelectInput( _display, _toplevel,
ExposureMask | StructureNotifyMask | ButtonPressMask);
}
// void GUI_GRAPHICS::postscript (void (*drawscreen) (void))
void GUI_GRAPHICS::postscript ()
{
// Takes a snapshot of the screen and stores it in pic?.ps. The
// first picture goes in pic1.ps, the second in pic2.ps, etc.
_piccount = 1;
int success;
char fname[20];
sprintf(fname, "results/pic%d.ps", _piccount);
success = init_postscript(fname);
if (success == 0)
return; // Couldn't open file, abort.
drawscreen();
close_postscript();
_piccount ++;
}
//void GUI_GRAPHICS::proceed (void (*drawscreen)(void))
void GUI_GRAPHICS::proceed ()
{
// Dummy routine. Just exit the event loop.
}
//void GUI_GRAPHICS::quit (void (*drawscreen) (void))
void GUI_GRAPHICS::quit ()
{
close_graphics();
exit(0);
}
////////////////////////////////////////////////////////////////////////////////
//
// GUI_GRAPHICS library
//
////////////////////////////////////////////////////////////////////////////////
GUI_GRAPHICS::GUI_GRAPHICS( TOPOLOGY *topology, VNOC *vnoc)
{
_is_gui_usable = false; // true only if asked by user thru line argument;
_display_type = SCREEN;
_topology = topology;
_vnoc = vnoc;
_menu_font_size = 14;
strcpy( _user_message, "\0");
// Graphics state. Set start-up defaults here
_currentcolor = BLACK;
_currentlinestyle = SOLID;
_currentlinewidth = 0;
_currentfontsize = 10;
_pic_on_screen = NO_PICTURE;
_show_congestion = false;
_show_links = false;
_router_width = 100.0;
_channel_width = 100.0;
_max_router_occupancy = 1.0;
}
bool GUI_GRAPHICS::build()
{
// construct all the gui stuff; before this the gui object was empty and
// acted only asa place holder, whcih is now populated if user asked for it;
if ( !_is_gui_usable)
return false;
// set up the display graphics;
init_graphics("vNOC: versatile NOC simulator");
// populate info on: _x_router_left, _y_router_bottom, _router_color,
// _router_occupancy
alloc_draw_structs();
return true;
}
int GUI_GRAPHICS::xcoord(float worldx)
{
// Translates from my internal coordinates to X Windows coordinates
// in the x direction. Add 0.5 at end for extra half-pixel accuracy.
int winx = (int) ((worldx - _xleft) * _xmult + 0.5);
// Avoid overflow in the X Window routines. This will allow horizontal
// and vertical lines to be drawn correctly regardless of zooming
winx = max (winx, MINPIXEL);
winx = min (winx, MAXPIXEL);
return (winx);
}
int GUI_GRAPHICS::ycoord(float worldy)
{
int winy = (int) ((worldy - _ytop) * _ymult + 0.5);
winy = max (winy, MINPIXEL);
winy = min (winy, MAXPIXEL);
return (winy);
}
void GUI_GRAPHICS::load_font(int pointsize)
{
// Makes sure the font of the specified size is loaded. Point_size
// MUST be between 1 and MAX_FONT_SIZE -- no check is performed here.
char fontname[128];
//sprintf(fontname,"-*-courier-medium-r-*--*-%d0-*-*-*-*-*-*",pointsize);
sprintf(fontname,"-adobe-helvetica-medium-o-normal--14-140-75-75-p-78-iso8859-1");
//sprintf(fontname,"-adobe-helvetica-medium-o-normal--14-140-*-*-*-*-*-*");
//if (( _font_info[pointsize] = XLoadQueryFont( _display, fontname)) == NULL) {
if (( _font_info[pointsize] = XLoadQueryFont( _display, "9x15")) == NULL) {
fprintf(stderr, "Cannot open desired font\n");
exit(1);
}
}
void GUI_GRAPHICS::force_setcolor(int cindex)
{
static char *ps_cnames[COLORS_NUMBER] = {"white", "black",
"grey55", "grey75", "blue", "green", "yellow", "cyan", "red",
"grey35", "grey15" }; // "darkgreen", "magenta"
_currentcolor = cindex;
if ( _display_type == SCREEN) {
XSetForeground( _display, _gc, _colors[cindex]);
} else {
fprintf( _ps_file, "%s\n", ps_cnames[cindex]);
}
}
void GUI_GRAPHICS::setcolor (int cindex)
{
if ( _currentcolor != cindex) {
force_setcolor( cindex);
}
}
void GUI_GRAPHICS::force_setlinestyle(int linestyle)
{
// Note SOLID is 0 and DASHED is 1 for linestyle.
// PostScript and X commands needed, respectively.
static char *ps_text[2] = {"linesolid", "linedashed"};
static int x_vals[2] = {LineSolid, LineOnOffDash};
_currentlinestyle = linestyle;
if ( _display_type == SCREEN) {
XSetLineAttributes( _display, _gc, _currentlinewidth,
x_vals[linestyle], CapButt, JoinMiter);
} else {
fprintf( _ps_file, "%s\n", ps_text[linestyle]);
}
}
void GUI_GRAPHICS::setlinestyle(int linestyle)
{
if (linestyle != _currentlinestyle)
force_setlinestyle(linestyle);
}
void GUI_GRAPHICS::force_setlinewidth (int linewidth)
{
// Note SOLID is 0 and DASHED is 1 for linestyle;
static int x_vals[2] = {LineSolid, LineOnOffDash};
_currentlinewidth = linewidth;
if ( _display_type == SCREEN) {
XSetLineAttributes( _display, _gc, linewidth, x_vals[ _currentlinestyle],
CapButt, JoinMiter);
} else {
fprintf( _ps_file, "%d setlinewidth\n", linewidth);
}
}
void GUI_GRAPHICS::setlinewidth (int linewidth)
{
if (linewidth != _currentlinewidth)
force_setlinewidth(linewidth);
}
void GUI_GRAPHICS::force_setfontsize (int pointsize)
{
// Valid point sizes are between 1 and MAX_FONT_SIZE
if (pointsize < 1)
pointsize = 1;
else if (pointsize > MAX_FONT_SIZE)
pointsize = MAX_FONT_SIZE;
_currentfontsize = pointsize;
if ( _display_type == SCREEN) {
if ( !_font_is_loaded[pointsize]) {
load_font(pointsize);
_font_is_loaded[pointsize] = 1;
}
XSetFont( _display, _gc, _font_info[pointsize]->fid);
}
else {
fprintf( _ps_file, "%d setfontsize\n",pointsize);
}
}
void GUI_GRAPHICS::setfontsize (int pointsize)
{
// For efficiency, this routine doesn't do anything if no change is
// implied. If you want to force the graphics context or PS file
// to have font info set, call force_setfontsize (this is necessary
// in initialization and X11 / Postscript switches).
if (pointsize != _currentfontsize)
force_setfontsize(pointsize);
}
void GUI_GRAPHICS::build_textarea(void)
{
// Creates a small window at the top of the graphics area for text messages
XSetWindowAttributes menu_attributes;
unsigned long valuemask;
_textarea = XCreateSimpleWindow( _display, _toplevel,
0, _top_height - T_AREA_HEIGHT, _display_width, T_AREA_HEIGHT - 4, 2,
_colors[BLACK], _colors[LIGHTGREY]);
menu_attributes.event_mask = ExposureMask;
// ButtonPresses in this area are ignored.
menu_attributes.do_not_propagate_mask = ButtonPressMask;
// Keep text area on bottom left
menu_attributes.win_gravity = SouthWestGravity;
valuemask = CWWinGravity | CWEventMask | CWDontPropagate;
XChangeWindowAttributes( _display, _textarea, valuemask, &menu_attributes);
XMapWindow( _display, _textarea);
}
void GUI_GRAPHICS::setpoly(int bnum, int xc, int yc, int r, float theta)
{
// Puts a triangle in the poly array for button[bnum]
int i;
_button[bnum].istext = 0;
_button[bnum].ispoly = 1;
for ( i=0; i<3; i++) {
_button[bnum].poly[i][0] = (int) (xc + r*cos(theta) + 0.5);
_button[bnum].poly[i][1] = (int) (yc + r*sin(theta) + 0.5);
theta += 2*PI/3;
}
}
void GUI_GRAPHICS::build_default_menu (void)
{
// Sets up the default menu buttons on the right hand side of the window.
XSetWindowAttributes menu_attributes;
unsigned long valuemask;
int i, xcen, x1, y1, bwid, bheight, space;
_menu = XCreateSimpleWindow( _display, _toplevel,
_top_width-MWIDTH, 0, MWIDTH-4, _display_height, 2,
_colors[BLACK], _colors[LIGHTGREY]);
menu_attributes.event_mask = ExposureMask;
// Ignore button presses on the menu background.
menu_attributes.do_not_propagate_mask = ButtonPressMask;
// Keep menu on top right
menu_attributes.win_gravity = NorthEastGravity;
valuemask = CWWinGravity | CWEventMask | CWDontPropagate;
XChangeWindowAttributes( _display, _menu, valuemask, &menu_attributes);
XMapWindow( _display, _menu);
_num_buttons = 11;
_button = (BUTTON_TYPE *) my_malloc(_num_buttons * sizeof(BUTTON_TYPE));
// Now do the arrow buttons
bwid = 28;
space = 3;
y1 = 10;
xcen = 51;
x1 = xcen - bwid/2;
_button[0].xleft = x1;
_button[0].ytop = y1;
setpoly (0, bwid/2, bwid/2, bwid/3, -PI/2.); // Up
_button[0].fcn = UP; // translate_up;
y1 += bwid + space;
x1 = xcen - 3*bwid/2 - space;
_button[1].xleft = x1;
_button[1].ytop = y1;
setpoly (1, bwid/2, bwid/2, bwid/3, PI); // Left
_button[1].fcn = LEFT; // translate_left;
x1 = xcen + bwid/2 + space;
_button[2].xleft = x1;
_button[2].ytop = y1;
setpoly (2, bwid/2, bwid/2, bwid/3, 0); // Right
_button[2].fcn = RIGHT; // translate_right;
y1 += bwid + space;
x1 = xcen - bwid/2;
_button[3].xleft = x1;
_button[3].ytop = y1;
setpoly (3, bwid/2, bwid/2, bwid/3, +PI/2.); // Down
_button[3].fcn = DOWN; // translate_down;
for ( i=0; i<4; i++) {
_button[i].width = bwid;
_button[i].height = bwid;
}
// Rectangular _buttons
y1 += bwid + space + 6;
space = 8;
bwid = 90;
bheight = 26;
x1 = xcen - bwid/2;
for ( i=4; i< _num_buttons; i++) {
_button[i].xleft = x1;
_button[i].ytop = y1;
y1 += bheight + space;
_button[i].istext = 1;
_button[i].ispoly = 0;
_button[i].width = bwid;
_button[i].height = bheight;
}
strcpy (_button[4].text,"Zoom In");
strcpy (_button[5].text,"Zoom Out");
strcpy (_button[6].text,"Zoom Fit");
strcpy (_button[7].text,"Window");
strcpy (_button[8].text,"PostScript");
strcpy (_button[9].text,"Proceed");
strcpy (_button[10].text,"Exit");
_button[4].fcn = ZOOM_IN; // zoom_in;
_button[5].fcn = ZOOM_OUT; // zoom_out;
_button[6].fcn = ZOOM_FIT; // zoom_fit;
_button[7].fcn = ADJUSTWIN; // adjustwin;
_button[8].fcn = PS; // postscript;
_button[9].fcn = PROCEED; // proceed;
_button[10].fcn = QUIT; // quit;
for ( i=0; i < _num_buttons; i++) {
map_button(i);
}
}
void GUI_GRAPHICS::map_button (int bnum)
{
// Maps a button onto the screen and set it up for input, etc.
_button[bnum].win =
XCreateSimpleWindow( _display, _menu,
_button[bnum].xleft, _button[bnum].ytop,
_button[bnum].width, _button[bnum].height,
0, _colors[WHITE], _colors[LIGHTGREY]);
XMapWindow( _display, _button[bnum].win);
XSelectInput( _display, _button[bnum].win, ButtonPressMask);
_button[bnum].ispressed = 1;
}
void GUI_GRAPHICS::unmap_button (int bnum)
{
// Unmaps a button from the screen.
XUnmapWindow( _display, _button[bnum].win);
}
//void GUI_GRAPHICS::create_button (char *prev_button_text, char *button_text,
//void (*button_func) (void (*drawscreen) (void)))
void GUI_GRAPHICS::create_button (char *prev_button_text, char *button_text,
BUTTON_FUNCTION button_func)
{
// Creates a new button below the button containing prev_button_text.
// The text and button function are set according to button_text and
// button_func, respectively.
int i, bnum, space;
space = 8;
// Only allow new buttons that are text (not poly) types.
bnum = -1;
for ( i=4; i < _num_buttons; i++) {
if ( _button[i].istext == 1 &&
strcmp( _button[i].text, prev_button_text) == 0) {
bnum = i + 1;
break;
}
}
if (bnum == -1) {
printf ("Error in create_button: button with text %s not found.\n",
prev_button_text);
exit(1);
}
_num_buttons ++;
_button = (BUTTON_TYPE *) my_realloc( _button, _num_buttons * sizeof(BUTTON_TYPE));
_button[_num_buttons-1].xleft = _button[_num_buttons-2].xleft;
_button[_num_buttons-1].ytop =
_button[_num_buttons-2].ytop + _button[_num_buttons-2].height + space;
_button[_num_buttons-1].height = _button[_num_buttons-2].height;
_button[_num_buttons-1].width = _button[_num_buttons-2].width;
map_button( _num_buttons-1);
for ( i = _num_buttons-1; i>bnum; i--) {
_button[i].ispoly = _button[i-1].ispoly;
// No poly copy for now, as I'm only providing the ability to create text
_button[i].istext = _button[i-1].istext;
strcpy( _button[i].text, _button[i-1].text);
_button[i].fcn = _button[i-1].fcn;
_button[i].ispressed = _button[i-1].ispressed;
}
_button[bnum].istext = 1;
_button[bnum].ispoly = 0;
strncpy( _button[bnum].text, button_text, BUTTON_TEXT_LEN);
_button[bnum].fcn = button_func;
_button[bnum].ispressed = 1;
}
void GUI_GRAPHICS::destroy_button(char *button_text)
{
// Destroys the _button with text button_text.
int i, bnum;
bnum = -1;
for ( i=4; i< _num_buttons; i++) {
if ( _button[i].istext == 1 &&
strcmp( _button[i].text, button_text) == 0) {
bnum = i;
break;
}
}
if ( bnum == -1) {
printf ("Error in destroy_button: button with text %s not found.\n", button_text);
exit (1);
}
for (i=bnum+1; i < _num_buttons; i++) {
_button[i-1].ispoly = _button[i].ispoly;
_button[i-1].istext = _button[i].istext;
strcpy( _button[i-1].text, _button[i].text);
_button[i-1].fcn = _button[i].fcn;
_button[i-1].ispressed = _button[i].ispressed;
}
unmap_button( _num_buttons - 1);
_num_buttons --;
_button = (BUTTON_TYPE *) my_realloc( _button, _num_buttons * sizeof(BUTTON_TYPE));
}
void GUI_GRAPHICS::init_graphics( char *window_name)
{
// Open the top level window, get the colors, 2 graphics
// contexts, load a font, and set up the toplevel window
// Calls build_default_menu to set up the default menu.
char *display_name = NULL;
int x, y; // window position
unsigned int border_width = 2; // ignored by OpenWindows
XTextProperty windowName;
// X Windows' names for my colours
char *cnames[COLORS_NUMBER] = { "white", "black", "grey55", "grey75", "blue",
"green", "yellow", "cyan", "red", "grey35", "grey15" }; // "RGBi:0.0/0.5/0.0", "magenta"
XColor exact_def;
Colormap cmap;
int i;
unsigned long valuemask = 0; // ignore XGCvalues and use defaults
XGCValues values;
XEvent event;
_display_type = SCREEN; // Graphics go to screen, not ps
for (i = 0; i <= MAX_FONT_SIZE; i++)
_font_is_loaded[i] = 0; // No fonts loaded yet
// connect to X server
if ( (_display = XOpenDisplay(display_name)) == NULL ) {
fprintf( stderr, "Cannot connect to X server %s\n",
XDisplayName(display_name));
exit(1);
}
// get screen size from display structure macro
_screen_num = DefaultScreen( _display);
_display_width = DisplayWidth( _display, _screen_num);
_display_height = DisplayHeight( _display, _screen_num);
x = y = 0;
_top_width = 2 * _display_width / 3;
_top_height = 4 * _display_height / 5;
cmap = DefaultColormap( _display, _screen_num);
_private_cmap = None;
for ( i = 0; i< COLORS_NUMBER; i++) {
if ( !XParseColor( _display, cmap, cnames[i], &exact_def)) {
fprintf(stderr, "Color name %s not in database", cnames[i]);
exit(-1);
}
if ( !XAllocColor( _display, cmap, &exact_def)) {
fprintf(stderr, "Couldn't allocate color %s.\n", cnames[i]);
if ( _private_cmap == None) {
fprintf(stderr, "Will try to allocate a private colourmap.\n");
_private_cmap = XCopyColormapAndFree( _display, cmap);
cmap = _private_cmap;
if ( !XAllocColor ( _display, cmap, &exact_def)) {
fprintf (stderr, "Couldn't allocate color %s as private.\n", cnames[i]);
exit(1);
}
}
else {
fprintf(stderr, "Couldn't allocate color %s as private.\n", cnames[i]);
exit(1);
}
}
_colors[i] = exact_def.pixel;
}
_toplevel = XCreateSimpleWindow( _display, RootWindow( _display, _screen_num),
x, y, _top_width, _top_height, border_width, _colors[BLACK],
_colors[WHITE]);
if ( _private_cmap != None) {
XSetWindowColormap( _display, _toplevel, _private_cmap);
}
XSelectInput( _display, _toplevel,
ExposureMask | StructureNotifyMask | ButtonPressMask);
// Create default Graphics Contexts; valuemask = 0 -> use defaults.
_gc = XCreateGC( _display, _toplevel, valuemask, &values);
_gc_menus = XCreateGC( _display, _toplevel, valuemask, &values);
// Create XOR graphics context for Rubber Banding
values.function = GXxor;
values.foreground = _colors[BLACK];
_gcxor = XCreateGC( _display, _toplevel,
(GCFunction | GCForeground), &values);
// specify font for menus;
load_font( _menu_font_size);