lib/igt_kms: Unify pipe name helpers
[platform/upstream/intel-gpu-tools.git] / tests / testdisplay.c
1 /*
2  * Copyright 2010 Intel Corporation
3  *   Jesse Barnes <jesse.barnes@intel.com>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23
24 /*
25  * This program is intended for testing of display functionality.  It should
26  * allow for testing of
27  *   - hotplug
28  *   - mode setting
29  *   - clone & twin modes
30  *   - panel fitting
31  *   - test patterns & pixel generators
32  * Additional programs can test the detected outputs against VBT provided
33  * device lists (both docked & undocked).
34  *
35  * TODO:
36  * - pixel generator in transcoder
37  * - test pattern reg in pipe
38  * - test patterns on outputs (e.g. TV)
39  * - handle hotplug (leaks crtcs, can't handle clones)
40  * - allow mode force
41  * - expose output specific controls
42  *  - e.g. DDC-CI brightness
43  *  - HDMI controls
44  *  - panel brightness
45  *  - DP commands (e.g. poweroff)
46  * - verify outputs against VBT/physical connectors
47  */
48 #ifdef HAVE_CONFIG_H
49 #include "config.h"
50 #endif
51
52 #include <cairo.h>
53 #include <errno.h>
54 #include <math.h>
55 #include <stdint.h>
56 #include <stdbool.h>
57 #include <strings.h>
58 #include <unistd.h>
59 #include <termios.h>
60 #include <sys/poll.h>
61 #include <sys/time.h>
62 #include <sys/ioctl.h>
63 #include <sys/types.h>
64 #include <sys/stat.h>
65
66 #include "ioctl_wrappers.h"
67 #include "drmtest.h"
68 #include "testdisplay.h"
69 #include "igt_kms.h"
70
71 #include <stdlib.h>
72 #include <signal.h>
73
74 #define SUBTEST_OPTS 1
75
76 static int tio_fd;
77 struct termios saved_tio;
78
79 drmModeRes *resources;
80 int drm_fd, modes;
81 int test_all_modes = 0, test_preferred_mode = 0, force_mode = 0, test_plane,
82     test_stereo_modes, enable_tiling;
83 int sleep_between_modes = 5;
84 int do_dpms = 0; /* This aliases to DPMS_ON */
85 uint32_t depth = 24, stride, bpp;
86 int qr_code = 0;
87 int specified_mode_num = -1, specified_disp_id = -1;
88
89 drmModeModeInfo force_timing;
90
91 int crtc_x, crtc_y, crtc_w, crtc_h, width, height;
92 unsigned int plane_fb_id;
93 unsigned int plane_crtc_id;
94 unsigned int plane_id;
95 int plane_width, plane_height;
96 static const uint32_t SPRITE_COLOR_KEY = 0x00aaaaaa;
97
98 /*
99  * Mode setting with the kernel interfaces is a bit of a chore.
100  * First you have to find the connector in question and make sure the
101  * requested mode is available.
102  * Then you need to find the encoder attached to that connector so you
103  * can bind it with a free crtc.
104  */
105 struct connector {
106         uint32_t id;
107         int mode_valid;
108         drmModeModeInfo mode;
109         drmModeEncoder *encoder;
110         drmModeConnector *connector;
111         int crtc;
112         int crtc_idx;
113         int pipe;
114 };
115
116 static void dump_connectors_fd(int drmfd)
117 {
118         int i, j;
119
120         drmModeRes *mode_resources = drmModeGetResources(drmfd);
121
122         if (!mode_resources) {
123                 igt_warn("drmModeGetResources failed: %s\n", strerror(errno));
124                 return;
125         }
126
127         igt_info("Connectors:\n");
128         igt_info("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\n");
129         for (i = 0; i < mode_resources->count_connectors; i++) {
130                 drmModeConnector *connector;
131
132                 connector = drmModeGetConnector(drmfd, mode_resources->connectors[i]);
133                 if (!connector) {
134                         igt_warn("could not get connector %i: %s\n", mode_resources->connectors[i], strerror(errno));
135                         continue;
136                 }
137
138                 igt_info("%d\t%d\t%s\t%s\t%dx%d\t\t%d\n", connector->connector_id, connector->encoder_id, kmstest_connector_status_str(connector->connection), kmstest_connector_type_str(connector->connector_type), connector->mmWidth, connector->mmHeight, connector->count_modes);
139
140                 if (!connector->count_modes)
141                         continue;
142
143                 igt_info("  modes:\n");
144                 igt_info("  name refresh (Hz) hdisp hss hse htot vdisp ""vss vse vtot flags type clock\n");
145                 for (j = 0; j < connector->count_modes; j++){
146                         igt_info("[%d]", j);
147                         kmstest_dump_mode(&connector->modes[j]);
148                 }
149
150                 drmModeFreeConnector(connector);
151         }
152         igt_info("\n");
153
154         drmModeFreeResources(mode_resources);
155 }
156
157 static void dump_crtcs_fd(int drmfd)
158 {
159         int i;
160         drmModeRes *mode_resources = drmModeGetResources(drmfd);
161
162         igt_info("CRTCs:\n");
163         igt_info("id\tfb\tpos\tsize\n");
164         for (i = 0; i < mode_resources->count_crtcs; i++) {
165                 drmModeCrtc *crtc;
166
167                 crtc = drmModeGetCrtc(drmfd, mode_resources->crtcs[i]);
168                 if (!crtc) {
169                         igt_warn("could not get crtc %i: %s\n", mode_resources->crtcs[i], strerror(errno));
170                         continue;
171                 }
172                 igt_info("%d\t%d\t(%d,%d)\t(%dx%d)\n", crtc->crtc_id, crtc->buffer_id, crtc->x, crtc->y, crtc->width, crtc->height);
173                 kmstest_dump_mode(&crtc->mode);
174
175                 drmModeFreeCrtc(crtc);
176         }
177         igt_info("\n");
178
179         drmModeFreeResources(mode_resources);
180 }
181
182 static void dump_info(void)
183 {
184         dump_connectors_fd(drm_fd);
185         dump_crtcs_fd(drm_fd);
186 }
187
188 static void connector_find_preferred_mode(uint32_t connector_id,
189                                           unsigned long crtc_idx_mask,
190                                           int mode_num, struct connector *c)
191 {
192         struct kmstest_connector_config config;
193
194         if (kmstest_get_connector_config(drm_fd, connector_id, crtc_idx_mask,
195                                          &config) < 0) {
196                 c->mode_valid = 0;
197                 return;
198         }
199
200         c->connector = config.connector;
201         c->encoder = config.encoder;
202         c->crtc = config.crtc->crtc_id;
203         c->crtc_idx = config.crtc_idx;
204         c->pipe = config.pipe;
205
206         if (mode_num != -1) {
207                 igt_assert(mode_num < config.connector->count_modes);
208                 c->mode = config.connector->modes[mode_num];
209         } else {
210                 c->mode = config.default_mode;
211         }
212         c->mode_valid = 1;
213 }
214
215 static void
216 paint_color_key(struct igt_fb *fb_info)
217 {
218         int i, j;
219         uint32_t *fb_ptr;
220
221         fb_ptr = gem_mmap(drm_fd, fb_info->gem_handle,
222                           fb_info->size, PROT_READ | PROT_WRITE);
223         igt_assert(fb_ptr);
224
225         for (i = crtc_y; i < crtc_y + crtc_h; i++)
226                 for (j = crtc_x; j < crtc_x + crtc_w; j++) {
227                         uint32_t offset;
228
229                         offset = (i * fb_info->stride / 4) + j;
230                         fb_ptr[offset] = SPRITE_COLOR_KEY;
231                 }
232
233         munmap(fb_ptr, fb_info->size);
234 }
235
236 static void paint_image(cairo_t *cr, const char *file)
237 {
238         int img_x, img_y, img_w, img_h, img_w_o, img_h_o;
239         double img_w_scale, img_h_scale;
240
241         cairo_surface_t *image;
242
243         img_y = height * (0.10 );
244         img_h = height * 0.08 * 4;
245         img_w = img_h;
246
247         img_x = (width / 2) - (img_w / 2);
248
249         image = cairo_image_surface_create_from_png(file);
250
251         img_w_o = cairo_image_surface_get_width(image);
252         img_h_o = cairo_image_surface_get_height(image);
253
254         cairo_translate(cr, img_x, img_y);
255
256         img_w_scale = (double)img_w / (double)img_w_o;
257         img_h_scale = (double)img_h / (double)img_h_o;
258         cairo_scale(cr, img_w_scale, img_h_scale);
259
260         cairo_set_source_surface(cr, image, 0, 0);
261         cairo_scale(cr, 1, 1);
262
263         cairo_paint(cr);
264         cairo_surface_destroy(image);
265 }
266
267 static void paint_output_info(struct connector *c, struct igt_fb *fb)
268 {
269         cairo_t *cr = igt_get_cairo_ctx(drm_fd, fb);
270         int l_width = fb->width;
271         int l_height = fb->height;
272         double str_width;
273         double x, y, top_y;
274         double max_width;
275         int i;
276
277         igt_paint_test_pattern(cr, l_width, l_height);
278
279         cairo_move_to(cr, l_width / 2, l_height / 2);
280
281         /* Print connector and mode name */
282         cairo_set_font_size(cr, 48);
283         igt_cairo_printf_line(cr, align_hcenter, 10, "%s",
284                  kmstest_connector_type_str(c->connector->connector_type));
285
286         cairo_set_font_size(cr, 36);
287         str_width = igt_cairo_printf_line(cr, align_hcenter, 10,
288                 "%s @ %dHz on %s encoder", c->mode.name, c->mode.vrefresh,
289                 kmstest_encoder_type_str(c->encoder->encoder_type));
290
291         cairo_rel_move_to(cr, -str_width / 2, 0);
292
293         /* List available modes */
294         cairo_set_font_size(cr, 18);
295         str_width = igt_cairo_printf_line(cr, align_left, 10,
296                                               "Available modes:");
297         cairo_rel_move_to(cr, str_width, 0);
298         cairo_get_current_point(cr, &x, &top_y);
299
300         max_width = 0;
301         for (i = 0; i < c->connector->count_modes; i++) {
302                 cairo_get_current_point(cr, &x, &y);
303                 if (y >= l_height) {
304                         x += max_width + 10;
305                         max_width = 0;
306                         cairo_move_to(cr, x, top_y);
307                 }
308                 str_width = igt_cairo_printf_line(cr, align_right, 10,
309                         "%s @ %dHz", c->connector->modes[i].name,
310                          c->connector->modes[i].vrefresh);
311                 if (str_width > max_width)
312                         max_width = str_width;
313         }
314
315         if (qr_code)
316                 paint_image(cr, IGT_DATADIR"/pass.png");
317
318         igt_assert(!cairo_status(cr));
319
320         cairo_destroy(cr);
321 }
322
323 static void sighandler(int signo)
324 {
325         return;
326 }
327
328 static void set_single(void)
329 {
330         int sigs[] = { SIGUSR1 };
331         struct sigaction sa;
332         sa.sa_handler = sighandler;
333
334         sigemptyset(&sa.sa_mask);
335
336         igt_warn_on_f(sigaction(sigs[0], &sa, NULL) == -1,
337                       "Could not set signal handler");
338 }
339
340 static void
341 set_mode(struct connector *c)
342 {
343         unsigned int fb_id = 0;
344         struct igt_fb fb_info[2] = { };
345         int j, test_mode_num, current_fb = 0, old_fb = -1;
346
347         test_mode_num = 1;
348         if (force_mode){
349                 memcpy( &c->mode, &force_timing, sizeof(force_timing));
350                 c->mode.vrefresh =(force_timing.clock*1e3)/(force_timing.htotal*force_timing.vtotal);
351                 c->mode_valid = 1;
352                 sprintf(c->mode.name, "%dx%d", force_timing.hdisplay, force_timing.vdisplay);
353         } else if (test_all_modes)
354                 test_mode_num = c->connector->count_modes;
355
356         for (j = 0; j < test_mode_num; j++) {
357
358                 if (test_all_modes)
359                         c->mode = c->connector->modes[j];
360
361                 /* set_mode() only tests 2D modes */
362                 if (c->mode.flags & DRM_MODE_FLAG_3D_MASK)
363                         continue;
364
365                 if (!c->mode_valid)
366                         continue;
367
368                 width = c->mode.hdisplay;
369                 height = c->mode.vdisplay;
370
371                 fb_id = igt_create_fb(drm_fd, width, height,
372                                           igt_bpp_depth_to_drm_format(bpp, depth),
373                                           enable_tiling, &fb_info[current_fb]);
374                 paint_output_info(c, &fb_info[current_fb]);
375                 paint_color_key(&fb_info[current_fb]);
376
377                 igt_info("CRTC(%u):[%d]", c->crtc, j);
378                 kmstest_dump_mode(&c->mode);
379                 if (drmModeSetCrtc(drm_fd, c->crtc, fb_id, 0, 0,
380                                    &c->id, 1, &c->mode)) {
381                         igt_warn("failed to set mode (%dx%d@%dHz): %s\n", width, height, c->mode.vrefresh, strerror(errno));
382                         continue;
383                 }
384
385                 if (old_fb != -1)
386                         igt_remove_fb(drm_fd, &fb_info[old_fb]);
387                 old_fb = current_fb;
388                 current_fb = 1 - current_fb;
389
390                 if (sleep_between_modes && test_all_modes && !qr_code)
391                         sleep(sleep_between_modes);
392
393                 if (do_dpms) {
394                         kmstest_set_connector_dpms(drm_fd, c->connector, do_dpms);
395                         sleep(sleep_between_modes);
396                         kmstest_set_connector_dpms(drm_fd, c->connector, DRM_MODE_DPMS_ON);
397                 }
398
399                 if (qr_code){
400                         set_single();
401                         pause();
402                 }
403         }
404
405         if (test_all_modes)
406                 igt_remove_fb(drm_fd, &fb_info[old_fb]);
407
408         drmModeFreeEncoder(c->encoder);
409         drmModeFreeConnector(c->connector);
410 }
411
412 struct box {
413         int x, y, width, height;
414 };
415
416 struct stereo_fb_layout {
417         int fb_width, fb_height;
418         struct box left, right;
419 };
420
421 static void box_init(struct box *box, int x, int y, int bwidth, int bheight)
422 {
423         box->x = x;
424         box->y = y;
425         box->width = bwidth;
426         box->height = bheight;
427 }
428
429 static void stereo_fb_layout_from_mode(struct stereo_fb_layout *layout,
430                                        drmModeModeInfo *mode)
431 {
432         unsigned int format = mode->flags & DRM_MODE_FLAG_3D_MASK;
433         const int hdisplay = mode->hdisplay, vdisplay = mode->vdisplay;
434         int middle;
435
436         switch (format) {
437         case DRM_MODE_FLAG_3D_TOP_AND_BOTTOM:
438                 layout->fb_width = hdisplay;
439                 layout->fb_height = vdisplay;
440
441                 middle = vdisplay / 2;
442                 box_init(&layout->left, 0, 0, hdisplay, middle);
443                 box_init(&layout->right,
444                          0, middle, hdisplay, vdisplay - middle);
445                 break;
446         case DRM_MODE_FLAG_3D_SIDE_BY_SIDE_HALF:
447                 layout->fb_width = hdisplay;
448                 layout->fb_height = vdisplay;
449
450                 middle = hdisplay / 2;
451                 box_init(&layout->left, 0, 0, middle, vdisplay);
452                 box_init(&layout->right,
453                          middle, 0, hdisplay - middle, vdisplay);
454                 break;
455         case DRM_MODE_FLAG_3D_FRAME_PACKING:
456         {
457                 int vactive_space = mode->vtotal - vdisplay;
458
459                 layout->fb_width = hdisplay;
460                 layout->fb_height = 2 * vdisplay + vactive_space;
461
462                 box_init(&layout->left,
463                          0, 0, hdisplay, vdisplay);
464                 box_init(&layout->right,
465                          0, vdisplay + vactive_space, hdisplay, vdisplay);
466                 break;
467         }
468         default:
469                 igt_assert(0);
470         }
471 }
472
473 static const char *stereo_mode_str(drmModeModeInfo *mode)
474 {
475         unsigned int layout = mode->flags & DRM_MODE_FLAG_3D_MASK;
476
477         switch (layout) {
478         case DRM_MODE_FLAG_3D_TOP_AND_BOTTOM:
479                 return "TB";
480         case DRM_MODE_FLAG_3D_SIDE_BY_SIDE_HALF:
481                 return "SbSH";
482         case DRM_MODE_FLAG_3D_FRAME_PACKING:
483                 return "FP";
484         default:
485                 igt_assert(0);
486         }
487 }
488
489 static uint32_t create_stereo_fb(drmModeModeInfo *mode, struct igt_fb *fb)
490 {
491         struct stereo_fb_layout layout;
492         cairo_t *cr;
493         uint32_t fb_id;
494
495         stereo_fb_layout_from_mode(&layout, mode);
496         fb_id = igt_create_fb(drm_fd, layout.fb_width, layout.fb_height,
497                                   igt_bpp_depth_to_drm_format(bpp, depth),
498                                   enable_tiling, fb);
499         cr = igt_get_cairo_ctx(drm_fd, fb);
500
501         igt_paint_image(cr, IGT_DATADIR"/1080p-left.png",
502                             layout.left.x, layout.left.y,
503                             layout.left.width, layout.left.height);
504         igt_paint_image(cr, IGT_DATADIR"/1080p-right.png",
505                             layout.right.x, layout.right.y,
506                             layout.right.width, layout.right.height);
507
508         cairo_destroy(cr);
509
510         {
511                 char buffer[64];
512
513                 snprintf(buffer, sizeof(buffer), "%dx%d@%dHz-%s.png",
514                          mode->hdisplay,
515                          mode->vdisplay,
516                          mode->vrefresh,
517                          stereo_mode_str(mode));
518
519                 igt_write_fb_to_png(drm_fd, fb, buffer);
520         }
521
522         return fb_id;
523 }
524
525 static void do_set_stereo_mode(struct connector *c)
526 {
527         uint32_t fb_id;
528         struct igt_fb fb_info;
529
530         fb_id = create_stereo_fb(&c->mode, &fb_info);
531
532         igt_warn_on_f(drmModeSetCrtc(drm_fd, c->crtc, fb_id, 0, 0, &c->id, 1, &c->mode),
533                       "failed to set mode (%dx%d@%dHz): %s\n", width, height, c->mode.vrefresh, strerror(errno));
534 }
535
536 static void
537 set_stereo_mode(struct connector *c)
538 {
539         int i, n;
540
541
542         if (specified_mode_num != -1)
543                 n = 1;
544         else
545                 n = c->connector->count_modes;
546
547         for (i = 0; i < n; i++) {
548                 if (specified_mode_num == -1)
549                         c->mode = c->connector->modes[i];
550
551                 if (!c->mode_valid)
552                         continue;
553
554                 if (!(c->mode.flags & DRM_MODE_FLAG_3D_MASK))
555                         continue;
556
557                 igt_info("CRTC(%u): [%d]", c->crtc, i);
558                 kmstest_dump_mode(&c->mode);
559                 do_set_stereo_mode(c);
560
561                 if (qr_code) {
562                         set_single();
563                         pause();
564                 } else if (sleep_between_modes)
565                         sleep(sleep_between_modes);
566
567                 if (do_dpms) {
568                         kmstest_set_connector_dpms(drm_fd, c->connector, DRM_MODE_DPMS_OFF);
569                         sleep(sleep_between_modes);
570                         kmstest_set_connector_dpms(drm_fd, c->connector, DRM_MODE_DPMS_ON);
571                 }
572         }
573
574         drmModeFreeEncoder(c->encoder);
575         drmModeFreeConnector(c->connector);
576 }
577
578 /*
579  * Re-probe outputs and light up as many as possible.
580  *
581  * On Intel, we have two CRTCs that we can drive independently with
582  * different timings and scanout buffers.
583  *
584  * Each connector has a corresponding encoder, except in the SDVO case
585  * where an encoder may have multiple connectors.
586  */
587 int update_display(void)
588 {
589         struct connector *connectors;
590         int c;
591
592         resources = drmModeGetResources(drm_fd);
593         if (!resources) {
594                 igt_warn("drmModeGetResources failed: %s\n", strerror(errno));
595                 return 0;
596         }
597
598         connectors = calloc(resources->count_connectors,
599                             sizeof(struct connector));
600         if (!connectors)
601                 return 0;
602
603         if (test_preferred_mode || test_all_modes ||
604             force_mode || specified_disp_id != -1) {
605                 unsigned long crtc_idx_mask = -1UL;
606
607                 /* Find any connected displays */
608                 for (c = 0; c < resources->count_connectors; c++) {
609                         struct connector *connector = &connectors[c];
610
611                         connector->id = resources->connectors[c];
612                         if (specified_disp_id != -1 &&
613                             connector->id != specified_disp_id)
614                                 continue;
615
616                         connector_find_preferred_mode(connector->id,
617                                                       crtc_idx_mask,
618                                                       specified_mode_num,
619                                                       connector);
620                         if (!connector->mode_valid)
621                                 continue;
622
623                         set_mode(connector);
624
625                         if (test_preferred_mode || force_mode ||
626                             specified_mode_num != -1)
627                                 crtc_idx_mask &= ~(1 << connector->crtc_idx);
628
629                 }
630         }
631
632         if (test_stereo_modes) {
633                 for (c = 0; c < resources->count_connectors; c++) {
634                         struct connector *connector = &connectors[c];
635
636                         connector->id = resources->connectors[c];
637                         if (specified_disp_id != -1 &&
638                             connector->id != specified_disp_id)
639                                 continue;
640
641                         connector_find_preferred_mode(connector->id,
642                                                       -1UL,
643                                                       specified_mode_num,
644                                                       connector);
645                         if (!connector->mode_valid)
646                                 continue;
647
648                         set_stereo_mode(connector);
649                 }
650         }
651
652         free(connectors);
653         drmModeFreeResources(resources);
654         return 1;
655 }
656
657 static char optstr[] = "3hiaf:s:d:p:mrto:j:";
658
659 static void __attribute__((noreturn)) usage(char *name)
660 {
661         igt_info("usage: %s [-hiasdpmtf]\n", name);
662         igt_info("\t-i\tdump info\n");
663         igt_info("\t-a\ttest all modes\n");
664         igt_info("\t-s\t<duration>\tsleep between each mode test\n");
665         igt_info("\t-d\t<depth>\tbit depth of scanout buffer\n");
666         igt_info("\t-p\t<planew,h>,<crtcx,y>,<crtcw,h> test overlay plane\n");
667         igt_info("\t-m\ttest the preferred mode\n");
668         igt_info("\t-3\ttest all 3D modes\n");
669         igt_info("\t-t\tuse a tiled framebuffer\n");
670         igt_info("\t-j\tdo dpms off, optional arg to select dpms leve (1-3)\n");
671         igt_info("\t-r\tprint a QR code on the screen whose content is \"pass\" for the automatic test\n");
672         igt_info("\t-o\t<id of the display>,<number of the mode>\tonly test specified mode on the specified display\n");
673         igt_info("\t-f\t<clock MHz>,<hdisp>,<hsync-start>,<hsync-end>,<htotal>,\n");
674         igt_info("\t\t<vdisp>,<vsync-start>,<vsync-end>,<vtotal>\n");
675         igt_info("\t\ttest force mode\n");
676         igt_info("\tDefault is to test all modes.\n");
677         exit((optopt) ? -1 : 0);
678 }
679
680 #define dump_resource(res) if (res) dump_##res()
681
682 static void cleanup_and_exit(int ret)
683 {
684         close(drm_fd);
685         exit(ret);
686 }
687
688 static gboolean input_event(GIOChannel *source, GIOCondition condition,
689                                 gpointer data)
690 {
691         gchar buf[2];
692         gsize count;
693
694         count = read(g_io_channel_unix_get_fd(source), buf, sizeof(buf));
695         if (buf[0] == 'q' && (count == 1 || buf[1] == '\n')) {
696                 cleanup_and_exit(0);
697         }
698
699         return TRUE;
700 }
701
702 static void enter_exec_path( char **argv )
703 {
704         char *exec_path = NULL;
705         char *pos = NULL;
706         short len_path = 0;
707         int ret;
708
709         len_path = strlen( argv[0] );
710         exec_path = (char*) malloc(len_path);
711
712         memcpy(exec_path, argv[0], len_path);
713         pos = strrchr(exec_path, '/');
714         if (pos != NULL)
715                 *(pos+1) = '\0';
716
717         ret = chdir(exec_path);
718         igt_assert(ret == 0);
719         free(exec_path);
720 }
721
722 static void restore_termio_mode(int sig)
723 {
724         tcsetattr(tio_fd, TCSANOW, &saved_tio);
725         close(tio_fd);
726 }
727
728 static void set_termio_mode(void)
729 {
730         struct termios tio;
731
732         /* don't attempt to set terminal attributes if not in the foreground
733          * process group */
734         if (getpgrp() != tcgetpgrp(STDOUT_FILENO))
735                 return;
736
737         tio_fd = dup(STDIN_FILENO);
738         tcgetattr(tio_fd, &saved_tio);
739         igt_install_exit_handler(restore_termio_mode);
740         tio = saved_tio;
741         tio.c_lflag &= ~(ICANON | ECHO);
742         tcsetattr(tio_fd, TCSANOW, &tio);
743 }
744
745 int main(int argc, char **argv)
746 {
747         int c;
748         int ret = 0;
749         GIOChannel *stdinchannel;
750         GMainLoop *mainloop;
751         float force_clock;
752         bool opt_dump_info = false;
753         struct option long_opts[] = {
754                 {"list-subtests", 0, 0, SUBTEST_OPTS},
755                 {"run-subtest", 1, 0, SUBTEST_OPTS},
756                 { 0, 0, 0, 0 }
757         };
758
759         igt_skip_on_simulation();
760
761         enter_exec_path( argv );
762
763         while ((c = getopt_long(argc, argv, optstr, long_opts, NULL)) != -1) {
764                 switch (c) {
765                 case '3':
766                         test_stereo_modes = 1;
767                         break;
768                 case 'i':
769                         opt_dump_info = true;
770                         break;
771                 case 'a':
772                         test_all_modes = 1;
773                         break;
774                 case 'f':
775                         force_mode = 1;
776                         if(sscanf(optarg,"%f,%hu,%hu,%hu,%hu,%hu,%hu,%hu,%hu",
777                                 &force_clock,&force_timing.hdisplay, &force_timing.hsync_start,&force_timing.hsync_end,&force_timing.htotal,
778                                 &force_timing.vdisplay, &force_timing.vsync_start, &force_timing.vsync_end, &force_timing.vtotal)!= 9)
779                                 usage(argv[0]);
780                         force_timing.clock = force_clock*1000;
781
782                         break;
783                 case 's':
784                         sleep_between_modes = atoi(optarg);
785                         break;
786                 case 'j':
787                         do_dpms = atoi(optarg);
788                         if (do_dpms == 0)
789                                 do_dpms = DRM_MODE_DPMS_OFF;
790                         break;
791                 case 'd':
792                         depth = atoi(optarg);
793                         igt_info("using depth %d\n", depth);
794                         break;
795                 case 'p':
796                         if (sscanf(optarg, "%d,%d,%d,%d,%d,%d", &plane_width,
797                                    &plane_height, &crtc_x, &crtc_y,
798                                    &crtc_w, &crtc_h) != 6)
799                                 usage(argv[0]);
800                         test_plane = 1;
801                         break;
802                 case 'm':
803                         test_preferred_mode = 1;
804                         break;
805                 case 't':
806                         enable_tiling = 1;
807                         break;
808                 case 'r':
809                         qr_code = 1;
810                         break;
811                 case 'o':
812                         sscanf(optarg, "%d,%d", &specified_disp_id, &specified_mode_num);
813                         break;
814                 case SUBTEST_OPTS:
815                         /* invalid subtest options */
816                         exit(IGT_EXIT_INVALID);
817                         break;
818                 default:
819                         /* fall through */
820                 case 'h':
821                         usage(argv[0]);
822                         break;
823                 }
824         }
825
826         set_termio_mode();
827
828         if (depth <= 8)
829                 bpp = 8;
830         else if (depth <= 16)
831                 bpp = 16;
832         else if (depth <= 32)
833                 bpp = 32;
834
835         if (!test_all_modes && !force_mode && !test_preferred_mode &&
836             specified_mode_num == -1 && !test_stereo_modes)
837                 test_all_modes = 1;
838
839         drm_fd = drm_open_any();
840
841         if (test_stereo_modes &&
842             drmSetClientCap(drm_fd, DRM_CLIENT_CAP_STEREO_3D, 1) < 0) {
843                 igt_warn("DRM_CLIENT_CAP_STEREO_3D failed\n");
844                 goto out_close;
845         }
846
847         if (opt_dump_info) {
848                 dump_info();
849                 goto out_close;
850         }
851
852         igt_set_vt_graphics_mode();
853
854         mainloop = g_main_loop_new(NULL, FALSE);
855         if (!mainloop) {
856                 igt_warn("failed to create glib mainloop\n");
857                 ret = -1;
858                 goto out_close;
859         }
860
861         if (!testdisplay_setup_hotplug()) {
862                 igt_warn("failed to initialize hotplug support\n");
863                 goto out_mainloop;
864         }
865
866         stdinchannel = g_io_channel_unix_new(0);
867         if (!stdinchannel) {
868                 igt_warn("failed to create stdin GIO channel\n");
869                 goto out_hotplug;
870         }
871
872         ret = g_io_add_watch(stdinchannel, G_IO_IN | G_IO_ERR, input_event,
873                              NULL);
874         if (ret < 0) {
875                 igt_warn("failed to add watch on stdin GIO channel\n");
876                 goto out_stdio;
877         }
878
879         ret = 0;
880
881         if (!update_display()) {
882                 ret = 1;
883                 goto out_stdio;
884         }
885
886         if (test_all_modes)
887                 goto out_stdio;
888
889         g_main_loop_run(mainloop);
890
891 out_stdio:
892         g_io_channel_shutdown(stdinchannel, TRUE, NULL);
893 out_hotplug:
894         testdisplay_cleanup_hotplug();
895 out_mainloop:
896         g_main_loop_unref(mainloop);
897 out_close:
898         close(drm_fd);
899
900         return ret;
901 }